-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Description of the issue
ClassicallyControlledOperation
was recently introduced to enable classical controls in Cirq. The method takes as input a sub_operation
and a list of conditions
, which correspond to measurement keys of previous measurements.
Cirq/cirq-core/cirq/ops/classically_controlled_operation.py
Lines 49 to 53 in 6119620
def __init__( | |
self, | |
sub_operation: 'cirq.Operation', | |
conditions: Sequence[Union[str, 'cirq.MeasurementKey', 'cirq.Condition', sympy.Basic]], | |
): |
The operations is implemented in a way that the qubit set it acts on remains the same as the set of qubits of the underlying operation to be controlled, and the operation "assumes" that measurements corresponding to the conditions have already occurred to the left of the operation.
This is a big problem for transformers because this introduces an implicit dependency between a measurement operation and a classically controlled operation which makes use of these measurement results.
How to reproduce the issue
>>> import cirq
>>> q = cirq.LineQubit.range(2)
>>> c = cirq.Circuit(cirq.H(q[0]), cirq.measure(q[0], key='m'), cirq.X(q[1]).with_classical_controls('m'))
>>> print(c)
0: ───H───M───────
║
1: ───────╫───X───
║ ║
m: ═══════@═══^═══
>>> cirq.SynchronizeTerminalMeasurements().optimize_circuit(c)
>>> print(c)
0: ───H───────────M───
║
1: ───────────X───╫───
║ ║
m: ═══════════^═══@═══
>>> cirq.Simulator().simulate(c)
ValueError: Measurement key m missing when testing classical control
Cirq version
0.14.0.dev