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

Parameter assignment of QuantumCircuitdoes not update the data attribute when inplace=True #13478

Closed
arthurostrauss opened this issue Nov 22, 2024 · 1 comment · Fixed by #13482
Labels
bug Something isn't working

Comments

@arthurostrauss
Copy link
Contributor

arthurostrauss commented Nov 22, 2024

Environment

  • Qiskit version: 1.2.4
  • Python version: 3.10
  • Operating system: Mac OS Sequoia

What is happening?

When writing the following:

import numpy as np

from qiskit.circuit.library import TwoLocal

num_qubits = 6
n_layers = 4

qc = TwoLocal(num_qubits, "rx", "cx", entanglement=[[(0, 1), (2, 3), (4, 5)], [(1, 2), (3, 4)]], reps=n_layers, flatten=True, skip_unentangled_qubits=True, skip_final_rotation_layer=True)
qc.assign_parameters(rotation_angles, inplace=True)
print(qc.data)

we get:

[CircuitInstruction(operation=Instruction(name='rx', num_qubits=1, num_clbits=0, params=[ParameterVectorElement(θ[0])]), qubits=(Qubit(QuantumRegister(6, 'q'), 0),), clbits=()), CircuitInstruction(operation=Instruction(name='rx', num_qubits=1, num_clbits=0, params=[ParameterVectorElement(θ[1])]), ...]

Meaning that instructions still store the reference to the original Parameters, which should have been replaced by the values.

This behavior does not appear when doing

qc=qc.assign_parameters(rotation_angles, inplace=False)

where in this case the binding is clearly visible on the instructions

How can we reproduce the issue?

import numpy as np

from qiskit.circuit.library import TwoLocal

num_qubits = 6
n_layers = 4

qc = TwoLocal(num_qubits, "rx", "cx", entanglement=[[(0, 1), (2, 3), (4, 5)], [(1, 2), (3, 4)]], reps=n_layers, flatten=True, skip_unentangled_qubits=True, skip_final_rotation_layer=True)

rotation_angles = np.random.random(qc.num_parameters)
qc.assign_parameters(rotation_angles, inplace=True)
print(qc.data)

What should happen?

Instructions should be binded with the numeric values in both cases. One should therefore take care of the inplace=True case to match the other case.

Any suggestions?

No response

@arthurostrauss arthurostrauss added the bug Something isn't working label Nov 22, 2024
@Cryoris
Copy link
Contributor

Cryoris commented Nov 22, 2024

It looks like this is caused by the caching of Python gates in the Rust-side CircuitData. Likely the cache is not properly cleared and the gate re-uses the old definition. Until we fix this, you can turn the caching off by installing Qiskit from source and then do

export QISKIT_NO_CACHE_GATES=1   <-- tell Qiskit not to cache gates
python setup.py build_rust --inplace --release  <-- rebuild Rust in release mode

Edit: This only happens with flatten=True. Using a plain circuit or using flatten=False followed by .decompose() does not raise this. Maybe flatten=True somehow builds an inconsistent/incompatible state...

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.

2 participants