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

Unexpected behavior of copy_empty_like() for BlueprintCircuits #13535

Open
ryan-woo opened this issue Dec 5, 2024 · 3 comments
Open

Unexpected behavior of copy_empty_like() for BlueprintCircuits #13535

ryan-woo opened this issue Dec 5, 2024 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@ryan-woo
Copy link

ryan-woo commented Dec 5, 2024

Environment

  • Qiskit version: 1.3.0
  • Python version: 3.12.5
  • Operating system: MacOS

What is happening?

QuantumCircuit.copy_empty_like() copies RealAmplitudes instruction to the clone.

How can we reproduce the issue?

>>> from qiskit.circuit.library import RealAmplitudes
>>> circuit = RealAmplitudes(num_qubits=2, reps=2)
>>> circuit.draw()
     ┌────────────────────────────────────────────────┐
q_0: ┤0                                               ├
     │  RealAmplitudes(θ[0],θ[1],θ[2],θ[3],θ[4],θ[5]) │
q_1: ┤1                                               ├
     └────────────────────────────────────────────────┘
>>> circuit.copy_empty_like().draw()
     ┌────────────────────────────────────────────────┐
q_0: ┤0                                               ├
     │  RealAmplitudes(θ[0],θ[1],θ[2],θ[3],θ[4],θ[5]) │
q_1: ┤1                                               ├
     └────────────────────────────────────────────────┘
>>> circuit.copy_empty_like() == circuit
True
>>> 

What should happen?

The copy should not have a gate

Any suggestions?

No response

@ryan-woo ryan-woo added the bug Something isn't working label Dec 5, 2024
@jakelishman
Copy link
Member

This is because RealAmplitudes is a BlueprintCircuit, whose data model is entirely broken. This is a large reason why we're moving away from QuantumCircuit subclasses in the circuit library.

Honestly, I'm not even sure how we can reasonably fix this. I suspect it might need to be that BlueprintCircuit.copy_empty_like manually constructs a new QuantumCircuit (not the BlueprintCircuit subclass) with the right metadata, because anything else will not return an empty circuit, because how the circuit library currently uses QuantumCircuit subclasses is basically completely incompatible with the concept of copy_empty_like.

@mtreinish
Copy link
Member

I think returning a QuantumCircuit instead of the BlueprintCircuit object that will reconstruct it's ._data automatically on access of the copy makes the most sense. It is what I would expect based on the definition of copy_empty_like(). That approach is arguably a breaking api change, although a bit of grey area because we didn't document a specialized version of the method for BlueprintCircuit classes and the QuantumCircuit.copy_empty_like docs just say it returns a QuantumCircuit object. Although I know that's more just an artifact of our docs configuration and not actual intent. Luckily as long as we don't intend to backport this change our next release is 2.0 and I feel it's totally fine to change there. Although I hope @Cryoris was planning to remove all the BlueprintCircuit stuff for 2.0 which would solve this too.

Just for the record too, this bug existed prior to 1.3.0, I was worried that something we changed around copy_empty_like() in 1.3.0 caused a regression for BlueprintCircuit But this behavior has been there since at least Qiskit 1.0 and I suspect much longer.

@Cryoris Cryoris changed the title copy_empty_like() sometimes copies instructions. Unexpected behavior of copy_empty_like() for BlueprintCircuits Dec 7, 2024
@Cryoris
Copy link
Contributor

Cryoris commented Dec 9, 2024

Yeah I agree that BlueprintCircuit.copy_empty_like should just return an empty circuit. I guess most users are not aware that these objects are BlueprintCircuits and how these work, so the anticipation would be just a plain, empty QuantumCircuit.

Although I hope @Cryoris was planning to remove all the BlueprintCircuit stuff for 2.0 which would solve this too.

I was hoping this too, but this depends: do we want to keep the "old way" of the circuit library around for all of 2.0? Sure we could change the RealAmplitudes & co objects to be QuantumCircuits instead of BlueprintCircuits but then we'd have to deprecate the BlueprintCircuit-specific methods and change the handling of these circuits -- so if we keep them around, maybe the best course of action is to leave them as is for 2.0?

@Cryoris Cryoris added this to the 1.3.2 milestone Dec 12, 2024
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

No branches or pull requests

4 participants