`circuit_from_qasm`: `cu3(theta, ...)` gets a -1 phase on the control's |1> branch when theta mod 4pi is in [2pi, 4pi)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 62/100
Research direction
QasmUGate.__init__ in cirq/circuits/qasm_output.py stores theta % 2; the fix has to keep the sign for the controlled case without changing u3 behavior. Start by re-running the issue's first reproducer, then check every place QasmUGate's stored theta is consumed (_unitary_, qasm_import's cu3 handling in cirq/contrib/qasm_import/) and the existing tests under cirq/circuits/qasm_output_test.py and cirq/contrib/qasm_import/. Done means the theta sweep is equal up to global phase for all inputs and the discontinuity at 2pi is gone.
Written by the indexing model from the issue text.
Description
Describe the issue
cirq.contrib.qasm_import.circuit_from_qasm imports cu3(theta,phi,lambda) with the wrong unitary when theta mod 4pi lies in [2pi, 4pi), which includes every small negative theta: the result is diag(1, 1, -1, -1) times the correct matrix, i.e. a phase of -1 on the control's |1> branch. For theta mod 4pi in [0, 2pi) the import matches Qiskit's CU3Gate.
The importer builds cu3 as ControlledGate(QasmUGate(theta/pi, phi/pi, lambda/pi)), and QasmUGate.__init__ stores theta % 2 (half turns; cirq/circuits/qasm_output.py). U3(theta + 2pi) = -U3(theta): a global phase for u3, but a relative phase once the gate is controlled. Storing theta % 4 would keep the sign (phi and lambda are 2pi-periodic, so % 2 is fine for them).
This is the case raised in a comment on #5959 on 2026-11-13 (cu3(2*pi,0,0)), which was closed after #7717. It affects the whole range, not only theta = 2pi, and the imported gate is discontinuous in theta at 2pi under any phase convention for cu3.
Explain how to reproduce the bug or problem
import numpy as np
import cirq
from cirq.contrib.qasm_import import circuit_from_qasm
def cu3(t, p, l): # Qiskit's CU3Gate (qelib1.inc in Qiskit), control = q[0] (most significant)
U = np.array([[np.cos(t / 2), -np.exp(1j * l) * np.sin(t / 2)],
[np.exp(1j * p) * np.sin(t / 2), np.exp(1j * (p + l)) * np.cos(t / 2)]])
M = np.eye(4, dtype=complex); M[2:, 2:] = U
return M
for theta in (0.4, -0.4, -0.4 + 4 * np.pi, 7.0):
c = circuit_from_qasm(f'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\ncu3({theta!r},0.4,-1.1) q[0],q[1];\n')
U = c.unitary(qubit_order=sorted(c.all_qubits()))
print(f"theta={theta:.4f} equal up to global phase: {cirq.equal_up_to_global_phase(U, cu3(theta, 0.4, -1.1))}"
f" equal to diag(1,1,-1,-1)*cu3: {cirq.equal_up_to_global_phase(U, np.diag([1, 1, -1, -1]) @ cu3(theta, 0.4, -1.1))}")
theta=0.4000 equal up to global phase: True equal to diag(1,1,-1,-1)*cu3: False
theta=-0.4000 equal up to global phase: False equal to diag(1,1,-1,-1)*cu3: True
theta=12.1664 equal up to global phase: False equal to diag(1,1,-1,-1)*cu3: True
theta=7.0000 equal up to global phase: False equal to diag(1,1,-1,-1)*cu3: True
The discontinuity, without reference to any convention:
import numpy as np
import cirq
from cirq.contrib.qasm_import import circuit_from_qasm
print("cirq", cirq.__version__)
def unitary(theta):
c = circuit_from_qasm(f'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\ncu3({theta!r},0.4,-1.1) q[0],q[1];\n')
return c.unitary(qubit_order=sorted(c.all_qubits()))
eps = 1e-3
print("max |U(2pi - eps) - U(2pi + eps)| =", np.abs(unitary(2 * np.pi - eps) - unitary(2 * np.pi + eps)).max())
print("max |U(1 - eps) - U(1 + eps)| =", np.abs(unitary(1 - eps) - unitary(1 + eps)).max())
cirq 1.7.0
max |U(2pi - eps) - U(2pi + eps)| = 1.9999997500000053
max |U(1 - eps) - U(1 + eps)| = 0.0008775825253243356
Reference: Qiskit's CU3Gate (and cu3 in Qiskit's qelib1.inc) is |0><0| (x) I + |1><1| (x) U3(theta,phi,lambda), the cu3() function above. Related: #5959, #7717.
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). Main 00a7e6c8dd has the same QasmUGate code.
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from quantumlib/Cirq
-
Potential missed optimization: `drop_diagonal_before_measurement` leaves an identity `PhasedXZGate` that blocks further removalPossibly taken @jangyoujin0917 claimed this 22 days ago. Openarea/gates area/optimization kind/feature-request triage/accepted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
quantumlib/Cirq#8523 · 1 comment ·
Maintainers usually reply within 1 day
-
`defer_measurements` ignores `BitMaskKeyCondition.index` and changes circuit behaviorMay be free again @Tejendra-06 claimed this 54 days ago, and no pull request is open. Opengood for learning help wanted kind/bug-report skill/experienced triage/accepted triage/wont-fix
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
quantumlib/Cirq#8361 · 8 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
kind/feature-request triage/discuss
quantumlib/Cirq#8598 ·
Maintainers usually reply within 1 day
-
area/docstrings area/google kind/bug-report triage/discuss
Difficulty 1/5 1-3 hours Newbie friendliness 91/100
quantumlib/Cirq#8587 ·
Maintainers usually reply within 1 day
-
kind/bug-report triage/discuss
Difficulty 2/5 1-3 hours Newbie friendliness 57/100
quantumlib/Cirq#8580 ·
Maintainers usually reply within 1 day
Similar issues
-
docs(contributing): the two things that silently block a PRPossibly taken A pull request linked to this issue is open or already merged. Openavailable documentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 95/100
Nixitov/Riptide-Native#944 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
JTHCode/secretguard#24 ·
Maintainers usually reply within 1 day
-
area: middleware documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 ·
Maintainers usually reply within 1 day