-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ClassicallyControlledOperation
breaks assumption that disjoint operations commute.
#4870
Comments
Marking this as high priority as this has potential to impact the design of either transformers or classically controlled operations. My main question is: Can we make If not, we need to find another way to capture this implicit dependency for transformers. |
We already have logic in circuit.append to ensure that when you append a control, it does not fall back beyond the measurement that creates the key. Cirq/cirq-core/cirq/circuits/circuit.py Line 1967 in 39ce4f0
I don't think it would be good for usability if we require users to state which qubits correspond to the measurement key when instantiating the classically controlled operation. Maybe there'd be some way to hack this in such that there's a lookup somewhere that we can get the qubits corresponding to the key, and can avoid the user needing to specify it manually, but nothing comes to mind. Also the latter solution won't work once we allow repeated measurement keys, as each measurement could correspond to different qubits (potentially, depending on the design). So, I think the best solution is to reuse the logic from Circuit.append if possible. |
I think Dax has the right idea - this will require some deeper awareness of measure/control keys in optimizers (possibly handled by a new protocol?) but it shouldn't affect anything which doesn't make use of CCOps. |
Note that CCO is currently also broken with the In the code snippet above, >>> cirq.commutes(c[1][q[0]], c[2][q[1]]) # checking if measurement and CCO X gate commute. Should have returned False.
True # Should have returned False. |
Also, just to reiterate my concerns, I think the problem is that the mentality of assuming operations commute if they don't share qubits is very deeply ingrained in Cirq code (and I assume also in Cirq users). This would be a sudden and unexpected change against that mentality, and I'd very much like if we can somehow reduce the impact. Another example which breaks: >>> print(c)
0: ───H───M───────
║
1: ───────╫───X───
║ ║
m: ═══════@═══^═══
>>> cirq.AlignRight()(c)
>>> print(c)
┌──┐
0: ───H─────M────
║
1: ────────X╫────
║║
m: ════════^@════
└──┘ |
ClassicallyControlledOperation
breaks with circuit optimizations. ClassicallyControlledOperation
breaks assumption that disjoint operations commute.
Though I agree that including control qubits in the CCO operation is also not a good idea because it can then lead to other bigger issues. For example, it introduces an additional barrier which takes away flexibility of moving the controlled operation around. So, in the following circuit it wouldn't be possible to put 0: ───H───M───H───
║
1: ───────╫───X───
║ ║
m: ═══════@═══^═══ |
This also inspires an explicit |
There's a protocol |
The fix was pretty straightforward. I added a note that if we ever allow repeated measurements then those functions will need to account for that as well (maybe it's better to just go ahead and add that now). Timely enough, I just added some caching for the control keys of CircuitOperation in #4855, so this is a perfect use case! Plausibly we could extract the condition into a shared function somewhere, but that would require circuit.append to keep calling the protocol on the passed-in op, and slow it down. Anyway, rule-of-three. |
Seems to be an issue with CI, something unrelated in Pandas is broken for that PR but the test passes locally. |
Description of the issue
ClassicallyControlledOperation
was recently introduced to enable classical controls in Cirq. The method takes as input asub_operation
and a list ofconditions
, which correspond to measurement keys of previous measurements.Cirq/cirq-core/cirq/ops/classically_controlled_operation.py
Lines 49 to 53 in 6119620
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
Cirq version
cc @95-martin-orion @daxfohl
The text was updated successfully, but these errors were encountered: