Skip to content

Commit

Permalink
Fix FakeToronto import and Fix Aer Deprecate Messages (backport #424) (
Browse files Browse the repository at this point in the history
…#451)

* Fix FakeToronto import (#424)

(cherry picked from commit 505495f)

# Conflicts:
#	test/algorithms/classifiers/test_neural_network_classifier.py
#	test/algorithms/classifiers/test_vqc.py
#	test/algorithms/regressors/test_neural_network_regressor.py
#	test/neural_networks/test_two_layer_qnn.py

* fix conflicts

Co-authored-by: Manoel Marques <[email protected]>
  • Loading branch information
mergify[bot] and manoelmarques authored Aug 10, 2022
1 parent 94b9db5 commit 49fd69d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 36 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 @@ -23,10 +23,11 @@
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, 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 @@ -43,18 +44,19 @@
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 @@ -21,10 +21,10 @@
import numpy as np
import scipy
from ddt import ddt, data
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

Expand All @@ -33,6 +33,7 @@
class TestVQC(QiskitMachineLearningTestCase):
"""VQC Tests."""

@unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test")
def setUp(self):
super().setUp()

Expand All @@ -41,12 +42,12 @@ def setUp(self):
# 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
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 @@ -11,19 +11,20 @@
# that they have been altered from the originals.
""" Test Neural Network Regressor """

import unittest
import os
import tempfile

from test import QiskitMachineLearningTestCase

import numpy as np
from ddt import ddt, data
from qiskit import Aer, QuantumCircuit
import qiskit
from qiskit.algorithms.optimizers import COBYLA, L_BFGS_B
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.algorithms import SerializableModelMixin
from qiskit_machine_learning.algorithms.regressors import NeuralNetworkRegressor
Expand All @@ -34,18 +35,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 @@ -174,3 +176,7 @@ class FakeModel(SerializableModelMixin):

finally:
os.remove(file_name)


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
9 changes: 5 additions & 4 deletions test/neural_networks/test_two_layer_qnn.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,9 +18,9 @@

import numpy as np
from ddt import ddt, data
from qiskit import Aer
import qiskit
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.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 49fd69d

Please sign in to comment.