From 772ad469bc8adbfca4046cc853606c70bc0e9a5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Wed, 14 Jun 2023 15:06:04 +0200 Subject: [PATCH 1/7] Fix optimal values --- qiskit/algorithms/eigensolvers/vqd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit/algorithms/eigensolvers/vqd.py b/qiskit/algorithms/eigensolvers/vqd.py index 6a0c8a0842b4..cbb4d56af313 100644 --- a/qiskit/algorithms/eigensolvers/vqd.py +++ b/qiskit/algorithms/eigensolvers/vqd.py @@ -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) From e67da54a021ffa918ee6c29ad4ae5e4ad90dcbc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Thu, 15 Jun 2023 14:03:22 +0200 Subject: [PATCH 2/7] Add unittest --- .../python/algorithms/eigensolvers/test_vqd.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/python/algorithms/eigensolvers/test_vqd.py b/test/python/algorithms/eigensolvers/test_vqd.py index 16652a259ebd..1c2b27d06a38 100644 --- a/test/python/algorithms/eigensolvers/test_vqd.py +++ b/test/python/algorithms/eigensolvers/test_vqd.py @@ -200,6 +200,24 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step): np.testing.assert_array_almost_equal(history["mean"], ref_mean, decimal=2) np.testing.assert_array_almost_equal(history["step"], ref_step, decimal=0) + @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) + def test_optimal_values(self, op): + """Test running same VQD twice to re-use optimizer, then switch optimizer""" + + vqd = VQD( + estimator=self.estimator, + fidelity=self.fidelity, + ansatz=RealAmplitudes(), + optimizer=SLSQP(), + k=2, + betas=self.betas, + ) + + result = vqd.compute_eigenvalues(operator=op) + np.testing.assert_array_almost_equal( + result.optimal_points, self.h2_energy_excited, decimal=3 + ) + @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) def test_vqd_optimizer(self, op): """Test running same VQD twice to re-use optimizer, then switch optimizer""" From f894a7eebf6b19429ddbbc4c5c39295b5497694c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Thu, 15 Jun 2023 14:09:27 +0200 Subject: [PATCH 3/7] Add reno --- releasenotes/notes/fix-vqd-result-27b26f0a6d49e7c7.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 releasenotes/notes/fix-vqd-result-27b26f0a6d49e7c7.yaml diff --git a/releasenotes/notes/fix-vqd-result-27b26f0a6d49e7c7.yaml b/releasenotes/notes/fix-vqd-result-27b26f0a6d49e7c7.yaml new file mode 100644 index 000000000000..8b2854c36da0 --- /dev/null +++ b/releasenotes/notes/fix-vqd-result-27b26f0a6d49e7c7.yaml @@ -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. From 5000d2fb02e8126b376d9cd30d2862c202af2755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Thu, 15 Jun 2023 14:13:50 +0200 Subject: [PATCH 4/7] Update unittest --- test/python/algorithms/eigensolvers/test_vqd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/python/algorithms/eigensolvers/test_vqd.py b/test/python/algorithms/eigensolvers/test_vqd.py index 1c2b27d06a38..fa191995fe81 100644 --- a/test/python/algorithms/eigensolvers/test_vqd.py +++ b/test/python/algorithms/eigensolvers/test_vqd.py @@ -202,7 +202,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step): @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) def test_optimal_values(self, op): - """Test running same VQD twice to re-use optimizer, then switch optimizer""" + """Test the VQD result.optimal_values field""" vqd = VQD( estimator=self.estimator, @@ -215,7 +215,7 @@ def test_optimal_values(self, op): result = vqd.compute_eigenvalues(operator=op) np.testing.assert_array_almost_equal( - result.optimal_points, self.h2_energy_excited, decimal=3 + result.optimal_values[-1], self.h2_energy_excited, decimal=3 ) @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) From 463e5393b8562623358e76f096d877b70edfb8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Thu, 15 Jun 2023 14:42:19 +0200 Subject: [PATCH 5/7] Revert "Update unittest" This reverts commit 5000d2fb02e8126b376d9cd30d2862c202af2755. --- test/python/algorithms/eigensolvers/test_vqd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/python/algorithms/eigensolvers/test_vqd.py b/test/python/algorithms/eigensolvers/test_vqd.py index fa191995fe81..1c2b27d06a38 100644 --- a/test/python/algorithms/eigensolvers/test_vqd.py +++ b/test/python/algorithms/eigensolvers/test_vqd.py @@ -202,7 +202,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step): @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) def test_optimal_values(self, op): - """Test the VQD result.optimal_values field""" + """Test running same VQD twice to re-use optimizer, then switch optimizer""" vqd = VQD( estimator=self.estimator, @@ -215,7 +215,7 @@ def test_optimal_values(self, op): result = vqd.compute_eigenvalues(operator=op) np.testing.assert_array_almost_equal( - result.optimal_values[-1], self.h2_energy_excited, decimal=3 + result.optimal_points, self.h2_energy_excited, decimal=3 ) @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) From 1e0b8e4cc63b6a8f36530776e3610f8bb37660c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Thu, 15 Jun 2023 15:43:21 +0200 Subject: [PATCH 6/7] Update test --- test/python/algorithms/eigensolvers/test_vqd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/python/algorithms/eigensolvers/test_vqd.py b/test/python/algorithms/eigensolvers/test_vqd.py index 1c2b27d06a38..35a16be08b3b 100644 --- a/test/python/algorithms/eigensolvers/test_vqd.py +++ b/test/python/algorithms/eigensolvers/test_vqd.py @@ -215,7 +215,7 @@ def test_optimal_values(self, op): result = vqd.compute_eigenvalues(operator=op) np.testing.assert_array_almost_equal( - result.optimal_points, self.h2_energy_excited, decimal=3 + result.optimal_values, self.h2_energy_excited[:2], decimal=3 ) @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) From bfa1bacccfc2ba7de5f0ee2fb7029b92a2c1ec1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Fri, 16 Jun 2023 14:07:13 +0200 Subject: [PATCH 7/7] Make test subtest --- .../algorithms/eigensolvers/test_vqd.py | 23 ++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/test/python/algorithms/eigensolvers/test_vqd.py b/test/python/algorithms/eigensolvers/test_vqd.py index 35a16be08b3b..a2520fcec815 100644 --- a/test/python/algorithms/eigensolvers/test_vqd.py +++ b/test/python/algorithms/eigensolvers/test_vqd.py @@ -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) @@ -200,24 +205,6 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step): np.testing.assert_array_almost_equal(history["mean"], ref_mean, decimal=2) np.testing.assert_array_almost_equal(history["step"], ref_step, decimal=0) - @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) - def test_optimal_values(self, op): - """Test running same VQD twice to re-use optimizer, then switch optimizer""" - - vqd = VQD( - estimator=self.estimator, - fidelity=self.fidelity, - ansatz=RealAmplitudes(), - optimizer=SLSQP(), - k=2, - betas=self.betas, - ) - - result = vqd.compute_eigenvalues(operator=op) - np.testing.assert_array_almost_equal( - result.optimal_values, self.h2_energy_excited[:2], decimal=3 - ) - @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) def test_vqd_optimizer(self, op): """Test running same VQD twice to re-use optimizer, then switch optimizer"""