Skip to content
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

Add slow_test as it's no longer part of Qiskit 1.0 #141

Merged
merged 5 commits into from
Feb 6, 2024
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
5 changes: 3 additions & 2 deletions test/__init__.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, 2023.
# (C) Copyright IBM 2018, 2024.
#
# 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 @@ -13,5 +13,6 @@
"""Algorithms test module"""

from .algorithms_test_case import QiskitAlgorithmsTestCase
from .decorators import slow_test

__all__ = ["QiskitAlgorithmsTestCase"]
__all__ = ["QiskitAlgorithmsTestCase", "slow_test"]
37 changes: 37 additions & 0 deletions test/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2017, 2024.
#
# 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
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.


"""Decorator for using with unit tests."""

import functools
import os
import unittest


def slow_test(func):
"""Decorator that signals that the test takes minutes to run.

Args:
func (callable): test function to be decorated.

Returns:
callable: the decorated function.
"""

@functools.wraps(func)
def _wrapper(*args, **kwargs):
if "run_slow" in os.environ.get("QISKIT_TESTS", ""):
raise unittest.SkipTest("Skipping slow tests")
return func(*args, **kwargs)

return _wrapper
22 changes: 10 additions & 12 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, 2023.
# (C) Copyright IBM 2022, 2024.
#
# 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 @@ -22,7 +22,6 @@
from qiskit.circuit.library import TwoLocal, RealAmplitudes
from qiskit.primitives import Sampler, Estimator
from qiskit.quantum_info import SparsePauliOp
from qiskit.quantum_info.operators import Operator

from qiskit_algorithms.eigensolvers import VQD, VQDResult
from qiskit_algorithms import AlgorithmError
Expand All @@ -39,7 +38,6 @@
("XX", 0.18093119978423156),
]
)
H2_OP = Operator(H2_SPARSE_PAULI.to_matrix())


@ddt
Expand All @@ -64,7 +62,7 @@ def setUp(self):
self.fidelity = ComputeUncompute(Sampler())
self.betas = [50, 50]

@data(H2_OP, H2_SPARSE_PAULI)
@data(H2_SPARSE_PAULI)
def test_basic_operator(self, op):
"""Test the VQD without aux_operators."""
wavefunction = self.ryrz_wavefunction
Expand Down Expand Up @@ -127,7 +125,7 @@ def test_beta_autoeval(self, op):
beta = float(logs.output[0].split()[-1])
self.assertAlmostEqual(beta, 20.40459399499687, 4)

@data(H2_OP, H2_SPARSE_PAULI)
@data(H2_SPARSE_PAULI)
def test_mismatching_num_qubits(self, op):
"""Ensuring circuit and operator mismatch is caught"""
wavefunction = QuantumCircuit(1)
Expand All @@ -143,7 +141,7 @@ def test_mismatching_num_qubits(self, op):
with self.assertRaises(AlgorithmError):
_ = vqd.compute_eigenvalues(operator=op)

@data(H2_OP, H2_SPARSE_PAULI)
@data(H2_SPARSE_PAULI)
def test_missing_varform_params(self, op):
"""Test specifying a variational form with no parameters raises an error."""
circuit = QuantumCircuit(op.num_qubits)
Expand All @@ -158,7 +156,7 @@ def test_missing_varform_params(self, op):
with self.assertRaises(AlgorithmError):
vqd.compute_eigenvalues(operator=op)

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

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

Expand Down Expand Up @@ -241,7 +239,7 @@ def run_check():
result = vqd.compute_eigenvalues(operator=op)
self.assertIsInstance(result, VQDResult)

@data(H2_OP, H2_SPARSE_PAULI)
@data(H2_SPARSE_PAULI)
def test_optimizer_list(self, op):
"""Test sending an optimizer list"""

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

@data(H2_OP, H2_SPARSE_PAULI)
@data(H2_SPARSE_PAULI)
def test_aux_operators_list(self, op):
"""Test list-based aux_operators."""
wavefunction = self.ry_wavefunction
Expand Down Expand Up @@ -334,7 +332,7 @@ def test_aux_operators_list(self, op):
self.assertIsInstance(result.aux_operators_evaluated[0][1][1], dict)
self.assertIsInstance(result.aux_operators_evaluated[0][3][1], dict)

@data(H2_OP, H2_SPARSE_PAULI)
@data(H2_SPARSE_PAULI)
def test_aux_operators_dict(self, op):
"""Test dictionary compatibility of aux_operators"""
wavefunction = self.ry_wavefunction
Expand Down Expand Up @@ -388,7 +386,7 @@ def test_aux_operators_dict(self, op):
self.assertIsInstance(result.aux_operators_evaluated[0]["aux_op2"][1], dict)
self.assertIsInstance(result.aux_operators_evaluated[0]["zero_operator"][1], dict)

@data(H2_OP, H2_SPARSE_PAULI)
@data(H2_SPARSE_PAULI)
def test_aux_operator_std_dev(self, op):
"""Test non-zero standard deviations of aux operators."""
wavefunction = self.ry_wavefunction
Expand Down
5 changes: 1 addition & 4 deletions test/gradients/test_estimator_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from qiskit.circuit.library import EfficientSU2, RealAmplitudes
from qiskit.circuit.library.standard_gates import RXXGate, RYYGate, RZXGate, RZZGate
from qiskit.primitives import Estimator
from qiskit.quantum_info import Operator, SparsePauliOp, Pauli
from qiskit.quantum_info import SparsePauliOp, Pauli
from qiskit.quantum_info.random import random_pauli_list

from qiskit_algorithms.gradients import (
Expand Down Expand Up @@ -70,9 +70,6 @@ def test_gradient_operators(self, grad):
op = SparsePauliOp.from_list([("Z", 1)])
value = gradient.run([qc], [op], [param]).result().gradients[0]
self.assertAlmostEqual(value[0], correct_result, 3)
op = Operator.from_label("Z")
value = gradient.run([qc], [op], [param]).result().gradients[0]
self.assertAlmostEqual(value[0], correct_result, 3)

@data(*gradient_factories)
def test_single_circuit_observable(self, grad):
Expand Down
21 changes: 10 additions & 11 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, 2023.
# (C) Copyright IBM 2018, 2024.
#
# 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 @@ -24,7 +24,7 @@
from qiskit.circuit import ParameterVector, QuantumCircuit
from qiskit.circuit.library import RealAmplitudes, TwoLocal
from qiskit.primitives import Sampler
from qiskit.quantum_info import Operator, Pauli, SparsePauliOp
from qiskit.quantum_info import Pauli, SparsePauliOp

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


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


@ddt
Expand All @@ -63,7 +62,7 @@ def setUp(self):
self.optimal_bitstring = "10"
algorithm_globals.random_seed = 42

@data(PAULI_OP, OP)
@data(PAULI_OP)
def test_exact_sampler(self, op):
"""Test the VQE on BasicAer's statevector simulator."""
thetas = ParameterVector("th", 4)
Expand Down Expand Up @@ -102,7 +101,7 @@ def test_exact_sampler(self, op):
self.assertEqual(result.best_measurement["bitstring"], self.optimal_bitstring)
self.assertEqual(result.best_measurement["value"], self.optimal_value)

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

@data(PAULI_OP, OP)
@data(PAULI_OP)
def test_ansatz_resize(self, op):
"""Test the ansatz is properly resized if it's a blueprint circuit."""
ansatz = RealAmplitudes(1, reps=1)
vqe = SamplingVQE(Sampler(), ansatz, SLSQP())
result = vqe.compute_minimum_eigenvalue(operator=op)
self.assertAlmostEqual(result.eigenvalue, self.optimal_value, places=5)

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

@data(PAULI_OP, OP)
@data(PAULI_OP)
def test_missing_varform_params(self, op):
"""Test specifying a variational form with no parameters raises an error."""
circuit = QuantumCircuit(op.num_qubits)
vqe = SamplingVQE(Sampler(), circuit, SLSQP())
with self.assertRaises(AlgorithmError):
vqe.compute_minimum_eigenvalue(operator=op)

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

@data(PAULI_OP, OP)
@data(PAULI_OP)
def test_auxops(self, op):
"""Test passing auxiliary operators."""
ansatz = RealAmplitudes(2, reps=1)
Expand Down Expand Up @@ -226,7 +225,7 @@ def test_nondiag_observable_raises(self):
with self.assertRaises(ValueError):
_ = vqe.compute_minimum_eigenvalue(Pauli("X"))

@data(PAULI_OP, OP)
@data(PAULI_OP)
def test_callback(self, op):
"""Test the callback on VQE."""
history = {
Expand Down
3 changes: 2 additions & 1 deletion test/minimum_eigensolvers/test_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 2022, 2023.
# (C) Copyright IBM 2022, 2024.
#
# 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 @@ -245,6 +245,7 @@ def test_reuse(self):
self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=5)

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

with self.subTest(msg="assert vqe works on re-use."):
result = vqe.compute_minimum_eigenvalue(operator=operator)
Expand Down
3 changes: 1 addition & 2 deletions test/optimizers/test_optimizer_aqgd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
"""Test of AQGD optimizer"""

import unittest
from test import QiskitAlgorithmsTestCase
from test import QiskitAlgorithmsTestCase, slow_test
import numpy as np
from ddt import ddt, data
from qiskit.circuit.library import RealAmplitudes
from qiskit.primitives import Estimator
from qiskit.quantum_info import SparsePauliOp
from qiskit.test import slow_test

from qiskit_algorithms import AlgorithmError
from qiskit_algorithms.gradients import LinCombEstimatorGradient
Expand Down
4 changes: 2 additions & 2 deletions test/time_evolvers/test_pvqd.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, 2023.
# (C) Copyright IBM 2018, 2024.
#
# 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 @@ -48,7 +48,7 @@ class WhatAmI(Gate):
def __init__(self, angle):
super().__init__(name="whatami", num_qubits=2, params=[angle])

def inverse(self):
def inverse(self, annotated: bool = False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably deserves a comment that can be removed once Qiskit 0.46 reaches eol.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a quick look at Qiskit/qiskit#11593 it looks like that argument is intended to live in 1.0+, so the comment wouldn't be necessary, right?

Copy link
Member Author

@woodsp-ibm woodsp-ibm Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That argument is new in 1.0 - and while adding it now is compatible with 0.46 as it was without causes lint to fail as you can see in the CI jobs which have recently been merged where the monitor jobs, on 1.0, failed (they are not required to pass to merge). Ie the extra param needs to stay there - it just happens to be backwards compatible with 0.46 doing it this way - as you can see it all passes - the main CI which is done using 0.46 presently and the monitor jobs using Qsikit main.

************* Module test.time_evolvers.test_pvqd
test/time_evolvers/test_pvqd.py:51:4: W0221: Number of parameters was 2 in 'Instruction.inverse' and is now 1 in overriding 'WhatAmI.inverse' method (arguments-differ)

And this is just a test case, not main code

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then I think that the comment is not necessary here.

return WhatAmI(-self.params[0])


Expand Down
Loading