`defer_measurements` ignores `BitMaskKeyCondition.index` and changes circuit behavior

Open Beginner friendly
#8,361 8 comments 0 reactions 1 assignee View on GitHub

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
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
tooling

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

good for learning help wanted kind/bug-report skill/experienced triage/accepted triage/wont-fix

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quantumlib/Cirq

All issues in quantumlib/Cirq

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.