Skip to content

Commit ea438fa

Browse files
committed
Fix tests to accomodate scipy 1.16.0 changes
1 parent bc071ca commit ea438fa

File tree

4 files changed

+105
-17
lines changed

4 files changed

+105
-17
lines changed

test/eigensolvers/test_vqd.py

Lines changed: 99 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2022, 2024.
3+
# (C) Copyright IBM 2022, 2025.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -16,7 +16,9 @@
1616
from test import QiskitAlgorithmsTestCase
1717

1818
import numpy as np
19+
import scipy
1920
from ddt import data, ddt
21+
from packaging.version import Version
2022

2123
from qiskit import QuantumCircuit
2224
from qiskit.circuit.library import TwoLocal, RealAmplitudes
@@ -175,7 +177,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step):
175177
history["metadata"].append(metadata)
176178
history["step"].append(step)
177179

178-
optimizer = COBYLA(maxiter=3)
180+
optimizer = COBYLA(maxiter=12)
179181
wavefunction = self.ry_wavefunction
180182

181183
vqd = VQD(
@@ -185,6 +187,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step):
185187
optimizer=optimizer,
186188
callback=store_intermediate_result,
187189
betas=self.betas,
190+
initial_point=[1] * 8,
188191
)
189192

190193
vqd.compute_eigenvalues(operator=op)
@@ -196,11 +199,98 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step):
196199
for params in history["parameters"]:
197200
self.assertTrue(all(isinstance(param, float) for param in params))
198201

199-
ref_eval_count = [1, 2, 3, 1, 2, 3]
200-
ref_mean = [-1.07, -1.45, -1.36, 1.24, 1.55, 1.07]
201-
# new ref_mean since the betas were changed
202+
ref_eval_count = [
203+
1,
204+
2,
205+
3,
206+
4,
207+
5,
208+
6,
209+
7,
210+
8,
211+
9,
212+
10,
213+
11,
214+
12,
215+
1,
216+
2,
217+
3,
218+
4,
219+
5,
220+
6,
221+
7,
222+
8,
223+
9,
224+
10,
225+
11,
226+
12,
227+
]
202228

203-
ref_step = [1, 1, 1, 2, 2, 2]
229+
ref_mean_pre_1_16 = [
230+
-1.08,
231+
-1.08,
232+
-1.0,
233+
-1.14,
234+
-1.17,
235+
-1.38,
236+
-1.0,
237+
-1.63,
238+
-1.45,
239+
-1.55,
240+
-1.63,
241+
-1.75,
242+
-1.04,
243+
-1.07,
244+
-0.72,
245+
-0.46,
246+
-0.71,
247+
-0.56,
248+
-0.92,
249+
-0.29,
250+
-0.89,
251+
-0.38,
252+
-1.06,
253+
-1.05,
254+
]
255+
ref_mean_1_16 = [
256+
-1.08,
257+
-1.08,
258+
-1.0,
259+
-1.14,
260+
-1.17,
261+
-1.38,
262+
-1.0,
263+
-1.63,
264+
-1.45,
265+
-1.55,
266+
-1.63,
267+
-1.75,
268+
-1.04,
269+
-1.07,
270+
-0.72,
271+
-0.46,
272+
-0.71,
273+
-0.56,
274+
-0.92,
275+
-0.29,
276+
-0.89,
277+
-0.38,
278+
-0.97,
279+
-1.16,
280+
]
281+
# Unlike in other places where COYBLA is used in tests and differences arose between
282+
# pre 1.16.0 versions and after, where in 1.16.0 scipy changed the COBYLA
283+
# implementation, I was not able to find changes that would reproduce the outcome so
284+
# tests passed no matter whether 1.16 or before was installed. Here the mean outcomes
285+
# match all but the last 2 values so I thought about comparing a subset but in the
286+
# end decided to go with different reference values based on scipy version
287+
ref_mean = (
288+
ref_mean_pre_1_16
289+
if Version(scipy.version.version) < Version("1.16.0")
290+
else ref_mean_1_16
291+
)
292+
293+
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]
204294

205295
np.testing.assert_array_almost_equal(history["eval_count"], ref_eval_count, decimal=0)
206296
np.testing.assert_array_almost_equal(history["mean"], ref_mean, decimal=2)
@@ -410,7 +500,7 @@ def test_aux_operator_std_dev(self, op):
410500
0.2442925,
411501
-1.51638917,
412502
],
413-
optimizer=COBYLA(maxiter=0),
503+
optimizer=COBYLA(maxiter=10),
414504
betas=self.betas,
415505
)
416506

@@ -423,7 +513,7 @@ def test_aux_operator_std_dev(self, op):
423513
# expectation values
424514
self.assertAlmostEqual(result.aux_operators_evaluated[0][0][0], 2.0, places=1)
425515
self.assertAlmostEqual(
426-
result.aux_operators_evaluated[0][1][0], 0.0019531249999999445, places=1
516+
result.aux_operators_evaluated[0][1][0], 0.7432341813894455, places=1
427517
)
428518
# metadata
429519
self.assertIsInstance(result.aux_operators_evaluated[0][0][1], dict)
@@ -435,9 +525,7 @@ def test_aux_operator_std_dev(self, op):
435525
self.assertEqual(len(result.aux_operators_evaluated[0]), 4)
436526
# expectation values
437527
self.assertAlmostEqual(result.aux_operators_evaluated[0][0][0], 2.0, places=1)
438-
self.assertAlmostEqual(
439-
result.aux_operators_evaluated[0][1][0], 0.0019531249999999445, places=1
440-
)
528+
self.assertAlmostEqual(result.aux_operators_evaluated[0][1][0], 0.743234181389445, places=1)
441529
self.assertEqual(result.aux_operators_evaluated[0][2][0], 0.0)
442530
self.assertEqual(result.aux_operators_evaluated[0][3][0], 0.0)
443531
# metadata

test/minimum_eigensolvers/test_qaoa.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2022, 2023.
3+
# (C) Copyright IBM 2022, 2025.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -180,7 +180,7 @@ def test_change_operator_size(self):
180180
with self.subTest(msg="QAOA 6x6"):
181181
self.assertIn(graph_solution, {"010101", "101010"})
182182

183-
@idata([[W2, S2, None], [W2, S2, [0.0, 0.0]], [W2, S2, [1.0, 0.8]]])
183+
@idata([[W2, S2, None], [W2, S2, [0.0, 0.5]], [W2, S2, [1.0, 0.8]]])
184184
@unpack
185185
def test_qaoa_initial_point(self, w, solutions, init_pt):
186186
"""Check first parameter value used is initial point as expected"""

test/minimum_eigensolvers/test_sampling_vqe.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2018, 2024.
3+
# (C) Copyright IBM 2018, 2025.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# 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):
185185
vqe = SamplingVQE(
186186
Sampler(),
187187
RealAmplitudes(),
188-
partial(scipy_minimize, method="COBYLA", options={"maxiter": 2}),
188+
partial(scipy_minimize, method="COBYLA", options={"maxiter": 8}),
189189
)
190190
result = vqe.compute_minimum_eigenvalue(Pauli("Z"))
191-
self.assertEqual(result.cost_function_evals, 2)
191+
self.assertEqual(result.cost_function_evals, 8)
192192

193193
def test_optimizer_callable(self):
194194
"""Test passing a optimizer directly as callable."""

test/optimizers/test_optimizers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def run_optimizer(
7575
grad: Whether to pass the gradient function as input.
7676
bounds: Optimizer bounds.
7777
"""
78-
x_0 = np.asarray([1.3, 0.7, 0.8, 1.9, 1.2])
78+
x_0 = np.asarray([1.13, 0.7, 0.8, 1.9, 1.2])
7979
jac = rosen_der if grad else None
8080

8181
res = optimizer.minimize(rosen, x_0, jac, bounds)

0 commit comments

Comments
 (0)