-
Notifications
You must be signed in to change notification settings - Fork 1.3k
to_qasm() on classically controlled multi-line gates (e.g. CCZ) only guards the first line #8329
Copy link
Copy link
Open
Labels
area/qasmgood for learningFor beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues"For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues"kind/bug-reportSomething doesn't seem to work.Something doesn't seem to work.no QC knowledge neededWant to contribute to Cirq, but don't know quantum computing? This issue is for you.Want to contribute to Cirq, but don't know quantum computing? This issue is for you.triage/acceptedA consensus emerged that this bug report, feature request, or other action should be worked onA consensus emerged that this bug report, feature request, or other action should be worked on
Description
Activity
Metadata
Metadata
Assignees
Labels
area/qasmgood for learningFor beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues"For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues"kind/bug-reportSomething doesn't seem to work.Something doesn't seem to work.no QC knowledge neededWant to contribute to Cirq, but don't know quantum computing? This issue is for you.Want to contribute to Cirq, but don't know quantum computing? This issue is for you.triage/acceptedA consensus emerged that this bug report, feature request, or other action should be worked onA consensus emerged that this bug report, feature request, or other action should be worked on
Describe the issue
When a
ClassicallyControlledOperationwraps a sub-operation whose QASM output spans multiple lines—for example,CCZ, which exports ash; ccx; h—to_qasm()prepends a singleif (...)prefix to the entire block.In both OpenQASM 2.0 and 3.0, a brace-less
ifguards only the single following statement. As a result, every subsequent line executes unconditionally.This means the exported QASM is semantically different from the original Cirq circuit, with no warning.
Root cause
ClassicallyControlledOperation._qasm_concatenates theifprefix directly with the raw QASM string returned by the wrapped operation:For
CCZ,CCZPowGate._qasm_returns a multi-line string whenexponent == 1:This implementation is in
three_qubit_gates.py.Among the standard-library gates,
CCZis a common multi-line case, whileCCXandCSWAPexport as single statements.Since QASM export is an interoperability boundary, downstream tools consuming this output can silently receive a circuit with different semantics.
Explain how to reproduce the bug or problem
The exported QASM contains:
The semantic discrepancy is observable.
Suggested fix
In
ClassicallyControlledOperation._qasm_, guard the entire sub-operation rather than only its first statement.if (...)condition to each statement insubop_qasm.Tell us the version of Cirq where this happens
Reproduced on Cirq v1.7.0 (latest release).
The affected code path is also unchanged on
mainat commit8122be0, confirmed by source inspection.