Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

QASM importer silently drops all but the first operation in conditional register broadcasts

Open WindOctober opened this issue 2 months ago • 2 comments

Describe the issue

The QASM importer silently drops all but the first operation when a classical condition guards a register-broadcast gate. For a two-qubit register, if(c==1) x q; imports as a conditional X on q[0] only; the X on q[1] is missing, changing measurement outcomes.

In cirq/contrib/qasm_import/_parser.py, p_if consumes only next(p[5]) from the gate-operation generator. It should wrap every yielded operation in a ClassicallyControlledOperation. The same issue affects user-defined gate broadcasts and QASM 3, which shares this parser rule.

Explain how to reproduce the bug or problem

Compare a conditional register-wide X with its explicit per-qubit expansion:

import cirq
from cirq.contrib.qasm_import import circuit_from_qasm

source = """OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[1];
x q[0];
measure q[0] -> c[0];
if(c==1) x q;
"""
expanded = source.replace(
    "if(c==1) x q;",
    "if(c==1) x q[0]; if(c==1) x q[1];",
)

for label, qasm in [("broadcast", source), ("expanded", expanded)]:
    circuit = circuit_from_qasm(qasm)
    count = sum(isinstance(op, cirq.ClassicallyControlledOperation)
                for op in circuit.all_operations())
    circuit.append(cirq.measure(cirq.NamedQubit("q_1"), key="out"))
    result = cirq.Simulator().run(circuit, repetitions=1000)
    print(label, "controlled ops:", count, "q1:", result.histogram(key="out"))

Observed output:

broadcast controlled ops: 1 q1: Counter({0: 1000})
expanded controlled ops: 2 q1: Counter({1: 1000})

Both forms should produce two conditional operations and measure q[1] = 1 on every shot.

Tell us the version of Cirq where this happens

Cirq 1.7.0, Python 3.11.4, Linux.

WindOctober avatar Sep 20 '26 07:09 WindOctober

Hi! I’d like to work on this issue. Could you please assign it to me? I’d be happy to investigate the QASM importer behavior and work on a fix. Thank you!

mahimagalla avatar Sep 27 '26 17:09 mahimagalla

@mahimagalla Thank you for your interest! I've assigned it to you now.

mhucka avatar Sep 27 '26 23:09 mhucka