Skip to content
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

Deepcopy of control flow with standalone clbit results in invalid circuit #10409

Closed
mtreinish opened this issue Jul 7, 2023 · 3 comments · Fixed by #10411
Closed

Deepcopy of control flow with standalone clbit results in invalid circuit #10409

mtreinish opened this issue Jul 7, 2023 · 3 comments · Fixed by #10411
Assignees
Labels
bug Something isn't working
Milestone

Comments

@mtreinish
Copy link
Member

Environment

  • Qiskit Terra version: 0.24.1 (but also on main)
  • Python version: 3.11
  • Operating system: Linux

What is happening?

When running a deepcopy of a circuit object using a clbit as a standalone bit as the condition for a control flow operation results in the deepcopied circuit being invalid so that qasm3 or qpy serialization, and visualization will fail because the internal state of the circuit is corrupted and not valid in the copy.

How can we reproduce the issue?

import copy

from qiskit.circuit import QuantumCircuit, Qubit, Clbit, ClassicalRegister, QuantumRegister, Gate, Parameter

bits = [Qubit(), Qubit(), Clbit()]
qc = QuantumCircuit(bits)
with qc.if_test((qc.clbits[0], 1)):
    qc.x(0)

copy_qc = copy.deepcopy(qc)
print(copy_qc)

What should happen?

It should print the visualization of the circuit. This will stack trace though during the circuit to dag conversion. You can also replace print(copy_qc) with something like:

import io
from qiskit.qpy import dump

with io.BytesIO() as fd:
    dump(copy_qc, fd)

this will also fail on a lookup of the clbit.

Any suggestions?

I think the special __deepcopy__ handling in the qiskit.circuit.Instruction class is not valid for the ControlFlowOp when the .condition attribute is a clbit. It might just be simplest to revert to the default deepcopy behavior for ControlFlowOp.

@mtreinish mtreinish added the bug Something isn't working label Jul 7, 2023
@mtreinish mtreinish added this to the 0.25.0 milestone Jul 7, 2023
@jakelishman
Copy link
Member

It's the Bit instances that are the problem here, really - a deepcopy of an immutable object should return itself. If we make Bit.__deepcopy__ and __copy__ return self, it should all automatically fix itself.

The handling would fail for a deep-copied regular instruction with a single clbit condition as well, there's just not really any point in our tests where we'd try and and see the failure.

@mtreinish
Copy link
Member Author

Yeah that's fair, adding a Bit.__deepcopy__ to return self would fix this. That's easy enough to add.

@eendebakpt
Copy link
Contributor

Perhaps #10385 is related. There also a deepcopy of an internal structure results in an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants