Skip to content

copy.deepcopy doesn't work on instances of QuantumCircuit#4

Open
malee4 wants to merge 3 commits intomainfrom
kernel_debug
Open

copy.deepcopy doesn't work on instances of QuantumCircuit#4
malee4 wants to merge 3 commits intomainfrom
kernel_debug

Conversation

@malee4
Copy link

@malee4 malee4 commented Sep 7, 2025

Changes made:

  1. Add pylatexenc and ipykernel to the list of installs in setup.py
  2. Change copy.deepcopy() instances in LogicalQ/Experiments.py to QuantumCircuit.copy() to prevent the kernel crash encountered as described here: copy.deepcopy doesn't work on QuantumCircuit Qiskit/qiskit#11610.

TODO:

  • Refactor to optimize list functions and for loops
  • Running execute_circuit with H2-1 on Mac M1 chips causes kernel crashes. The issue lies somewhere in the noise model with the amplitude damping error.

@RasmitDevkota
Copy link
Member

Hey, thanks for making the branch and opening the PR! A few quick questions:

  1. Have we verified that the QuantumCircuit.copy method behaves well with LogicalCircuit? I ask because, off the top of my head, I'm not fully aware of what that method does, and so I don't know, for example, whether it preserves our special metadata and box labels which are necessary during execute_circuits.
  2. What is ipykernel necessary for? Also, I've been reluctant to add pylatexenc to setup.py - since it isn't an explicit import and I'm not sure of any reason why code that doesn't use it wouldn't run without the import, I would classify it as an optional dependency, which it is generally best practice not to force the user to install. In the case of pytket imports, it seemed like you and others simply couldn't run anything without having those imports, which makes sense because it is an explicit import on our end, so we sort of put ourself into the rabbit hole of forcing the user to install those, but it's definitely not best practice because most users will likely never have to touch pytket. Open to any opinions on this though! Maybe we can argue that pylatexenc is such an important method for visualization that the user should just install it anyways?

@RasmitDevkota RasmitDevkota changed the title Kernel debug copy.deepcopy doesn't work on instances of QuantumCircuit Sep 8, 2025
@RasmitDevkota
Copy link
Member

RasmitDevkota commented Sep 8, 2025

For your TODO's, I'd say let's merge them in a separate PR - this is a quick and easy PR to get in whereas the kernel debugging will be more involved, so let's work on that in a separate PR. I'd like to get these changes merged ASAP so we can pull them into the compat-qtm branch.

@malee4
Copy link
Author

malee4 commented Sep 8, 2025 via email

@RasmitDevkota
Copy link
Member

Thanks for the quick response!

  1. All objects passed in are indeed instances of QuantumCircuit, but our primary use for this method is in fact LogicalCircuits. A quick scan through thr underlying Qiskit source confirms the Qiskit copy method will try to preserve metadata, so my new question is whether it preserves our metadata and, if not, whether or not that metadata is even needed in execute_circuits. I will have to spend more time looking at the Qiskit source to understand what all is happening there. Here's where that code starts if you'd like to take a look as well. https://github.com/Qiskit/qiskit/blob/d16e6c919c48737081ca9aef71fb5ce8a30c68b9/qiskit/circuit/quantumcircuit.py#L4102-L4113
  2. I absolutely agree that I'd like the default user behavior to be seamless, maybe the minimal installation principle is just something I and a minority of other users would like to follow 😅. For motivation, the reason I bring it up is that maintaining package compatibility can become really annoying with Qiskit, but maybe we can just pull these changes and provide an alternative install option for minimal dependencies.

tl;dr Everything looks good once we check that the copy method won't break our LogicalCircuits!

@malee4
Copy link
Author

malee4 commented Sep 8, 2025 via email

@RasmitDevkota
Copy link
Member

RasmitDevkota commented Sep 8, 2025

Sounds good, I would maybe actually recommend just starting with a test case in a notebook - make a LogicalCircuit with QEC cycles, copy it, and check that it looks correct. I'm actually 99% certain the copy will break the LogicalCircuit structure and turn it into a regular QuantumCircuit, but as long as the circuit data is fully preserved that's OK - it will happen anyways during transpilation steps. What I'm worried about personally is whether it will affect our instructions, like will it preserve box labels? Those are absolutely necessary.

@RasmitDevkota
Copy link
Member

Also, it looks like ipykernel is installed with jupyter, so I don't think it's necessary that we specify it explicitly.

@RasmitDevkota
Copy link
Member

Just checking in, were you able to test the copy method out? Let me know if you need any help with this!

@RasmitDevkota RasmitDevkota added the bug Something isn't working label Sep 21, 2025
@RasmitDevkota RasmitDevkota self-assigned this Sep 21, 2025
@RasmitDevkota
Copy link
Member

RasmitDevkota commented Oct 24, 2025

Just coming back to this during other testing to note that it looks like the LogicalCircuit.copy method (just from us inheriting QuantumCircuit.copy) maintains the LogicalCircuit type, but this doesn't mean it fully deepcopies the underlying data structures. Some preliminary testing suggests it should be, but I'd still like to see more rigorous testing just in case.

(Image courtesy @NolanHeffner)
image

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 this pull request may close these issues.

2 participants