`circuit_from_qasm`: `cu3(theta, ...)` gets a -1 phase on the control's |1> branch when theta mod 4pi is in [2pi, 4pi)

Open
#8,542 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
compilers

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

triage/discuss

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

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.