QASM export silently ignores KeyCondition.index and reads the latest measurement instead
Describe the issue
Circuit.to_qasm() silently ignores KeyCondition.index. If a measurement key is reused and a condition refers to an earlier record, the exported program instead tests the latest measurement, changing the circuit's behavior.
KeyCondition._qasm_ in cirq/value/condition.py does not read self.index, and the exporter allocates a single classical register per key. Subsequent measurements overwrite the value needed by the historical condition.
The exporter should preserve the referenced record or explicitly reject unsupported historical indices. The same index omission affects QASM 3 export.
Explain how to reproduce the bug or problem
Export a circuit whose condition refers to the first of two measurements, then import and execute the exported QASM:
from collections import Counter
import cirq
from cirq.contrib.qasm_import import circuit_from_qasm
q0, q1, q2 = cirq.LineQubit.range(3)
circuit = cirq.Circuit(
cirq.X(q0),
cirq.measure(q0, key="a"), # First record: 1.
cirq.measure(q1, key="a"), # Second record: 0.
cirq.X(q2).with_classical_controls(
cirq.KeyCondition(cirq.MeasurementKey("a"), index=0)
),
cirq.measure(q2, key="out"),
)
qasm = circuit.to_qasm()
print(qasm)
simulator = cirq.Simulator()
for label, candidate, key in [
("original", circuit, "out"),
("exported", circuit_from_qasm(qasm), "m_out_0"),
]:
result = simulator.run(candidate, repetitions=1000)
print(label, Counter(result.records[key].flatten().tolist()))
The relevant exported statements are:
measure q[0] -> m_a[0];
measure q[1] -> m_a[0];
if (m_a==1) x q[2];
Observed results:
original Counter({1: 1000})
exported Counter({0: 1000})
Both should measure out = 1: the first record is 1, so the conditional X must execute. The exported guard instead reads the second record, which is 0.
The official documentation allows multiple measurements with the same key and defines which record an index selects. In particular, index=0 explicitly selects the first measurement, not the latest:
“Here
0refers to the first measurement, and-1refers to the most recent.” —ClassicalDataStoreReader.get_int
Tell us the version of Cirq where this happens
Cirq 1.7.0, Python 3.11.4, Linux.
I would like to work on this issue.
It looks like KeyCondition._qasm_ in cirq/value/condition.py does not take self.index into account during QASM export, leading to register overwrites on repeated measurement keys. I can implement handling for self.index (or explicit validation) and add regression tests for circuit QASM exports with indexed conditions. Could you please assign this to me?
Discussed in Cirq Cynq 2026-09-30: this looks like a real issue, but we are not sure about whether it's a bug in the code or a documentation problem. Needs to be investigated further.
Thanks for the clarification. :)
I see it now. The KeyCondition.index was checked in the initial implementation of KeyCondition.qasm -
https://github.com/quantumlib/Cirq/blob/43955b326ee48a8688e14903b292395a148debdf/cirq-core/cirq/value/condition.py#L122-L126
but that got removed later when qasm was disabled in #5434 and the check was not restored in the reimplementation of qasm later in #6871. I think we should just put the check back, i.e., disallow qasm for KeyCondition with a non-default index.
@WindOctober - can you please add a check for self.index != -1 to raise ValueError in such case?
I think that is a better option than trying to do some convoluted qasm logic that would respect KeyCondition.index as that would likely just introduce more corner-case bugs.