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

VQD with primitives for k > 2 fails to validate parameter values #8982

Closed
JoelHBierman opened this issue Oct 21, 2022 · 1 comment · Fixed by #8989
Closed

VQD with primitives for k > 2 fails to validate parameter values #8982

JoelHBierman opened this issue Oct 21, 2022 · 1 comment · Fixed by #8989
Labels
bug Something isn't working mod: algorithms Related to the Algorithms module
Milestone

Comments

@JoelHBierman
Copy link

JoelHBierman commented Oct 21, 2022

Environment

  • Qiskit Terra version: 0.22.0
  • Python version: 3.9.6
  • Operating system: Fedora 36

What is happening?

When running VQD with primitives, the program fails to validate parameter values when calculating fidelities for k > 2. The ground and first excited states are found successfully, but when the algorithm gets to the second excited state, the sampler used for the fidelity fails to validate the parameters when VQD passes the parameters to the run() method of ComputeUncompute. This seems to have slipped past unit tests because those for VQD only test for k=1 and k=2.

How can we reproduce the issue?

The following code reproduces the issue:

from qiskit.algorithms.eigensolvers import VQD
from qiskit.algorithms.state_fidelities import ComputeUncompute
from qiskit.primitives import Estimator, Sampler
from qiskit.algorithms.optimizers import COBYLA
from qiskit.circuit.library import RealAmplitudes
from qiskit.quantum_info import Pauli
from qiskit_aer import AerSimulator

backend = AerSimulator(method='statevector')
qubit_op = Pauli('ZZ')
ansatz = RealAmplitudes(2)

cobyla = COBYLA()
estimator = Estimator()
sampler = Sampler()
fidelity = ComputeUncompute(sampler=sampler)
betas = [2, 2]

def print_intermediate_result(eval_count, params, estimated_value, estimation_metadata, current_step):

    print(f'eval count: {eval_count}, energy: {estimated_value}, step: {current_step}')

vqd_instance = VQD(estimator=estimator,
                   fidelity=fidelity,
                   ansatz=ansatz,
                   optimizer=cobyla,
                   k=3,
                   betas=betas,
                   initial_point=None,
                   callback=print_intermediate_result)

result = vqd_instance.compute_eigenvalues(operator=qubit_op)

print(result.eigenvalues)

This produces the following traceback:

capi_return is NULL
Call-back cb_calcfc_in__cobyla__user__routines failed.
Traceback (most recent call last):
  File "/home/joel/Desktop/electronic-structure-methods/test/VQD/VQD_ bugtest.py", line 32, in <module>
    result = vqd_instance.compute_eigenvalues(operator=qubit_op)
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/qiskit/algorithms/eigensolvers/vqd.py", line 256, in compute_eigenvalues
    opt_result = self.optimizer.minimize(
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/qiskit/algorithms/optimizers/scipy_optimizer.py", line 148, in minimize
    raw_result = minimize(
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/scipy/optimize/_minimize.py", line 705, in minimize
    res = _minimize_cobyla(fun, x0, args, constraints, callback=callback,
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/scipy/optimize/_cobyla_py.py", line 34, in wrapper
    return func(*args, **kwargs)
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/scipy/optimize/_cobyla_py.py", line 273, in _minimize_cobyla
    xopt, info = cobyla.minimize(calcfc, m=m, x=np.copy(x0), rhobeg=rhobeg,
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/scipy/optimize/_cobyla_py.py", line 261, in calcfc
    f = fun(np.copy(x), *args)
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/qiskit/algorithms/eigensolvers/vqd.py", line 363, in evaluate_energy
    costs = fidelity_job.result().fidelities
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/qiskit/primitives/primitive_job.py", line 50, in result
    return self._future.result()
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/concurrent/futures/_base.py", line 438, in result
    return self.__get_result()
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/concurrent/futures/_base.py", line 390, in __get_result
    raise self._exception
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/concurrent/futures/thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/qiskit/algorithms/state_fidelities/compute_uncompute.py", line 141, in _run
    job = self._sampler.run(circuits=circuits, parameter_values=values, **opts.__dict__)
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/qiskit/primitives/base/base_sampler.py", line 172, in run
    parameter_values = self._validate_parameter_values(
  File "/home/joel/miniconda3/envs/Qiskit-0390/lib/python3.9/site-packages/qiskit/primitives/base/base_primitive.py", line 108, in _validate_parameter_values
    raise TypeError("Invalid parameter values, expected Sequence[Sequence[float]].")
TypeError: Invalid parameter values, expected Sequence[Sequence[float]].

What should happen?

Algorithm completes successfully.

Any suggestions?

I am still looking into why this occurs, but my guess is that because the fidelity for the first excited state is computed successfully, there might be some error in the arguments that VQD passes to the run() method of ComputeUncompute on line 357 of vqd.py that is only problematic when len(prev_circs) > 1.

@JoelHBierman JoelHBierman added the bug Something isn't working label Oct 21, 2022
@woodsp-ibm woodsp-ibm added the mod: algorithms Related to the Algorithms module label Oct 21, 2022
@woodsp-ibm
Copy link
Member

@ElePT

@Cryoris Cryoris added this to the 0.22.1 milestone Oct 22, 2022
@mergify mergify bot closed this as completed in #8989 Oct 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: algorithms Related to the Algorithms module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants