Skip to content

Compatibility Issue Between BaseEstimatorGradient and StatevectorEstimator #220

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

Open
VojtechNovak opened this issue Mar 10, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@VojtechNovak
Copy link

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.

@VojtechNovak VojtechNovak added the bug Something isn't working label Mar 10, 2025
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

1 participant