Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.1k stars 2.34k forks source link

EfficientSU2 attribute assign parameters not working properly #12641

Closed Wilmer-Maldonado closed 3 months ago

Wilmer-Maldonado commented 3 months ago

Environment

What is happening?

I assign parameters to EfficientSU2 circuit the same way I do with circuits I compose myself in qiskit with parametrized gates. Unfortunately, when I pass the dict {theta[0]: float, theta[1]: float ... so on for the number of parameters in this case 84} to .assign_parameters(dict) its not updating the parameters. It doesn't error out, the code executes but I get ParameterView([]) as output for circuit.parameters, so it didn't update correctly.

How can we reproduce the issue?

from qiskit import QuantumCircuit
from qiskit.circuit.library import EfficientSU2
from qiskit_algorithms.utils import algorithm_globals
from qiskit.circuit import ParameterVector

# Code with Bug 
algorithm_globals.random_seed = 123456

qc = QuantumCircuit(6)
qc.h(qc.qubits)

ansatz = EfficientSU2(6, reps=6)
qc.compose(ansatz, inplace=True)
qc.parameters

print(qc.draw())

theta_values = {}
wts = algorithm_globals.random.random(qc.num_parameters)
for i, param in enumerate(qc.parameters):
    theta_values[param] = wts[i]
print(theta_values)

qc_bound = qc.assign_parameters(theta_values)
qc_bound.parameters

Undesired output -> ParameterView([])

# What should happen? Sample Code of when it works for me just not for EfficientSU2

# Step 1: Create a ParameterVector with 2 elements
theta = ParameterVector('θ', 2)

# Step 2: Create a Quantum Circuit and add parameterized gates
qc = QuantumCircuit(1)
qc.rx(theta[0], 0)
qc.ry(theta[1], 0)

# Display the circuit
print(qc.draw())

# Step 3: Define the specific values for the parameters
theta_values = {theta[0]: 0.5, theta[1]: 1.0}
print(theta_values)
# Step 4: Bind the parameter values to the circuit
qc_bound = qc.assign_parameters(theta_values)

# Display the bound circuit
print(qc_bound.draw()) -> outputs updated circuit with correct updated theta values

Any suggestions?

No response

Wilmer-Maldonado commented 3 months ago

Resolved.