`defer_measurements` ignores `BitMaskKeyCondition.index` and changes circuit behavior
Maintainers usually reply within 1 day
@Tejendra-06 is already working on this.
Since Oct 12, 2026.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 74/100
Research direction
Start in measurement_transformers.py and read how defer_measurements handles KeyCondition and BitMaskKeyCondition; then inspect the related deferred-measurement tests. Add a regression test based on the provided circuit, verifying that deferral preserves the earlier record index or explicitly rejects the unsupported condition. Done means the original and deferred circuits agree on the output.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 1.3k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 48
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from quantumlib/Cirq
-
Potential missed optimization: `drop_diagonal_before_measurement` leaves an identity `PhasedXZGate` that blocks further removalPossibly taken @jangyoujin0917 claimed this 22 days ago. Openarea/gates area/optimization kind/feature-request triage/accepted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
quantumlib/Cirq#8523 · 1 comment ·
Maintainers usually reply within 1 day
-
kind/feature-request triage/discuss
quantumlib/Cirq#8598 ·
Maintainers usually reply within 1 day
-
area/docstrings area/google kind/bug-report triage/discuss
Difficulty 1/5 1-3 hours Newbie friendliness 91/100
quantumlib/Cirq#8587 ·
Maintainers usually reply within 1 day
-
kind/bug-report triage/discuss
Difficulty 2/5 1-3 hours Newbie friendliness 57/100
quantumlib/Cirq#8580 ·
Maintainers usually reply within 1 day
-
triage/discuss
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
quantumlib/Cirq#8558 ·
Maintainers usually reply within 1 day
Similar issues
-
docs(contributing): the two things that silently block a PRPossibly taken A pull request linked to this issue is open or already merged. Openavailable documentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 95/100
Nixitov/Riptide-Native#944 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
JTHCode/secretguard#24 ·
Maintainers usually reply within 1 day
-
area: middleware documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 ·
Maintainers usually reply within 1 day