Skip to content

Commit 2245288

Browse files
authored
Add slow_test as it's no longer part of Qiskit 1.0 (#141)
1 parent be2a852 commit 2245288

File tree

8 files changed

+66
-34
lines changed

8 files changed

+66
-34
lines changed

test/__init__.py

+3-2
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, 2023.
3+
# (C) Copyright IBM 2018, 2024.
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
@@ -13,5 +13,6 @@
1313
"""Algorithms test module"""
1414

1515
from .algorithms_test_case import QiskitAlgorithmsTestCase
16+
from .decorators import slow_test
1617

17-
__all__ = ["QiskitAlgorithmsTestCase"]
18+
__all__ = ["QiskitAlgorithmsTestCase", "slow_test"]

test/decorators.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This code is part of a Qiskit project.
2+
#
3+
# (C) Copyright IBM 2017, 2024.
4+
#
5+
# This code is licensed under the Apache License, Version 2.0. You may
6+
# obtain a copy of this license in the LICENSE.txt file in the root directory
7+
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8+
#
9+
# Any modifications or derivative works of this code must retain this
10+
# copyright notice, and modified files need to carry a notice indicating
11+
# that they have been altered from the originals.
12+
13+
14+
"""Decorator for using with unit tests."""
15+
16+
import functools
17+
import os
18+
import unittest
19+
20+
21+
def slow_test(func):
22+
"""Decorator that signals that the test takes minutes to run.
23+
24+
Args:
25+
func (callable): test function to be decorated.
26+
27+
Returns:
28+
callable: the decorated function.
29+
"""
30+
31+
@functools.wraps(func)
32+
def _wrapper(*args, **kwargs):
33+
if "run_slow" in os.environ.get("QISKIT_TESTS", ""):
34+
raise unittest.SkipTest("Skipping slow tests")
35+
return func(*args, **kwargs)
36+
37+
return _wrapper

test/eigensolvers/test_vqd.py

+10-12
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, 2024.
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
@@ -22,7 +22,6 @@
2222
from qiskit.circuit.library import TwoLocal, RealAmplitudes
2323
from qiskit.primitives import Sampler, Estimator
2424
from qiskit.quantum_info import SparsePauliOp
25-
from qiskit.quantum_info.operators import Operator
2625

2726
from qiskit_algorithms.eigensolvers import VQD, VQDResult
2827
from qiskit_algorithms import AlgorithmError
@@ -39,7 +38,6 @@
3938
("XX", 0.18093119978423156),
4039
]
4140
)
42-
H2_OP = Operator(H2_SPARSE_PAULI.to_matrix())
4341

4442

4543
@ddt
@@ -64,7 +62,7 @@ def setUp(self):
6462
self.fidelity = ComputeUncompute(Sampler())
6563
self.betas = [50, 50]
6664

67-
@data(H2_OP, H2_SPARSE_PAULI)
65+
@data(H2_SPARSE_PAULI)
6866
def test_basic_operator(self, op):
6967
"""Test the VQD without aux_operators."""
7068
wavefunction = self.ryrz_wavefunction
@@ -127,7 +125,7 @@ def test_beta_autoeval(self, op):
127125
beta = float(logs.output[0].split()[-1])
128126
self.assertAlmostEqual(beta, 20.40459399499687, 4)
129127

130-
@data(H2_OP, H2_SPARSE_PAULI)
128+
@data(H2_SPARSE_PAULI)
131129
def test_mismatching_num_qubits(self, op):
132130
"""Ensuring circuit and operator mismatch is caught"""
133131
wavefunction = QuantumCircuit(1)
@@ -143,7 +141,7 @@ def test_mismatching_num_qubits(self, op):
143141
with self.assertRaises(AlgorithmError):
144142
_ = vqd.compute_eigenvalues(operator=op)
145143

146-
@data(H2_OP, H2_SPARSE_PAULI)
144+
@data(H2_SPARSE_PAULI)
147145
def test_missing_varform_params(self, op):
148146
"""Test specifying a variational form with no parameters raises an error."""
149147
circuit = QuantumCircuit(op.num_qubits)
@@ -158,7 +156,7 @@ def test_missing_varform_params(self, op):
158156
with self.assertRaises(AlgorithmError):
159157
vqd.compute_eigenvalues(operator=op)
160158

161-
@data(H2_OP, H2_SPARSE_PAULI)
159+
@data(H2_SPARSE_PAULI)
162160
def test_callback(self, op):
163161
"""Test the callback on VQD."""
164162
history = {"eval_count": [], "parameters": [], "mean": [], "metadata": [], "step": []}
@@ -202,7 +200,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step):
202200
np.testing.assert_array_almost_equal(history["mean"], ref_mean, decimal=2)
203201
np.testing.assert_array_almost_equal(history["step"], ref_step, decimal=0)
204202

205-
@data(H2_OP, H2_SPARSE_PAULI)
203+
@data(H2_SPARSE_PAULI)
206204
def test_vqd_optimizer(self, op):
207205
"""Test running same VQD twice to re-use optimizer, then switch optimizer"""
208206

@@ -241,7 +239,7 @@ def run_check():
241239
result = vqd.compute_eigenvalues(operator=op)
242240
self.assertIsInstance(result, VQDResult)
243241

244-
@data(H2_OP, H2_SPARSE_PAULI)
242+
@data(H2_SPARSE_PAULI)
245243
def test_optimizer_list(self, op):
246244
"""Test sending an optimizer list"""
247245

@@ -281,7 +279,7 @@ def test_optimizer_list(self, op):
281279
result.eigenvalues.real, self.h2_energy_excited[:2], decimal=3
282280
)
283281

284-
@data(H2_OP, H2_SPARSE_PAULI)
282+
@data(H2_SPARSE_PAULI)
285283
def test_aux_operators_list(self, op):
286284
"""Test list-based aux_operators."""
287285
wavefunction = self.ry_wavefunction
@@ -334,7 +332,7 @@ def test_aux_operators_list(self, op):
334332
self.assertIsInstance(result.aux_operators_evaluated[0][1][1], dict)
335333
self.assertIsInstance(result.aux_operators_evaluated[0][3][1], dict)
336334

337-
@data(H2_OP, H2_SPARSE_PAULI)
335+
@data(H2_SPARSE_PAULI)
338336
def test_aux_operators_dict(self, op):
339337
"""Test dictionary compatibility of aux_operators"""
340338
wavefunction = self.ry_wavefunction
@@ -388,7 +386,7 @@ def test_aux_operators_dict(self, op):
388386
self.assertIsInstance(result.aux_operators_evaluated[0]["aux_op2"][1], dict)
389387
self.assertIsInstance(result.aux_operators_evaluated[0]["zero_operator"][1], dict)
390388

391-
@data(H2_OP, H2_SPARSE_PAULI)
389+
@data(H2_SPARSE_PAULI)
392390
def test_aux_operator_std_dev(self, op):
393391
"""Test non-zero standard deviations of aux operators."""
394392
wavefunction = self.ry_wavefunction

test/gradients/test_estimator_gradient.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from qiskit.circuit.library import EfficientSU2, RealAmplitudes
2525
from qiskit.circuit.library.standard_gates import RXXGate, RYYGate, RZXGate, RZZGate
2626
from qiskit.primitives import Estimator
27-
from qiskit.quantum_info import Operator, SparsePauliOp, Pauli
27+
from qiskit.quantum_info import SparsePauliOp, Pauli
2828
from qiskit.quantum_info.random import random_pauli_list
2929

3030
from qiskit_algorithms.gradients import (
@@ -70,9 +70,6 @@ def test_gradient_operators(self, grad):
7070
op = SparsePauliOp.from_list([("Z", 1)])
7171
value = gradient.run([qc], [op], [param]).result().gradients[0]
7272
self.assertAlmostEqual(value[0], correct_result, 3)
73-
op = Operator.from_label("Z")
74-
value = gradient.run([qc], [op], [param]).result().gradients[0]
75-
self.assertAlmostEqual(value[0], correct_result, 3)
7673

7774
@data(*gradient_factories)
7875
def test_single_circuit_observable(self, grad):

test/minimum_eigensolvers/test_sampling_vqe.py

+10-11
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, 2023.
3+
# (C) Copyright IBM 2018, 2024.
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
@@ -24,7 +24,7 @@
2424
from qiskit.circuit import ParameterVector, QuantumCircuit
2525
from qiskit.circuit.library import RealAmplitudes, TwoLocal
2626
from qiskit.primitives import Sampler
27-
from qiskit.quantum_info import Operator, Pauli, SparsePauliOp
27+
from qiskit.quantum_info import Pauli, SparsePauliOp
2828

2929
from qiskit_algorithms import AlgorithmError
3030
from qiskit_algorithms.minimum_eigensolvers import SamplingVQE
@@ -50,7 +50,6 @@ def _mock_optimizer(fun, x0, jac=None, bounds=None, inputs=None):
5050

5151

5252
PAULI_OP = SparsePauliOp(["ZZ", "IZ", "II"], coeffs=[1, -0.5, 0.12])
53-
OP = Operator(PAULI_OP.to_matrix())
5453

5554

5655
@ddt
@@ -63,7 +62,7 @@ def setUp(self):
6362
self.optimal_bitstring = "10"
6463
algorithm_globals.random_seed = 42
6564

66-
@data(PAULI_OP, OP)
65+
@data(PAULI_OP)
6766
def test_exact_sampler(self, op):
6867
"""Test the VQE on BasicAer's statevector simulator."""
6968
thetas = ParameterVector("th", 4)
@@ -102,7 +101,7 @@ def test_exact_sampler(self, op):
102101
self.assertEqual(result.best_measurement["bitstring"], self.optimal_bitstring)
103102
self.assertEqual(result.best_measurement["value"], self.optimal_value)
104103

105-
@data(PAULI_OP, OP)
104+
@data(PAULI_OP)
106105
def test_invalid_initial_point(self, op):
107106
"""Test the proper error is raised when the initial point has the wrong size."""
108107
ansatz = RealAmplitudes(2, reps=1)
@@ -113,15 +112,15 @@ def test_invalid_initial_point(self, op):
113112
with self.assertRaises(ValueError):
114113
_ = vqe.compute_minimum_eigenvalue(operator=op)
115114

116-
@data(PAULI_OP, OP)
115+
@data(PAULI_OP)
117116
def test_ansatz_resize(self, op):
118117
"""Test the ansatz is properly resized if it's a blueprint circuit."""
119118
ansatz = RealAmplitudes(1, reps=1)
120119
vqe = SamplingVQE(Sampler(), ansatz, SLSQP())
121120
result = vqe.compute_minimum_eigenvalue(operator=op)
122121
self.assertAlmostEqual(result.eigenvalue, self.optimal_value, places=5)
123122

124-
@data(PAULI_OP, OP)
123+
@data(PAULI_OP)
125124
def test_invalid_ansatz_size(self, op):
126125
"""Test an error is raised if the ansatz has the wrong number of qubits."""
127126
ansatz = QuantumCircuit(1)
@@ -131,15 +130,15 @@ def test_invalid_ansatz_size(self, op):
131130
with self.assertRaises(AlgorithmError):
132131
_ = vqe.compute_minimum_eigenvalue(operator=op)
133132

134-
@data(PAULI_OP, OP)
133+
@data(PAULI_OP)
135134
def test_missing_varform_params(self, op):
136135
"""Test specifying a variational form with no parameters raises an error."""
137136
circuit = QuantumCircuit(op.num_qubits)
138137
vqe = SamplingVQE(Sampler(), circuit, SLSQP())
139138
with self.assertRaises(AlgorithmError):
140139
vqe.compute_minimum_eigenvalue(operator=op)
141140

142-
@data(PAULI_OP, OP)
141+
@data(PAULI_OP)
143142
def test_batch_evaluate_slsqp(self, op):
144143
"""Test batching with SLSQP (as representative of SciPyOptimizer)."""
145144
optimizer = SLSQP(max_evals_grouped=10)
@@ -198,7 +197,7 @@ def test_optimizer_callable(self):
198197
result = vqe.compute_minimum_eigenvalue(Pauli("Z"))
199198
self.assertTrue(np.all(result.optimal_point == np.zeros(ansatz.num_parameters)))
200199

201-
@data(PAULI_OP, OP)
200+
@data(PAULI_OP)
202201
def test_auxops(self, op):
203202
"""Test passing auxiliary operators."""
204203
ansatz = RealAmplitudes(2, reps=1)
@@ -226,7 +225,7 @@ def test_nondiag_observable_raises(self):
226225
with self.assertRaises(ValueError):
227226
_ = vqe.compute_minimum_eigenvalue(Pauli("X"))
228227

229-
@data(PAULI_OP, OP)
228+
@data(PAULI_OP)
230229
def test_callback(self, op):
231230
"""Test the callback on VQE."""
232231
history = {

test/minimum_eigensolvers/test_vqe.py

+2-1
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, 2024.
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
@@ -245,6 +245,7 @@ def test_reuse(self):
245245
self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=5)
246246

247247
operator = Operator(np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3]]))
248+
operator = SparsePauliOp.from_operator(operator)
248249

249250
with self.subTest(msg="assert vqe works on re-use."):
250251
result = vqe.compute_minimum_eigenvalue(operator=operator)

test/optimizers/test_optimizer_aqgd.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
"""Test of AQGD optimizer"""
1414

1515
import unittest
16-
from test import QiskitAlgorithmsTestCase
16+
from test import QiskitAlgorithmsTestCase, slow_test
1717
import numpy as np
1818
from ddt import ddt, data
1919
from qiskit.circuit.library import RealAmplitudes
2020
from qiskit.primitives import Estimator
2121
from qiskit.quantum_info import SparsePauliOp
22-
from qiskit.test import slow_test
2322

2423
from qiskit_algorithms import AlgorithmError
2524
from qiskit_algorithms.gradients import LinCombEstimatorGradient

test/time_evolvers/test_pvqd.py

+2-2
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, 2023.
3+
# (C) Copyright IBM 2018, 2024.
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
@@ -48,7 +48,7 @@ class WhatAmI(Gate):
4848
def __init__(self, angle):
4949
super().__init__(name="whatami", num_qubits=2, params=[angle])
5050

51-
def inverse(self):
51+
def inverse(self, annotated: bool = False):
5252
return WhatAmI(-self.params[0])
5353

5454

0 commit comments

Comments
 (0)