-
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
Inconsistent behavior of Default Insert Strategy #6711
Comments
csynque meeting - H gates for the q_3 and q_4 qubits at the 0 position seem incorrect, indeed it looks like a bug here. |
The code is pretty hard to follow, but definitely looks like https://github.com/quantumlib/Cirq/blob/main/cirq-core/cirq/circuits/circuit.py#L2167 causes the splitter index to increment (note it passes Also I haven't checked, but i assume |
Okay, yeah, if the first qubit already has a gate, it falls all the way back to However if the first qubit does not have a gate, then the But in general the splitter index incrementing in line 2167 seems to assume that all inserted operations are on the same qubits, and will thus be inserted after the other. This is a bad assumption, as this case shows. So the function needs to track what qubits have been inserted to what indexes, and determine Anyway, the behavior for multi-gate inserts in |
Tried a few things but nothing worked. Biggest issue is here Cirq/cirq-core/cirq/circuits/circuit.py Line 2117 in 79d562a
Here's a minimal test. One could argue, you're doing different things so it's expected the results will be different. Which is valid. But regardless I'd expect both of the below to have two Y gates at the beginning of the circuit. def test_insert_qubit_order():
q0, q1 = cirq.LineQubit.range(2)
c0 = cirq.Circuit(cirq.X(q0))
c0.insert(0, cirq.Y.on_each(q0, q1))
c1 = cirq.Circuit(cirq.X(q0))
c1.insert(0, cirq.Y.on_each(q1, q0)) # opposite order
assert c0 == c1 |
Found a fix: #6978 I'm not going to merge it yet (in fact I'll close it for now, to keep the PR list clean), because it's ugly and a little slow (test_append_speed about 30% slower). I think it also might need specific handling around measurement keys. But the idea is, instead of looking at one op at a time, to look at all consecutive new ops that could fit together on one moment, and if any of them would require inserting a new moment, then insert that new moment before placing any of them. That avoids the problem where the earlier ops of that group get shifted forward when the new moment is inserted after they've been placed. That at least proves out the reason for the problem. I'm not sure I'll have time to fix the perf or clean up the code. If anyone wants to take it over, feel free. |
Also note that while investigating the issue, I noticed that the existing behavior of INLINE is probably not what we really wanted, or what users would intuitively expect. In particular, Cirq/cirq-core/cirq/circuits/circuit_test.py Lines 861 to 868 in 79d562a
My guess is it was a result of the same problem; |
Description of the issue
The default insert strategy is placing the given list of gates in different positions.
How to reproduce the issue
mycircuit.qasm
Cirq version
You can get the cirq version by printing
cirq.__version__
. From the command line:The text was updated successfully, but these errors were encountered: