-
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
Fix commutation of classical controls #4872
Conversation
Does this also fix issues with |
I can't make a blanket statement, but most optimizers should either use commutes protocol or circuit.append, and those would be fixed. I'm not aware of an issue with decompose. |
Running the test snippet from #4870 it looks like the operations are still hopping over one another and the simulation is breaking. Am I missing something ? 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)
cirq.SynchronizeTerminalMeasurements().optimize_circuit(c)
print(c)
cirq.Simulator().simulate(c) |
Ah, I didn't realize it's an inplace transformer, so the test I added doesn't do anything. Changing my test to Anyway, that whole transformer has major issues. It doesn't handle multi-qubit measurements either. The following breaks: def test_multi_qubit():
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
cirq.measure(q0, q1, key='m'), cirq.H(q1)
)
assert_optimizes(before=circuit, after=circuit.copy()) That said, I made the same error in the alight_right test, and after fixing the test, it turns red again. This time the problem is just that |
I'll add some extra tests to the commutes protocol before merging. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM % nit.
* Fix commutation of classical controls * Add moment.control_keys protocol * Improve handling of classical controls in commutation * move align tests
* Fix commutation of classical controls * Add moment.control_keys protocol * Improve handling of classical controls in commutation * move align tests
Fixes #4870: Adds logic to ensure classically controlled ops do not commute with measurements of the same key (fixes #4880). Also hardens the logic in circuit.append to apply this logic bidirectionally (fixes #4882 too).