diff --git a/qiskit_optimization/runtime/vqe_program.py b/qiskit_optimization/runtime/vqe_program.py index 2375dfb54..d0cf8598f 100644 --- a/qiskit_optimization/runtime/vqe_program.py +++ b/qiskit_optimization/runtime/vqe_program.py @@ -222,6 +222,10 @@ def wrapped_callback(*args): else: return None + @classmethod + def supports_aux_operators(cls) -> bool: + return True + def compute_minimum_eigenvalue( self, operator: OperatorBase, aux_operators: Optional[List[Optional[OperatorBase]]] = None ) -> MinimumEigensolverResult: diff --git a/test/algorithms/test_min_eigen_optimizer.py b/test/algorithms/test_min_eigen_optimizer.py index c91afaa18..c7f7f1643 100644 --- a/test/algorithms/test_min_eigen_optimizer.py +++ b/test/algorithms/test_min_eigen_optimizer.py @@ -17,17 +17,22 @@ QiskitOptimizationTestCase, requires_extra_library, ) +from test.runtime.fake_vqeruntime import FakeRuntimeProvider 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,6 +44,7 @@ QuadraticProgramToQubo, ) from qiskit_optimization.problems import QuadraticProgram +from qiskit_optimization.runtime import VQEProgram, QAOAProgram from qiskit_optimization.translators import from_docplex_mp @@ -353,6 +359,38 @@ 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") + def test_runtime(self, subroutine): + """Test vqe and qaoa runtime""" + optimizer = {"name": "SPSA", "maxiter": 100} + backend = QasmSimulatorPy() + provider = FakeRuntimeProvider() + + if subroutine == "vqe": + ry_ansatz = TwoLocal(5, "ry", "cz", reps=3, entanglement="full") + initial_point = np.random.default_rng(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.default_rng(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__": unittest.main()