`defer_measurements` ignores `BitMaskKeyCondition.index` and changes circuit behavior
Describe the issue
cirq.defer_measurements does not preserve the record index of a BitMaskKeyCondition. When a measurement key is reused, a condition referring to an earlier record can become an unconditional operation after deferral, changing the measurement result.
The index should be preserved, or unsupported conditions should be rejected explicitly.
Explain how to reproduce the bug or problem
from collections import Counter
import cirq
q0, q1, q2 = cirq.LineQubit.range(3)
condition = cirq.BitMaskKeyCondition(
cirq.MeasurementKey("a"), index=0, target_value=0, equal_target=True
)
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(condition),
cirq.measure(q2, key="out"),
)
simulator = cirq.Simulator()
for label, candidate in [
("original", circuit),
("deferred", cirq.defer_measurements(circuit)),
]:
result = simulator.run(candidate, repetitions=1000)
print(label, Counter(result.records["out"].flatten().tolist()))
Observed output:
original Counter({0: 1000})
deferred Counter({1: 1000})
The condition tests whether the first record is zero, which is false. Both circuits should therefore measure out = 0 on every shot.
Tell us the version of Cirq where this happens
Cirq 1.7.0, Python 3.11.4, Linux.
Root cause
In measurement_transformers.py, defer_measurements preserves condition.index only for KeyCondition; BitMaskKeyCondition falls through to index -1. It therefore enumerates the latest record while the original condition still reads record 0, which is zero-filled in the synthetic datastore. In this example, the condition becomes true for every enumerated value.
Unlike #8360, where KeyCondition._qasm_ ignores the index during QASM export, this is a separate type-dispatch omission in the deferred-measurement transformer. No QASM conversion is involved, and deferral correctly preserves index=0 for KeyCondition in the corresponding test.
Hi! I'd like to work on this issue. Could you please assign it to me?
I’d like to work on this issue. I’ll investigate how defer_measurements handles BitMaskKeyCondition.index and add a regression test to ensure the measurement condition remains consistent after deferral.
Hi! I’d be interested in working on this issue. I understand that defer_measurements is not preserving the index of BitMaskKeyCondition, which can change the circuit’s behavior. I’d like to investigate the existing handling for KeyCondition, add a regression test for this case, and implement the corresponding fix. Could you assign this issue to me?
cynq meeting - the order of measurements to the key "a" changed after defer_measurements transformation changing the value of BitMaskCondition.
Conclusion - this is a corner case which may be more costly to fix than it is worth, let us update the docstring to defer_measument with a caveat that the order of measurements may change.
labeling as triage/accepted to ensure the docstring is updated as needed
Thanks for the clarification. I agree with this assessment and the proposed documentation update.
Might just be including the class here https://github.com/quantumlib/Cirq/blob/48f0c722847e5cc05c56fecc672557f2d343791c/cirq-core/cirq/transformers/measurement_transformers.py#L131. Maybe investigate whether index should be supported on all condition types so this doesn't have to be special-cased.
Going by the cynq conclusion, I can put up a small PR that updates the defer_measurements docstring with a caveat that the order of measurements to a reused key can change, so BitMaskKeyCondition.index may not refer to the same record after deferral.
I'll also look at @daxfohl's point about the KeyCondition check on line 131 and mention in the PR whether index looks reasonable to support across condition types, but keep the change itself to the docs. Will link it here shortly.