Skip to content

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

@arthurostrauss

Description

@arthurostrauss

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions