diff --git a/docs/tutorials/05_qaoa.ipynb b/docs/tutorials/05_qaoa.ipynb index 2c16c417..cf8801b9 100644 --- a/docs/tutorials/05_qaoa.ipynb +++ b/docs/tutorials/05_qaoa.ipynb @@ -312,7 +312,7 @@ "from qiskit_algorithms import SamplingVQE\n", "from qiskit_algorithms.utils import algorithm_globals\n", "\n", - "algorithm_globals.random_seed = 10598\n", + "algorithm_globals.random_seed = 13345\n", "\n", "optimizer = COBYLA()\n", "ansatz = TwoLocal(qubit_op.num_qubits, \"ry\", \"cz\", reps=2, entanglement=\"linear\")\n", @@ -385,7 +385,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.0" + "version": "3.12.9" }, "vscode": { "interpreter": { diff --git a/test/eigensolvers/test_vqd.py b/test/eigensolvers/test_vqd.py index c95d798c..a4d2fbe7 100644 --- a/test/eigensolvers/test_vqd.py +++ b/test/eigensolvers/test_vqd.py @@ -1,6 +1,6 @@ # This code is part of a Qiskit project. # -# (C) Copyright IBM 2022, 2024. +# (C) Copyright IBM 2022, 2025. # # This code is licensed under the Apache License, Version 2.0. You may # obtain a copy of this license in the LICENSE.txt file in the root directory @@ -16,7 +16,9 @@ from test import QiskitAlgorithmsTestCase import numpy as np +import scipy from ddt import data, ddt +from packaging.version import Version from qiskit import QuantumCircuit from qiskit.circuit.library import TwoLocal, RealAmplitudes @@ -175,7 +177,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step): history["metadata"].append(metadata) history["step"].append(step) - optimizer = COBYLA(maxiter=3) + optimizer = COBYLA(maxiter=12) wavefunction = self.ry_wavefunction vqd = VQD( @@ -185,6 +187,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step): optimizer=optimizer, callback=store_intermediate_result, betas=self.betas, + initial_point=[1] * 8, ) vqd.compute_eigenvalues(operator=op) @@ -196,11 +199,98 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step): for params in history["parameters"]: self.assertTrue(all(isinstance(param, float) for param in params)) - ref_eval_count = [1, 2, 3, 1, 2, 3] - ref_mean = [-1.07, -1.45, -1.36, 1.24, 1.55, 1.07] - # new ref_mean since the betas were changed + ref_eval_count = [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + ] - ref_step = [1, 1, 1, 2, 2, 2] + ref_mean_pre_1_16 = [ + -1.08, + -1.08, + -1.0, + -1.14, + -1.17, + -1.38, + -1.0, + -1.63, + -1.45, + -1.55, + -1.63, + -1.75, + -1.04, + -1.07, + -0.72, + -0.46, + -0.71, + -0.56, + -0.92, + -0.29, + -0.89, + -0.38, + -1.06, + -1.05, + ] + ref_mean_1_16 = [ + -1.08, + -1.08, + -1.0, + -1.14, + -1.17, + -1.38, + -1.0, + -1.63, + -1.45, + -1.55, + -1.63, + -1.75, + -1.04, + -1.07, + -0.72, + -0.46, + -0.71, + -0.56, + -0.92, + -0.29, + -0.89, + -0.38, + -0.97, + -1.16, + ] + # Unlike in other places where COYBLA is used in tests and differences arose between + # pre 1.16.0 versions and after, where in 1.16.0 scipy changed the COBYLA + # implementation, I was not able to find changes that would reproduce the outcome so + # tests passed no matter whether 1.16 or before was installed. Here the mean outcomes + # match all but the last 2 values so I thought about comparing a subset but in the + # end decided to go with different reference values based on scipy version + ref_mean = ( + ref_mean_pre_1_16 + if Version(scipy.version.version) < Version("1.16.0") + else ref_mean_1_16 + ) + + ref_step = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] np.testing.assert_array_almost_equal(history["eval_count"], ref_eval_count, decimal=0) np.testing.assert_array_almost_equal(history["mean"], ref_mean, decimal=2) @@ -410,7 +500,7 @@ def test_aux_operator_std_dev(self, op): 0.2442925, -1.51638917, ], - optimizer=COBYLA(maxiter=0), + optimizer=COBYLA(maxiter=10), betas=self.betas, ) @@ -423,7 +513,7 @@ def test_aux_operator_std_dev(self, op): # expectation values self.assertAlmostEqual(result.aux_operators_evaluated[0][0][0], 2.0, places=1) self.assertAlmostEqual( - result.aux_operators_evaluated[0][1][0], 0.0019531249999999445, places=1 + result.aux_operators_evaluated[0][1][0], 0.7432341813894455, places=1 ) # metadata self.assertIsInstance(result.aux_operators_evaluated[0][0][1], dict) @@ -435,9 +525,7 @@ def test_aux_operator_std_dev(self, op): self.assertEqual(len(result.aux_operators_evaluated[0]), 4) # expectation values self.assertAlmostEqual(result.aux_operators_evaluated[0][0][0], 2.0, places=1) - self.assertAlmostEqual( - result.aux_operators_evaluated[0][1][0], 0.0019531249999999445, places=1 - ) + self.assertAlmostEqual(result.aux_operators_evaluated[0][1][0], 0.743234181389445, places=1) self.assertEqual(result.aux_operators_evaluated[0][2][0], 0.0) self.assertEqual(result.aux_operators_evaluated[0][3][0], 0.0) # metadata diff --git a/test/minimum_eigensolvers/test_qaoa.py b/test/minimum_eigensolvers/test_qaoa.py index 17a15773..9770e7a2 100644 --- a/test/minimum_eigensolvers/test_qaoa.py +++ b/test/minimum_eigensolvers/test_qaoa.py @@ -1,6 +1,6 @@ # This code is part of a Qiskit project. # -# (C) Copyright IBM 2022, 2023. +# (C) Copyright IBM 2022, 2025. # # This code is licensed under the Apache License, Version 2.0. You may # obtain a copy of this license in the LICENSE.txt file in the root directory @@ -120,7 +120,7 @@ def test_qaoa_qc_mixer(self, w, prob, solutions): def test_qaoa_qc_mixer_many_parameters(self): """QAOA test with a mixer as a parameterized circuit with the num of parameters > 1.""" - optimizer = COBYLA() + optimizer = COBYLA(maxiter=10000) qubit_op, _ = self._get_operator(W1) num_qubits = qubit_op.num_qubits @@ -129,7 +129,7 @@ def test_qaoa_qc_mixer_many_parameters(self): theta = Parameter("θ" + str(i)) mixer.rx(theta, range(num_qubits)) - qaoa = QAOA(self.sampler, optimizer, reps=2, mixer=mixer) + qaoa = QAOA(self.sampler, optimizer, reps=2, mixer=mixer, initial_point=[1] * 10) result = qaoa.compute_minimum_eigenvalue(operator=qubit_op) x = self._sample_most_likely(result.eigenstate) self.log.debug(x) @@ -180,7 +180,7 @@ def test_change_operator_size(self): with self.subTest(msg="QAOA 6x6"): self.assertIn(graph_solution, {"010101", "101010"}) - @idata([[W2, S2, None], [W2, S2, [0.0, 0.0]], [W2, S2, [1.0, 0.8]]]) + @idata([[W2, S2, None], [W2, S2, [0.0, 0.5]], [W2, S2, [1.0, 0.8]]]) @unpack def test_qaoa_initial_point(self, w, solutions, init_pt): """Check first parameter value used is initial point as expected""" diff --git a/test/minimum_eigensolvers/test_sampling_vqe.py b/test/minimum_eigensolvers/test_sampling_vqe.py index 35b84a3a..a6452ba2 100644 --- a/test/minimum_eigensolvers/test_sampling_vqe.py +++ b/test/minimum_eigensolvers/test_sampling_vqe.py @@ -1,6 +1,6 @@ # This code is part of a Qiskit project. # -# (C) Copyright IBM 2018, 2024. +# (C) Copyright IBM 2018, 2025. # # This code is licensed under the Apache License, Version 2.0. You may # obtain a copy of this license in the LICENSE.txt file in the root directory @@ -185,10 +185,10 @@ def test_optimizer_scipy_callable(self): vqe = SamplingVQE( Sampler(), RealAmplitudes(), - partial(scipy_minimize, method="COBYLA", options={"maxiter": 2}), + partial(scipy_minimize, method="COBYLA", options={"maxiter": 8}), ) result = vqe.compute_minimum_eigenvalue(Pauli("Z")) - self.assertEqual(result.cost_function_evals, 2) + self.assertEqual(result.cost_function_evals, 8) def test_optimizer_callable(self): """Test passing a optimizer directly as callable.""" diff --git a/test/optimizers/test_optimizers.py b/test/optimizers/test_optimizers.py index 273ceb34..c74aad13 100644 --- a/test/optimizers/test_optimizers.py +++ b/test/optimizers/test_optimizers.py @@ -75,7 +75,7 @@ def run_optimizer( grad: Whether to pass the gradient function as input. bounds: Optimizer bounds. """ - x_0 = np.asarray([1.3, 0.7, 0.8, 1.9, 1.2]) + x_0 = np.asarray([1.13, 0.7, 0.8, 1.9, 1.2]) jac = rosen_der if grad else None res = optimizer.minimize(rosen, x_0, jac, bounds)