Skip to content

Commit

Permalink
Fix VQD's optimal_values (#10279) (#10312)
Browse files Browse the repository at this point in the history
* Fix optimal values

* Add unittest

* Add reno

* Update unittest

* Revert "Update unittest"

This reverts commit 5000d2f.

* Update test

* Make test subtest

(cherry picked from commit 93a8172)

Co-authored-by: Elena Peña Tapia <[email protected]>
  • Loading branch information
mergify[bot] and ElePT authored Jun 20, 2023
1 parent e0a1288 commit 485eb76
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/algorithms/eigensolvers/vqd.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def _update_vqd_result(
else np.array([opt_result.x])
)
result.optimal_parameters.append(dict(zip(ansatz.parameters, opt_result.x)))
result.optimal_values = np.concatenate([result.optimal_points, [opt_result.x]])
result.optimal_values = np.concatenate([result.optimal_values, [opt_result.fun]])
result.cost_function_evals = np.concatenate([result.cost_function_evals, [opt_result.nfev]])
result.optimizer_times = np.concatenate([result.optimizer_times, [eval_time]])
result.eigenvalues.append(opt_result.fun + 0j)
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/fix-vqd-result-27b26f0a6d49e7c7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed bug in :class:`~qiskit.algorithms.eigensolvers.VQD` where ``result.optimal_values`` was a
copy of ``result.optimal_points``. It now returns the corresponding values.
5 changes: 5 additions & 0 deletions test/python/algorithms/eigensolvers/test_vqd.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ def test_basic_operator(self, op):
)
np.testing.assert_array_almost_equal(job.result().values, result.eigenvalues, 6)

with self.subTest(msg="assert returned values are eigenvalues"):
np.testing.assert_array_almost_equal(
result.optimal_values, self.h2_energy_excited[:2], decimal=3
)

def test_full_spectrum(self):
"""Test obtaining all eigenvalues."""
vqd = VQD(self.estimator, self.fidelity, self.ryrz_wavefunction, optimizer=L_BFGS_B(), k=4)
Expand Down

0 comments on commit 485eb76

Please sign in to comment.