Skip to content

Fix classically controlled multiline QASM output - #8376

Open
akshaysoftware wants to merge 1 commit into
quantumlib:mainfrom
akshaysoftware:fix-classically-controlled-qasm
Open

akshaysoftware wants to merge 1 commit into
quantumlib:mainfrom
akshaysoftware:fix-classically-controlled-qasm

Conversation

@akshaysoftware

Copy link
Copy Markdown

Fixes #8329.

Updates ClassicallyControlledOperation._qasm_ so multi-statement QASM output is handled correctly:

  • OpenQASM 2.0: prefixes each emitted statement with the classical condition.
  • OpenQASM 3.0: wraps the full multi-statement operation in a conditional block.
  • Preserves the existing behaviour for single-statement operations.

Adds regression tests for both QASM 2.0 and 3.0 using classically controlled CCZ.

@akshaysoftware
akshaysoftware requested a review from a team as a code owner September 23, 2026 22:42
@github-actions github-actions Bot added the size: M 50< lines changed <250 label Sep 23, 2026
@google-cla

google-cla Bot commented Sep 23, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@arettig arettig self-assigned this Sep 28, 2026

@arettig arettig left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! Just a couple minor comments below.



def test_qasm_multiline_classically_controlled_operation_qasm2() -> None:
q0, q1, q2, q3 = cirq.LineQubit.range(4)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This test could be done with 3 qubits if you move the measurement gate.



def test_qasm_multiline_classically_controlled_operation_qasm3() -> None:
q0, q1, q2, q3 = cirq.LineQubit.range(4)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: same as above, could be done in 3 qubits.

condition_qasm = " && ".join(protocols.qasm(c, args=args) for c in self._conditions)
return f'if ({condition_qasm}) {subop_qasm}'

if subop_qasm.count('\n') <= 1:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically you could have multiple gates on a single line which would break this. It looks like no built-in gates do this, so it's only a problem for user-defined gates.

Relatedly, if subop_qasm is only whitespace, this will output a hanging if statement that applies to whatever comes next.

Maybe count the number of semicolons instead?

@codecov

codecov Bot commented Sep 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.59%. Comparing base (14a2359) to head (a0ca2d1).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8376      +/-   ##
==========================================
- Coverage   99.59%   99.59%   -0.01%     
==========================================
  Files        1131     1131              
  Lines      103591   103607      +16     
==========================================
+ Hits       103170   103185      +15     
- Misses        421      422       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: M 50< lines changed <250

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_qasm() on classically controlled multi-line gates (e.g. CCZ) only guards the first line

2 participants