Skip to content

Fix tests to accomodate scipy 1.16.0 changes #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/tutorials/05_qaoa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -385,7 +385,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
"version": "3.12.9"
},
"vscode": {
"interpreter": {
Expand Down
110 changes: 99 additions & 11 deletions test/eigensolvers/test_vqd.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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,
)

Expand All @@ -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)
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/minimum_eigensolvers/test_qaoa.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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"""
Expand Down
6 changes: 3 additions & 3 deletions test/minimum_eigensolvers/test_sampling_vqe.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion test/optimizers/test_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading