-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Flattened conditional circuits give incorrect results #6583
Comments
Follow up building with a flat circuit this way looks to give correct result: teleport = QuantumCircuit(3, 2)
teleport.h(1)
teleport.cx(1, 2)
teleport.cx(0, 1)
teleport.h(0)
teleport.measure(0, 0)
teleport.measure(1, 1)
creg = teleport.cregs[0]
teleport.z(2).c_if(creg[0], 1)
teleport.x(2).c_if(creg[1], 1)
# Compose with another circuit
circ = QuantumCircuit(3, 3)
circ = circ.compose(teleport, range(3), range(2))
circ.measure(2, 2) assembled qobj
However this circuit breaks the drawer:
Stack trace:
|
I believe this is an issue in After #6018 , this should at least be possible for the case where gates are conditioned on a single The drawing error I believe is fixed by #6261 . |
I think that this trouble is not just with single-bit cregs. For instance take the following circuit q = QuantumRegister(2)
c = ClassicalRegister(2)
qc = QuantumCircuit(q, c)
qc.h(0).c_if(c, 1)
circ = QuantumCircuit(3,3)
circ = circ.compose(qc, [0,1], [1,2]) This circuit outputs
This error seems to happen whenever a circuit As for the conditioning on single However, we could extend the type checking to also check if the conditioned register is of length 1 and then if the length was 1, then mapping of condition could proceed similar to that done for a if isinstance(condition[0], Clbit):
cond_creg = [condition[0]]
elif len(list(condition[0]))==1:
cond_creg = list(condition[0])
else:
cond_creg = condition[0]
is_reg = True
... in line 584 in the |
The drawer issue is now fixed by #7285 (#6261 didn't appear to get us all the way when I tested it just now). For the rest: the conditions issue could now be solved by adding additional classical registers to the circuit after composition, that cover the specific bits we need - now that a bit is the fundamental data unit, and registers are just aliases to collections of bits, we can add what we need to make the condition work. In practice, though, |
Bumping this again because its still an issue (see qiskit-community/qiskit-experiments#943 for more details). The core issue seems to be if I have Eg to use the teleport example: from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
qr = QuantumRegister(3, 'qr')
c0 = ClassicalRegister(1, 'c0')
c1 = ClassicalRegister(1, 'c1')
circuit1 = QuantumCircuit(qr, c0, c1)
circuit1.h(1)
circuit1.cx(1, 2)
circuit1.cx(0, 1)
circuit1.h(0)
circuit1.measure(0, c0)
circuit1.measure(1, c1)
circuit1.z(2).c_if(c0, 1)
circuit1.x(2).c_if(c1, 1)
circuit1.draw()
Composing with a flat register circuit2 = QuantumCircuit(3, 2)
circuit2.compose(circuit1, inplace=True)
circuit2.draw() gives
Composing with an enlarged register circuit3 = QuantumCircuit(3, 4)
circuit3.compose(circuit1, clbits=[0, 1], inplace=True)
circuit3.draw() gives
|
I will prepare a fix for this for Terra 0.23. There's other stuff in |
I've made #9206 that should fix this. Just to note: the drawers don't handle overlapping registers correctly, so it may well still look like there's an issue, but if you inspect the objects to look in their conditions, registers and the bits in those conditions, you should find it's correct. I've checked that the Aer simulations at the top work as expected with them, but I can't make any promises about how well it'll serialise to actual backends - I'm not sure how well the aliasing register model will transport. |
Information
What is the current behavior?
Flattening a conditional circuit gives incorrect results. It looks like the issue is probably in how the conditionals are implemented:
Steps to reproduce the problem
Manually adding an additional registers to a teleport circuit works:
But composing and flattening gives wrong answer:
What is the expected behavior?
Flattening the conditional circuit should implement the same circuit.
Suggested solutions
From the drawer it looks liek the flattened circuit is handling conditions on the joint circuit wrong:
Looks like conditions are triggering on creg values
01
and10
, but nothing on11
which should trigger both conditional gates.Looking at the qobj that gets assembled from these circuits we can see the difference:
Multi-register assembled qobj instructions:
assemble(circ).experiments[0].instructions =
Flattened circuit assembled qobj instructions:
You can see that in the second case the
mask
value is being set incorrectly to0x7 = 0b111
in both case to the full 3-bit register rather than the the single bit values.The text was updated successfully, but these errors were encountered: