`circuit_from_qasm` imports `rc3x` as a 4-qubit `MatrixGate` that `cirq.qasm()` cannot export

Open
#8,543 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
tooling

Research direction

Run the reproducer against cirq.contrib.qasm_import.circuit_from_qasm and inspect how qelib1.inc gates are mapped there, especially the existing handling of multi-qubit gates such as rccx and cswap. Compare that mapping with what cirq.qasm() can export, then decide whether rc3x should be decomposed on import using its qelib1.inc body (h, t, tdg, cx) or handled in the exporter. Done means the OpenQASM 2.0 round trip succeeds without a MatrixGate export error and import/export tests cover rc3x.

Written by the indexing model from the issue text.

Description

triage/discuss

Describe the issue

circuit_from_qasm imports rc3x (from Qiskit's qelib1.inc) as a single 4-qubit cirq.MatrixGate. cirq.qasm() cannot write that gate, so an OpenQASM 2.0 round trip of a valid circuit fails. The matrix itself is correct (equal to the rc3x definition in Qiskit's qelib1.inc up to global phase), and the qelib1.inc body of rc3x (h, t, tdg, cx), parsed by Cirq, exports. Of the other multi-qubit qelib1.inc gates, rccx (imported as a 3-qubit MatrixGate), c3x, c3sqrtx, c4x and cswap all export.

Explain how to reproduce the bug or problem

import cirq
from cirq.contrib.qasm_import import circuit_from_qasm

c = circuit_from_qasm('OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[4];\nrc3x q[0],q[1],q[2],q[3];\n')
print([type(op.gate).__name__ for op in c.all_operations()])
print(cirq.qasm(c))
['MatrixGate']
Traceback (most recent call last):
...
ValueError: Cannot output operation as QASM: cirq.MatrixGate(np.array([[(1+0j), 0j, 0j, 0j, 0j, 0j, 0j, 0j, 0j, 0j, 0j, 0j, 0j, 0j, 0j, 0j], ...

Expected: the importer decomposes rc3x using its definition in Qiskit's qelib1.inc (h, t, tdg, cx), or the exporter decomposes the matrix, so cirq.qasm() succeeds.

Tell us the version of Cirq where this happens

cirq-core 1.7.0 and 1.8.0.dev20261121043546 (ply 3.11, numpy 2.5.5, Python 3.12.15, Linux x86_64).

Found while cross-checking simulators with Quantum Rosetta (https://github.com/SeQuSoS-Project/QRosetta), which runs one OpenQASM 2.0 circuit through several SDKs and compares their results.

I prepared this report with help from an AI assistant and checked the reproducer and results myself.

Dominant language
Python
Stars
5.1k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
48

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quantumlib/Cirq

All issues in quantumlib/Cirq

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.