-
Notifications
You must be signed in to change notification settings - Fork 71
Description
Environment
- Qiskit Algorithms version: 0.3.1
- Python version: 3.12.4
- Operating system: Ubuntu 24.04.1 LTS
What is happening?
There is a compatibility issue between BaseEstimatorGradient (from qiskit-algorithms) and StatevectorEstimator (from qiskit-primitives). Specifically, BaseEstimatorGradient expects the run method of the estimator to accept three positional arguments (circuits, observables, and parameter_values), but StatevectorEstimator.run() only accepts a single pubs argument (an iterable of (circuit, observable, parameter_values) tuples).
This mismatch causes the following error when using ParamShiftEstimatorGradient with StatevectorEstimator:
TypeError: StatevectorEstimator.run() takes 2 positional arguments but 4 were given
Additionally, even when using a custom wrapper to adapt the inputs, the job fails with:
AlgorithmError: 'Estimator job failed.'
How can we reproduce the issue?
from qiskit.circuit import QuantumCircuit, QuantumRegister, Parameter
from qiskit.quantum_info import SparsePauliOp
import numpy as np
# Instantiate the quantum circuit
a = Parameter("a")
b = Parameter("b")
q = QuantumRegister(1)
qc = QuantumCircuit(q)
qc.h(q)
qc.rz(a, q[0])
qc.rx(b, q[0])
#display(qc.draw("mpl"))
# Instantiate the Hamiltonian observable 2X+Z
H = SparsePauliOp.from_list([("X", 2), ("Z", 1)])
# Parameter list
params = [[np.pi / 4, 0]]
from qiskit.primitives import StatevectorEstimator
from qiskit_algorithms.gradients import ParamShiftEstimatorGradient
# Define the estimator
estimator = StatevectorEstimator()
# Define the gradient
gradient = ParamShiftEstimatorGradient(estimator)
# Evaluate the gradient of the circuits using parameter shift gradients
pse_grad_result = gradient.run(qc, H, params).result().gradients
print("State estimator gradient computed with parameter shift", pse_grad_result)
What should happen?
The run method of ParamShiftEstimatorGradient should internally adapt its inputs to match the pubs format expected by StatevectorEstimator.
Any suggestions?
Update ParamShiftEstimatorGradient:
Modify ParamShiftEstimatorGradient to use the pubs format when calling the underlying estimator's run method. For example:
pubs = list(zip(circuits, observables, parameter_values))
job = self._estimator.run(pubs=pubs, **options)
Add Compatibility Layer:
Provide a compatibility layer in qiskit-algorithms to handle differences between the old and new estimator APIs.