Skip to content

Commit

Permalink
Fix FakeToronto import (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
manoelmarques authored Jun 24, 2022
1 parent fb307ee commit 505495f
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 35 deletions.
10 changes: 6 additions & 4 deletions test/algorithms/classifiers/test_neural_network_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions test/algorithms/classifiers/test_vqc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,19 +70,20 @@ 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
self.num_classes_by_batch = []

# 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,
Expand Down
16 changes: 11 additions & 5 deletions test/algorithms/regressors/test_neural_network_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -239,3 +241,7 @@ def store_loss(_, loss):
regressor.fit(features, labels)

self.assertEqual(len(loss_history), 3)


if __name__ == "__main__":
unittest.main()
13 changes: 7 additions & 6 deletions test/algorithms/regressors/test_vqr.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,29 +18,30 @@
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


@ddt
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,
Expand Down
11 changes: 6 additions & 5 deletions test/circuit/library/test_raw_feature_vector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020, 2021.
# (C) Copyright IBM 2020, 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
Expand All @@ -17,13 +17,13 @@
from test import QiskitMachineLearningTestCase

import numpy as np
from qiskit import transpile, Aer
import qiskit
from qiskit.algorithms.optimizers import COBYLA
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import RealAmplitudes
from qiskit.exceptions import QiskitError
from qiskit.quantum_info import Statevector
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.circuit.library import RawFeatureVector
Expand All @@ -45,7 +45,7 @@ def test_construction(self):

with self.subTest("check unrolling fails"):
with self.assertRaises(QiskitError):
_ = transpile(circuit, basis_gates=["u", "cx"], optimization_level=0)
_ = qiskit.transpile(circuit, basis_gates=["u", "cx"], optimization_level=0)

def test_fully_bound(self):
"""Test fully binding the circuit works."""
Expand Down Expand Up @@ -90,13 +90,14 @@ def test_partially_bound(self):
self.assertEqual(circuit.num_qubits, bound.num_qubits)
self.assertEqual(circuit.feature_dimension, bound.feature_dimension)

@unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test")
def test_usage_in_vqc(self):
"""Test using the circuit the a single VQC iteration works."""

# specify quantum instance and random seed
algorithm_globals.random_seed = 12345
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,
)
Expand Down
2 changes: 1 addition & 1 deletion test/connectors/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions test/neural_networks/test_circuit_qnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
Expand Down
8 changes: 4 additions & 4 deletions test/neural_networks/test_effective_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import numpy as np
from ddt import ddt, data, unpack

from qiskit import Aer, QuantumCircuit
import qiskit
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import ZFeatureMap, RealAmplitudes
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit.utils import optionals
from qiskit.utils import QuantumInstance, algorithm_globals, optionals

from qiskit.opflow import PauliSumOp
from qiskit_machine_learning.neural_networks import TwoLayerQNN, CircuitQNN
Expand All @@ -42,7 +42,7 @@ def setUp(self):

algorithm_globals.random_seed = 1234
qi_sv = 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,
)
Expand Down
2 changes: 1 addition & 1 deletion test/neural_networks/test_opflow_qnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
7 changes: 4 additions & 3 deletions test/neural_networks/test_two_layer_qnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import numpy as np
from ddt import ddt, data
from qiskit import Aer
import qiskit
from qiskit.circuit.library import RealAmplitudes, ZFeatureMap, ZZFeatureMap
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit.utils import QuantumInstance, algorithm_globals, optionals

from qiskit_machine_learning.neural_networks import TwoLayerQNN

Expand All @@ -29,12 +29,13 @@
class TestTwoLayerQNN(QiskitMachineLearningTestCase):
"""Two Layer QNN Tests."""

@unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test")
def setUp(self):
super().setUp()
algorithm_globals.random_seed = 12345
# specify "run configuration"
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,
)
Expand Down

0 comments on commit 505495f

Please sign in to comment.