1
1
# This code is part of a Qiskit project.
2
2
#
3
- # (C) Copyright IBM 2022, 2024 .
3
+ # (C) Copyright IBM 2022, 2025 .
4
4
#
5
5
# This code is licensed under the Apache License, Version 2.0. You may
6
6
# obtain a copy of this license in the LICENSE.txt file in the root directory
16
16
from test import QiskitAlgorithmsTestCase
17
17
18
18
import numpy as np
19
+ import scipy
19
20
from ddt import data , ddt
21
+ from packaging .version import Version
20
22
21
23
from qiskit import QuantumCircuit
22
24
from qiskit .circuit .library import TwoLocal , RealAmplitudes
@@ -175,7 +177,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step):
175
177
history ["metadata" ].append (metadata )
176
178
history ["step" ].append (step )
177
179
178
- optimizer = COBYLA (maxiter = 3 )
180
+ optimizer = COBYLA (maxiter = 12 )
179
181
wavefunction = self .ry_wavefunction
180
182
181
183
vqd = VQD (
@@ -185,6 +187,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step):
185
187
optimizer = optimizer ,
186
188
callback = store_intermediate_result ,
187
189
betas = self .betas ,
190
+ initial_point = [1 ] * 8 ,
188
191
)
189
192
190
193
vqd .compute_eigenvalues (operator = op )
@@ -196,11 +199,98 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step):
196
199
for params in history ["parameters" ]:
197
200
self .assertTrue (all (isinstance (param , float ) for param in params ))
198
201
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
+ ]
202
228
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 ]
204
294
205
295
np .testing .assert_array_almost_equal (history ["eval_count" ], ref_eval_count , decimal = 0 )
206
296
np .testing .assert_array_almost_equal (history ["mean" ], ref_mean , decimal = 2 )
@@ -410,7 +500,7 @@ def test_aux_operator_std_dev(self, op):
410
500
0.2442925 ,
411
501
- 1.51638917 ,
412
502
],
413
- optimizer = COBYLA (maxiter = 0 ),
503
+ optimizer = COBYLA (maxiter = 10 ),
414
504
betas = self .betas ,
415
505
)
416
506
@@ -423,7 +513,7 @@ def test_aux_operator_std_dev(self, op):
423
513
# expectation values
424
514
self .assertAlmostEqual (result .aux_operators_evaluated [0 ][0 ][0 ], 2.0 , places = 1 )
425
515
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
427
517
)
428
518
# metadata
429
519
self .assertIsInstance (result .aux_operators_evaluated [0 ][0 ][1 ], dict )
@@ -435,9 +525,7 @@ def test_aux_operator_std_dev(self, op):
435
525
self .assertEqual (len (result .aux_operators_evaluated [0 ]), 4 )
436
526
# expectation values
437
527
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 )
441
529
self .assertEqual (result .aux_operators_evaluated [0 ][2 ][0 ], 0.0 )
442
530
self .assertEqual (result .aux_operators_evaluated [0 ][3 ][0 ], 0.0 )
443
531
# metadata
0 commit comments