diff --git a/test/algorithms/classifiers/test_neural_network_classifier.py b/test/algorithms/classifiers/test_neural_network_classifier.py index df2241044..a9ff7724f 100644 --- a/test/algorithms/classifiers/test_neural_network_classifier.py +++ b/test/algorithms/classifiers/test_neural_network_classifier.py @@ -21,10 +21,11 @@ import numpy as np import scipy from ddt import ddt, data, idata, unpack -from qiskit import Aer, QuantumCircuit +import qiskit from qiskit.algorithms.optimizers import COBYLA, L_BFGS_B, SPSA, Optimizer +from qiskit.circuit import QuantumCircuit from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap -from qiskit.utils import QuantumInstance, algorithm_globals +from qiskit.utils import QuantumInstance, algorithm_globals, optionals from qiskit_machine_learning.algorithms import SerializableModelMixin from qiskit_machine_learning.algorithms.classifiers import NeuralNetworkClassifier @@ -48,18 +49,19 @@ def _one_hot_encode(y: np.ndarray) -> np.ndarray: class TestNeuralNetworkClassifier(QiskitMachineLearningTestCase): """Neural Network Classifier Tests.""" + @unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test") def setUp(self): super().setUp() # specify quantum instances algorithm_globals.random_seed = 12345 self.sv_quantum_instance = QuantumInstance( - Aer.get_backend("aer_simulator_statevector"), + qiskit.providers.aer.Aer.get_backend("aer_simulator_statevector"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ) self.qasm_quantum_instance = QuantumInstance( - Aer.get_backend("aer_simulator"), + qiskit.providers.aer.Aer.get_backend("aer_simulator"), shots=100, seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, diff --git a/test/algorithms/classifiers/test_vqc.py b/test/algorithms/classifiers/test_vqc.py index cc9b6cb3f..a88c6ae66 100644 --- a/test/algorithms/classifiers/test_vqc.py +++ b/test/algorithms/classifiers/test_vqc.py @@ -27,10 +27,10 @@ from sklearn.datasets import make_classification from sklearn.preprocessing import MinMaxScaler, OneHotEncoder -from qiskit import Aer +import qiskit from qiskit.algorithms.optimizers import COBYLA, L_BFGS_B from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap -from qiskit.utils import QuantumInstance, algorithm_globals +from qiskit.utils import QuantumInstance, algorithm_globals, optionals from qiskit_machine_learning.algorithms import VQC from qiskit_machine_learning.exceptions import QiskitMachineLearningError @@ -70,6 +70,7 @@ def _create_dataset(n_samples: int, n_classes: int, one_hot=True) -> _Dataset: class TestVQC(QiskitMachineLearningTestCase): """VQC Tests.""" + @unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test") def setUp(self): super().setUp() algorithm_globals.random_seed = 1111111 @@ -77,12 +78,12 @@ def setUp(self): # Set-up the quantum instances. statevector = QuantumInstance( - Aer.get_backend("aer_simulator_statevector"), + qiskit.providers.aer.Aer.get_backend("aer_simulator_statevector"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ) qasm = QuantumInstance( - Aer.get_backend("aer_simulator"), + qiskit.providers.aer.Aer.get_backend("aer_simulator"), shots=100, seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, diff --git a/test/algorithms/regressors/test_neural_network_regressor.py b/test/algorithms/regressors/test_neural_network_regressor.py index 7d16b9fa2..2aa245162 100644 --- a/test/algorithms/regressors/test_neural_network_regressor.py +++ b/test/algorithms/regressors/test_neural_network_regressor.py @@ -10,6 +10,7 @@ # copyright notice, and modified files need to carry a notice indicating # that they have been altered from the originals. """ Test Neural Network Regressor """ +import unittest import itertools import os import tempfile @@ -19,12 +20,12 @@ import numpy as np from ddt import ddt, unpack, idata -from qiskit import Aer, QuantumCircuit +import qiskit from qiskit.algorithms.optimizers import COBYLA, L_BFGS_B, SPSA -from qiskit.circuit import Parameter +from qiskit.circuit import Parameter, QuantumCircuit from qiskit.circuit.library import ZZFeatureMap, RealAmplitudes from qiskit.opflow import PauliSumOp -from qiskit.utils import QuantumInstance, algorithm_globals +from qiskit.utils import QuantumInstance, algorithm_globals, optionals from qiskit_machine_learning import QiskitMachineLearningError from qiskit_machine_learning.algorithms import SerializableModelMixin @@ -40,18 +41,19 @@ class TestNeuralNetworkRegressor(QiskitMachineLearningTestCase): """Test Neural Network Regressor.""" + @unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test") def setUp(self): super().setUp() # specify quantum instances algorithm_globals.random_seed = 12345 self.sv_quantum_instance = QuantumInstance( - Aer.get_backend("aer_simulator_statevector"), + qiskit.providers.aer.Aer.get_backend("aer_simulator_statevector"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ) self.qasm_quantum_instance = QuantumInstance( - Aer.get_backend("aer_simulator"), + qiskit.providers.aer.Aer.get_backend("aer_simulator"), shots=100, seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, @@ -239,3 +241,7 @@ def store_loss(_, loss): regressor.fit(features, labels) self.assertEqual(len(loss_history), 3) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/algorithms/regressors/test_vqr.py b/test/algorithms/regressors/test_vqr.py index 598c94c5f..bdbac4178 100644 --- a/test/algorithms/regressors/test_vqr.py +++ b/test/algorithms/regressors/test_vqr.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2018, 2021. +# (C) Copyright IBM 2018, 2022. # # 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 @@ -18,10 +18,10 @@ import numpy as np from ddt import data, ddt -from qiskit import Aer, QuantumCircuit +import qiskit from qiskit.algorithms.optimizers import COBYLA, L_BFGS_B -from qiskit.circuit import Parameter -from qiskit.utils import QuantumInstance, algorithm_globals +from qiskit.circuit import Parameter, QuantumCircuit +from qiskit.utils import QuantumInstance, algorithm_globals, optionals from qiskit_machine_learning.algorithms import VQR @@ -29,18 +29,19 @@ class TestVQR(QiskitMachineLearningTestCase): """VQR Tests.""" + @unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test") def setUp(self): super().setUp() # specify quantum instances algorithm_globals.random_seed = 12345 self.sv_quantum_instance = QuantumInstance( - Aer.get_backend("aer_simulator_statevector"), + qiskit.providers.aer.Aer.get_backend("aer_simulator_statevector"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ) self.qasm_quantum_instance = QuantumInstance( - Aer.get_backend("aer_simulator"), + qiskit.providers.aer.Aer.get_backend("aer_simulator"), shots=100, seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, diff --git a/test/connectors/test_torch.py b/test/connectors/test_torch.py index 5151c3eeb..6615dd7a6 100644 --- a/test/connectors/test_torch.py +++ b/test/connectors/test_torch.py @@ -34,7 +34,7 @@ def setup_test(self): algorithm_globals.random_seed = 12345 # specify quantum instances self._sv_quantum_instance = QuantumInstance( - qiskit.Aer.get_backend("aer_simulator_statevector"), + qiskit.providers.aer.Aer.get_backend("aer_simulator_statevector"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ) diff --git a/test/neural_networks/test_circuit_qnn.py b/test/neural_networks/test_circuit_qnn.py index 80e2c408f..2c18e35fb 100644 --- a/test/neural_networks/test_circuit_qnn.py +++ b/test/neural_networks/test_circuit_qnn.py @@ -25,7 +25,7 @@ from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap from qiskit.utils import QuantumInstance, algorithm_globals, optionals from qiskit.compiler.transpiler import PassManagerConfig, level_1_pass_manager, level_2_pass_manager -from qiskit.test.mock import FakeToronto +from qiskit.providers.fake_provider import FakeToronto from qiskit_machine_learning import QiskitMachineLearningError from qiskit_machine_learning.neural_networks import CircuitQNN @@ -52,7 +52,7 @@ def setUp(self): algorithm_globals.random_seed = 12345 # specify "run configuration" self.quantum_instance_sv = QuantumInstance( - qiskit.Aer.get_backend("aer_simulator_statevector"), + qiskit.providers.aer.Aer.get_backend("aer_simulator_statevector"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, ) diff --git a/test/neural_networks/test_opflow_qnn.py b/test/neural_networks/test_opflow_qnn.py index f171dd8ed..7e319d9b1 100644 --- a/test/neural_networks/test_opflow_qnn.py +++ b/test/neural_networks/test_opflow_qnn.py @@ -43,7 +43,7 @@ def setUp(self): algorithm_globals.random_seed = 12345 # specify quantum instances self.sv_quantum_instance = QuantumInstance( - qiskit.Aer.get_backend("aer_simulator_statevector"), + qiskit.providers.aer.Aer.get_backend("aer_simulator_statevector"), seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, )