From 34e1c014f23c6fe903d509c5dad11390bee5b971 Mon Sep 17 00:00:00 2001 From: Lachermeier Date: Wed, 4 Aug 2021 10:02:20 +0200 Subject: [PATCH 01/10] override supports_aux_operators --- qiskit_optimization/runtime/vqe_program.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/qiskit_optimization/runtime/vqe_program.py b/qiskit_optimization/runtime/vqe_program.py index 2375dfb54..e7c641e04 100644 --- a/qiskit_optimization/runtime/vqe_program.py +++ b/qiskit_optimization/runtime/vqe_program.py @@ -222,6 +222,19 @@ def wrapped_callback(*args): else: return None + @classmethod + def supports_aux_operators(cls) -> bool: + """Whether computing the expectation value of auxiliary operators is supported. + + If the minimum eigensolver computes an eigenstate of the main operator then it + can compute the expectation value of the aux_operators for that state. Otherwise + they will be ignored. + + Returns: + True if aux_operator expectations can be evaluated, False otherwise + """ + return True + def compute_minimum_eigenvalue( self, operator: OperatorBase, aux_operators: Optional[List[Optional[OperatorBase]]] = None ) -> MinimumEigensolverResult: From 4422c9fd43236c4ef5c23bd059a99693348e4c4b Mon Sep 17 00:00:00 2001 From: Lachermeier Date: Wed, 4 Aug 2021 13:31:52 +0200 Subject: [PATCH 02/10] removed docstring --- qiskit_optimization/runtime/vqe_program.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/qiskit_optimization/runtime/vqe_program.py b/qiskit_optimization/runtime/vqe_program.py index e7c641e04..d0cf8598f 100644 --- a/qiskit_optimization/runtime/vqe_program.py +++ b/qiskit_optimization/runtime/vqe_program.py @@ -224,15 +224,6 @@ def wrapped_callback(*args): @classmethod def supports_aux_operators(cls) -> bool: - """Whether computing the expectation value of auxiliary operators is supported. - - If the minimum eigensolver computes an eigenstate of the main operator then it - can compute the expectation value of the aux_operators for that state. Otherwise - they will be ignored. - - Returns: - True if aux_operator expectations can be evaluated, False otherwise - """ return True def compute_minimum_eigenvalue( From 7d2aa749c70ceca561b60b6ea3766c4e2b7d4901 Mon Sep 17 00:00:00 2001 From: Lachermeier Date: Fri, 6 Aug 2021 17:30:48 +0200 Subject: [PATCH 03/10] added tests --- test/algorithms/test_min_eigen_optimizer.py | 56 ++++++++++++++++++--- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/test/algorithms/test_min_eigen_optimizer.py b/test/algorithms/test_min_eigen_optimizer.py index c91afaa18..d4b35999d 100644 --- a/test/algorithms/test_min_eigen_optimizer.py +++ b/test/algorithms/test_min_eigen_optimizer.py @@ -13,21 +13,18 @@ """ Test Min Eigen Optimizer """ import unittest -from test.optimization_test_case import ( - QiskitOptimizationTestCase, - requires_extra_library, -) import numpy as np from ddt import data, ddt from docplex.mp.model import Model - from qiskit import BasicAer from qiskit.algorithms import QAOA, VQE, NumPyMinimumEigensolver from qiskit.algorithms.optimizers import COBYLA, SPSA from qiskit.circuit.library import TwoLocal +from qiskit.providers.basicaer import QasmSimulatorPy from qiskit.utils import QuantumInstance, algorithm_globals -from qiskit_optimization.algorithms import CplexOptimizer, MinimumEigenOptimizer + +from qiskit_optimization.algorithms import CplexOptimizer, MinimumEigenOptimizer, MinimumEigenOptimizationResult from qiskit_optimization.algorithms.optimization_algorithm import ( OptimizationResultStatus, ) @@ -39,7 +36,13 @@ QuadraticProgramToQubo, ) from qiskit_optimization.problems import QuadraticProgram +from qiskit_optimization.runtime import VQEProgram, QAOAProgram from qiskit_optimization.translators import from_docplex_mp +from test.optimization_test_case import ( + QiskitOptimizationTestCase, + requires_extra_library, +) +from test.runtime.fake_vqeruntime import FakeRuntimeProvider @ddt @@ -353,6 +356,47 @@ def test_samples_vqe(self, simulator): self.assertAlmostEqual(results.raw_samples[0].fval, opt_sol) self.assertEqual(results.raw_samples[0].status, success) + def test_vqe_program(self): + """Test with VQEProgram""" + ry_ansatz = TwoLocal(5, "ry", "cz", reps=3, entanglement="full") + optimizer = {"name": "SPSA", "maxiter": 100} + initial_point = np.random.RandomState(42).random(ry_ansatz.num_parameters) + backend = QasmSimulatorPy() + provider = FakeRuntimeProvider() + + vqe_program = VQEProgram( + ansatz=ry_ansatz, + optimizer=optimizer, + initial_point=initial_point, + backend=backend, + provider=provider + ) + + vqe = MinimumEigenOptimizer(vqe_program) + results = vqe.solve(self.op_ordering) + + self.assertIsInstance(results, MinimumEigenOptimizationResult) + + def test_qaoa_program(self): + """Test with QAOAProgram""" + optimizer = {"name": "SPSA", "maxiter": 100} + reps = 2 + initial_point = np.random.RandomState(42).random(2 * reps) + backend = QasmSimulatorPy() + provider = FakeRuntimeProvider() + + qaoa_program = QAOAProgram( + optimizer=optimizer, + reps=reps, + initial_point=initial_point, + backend=backend, + provider=provider, + ) + + qaoa = MinimumEigenOptimizer(qaoa_program) + results = qaoa.solve(self.op_minimize) + + self.assertIsInstance(results, MinimumEigenOptimizationResult) if __name__ == "__main__": unittest.main() From f5f540e0383b81aa087a9b4dc9967dbbe90eb1f5 Mon Sep 17 00:00:00 2001 From: Lachermeier Date: Fri, 6 Aug 2021 17:31:43 +0200 Subject: [PATCH 04/10] added tests --- test/algorithms/test_min_eigen_optimizer.py | 76 ++++++++++----------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/test/algorithms/test_min_eigen_optimizer.py b/test/algorithms/test_min_eigen_optimizer.py index d4b35999d..e1bfbad3c 100644 --- a/test/algorithms/test_min_eigen_optimizer.py +++ b/test/algorithms/test_min_eigen_optimizer.py @@ -357,46 +357,46 @@ def test_samples_vqe(self, simulator): self.assertEqual(results.raw_samples[0].status, success) def test_vqe_program(self): - """Test with VQEProgram""" - ry_ansatz = TwoLocal(5, "ry", "cz", reps=3, entanglement="full") - optimizer = {"name": "SPSA", "maxiter": 100} - initial_point = np.random.RandomState(42).random(ry_ansatz.num_parameters) - backend = QasmSimulatorPy() - provider = FakeRuntimeProvider() - - vqe_program = VQEProgram( - ansatz=ry_ansatz, - optimizer=optimizer, - initial_point=initial_point, - backend=backend, - provider=provider - ) - - vqe = MinimumEigenOptimizer(vqe_program) - results = vqe.solve(self.op_ordering) - - self.assertIsInstance(results, MinimumEigenOptimizationResult) + """Test with VQEProgram""" + ry_ansatz = TwoLocal(5, "ry", "cz", reps=3, entanglement="full") + optimizer = {"name": "SPSA", "maxiter": 100} + initial_point = np.random.RandomState(42).random(ry_ansatz.num_parameters) + backend = QasmSimulatorPy() + provider = FakeRuntimeProvider() + + vqe_program = VQEProgram( + ansatz=ry_ansatz, + optimizer=optimizer, + initial_point=initial_point, + backend=backend, + provider=provider + ) + + vqe = MinimumEigenOptimizer(vqe_program) + results = vqe.solve(self.op_ordering) + + self.assertIsInstance(results, MinimumEigenOptimizationResult) def test_qaoa_program(self): - """Test with QAOAProgram""" - optimizer = {"name": "SPSA", "maxiter": 100} - reps = 2 - initial_point = np.random.RandomState(42).random(2 * reps) - backend = QasmSimulatorPy() - provider = FakeRuntimeProvider() - - qaoa_program = QAOAProgram( - optimizer=optimizer, - reps=reps, - initial_point=initial_point, - backend=backend, - provider=provider, - ) - - qaoa = MinimumEigenOptimizer(qaoa_program) - results = qaoa.solve(self.op_minimize) - - self.assertIsInstance(results, MinimumEigenOptimizationResult) + """Test with QAOAProgram""" + optimizer = {"name": "SPSA", "maxiter": 100} + reps = 2 + initial_point = np.random.RandomState(42).random(2 * reps) + backend = QasmSimulatorPy() + provider = FakeRuntimeProvider() + + qaoa_program = QAOAProgram( + optimizer=optimizer, + reps=reps, + initial_point=initial_point, + backend=backend, + provider=provider, + ) + + qaoa = MinimumEigenOptimizer(qaoa_program) + results = qaoa.solve(self.op_minimize) + + self.assertIsInstance(results, MinimumEigenOptimizationResult) if __name__ == "__main__": unittest.main() From d4bb49a6809ca432036a18bc56aafe8dc31573d3 Mon Sep 17 00:00:00 2001 From: Lachermeier Date: Fri, 6 Aug 2021 18:16:23 +0200 Subject: [PATCH 05/10] add release note --- test/algorithms/test_min_eigen_optimizer.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/algorithms/test_min_eigen_optimizer.py b/test/algorithms/test_min_eigen_optimizer.py index e1bfbad3c..aab284e9a 100644 --- a/test/algorithms/test_min_eigen_optimizer.py +++ b/test/algorithms/test_min_eigen_optimizer.py @@ -24,7 +24,11 @@ from qiskit.providers.basicaer import QasmSimulatorPy from qiskit.utils import QuantumInstance, algorithm_globals -from qiskit_optimization.algorithms import CplexOptimizer, MinimumEigenOptimizer, MinimumEigenOptimizationResult +from qiskit_optimization.algorithms import ( + CplexOptimizer, + MinimumEigenOptimizer, + MinimumEigenOptimizationResult, +) from qiskit_optimization.algorithms.optimization_algorithm import ( OptimizationResultStatus, ) @@ -369,7 +373,7 @@ def test_vqe_program(self): optimizer=optimizer, initial_point=initial_point, backend=backend, - provider=provider + provider=provider, ) vqe = MinimumEigenOptimizer(vqe_program) @@ -398,5 +402,6 @@ def test_qaoa_program(self): self.assertIsInstance(results, MinimumEigenOptimizationResult) + if __name__ == "__main__": unittest.main() From e9289b58a9ad96b074fe7ab3728ef10da0f05d1c Mon Sep 17 00:00:00 2001 From: Lachermeier Date: Tue, 10 Aug 2021 10:34:50 +0200 Subject: [PATCH 06/10] reordered imports + merged tests into one --- test/algorithms/test_min_eigen_optimizer.py | 74 +++++++++------------ 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/test/algorithms/test_min_eigen_optimizer.py b/test/algorithms/test_min_eigen_optimizer.py index aab284e9a..13aa1dd44 100644 --- a/test/algorithms/test_min_eigen_optimizer.py +++ b/test/algorithms/test_min_eigen_optimizer.py @@ -13,6 +13,11 @@ """ Test Min Eigen Optimizer """ import unittest +from test.optimization_test_case import ( + QiskitOptimizationTestCase, + requires_extra_library, +) +from test.runtime.fake_vqeruntime import FakeRuntimeProvider import numpy as np from ddt import data, ddt @@ -23,7 +28,6 @@ from qiskit.circuit.library import TwoLocal from qiskit.providers.basicaer import QasmSimulatorPy from qiskit.utils import QuantumInstance, algorithm_globals - from qiskit_optimization.algorithms import ( CplexOptimizer, MinimumEigenOptimizer, @@ -42,11 +46,6 @@ from qiskit_optimization.problems import QuadraticProgram from qiskit_optimization.runtime import VQEProgram, QAOAProgram from qiskit_optimization.translators import from_docplex_mp -from test.optimization_test_case import ( - QiskitOptimizationTestCase, - requires_extra_library, -) -from test.runtime.fake_vqeruntime import FakeRuntimeProvider @ddt @@ -360,47 +359,36 @@ def test_samples_vqe(self, simulator): self.assertAlmostEqual(results.raw_samples[0].fval, opt_sol) self.assertEqual(results.raw_samples[0].status, success) - def test_vqe_program(self): - """Test with VQEProgram""" - ry_ansatz = TwoLocal(5, "ry", "cz", reps=3, entanglement="full") - optimizer = {"name": "SPSA", "maxiter": 100} - initial_point = np.random.RandomState(42).random(ry_ansatz.num_parameters) - backend = QasmSimulatorPy() - provider = FakeRuntimeProvider() - - vqe_program = VQEProgram( - ansatz=ry_ansatz, - optimizer=optimizer, - initial_point=initial_point, - backend=backend, - provider=provider, - ) - - vqe = MinimumEigenOptimizer(vqe_program) - results = vqe.solve(self.op_ordering) - - self.assertIsInstance(results, MinimumEigenOptimizationResult) - - def test_qaoa_program(self): - """Test with QAOAProgram""" + @data('vqe', 'qaoa') + def test_runtime(self, subroutine): optimizer = {"name": "SPSA", "maxiter": 100} - reps = 2 - initial_point = np.random.RandomState(42).random(2 * reps) backend = QasmSimulatorPy() provider = FakeRuntimeProvider() - qaoa_program = QAOAProgram( - optimizer=optimizer, - reps=reps, - initial_point=initial_point, - backend=backend, - provider=provider, - ) - - qaoa = MinimumEigenOptimizer(qaoa_program) - results = qaoa.solve(self.op_minimize) - - self.assertIsInstance(results, MinimumEigenOptimizationResult) + if subroutine == 'vqe': + ry_ansatz = TwoLocal(5, "ry", "cz", reps=3, entanglement="full") + initial_point = np.random.RandomState(42).random(ry_ansatz.num_parameters) + solver = VQEProgram( + ansatz=ry_ansatz, + optimizer=optimizer, + initial_point=initial_point, + backend=backend, + provider=provider, + ) + else: + reps = 2 + initial_point = np.random.RandomState(42).random(2 * reps) + solver = QAOAProgram( + optimizer=optimizer, + reps=reps, + initial_point=initial_point, + backend=backend, + provider=provider, + ) + + opt = MinimumEigenOptimizer(solver) + result = opt.solve(self.op_ordering) + self.assertIsInstance(result, MinimumEigenOptimizationResult) if __name__ == "__main__": From 8b4434fc9f1e057a7e6b5d02e85e2c96a7daa392 Mon Sep 17 00:00:00 2001 From: Lachermeier Date: Fri, 13 Aug 2021 09:34:40 +0200 Subject: [PATCH 07/10] added method docstring --- test/algorithms/test_min_eigen_optimizer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/algorithms/test_min_eigen_optimizer.py b/test/algorithms/test_min_eigen_optimizer.py index 13aa1dd44..6dd04bc71 100644 --- a/test/algorithms/test_min_eigen_optimizer.py +++ b/test/algorithms/test_min_eigen_optimizer.py @@ -361,6 +361,7 @@ def test_samples_vqe(self, simulator): @data('vqe', 'qaoa') def test_runtime(self, subroutine): + """Test vqe and qaoa runtime""" optimizer = {"name": "SPSA", "maxiter": 100} backend = QasmSimulatorPy() provider = FakeRuntimeProvider() From 07d851829d0a237142ff513e492709a29cf66aed Mon Sep 17 00:00:00 2001 From: Lachermeier Date: Fri, 13 Aug 2021 10:29:16 +0200 Subject: [PATCH 08/10] fixed formatting --- test/algorithms/test_min_eigen_optimizer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/algorithms/test_min_eigen_optimizer.py b/test/algorithms/test_min_eigen_optimizer.py index 6dd04bc71..c87147aaf 100644 --- a/test/algorithms/test_min_eigen_optimizer.py +++ b/test/algorithms/test_min_eigen_optimizer.py @@ -359,14 +359,14 @@ def test_samples_vqe(self, simulator): self.assertAlmostEqual(results.raw_samples[0].fval, opt_sol) self.assertEqual(results.raw_samples[0].status, success) - @data('vqe', 'qaoa') + @data("vqe", "qaoa") def test_runtime(self, subroutine): """Test vqe and qaoa runtime""" optimizer = {"name": "SPSA", "maxiter": 100} backend = QasmSimulatorPy() provider = FakeRuntimeProvider() - if subroutine == 'vqe': + if subroutine == "vqe": ry_ansatz = TwoLocal(5, "ry", "cz", reps=3, entanglement="full") initial_point = np.random.RandomState(42).random(ry_ansatz.num_parameters) solver = VQEProgram( From 166fd49bbd826b39e7d396f906346a9c78fed698 Mon Sep 17 00:00:00 2001 From: lasys <43267480+lasys@users.noreply.github.com> Date: Mon, 16 Aug 2021 11:21:17 +0200 Subject: [PATCH 09/10] Update test/algorithms/test_min_eigen_optimizer.py Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com> --- test/algorithms/test_min_eigen_optimizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/algorithms/test_min_eigen_optimizer.py b/test/algorithms/test_min_eigen_optimizer.py index c87147aaf..ef2b7aa21 100644 --- a/test/algorithms/test_min_eigen_optimizer.py +++ b/test/algorithms/test_min_eigen_optimizer.py @@ -368,7 +368,7 @@ def test_runtime(self, subroutine): if subroutine == "vqe": ry_ansatz = TwoLocal(5, "ry", "cz", reps=3, entanglement="full") - initial_point = np.random.RandomState(42).random(ry_ansatz.num_parameters) + initial_point = np.random.default_rng(42).random(ry_ansatz.num_parameters) solver = VQEProgram( ansatz=ry_ansatz, optimizer=optimizer, From ceba63cea65bd0c7a3a9e04ab369dabe691ddaa8 Mon Sep 17 00:00:00 2001 From: lasys <43267480+lasys@users.noreply.github.com> Date: Mon, 16 Aug 2021 11:21:24 +0200 Subject: [PATCH 10/10] Update test/algorithms/test_min_eigen_optimizer.py Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com> --- test/algorithms/test_min_eigen_optimizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/algorithms/test_min_eigen_optimizer.py b/test/algorithms/test_min_eigen_optimizer.py index ef2b7aa21..c7f7f1643 100644 --- a/test/algorithms/test_min_eigen_optimizer.py +++ b/test/algorithms/test_min_eigen_optimizer.py @@ -378,7 +378,7 @@ def test_runtime(self, subroutine): ) else: reps = 2 - initial_point = np.random.RandomState(42).random(2 * reps) + initial_point = np.random.default_rng(42).random(2 * reps) solver = QAOAProgram( optimizer=optimizer, reps=reps,