Skip to content

Measurement and control keys can overlap in the same moment after circuit.insert #7829

@codrut3

Description

@codrut3

Describe the issue

circuit.insert uses a helper method _group_into_moment_compatible to group operations that can be placed in the same moment, before attempting to insert them.

_group_into_moment_compatible doesn't check measurement and control keys, and so two possibly incompatible operations can be placed in the same moment.

Note that this happens only if strategy EARLIEST is used with an insertion index less than the number of moments. If the operations are always appended at the end of the circuit, then _PlacementCache is used, which checks for measurement and control keys.
Other helper methods for circuit.insert also check measurement and control keys (example).

It seems to me circuit.insert is inconsistent and I would like to understand what needs to be checked:

  • can two operations that measure qubits and have the same measurement key be in the same moment? For example, cirq.measure(q0, key="k") and cirq.measure(q1, key="k").
  • can two operations that use the same key for measurement and control be in the same moment? For example, cirq.measure(q0, key="k") followed by cirq.X(q1).with_classical_controls("k").

See also this comment by @daxfohl that mentioned that multiple measurements to the same key in the same moment was a requirement from the HW team.

Adding @ikd-sci and @chriseclectic for feedback if the current behavior is needed in some situations.

Explain how to reproduce the bug or problem

import cirq

q0, q1 = cirq.LineQubit.range(2)
c = cirq.Circuit(cirq.X(q0), cirq.X(q1))
ops_to_insert = [cirq.measure(q0, key="k"), cirq.X(q1).with_classical_controls("k")]
c.insert(0, ops_to_insert, strategy=cirq.InsertStrategy.EARLIEST)

print(repr(c))

Output:

cirq.Circuit([
    cirq.Moment(
        cirq.measure(cirq.LineQubit(0), key=cirq.MeasurementKey(name='k')),
        cirq.ClassicallyControlledOperation(cirq.X(cirq.LineQubit(1)), [cirq.KeyCondition(cirq.MeasurementKey(name='k'))]),
    ),
    cirq.Moment(
        cirq.X(cirq.LineQubit(0)),
        cirq.X(cirq.LineQubit(1)),
    ),
])

Tell us the version of Cirq where this happens

1.7.0.dev0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions