From 3ca98826457135882e3fe374a0c774f1f18db8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= <57907331+ElePT@users.noreply.github.com> Date: Wed, 31 Jan 2024 00:48:55 +0100 Subject: [PATCH] Deprecate provider-specific fake backends,FakeProvider class and related tools in 0.46 (#11381) * Deprecate fake provider class and device-specific fake backends. Leave fake backend base classes and special fake backends for testing purposes. * Fix lint * Apply suggestions from Kevin's code review Co-authored-by: Kevin Hartman * Move warning to after construction * Adjust warnings * Deprecate fake backend V2 * Reno fix * Update copyright * Make FakeBackendV2 message more specific * Fix removal timeline --------- Co-authored-by: Kevin Hartman --- .../providers/fake_provider/fake_backend.py | 15 +++++- .../fake_provider/fake_backend_v2.py | 22 ++++++++ qiskit/providers/fake_provider/fake_job.py | 9 +++- .../providers/fake_provider/fake_mumbai_v2.py | 8 +++ .../providers/fake_provider/fake_provider.py | 30 ++++++++++- .../fake_provider/fake_pulse_backend.py | 20 +++++++- .../fake_provider/fake_qasm_backend.py | 16 +++++- .../fake_provider/fake_qasm_simulator.py | 7 +++ qiskit/providers/fake_provider/fake_qobj.py | 9 +++- .../utils/configurable_backend.py | 2 +- qiskit/test/base.py | 7 ++- ...recate-fake-backends-4dd275cf9a30d41f.yaml | 51 +++++++++++++++++++ test/python/algorithms/test_backendv1.py | 4 +- test/python/algorithms/test_backendv2.py | 7 ++- test/python/providers/test_backend_v2.py | 23 ++++++--- .../providers/test_backendconfiguration.py | 3 +- .../providers/test_backendproperties.py | 3 +- test/python/providers/test_fake_backends.py | 7 ++- .../tools/monitor/test_backend_monitor.py | 3 +- .../transpiler/test_gates_in_basis_pass.py | 21 ++++++-- .../transpiler/test_high_level_synthesis.py | 31 ++++++++--- test/python/transpiler/test_target.py | 9 +++- .../transpiler/test_unitary_synthesis.py | 17 +++++-- test/python/visualization/test_gate_map.py | 15 +++--- 24 files changed, 291 insertions(+), 48 deletions(-) create mode 100644 releasenotes/notes/deprecate-fake-backends-4dd275cf9a30d41f.yaml diff --git a/qiskit/providers/fake_provider/fake_backend.py b/qiskit/providers/fake_provider/fake_backend.py index 1b092d16a5e9..e4cd0254c314 100644 --- a/qiskit/providers/fake_provider/fake_backend.py +++ b/qiskit/providers/fake_provider/fake_backend.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2019, 2023. +# (C) Copyright IBM 2019, 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 @@ -33,6 +33,7 @@ from qiskit.providers import basic_provider from qiskit.transpiler import Target from qiskit.providers.backend_compat import convert_to_target +from qiskit.utils.deprecation import deprecate_func from .utils.json_decoder import ( decode_backend_configuration, @@ -56,7 +57,7 @@ class FakeBackendV2(BackendV2): The class inherits :class:`~qiskit.providers.BackendV2` class. This version differs from earlier :class:`~qiskit.providers.fake_provider.FakeBackend` (V1) class in a - few aspects. Firstly, configuration attribute no longer exsists. Instead, + few aspects. Firstly, configuration attribute no longer exists. Instead, attributes exposing equivalent required immutable properties of the backend device are added. For example ``fake_backend.configuration().n_qubits`` is accessible from ``fake_backend.num_qubits`` now. Secondly, this version @@ -71,6 +72,16 @@ class FakeBackendV2(BackendV2): defs_filename = None backend_name = None + @deprecate_func( + additional_msg="All fake backend instances based on real device snapshots " + "(`FakeVigo`,`FakeSherbrooke`,...) have been migrated to the " + "`qiskit_ibm_runtime` package. " + "To migrate your code, run `pip install qiskit-ibm-runtime` and use " + "`from qiskit_ibm_runtime.fake_provider import FakeExample` " + "instead of `from qiskit.providers.fake_provider import FakeExample`. ", + since="0.46.0", + removal_timeline="in qiskit 1.0", + ) def __init__(self): """FakeBackendV2 initializer.""" self._conf_dict = self._get_conf_dict_from_json() diff --git a/qiskit/providers/fake_provider/fake_backend_v2.py b/qiskit/providers/fake_provider/fake_backend_v2.py index b14b87bc35a8..f37e0295f454 100644 --- a/qiskit/providers/fake_provider/fake_backend_v2.py +++ b/qiskit/providers/fake_provider/fake_backend_v2.py @@ -32,11 +32,19 @@ from qiskit.providers.options import Options from qiskit.transpiler import Target, InstructionProperties from qiskit.providers.basic_provider.basic_simulator import BasicSimulator +from qiskit.utils.deprecation import deprecate_func class FakeBackendV2(BackendV2): """A mock backend that doesn't implement run() to test compatibility with Terra internals.""" + @deprecate_func( + additional_msg="Use the `qiskit.providers.basic_provider.GenericBackendV2` " + "class instead.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self): super().__init__( None, @@ -111,6 +119,13 @@ def qubit_properties(self, qubit): class FakeBackend5QV2(BackendV2): """A mock backend that doesn't implement run() to test compatibility with Terra internals.""" + @deprecate_func( + additional_msg="Use the `qiskit.providers.basic_provider.GenericBackendV2` " + "class instead.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self, bidirectional=True): super().__init__( None, @@ -183,6 +198,13 @@ def run(self, run_input, **options): class FakeBackendSimple(BackendV2): """A fake simple backend that wraps BasicSimulator to implement run().""" + @deprecate_func( + additional_msg="Use the `qiskit.providers.basic_provider.GenericBackendV2` " + "class instead.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self): super().__init__( None, diff --git a/qiskit/providers/fake_provider/fake_job.py b/qiskit/providers/fake_provider/fake_job.py index 8d2e394c2bb6..df1866871023 100644 --- a/qiskit/providers/fake_provider/fake_job.py +++ b/qiskit/providers/fake_provider/fake_job.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2019. +# (C) Copyright IBM 2019, 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 @@ -19,6 +19,7 @@ from qiskit.providers import JobV1 from qiskit.providers.jobstatus import JobStatus +from qiskit.utils.deprecation import deprecate_func class FakeJob(JobV1): @@ -26,6 +27,12 @@ class FakeJob(JobV1): _executor = futures.ThreadPoolExecutor() + @deprecate_func( + additional_msg="Use the `qiskit.providers.JobV1` class directly instead.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self, backend, job_id, fn): super().__init__(backend, job_id) self._backend = backend diff --git a/qiskit/providers/fake_provider/fake_mumbai_v2.py b/qiskit/providers/fake_provider/fake_mumbai_v2.py index 8474319dfa62..55748bf1b955 100644 --- a/qiskit/providers/fake_provider/fake_mumbai_v2.py +++ b/qiskit/providers/fake_provider/fake_mumbai_v2.py @@ -30,11 +30,19 @@ from qiskit.providers.backend import BackendV2, QubitProperties from qiskit.providers.options import Options from qiskit.transpiler import Target, InstructionProperties +from qiskit.utils.deprecation import deprecate_func class FakeMumbaiFractionalCX(BackendV2): """A fake mumbai backend.""" + @deprecate_func( + additional_msg="Use the `qiskit.providers.basic_provider.GenericBackendV2` " + "class instead.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self): super().__init__( name="FakeMumbaiFractionalCX", diff --git a/qiskit/providers/fake_provider/fake_provider.py b/qiskit/providers/fake_provider/fake_provider.py index 1268fabf17d0..aa522e931d94 100644 --- a/qiskit/providers/fake_provider/fake_provider.py +++ b/qiskit/providers/fake_provider/fake_provider.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2019. +# (C) Copyright IBM 2019, 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 @@ -18,6 +18,7 @@ from qiskit.providers.provider import ProviderV1 from qiskit.providers.exceptions import QiskitBackendNotFoundError +from qiskit.utils.deprecation import deprecate_func from .backends import * from .fake_qasm_simulator import FakeQasmSimulator @@ -28,6 +29,15 @@ class FakeProviderFactory: """Fake provider factory class.""" + @deprecate_func( + additional_msg="This class has been migrated to the `qiskit_ibm_runtime` package. " + "To migrate your code, run `pip install qiskit-ibm-runtime` and " + "use `from qiskit_ibm_runtime.fake_provider import FakeProviderExample` " + "instead of `from qiskit.providers.fake_provider import FakeProviderExample`.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self): self.fake_provider = FakeProvider() @@ -86,6 +96,15 @@ def get_backend(self, name=None, **kwargs): def backends(self, name=None, **kwargs): return self._backends + @deprecate_func( + additional_msg="This class has been migrated to the `qiskit_ibm_runtime` package. " + "To migrate your code, run `pip install qiskit-ibm-runtime` and " + "use `from qiskit_ibm_runtime.fake_provider import FakeProviderExample` " + "instead of `from qiskit.providers.fake_provider import FakeProviderExample`.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self): self._backends = [ FakeAlmadenV2(), @@ -160,6 +179,15 @@ def get_backend(self, name=None, **kwargs): def backends(self, name=None, **kwargs): return self._backends + @deprecate_func( + additional_msg="This class has been migrated to the `qiskit_ibm_runtime` package. " + "To migrate your code, run `pip install qiskit-ibm-runtime` and " + "use `from qiskit_ibm_runtime.fake_provider import FakeProviderExample` " + "instead of `from qiskit.providers.fake_provider import FakeProviderExample`.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self): self._backends = [ FakeAlmaden(), diff --git a/qiskit/providers/fake_provider/fake_pulse_backend.py b/qiskit/providers/fake_provider/fake_pulse_backend.py index fdb02f7d4350..21f5cc2d9bc9 100644 --- a/qiskit/providers/fake_provider/fake_pulse_backend.py +++ b/qiskit/providers/fake_provider/fake_pulse_backend.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2020. +# (C) Copyright IBM 2020, 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 @@ -14,6 +14,8 @@ Fake backend abstract class for mock backends supporting OpenPulse. """ +import warnings + from qiskit.exceptions import QiskitError from qiskit.providers.models import PulseBackendConfiguration, PulseDefaults @@ -26,6 +28,22 @@ class FakePulseBackend(FakeQasmBackend): defs_filename = None + def __init__(self): + super().__init__() + # This is a deprecation warning for the subclasses. + # FakePulseBackend is not deprecated. + warnings.warn( + message="All fake backend instances based on real device snapshots (`FakeVigo`," + "`FakeSherbrooke`,...) have been migrated to the `qiskit_ibm_runtime` package. " + "These classes are deprecated as of qiskit 0.46.0 and will be removed in qiskit 1.0.0. " + "To migrate your code, run `pip install qiskit-ibm-runtime` and use " + "`from qiskit_ibm_runtime.fake_provider import FakeExample` " + "instead of `from qiskit.providers.fake_provider import FakeExample`. " + "If you are using a custom fake backend implementation, you don't need to take any action.", + category=DeprecationWarning, + stacklevel=2, + ) + def defaults(self): """Returns a snapshot of device defaults""" if not self._defaults: diff --git a/qiskit/providers/fake_provider/fake_qasm_backend.py b/qiskit/providers/fake_provider/fake_qasm_backend.py index 55dad4aff601..cfdf03d5bd02 100644 --- a/qiskit/providers/fake_provider/fake_qasm_backend.py +++ b/qiskit/providers/fake_provider/fake_qasm_backend.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2020. +# (C) Copyright IBM 2020, 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 @@ -16,6 +16,7 @@ import json import os +import warnings from qiskit.exceptions import QiskitError from qiskit.providers.models import BackendProperties, QasmBackendConfiguration @@ -40,6 +41,19 @@ def __init__(self): self._defaults = None self._properties = None super().__init__(configuration) + # This is a deprecation warning for the subclasses. + # FakeQasmBackend is not deprecated. + warnings.warn( + message="All fake backend instances based on real device snapshots (`FakeVigo`," + "`FakeSherbrooke`,...) have been migrated to the `qiskit_ibm_runtime` package. " + "These classes are deprecated as of qiskit 0.46.0 and will be removed in qiskit 1.0.0. " + "To migrate your code, run `pip install qiskit-ibm-runtime` and use " + "`from qiskit_ibm_runtime.fake_provider import FakeExample` " + "instead of `from qiskit.providers.fake_provider import FakeExample`. " + "If you are using a custom fake backend implementation, you don't need to take any action.", + category=DeprecationWarning, + stacklevel=2, + ) def properties(self): """Returns a snapshot of device properties""" diff --git a/qiskit/providers/fake_provider/fake_qasm_simulator.py b/qiskit/providers/fake_provider/fake_qasm_simulator.py index 055fb613e999..688177ea79da 100644 --- a/qiskit/providers/fake_provider/fake_qasm_simulator.py +++ b/qiskit/providers/fake_provider/fake_qasm_simulator.py @@ -15,6 +15,7 @@ """ from qiskit.providers.models import GateConfig, QasmBackendConfiguration +from qiskit.utils.deprecation import deprecate_func from .fake_backend import FakeBackend @@ -22,6 +23,12 @@ class FakeQasmSimulator(FakeBackend): """A fake simulator backend.""" + @deprecate_func( + additional_msg="Use the `qiskit.providers.basic_provider.BasicSimulator` class instead.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self): configuration = QasmBackendConfiguration( backend_name="fake_qasm_simulator", diff --git a/qiskit/providers/fake_provider/fake_qobj.py b/qiskit/providers/fake_provider/fake_qobj.py index d6dd7fbd2655..5d2b1c2c0ee3 100644 --- a/qiskit/providers/fake_provider/fake_qobj.py +++ b/qiskit/providers/fake_provider/fake_qobj.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2019. +# (C) Copyright IBM 2019, 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 @@ -23,6 +23,7 @@ QasmQobjExperiment, QasmQobjConfig, ) +from qiskit.utils.deprecation import deprecate_func from .fake_qasm_simulator import FakeQasmSimulator @@ -30,6 +31,12 @@ class FakeQobj(QasmQobj): """A fake `Qobj` instance.""" + @deprecate_func( + additional_msg="Use the `qiskit.qobj.QasmQobj` class directly instead.", + since="0.46.0", + removal_timeline="in qiskit 1.0", + package_name="qiskit", + ) def __init__(self): qobj_id = "test_id" config = QasmQobjConfig(shots=1024, memory_slots=1) diff --git a/qiskit/providers/fake_provider/utils/configurable_backend.py b/qiskit/providers/fake_provider/utils/configurable_backend.py index 0f3c323d759d..c4ace51cdde9 100644 --- a/qiskit/providers/fake_provider/utils/configurable_backend.py +++ b/qiskit/providers/fake_provider/utils/configurable_backend.py @@ -38,7 +38,7 @@ class ConfigurableFakeBackend(FakeBackend): @deprecate_func( since="0.46.0", additional_msg="Use a suitable FakeBackend instead.", - removal_timeline="Qiskit 1.0", + removal_timeline="in qiskit 1.0", ) def __init__( self, diff --git a/qiskit/test/base.py b/qiskit/test/base.py index 353921f8888b..70bb097ae4ba 100644 --- a/qiskit/test/base.py +++ b/qiskit/test/base.py @@ -245,7 +245,6 @@ def setUpClass(cls): ] for msg in allow_DeprecationWarning_message: warnings.filterwarnings("default", category=DeprecationWarning, message=msg) - allow_aer_DeprecationWarning_message = [ # This warning should be fixed once Qiskit/qiskit-aer#1761 is in a release version of Aer. "Setting metadata to None.*", @@ -257,11 +256,15 @@ def setUpClass(cls): "The qiskit.extensions module is deprecated since Qiskit 0.46.0. It will be removed " "in the Qiskit 1.0 release.", ] - for msg in allow_aer_DeprecationWarning_message: warnings.filterwarnings( "default", category=DeprecationWarning, module="qiskit_aer.*", message=msg ) + # Ignore fake backend deprecation warnings to avoid over-crowding the test log + ignore_fake_backend_message = r".*have been migrated to the `qiskit_ibm_runtime` package.*" + warnings.filterwarnings( + "ignore", category=DeprecationWarning, message=ignore_fake_backend_message + ) class FullQiskitTestCase(QiskitTestCase): diff --git a/releasenotes/notes/deprecate-fake-backends-4dd275cf9a30d41f.yaml b/releasenotes/notes/deprecate-fake-backends-4dd275cf9a30d41f.yaml new file mode 100644 index 000000000000..a41e1b554bad --- /dev/null +++ b/releasenotes/notes/deprecate-fake-backends-4dd275cf9a30d41f.yaml @@ -0,0 +1,51 @@ +--- +deprecations: + - | + The :mod:`qiskit.providers.fake_provider` module has been migrated to + the ``qiskit-ibm-runtime`` Python package. For this reason, the following elements in + the :mod:`qiskit.providers.fake_provider` have been deprecated as of Qiskit 0.46 and will be + removed in Qiskit 1.0: + + * :class:`qiskit.providers.fake_provider.FakeProvider` + * :class:`qiskit.providers.fake_provider.FakeProviderForBackendV2` + * :class:`qiskit.providers.fake_provider.FakeProviderFactory` + * :class:`qiskit.providers.fake_provider.fake_backends.FakeBackendV2` + * any fake backend contained in :mod:`qiskit.providers.fake_provider.backends` + (accessible through the provider) + * :class:`qiskit.providers.fake_provider.FakeQasmSimulator` + * :class:`qiskit.providers.fake_provider.FakeJob` + * :class:`qiskit.providers.fake_provider.FakeQobj` + + Migration example to the new fake provider:: + + # Legacy path + from qiskit.providers.fake_provider import FakeProvider, FakeSherbrooke + backend1 = FakeProvider().get_backend("fake_ourense") + backend2 = FakeSherbrooke() + + # New path + # run "pip install qiskit-ibm-runtime" + from qiskit_ibm_runtime.fake_provider import FakeProvider, FakeSherbrooke + backend1 = FakeProvider().get_backend("fake_ourense") + backend2 = FakeSherbrooke() + + Additionally, the following fake backends designed for special testing purposes have been superseded + by the new :class:`.GenericBackendV2` class, and are also deprecated as of Qiskit 0.46: + + * :class:`qiskit.providers.fake_provider.fake_backend_v2.FakeBackendV2` + * :class:`qiskit.providers.fake_provider.fake_backend_v2.FakeBackendV2LegacyQubitProps` + * :class:`qiskit.providers.fake_provider.fake_backend_v2.FakeBackend5QV2` + * :class:`qiskit.providers.fake_provider.fake_backend_v2.FakeBackendSimple` + + Migration example to the new :class:`.GenericBackendV2` class:: + + # Legacy path + from qiskit.providers.fake_provider import FakeBackend5QV2 + backend = FakeBackend5QV2() + + # New path + from qiskit.providers.fake_provider import GenericBackendV2 + backend = GenericBackendV2(num_qubits=5) + # note that this class will generate 5q backend with generic + # properties that serves the same purpose as FakeBackend5QV2 + # but will generate different results diff --git a/test/python/algorithms/test_backendv1.py b/test/python/algorithms/test_backendv1.py index d050399ad728..40ce7cd374dd 100644 --- a/test/python/algorithms/test_backendv1.py +++ b/test/python/algorithms/test_backendv1.py @@ -31,7 +31,9 @@ class TestBackendV1(QiskitAlgorithmsTestCase): def setUp(self): super().setUp() - self._provider = FakeProvider() + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + self._provider = FakeProvider() self._qasm = self._provider.get_backend("fake_qasm_simulator") self.seed = 50 diff --git a/test/python/algorithms/test_backendv2.py b/test/python/algorithms/test_backendv2.py index 0987a631f690..522d03cd428a 100644 --- a/test/python/algorithms/test_backendv2.py +++ b/test/python/algorithms/test_backendv2.py @@ -13,6 +13,7 @@ """Test Providers that support BackendV2 interface""" import unittest +import warnings from test.python.algorithms import QiskitAlgorithmsTestCase from qiskit import QuantumCircuit from qiskit.providers.fake_provider import FakeProvider @@ -29,8 +30,10 @@ class TestBackendV2(QiskitAlgorithmsTestCase): def setUp(self): super().setUp() - self._provider = FakeProvider() - self._qasm = FakeBackendSimple() + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + self._provider = FakeProvider() + self._qasm = FakeBackendSimple() self.seed = 50 def test_vqe_qasm(self): diff --git a/test/python/providers/test_backend_v2.py b/test/python/providers/test_backend_v2.py index 970224c55dbc..00a6b69400a7 100644 --- a/test/python/providers/test_backend_v2.py +++ b/test/python/providers/test_backend_v2.py @@ -14,6 +14,7 @@ # pylint: disable=missing-module-docstring import math +import warnings from test import combine @@ -38,7 +39,9 @@ class TestBackendV2(QiskitTestCase): def setUp(self): super().setUp() - self.backend = FakeBackendV2() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + self.backend = FakeBackendV2() def assertMatchesTargetConstraints(self, tqc, target): qubit_indices = {qubit: index for index, qubit in enumerate(tqc.qubits)} @@ -61,15 +64,19 @@ def test_qubit_properties(self): def test_legacy_qubit_properties(self): """Test that qubit props work for backends not using properties in target.""" - props = FakeBackendV2LegacyQubitProps().qubit_properties([1, 0]) + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + props = FakeBackendV2LegacyQubitProps().qubit_properties([1, 0]) self.assertEqual([73.09352e-6, 63.48783e-6], [x.t1 for x in props]) self.assertEqual([126.83382e-6, 112.23246e-6], [x.t2 for x in props]) self.assertEqual([5.26722e9, 5.17538e9], [x.frequency for x in props]) def test_no_qubit_properties_raises(self): """Ensure that if no qubit properties are defined we raise correctly.""" - with self.assertRaises(NotImplementedError): - FakeBackendSimple().qubit_properties(0) + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + with self.assertRaises(NotImplementedError): + FakeBackendSimple().qubit_properties(0) def test_option_bounds(self): """Test that option bounds are enforced.""" @@ -101,7 +108,9 @@ def test_transpile(self, opt_level): name="{gate}_level_{opt_level}_bidirectional_{bidirectional}", ) def test_5q_ghz(self, opt_level, gate, bidirectional): - backend = FakeBackend5QV2(bidirectional) + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2(bidirectional) qc = QuantumCircuit(5) qc.h(0) getattr(qc, gate)(0, 1) @@ -158,7 +167,9 @@ def test_transpile_relies_on_gate_direction(self): def test_transpile_mumbai_target(self): """Test that transpile respects a more involved target for a fake mumbai.""" - backend = FakeMumbaiFractionalCX() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeMumbaiFractionalCX() qc = QuantumCircuit(2) qc.h(0) qc.cx(1, 0) diff --git a/test/python/providers/test_backendconfiguration.py b/test/python/providers/test_backendconfiguration.py index e418fef2e19a..a05f11386867 100644 --- a/test/python/providers/test_backendconfiguration.py +++ b/test/python/providers/test_backendconfiguration.py @@ -27,7 +27,8 @@ class TestBackendConfiguration(QiskitTestCase): def setUp(self): super().setUp() - self.provider = FakeProvider() + with self.assertWarns(DeprecationWarning): + self.provider = FakeProvider() self.config = self.provider.get_backend("fake_openpulse_2q").configuration() def test_simple_config(self): diff --git a/test/python/providers/test_backendproperties.py b/test/python/providers/test_backendproperties.py index 3bb15bda1064..74f39cc9e01b 100644 --- a/test/python/providers/test_backendproperties.py +++ b/test/python/providers/test_backendproperties.py @@ -27,7 +27,8 @@ class BackendpropertiesTestCase(QiskitTestCase): def setUp(self): super().setUp() - self.provider = FakeProvider() + with self.assertWarns(DeprecationWarning): + self.provider = FakeProvider() self.backend = self.provider.get_backend("fake_ourense") self.properties = self.backend.properties() self.ref_gate = next( diff --git a/test/python/providers/test_fake_backends.py b/test/python/providers/test_fake_backends.py index eaaf79bfc462..efa1dd3ebf2f 100644 --- a/test/python/providers/test_fake_backends.py +++ b/test/python/providers/test_fake_backends.py @@ -17,6 +17,7 @@ import itertools import operator import unittest +import warnings from test import combine from ddt import ddt, data @@ -76,8 +77,10 @@ SwitchCaseOp, ) -FAKE_PROVIDER_FOR_BACKEND_V2 = FakeProviderForBackendV2() -FAKE_PROVIDER = FakeProvider() +with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + FAKE_PROVIDER_FOR_BACKEND_V2 = FakeProviderForBackendV2() + FAKE_PROVIDER = FakeProvider() @ddt diff --git a/test/python/tools/monitor/test_backend_monitor.py b/test/python/tools/monitor/test_backend_monitor.py index a2c4802802e6..2e212cdd6901 100644 --- a/test/python/tools/monitor/test_backend_monitor.py +++ b/test/python/tools/monitor/test_backend_monitor.py @@ -61,7 +61,8 @@ def setUp(self): self.import_error = True qiskit.IBMQ = None self.ibmq_back = qiskit.IBMQ - qiskit.IBMQ = FakeProviderFactory() + with self.assertWarns(DeprecationWarning): + qiskit.IBMQ = FakeProviderFactory() self.addCleanup(self._restore_ibmq) if hasattr(providers, "ibmq"): self.prov_backup = providers.ibmq diff --git a/test/python/transpiler/test_gates_in_basis_pass.py b/test/python/transpiler/test_gates_in_basis_pass.py index e181423a00ae..bf530a45e73e 100644 --- a/test/python/transpiler/test_gates_in_basis_pass.py +++ b/test/python/transpiler/test_gates_in_basis_pass.py @@ -11,6 +11,7 @@ # that they have been altered from the originals. """Test GatesInBasis pass.""" +import warnings from qiskit.circuit import QuantumCircuit, ForLoopOp, IfElseOp, SwitchCaseOp, Clbit from qiskit.circuit.library import HGate, CXGate, UGate, XGate, ZGate @@ -95,7 +96,9 @@ def test_all_gates_in_basis_after_translation(self): def test_all_gates_in_basis_with_target(self): """Test circuit with all gates in basis with target.""" - target = FakeBackend5QV2().target + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + target = FakeBackend5QV2().target basis_gates = ["cx", "u"] # not used property_set = {} analysis_pass = GatesInBasis(basis_gates, target=target) @@ -108,7 +111,9 @@ def test_all_gates_in_basis_with_target(self): def test_all_gates_not_in_basis_with_target(self): """Test circuit with not all gates in basis with target.""" - target = FakeBackend5QV2().target + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + target = FakeBackend5QV2().target basis_gates = ["cx", "h"] property_set = {} analysis_pass = GatesInBasis(basis_gates, target=target) @@ -121,7 +126,9 @@ def test_all_gates_not_in_basis_with_target(self): def test_all_gates_in_basis_not_on_all_qubits_with_target(self): """Test circuit with gate in global basis but not local basis.""" - target = FakeBackend5QV2().target + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + target = FakeBackend5QV2().target basis_gates = ["ecr", "cx", "h"] property_set = {} analysis_pass = GatesInBasis(basis_gates, target=target) @@ -134,7 +141,9 @@ def test_all_gates_in_basis_not_on_all_qubits_with_target(self): def test_all_gates_in_basis_empty_circuit_with_target(self): """Test circuit with no gates with target.""" - target = FakeBackend5QV2().target + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + target = FakeBackend5QV2().target basis_gates = ["cx", "u"] property_set = {} analysis_pass = GatesInBasis(basis_gates, target=target) @@ -187,7 +196,9 @@ def test_all_gates_not_in_ideal_sim_target(self): def test_all_gates_in_basis_after_translation_with_target(self): """Test circuit with gates in basis after conditional translation.""" - target = FakeBackend5QV2().target + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + target = FakeBackend5QV2().target basis_gates = ["cx", "u"] property_set = {} analysis_pass = GatesInBasis(basis_gates, target) diff --git a/test/python/transpiler/test_high_level_synthesis.py b/test/python/transpiler/test_high_level_synthesis.py index 962a3b87b66e..b667df7974ef 100644 --- a/test/python/transpiler/test_high_level_synthesis.py +++ b/test/python/transpiler/test_high_level_synthesis.py @@ -16,6 +16,8 @@ import unittest.mock +import warnings + import numpy as np from qiskit.circuit import ( QuantumCircuit, @@ -463,13 +465,16 @@ def test_target_gets_passed_to_plugins(self): qc.append(OpA(), [0]) mock_plugin_manager = MockPluginManager + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2() with unittest.mock.patch( "qiskit.transpiler.passes.synthesis.high_level_synthesis.HighLevelSynthesisPluginManager", wraps=mock_plugin_manager, ): hls_config = HLSConfig(op_a=["needs_coupling_map"]) pm_good = PassManager( - [HighLevelSynthesis(hls_config=hls_config, target=FakeBackend5QV2().target)] + [HighLevelSynthesis(hls_config=hls_config, target=backend.target)] ) # HighLevelSynthesis is initialized with target. @@ -855,7 +860,9 @@ def test_control_high_level_object(self): annotated_linear_function = AnnotatedOperation(linear_function, ControlModifier(1)) qc = QuantumCircuit(3) qc.append(annotated_linear_function, [0, 1, 2]) - backend = FakeBackend5QV2() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2() qct = HighLevelSynthesis(target=backend.target)(qc) self.assertEqual(Operator(qc), Operator(qct)) @@ -868,7 +875,9 @@ def test_transpile_control_high_level_object(self): annotated_linear_function = AnnotatedOperation(linear_function, ControlModifier(1)) qc = QuantumCircuit(3) qc.append(annotated_linear_function, [0, 1, 2]) - backend = FakeBackend5QV2() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2() qct = transpile(qc, target=backend.target) ops = qct.count_ops().keys() for op in ops: @@ -883,7 +892,9 @@ def test_inverse_high_level_object(self): annotated_linear_function = AnnotatedOperation(linear_function, InverseModifier()) qc = QuantumCircuit(3) qc.append(annotated_linear_function, [0, 1]) - backend = FakeBackend5QV2() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2() qct = HighLevelSynthesis(target=backend.target)(qc) self.assertEqual(Operator(qc), Operator(qct)) @@ -896,7 +907,9 @@ def test_transpile_inverse_high_level_object(self): annotated_linear_function = AnnotatedOperation(linear_function, InverseModifier()) qc = QuantumCircuit(3) qc.append(annotated_linear_function, [0, 1]) - backend = FakeBackend5QV2() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2() qct = transpile(qc, target=backend.target) ops = qct.count_ops().keys() for op in ops: @@ -911,7 +924,9 @@ def test_power_high_level_object(self): annotated_linear_function = AnnotatedOperation(linear_function, PowerModifier(3)) qc = QuantumCircuit(3) qc.append(annotated_linear_function, [0, 1]) - backend = FakeBackend5QV2() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2() qct = HighLevelSynthesis(target=backend.target)(qc) self.assertEqual(Operator(qc), Operator(qct)) @@ -924,7 +939,9 @@ def test_transpile_power_high_level_object(self): annotated_linear_function = AnnotatedOperation(linear_function, PowerModifier(3)) qc = QuantumCircuit(3) qc.append(annotated_linear_function, [0, 1]) - backend = FakeBackend5QV2() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2() qct = transpile(qc, target=backend.target) ops = qct.count_ops().keys() for op in ops: diff --git a/test/python/transpiler/test_target.py b/test/python/transpiler/test_target.py index e6eb57042ea7..b6fae13d6c04 100644 --- a/test/python/transpiler/test_target.py +++ b/test/python/transpiler/test_target.py @@ -13,6 +13,7 @@ # pylint: disable=missing-docstring import math +import warnings from qiskit.circuit.library import ( RZGate, @@ -55,7 +56,9 @@ class TestTarget(QiskitTestCase): def setUp(self): super().setUp() - self.fake_backend = FakeBackendV2() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + self.fake_backend = FakeBackendV2() self.fake_backend_target = self.fake_backend.target self.theta = Parameter("theta") self.phi = Parameter("phi") @@ -1052,7 +1055,9 @@ def test_instruction_supported(self): self.assertFalse(self.ideal_sim_target.instruction_supported("cx", (0, 1, 2))) def test_instruction_supported_parameters(self): - mumbai = FakeMumbaiFractionalCX() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + mumbai = FakeMumbaiFractionalCX() self.assertTrue( mumbai.target.instruction_supported( qargs=(0, 1), operation_class=RZXGate, parameters=[math.pi / 4] diff --git a/test/python/transpiler/test_unitary_synthesis.py b/test/python/transpiler/test_unitary_synthesis.py index 93302ca78fd1..6eb872800aa1 100644 --- a/test/python/transpiler/test_unitary_synthesis.py +++ b/test/python/transpiler/test_unitary_synthesis.py @@ -15,6 +15,7 @@ """ Tests for the default UnitarySynthesis transpiler pass. """ +import warnings from test import combine import unittest @@ -604,7 +605,9 @@ def test_coupling_map_transpile(self, opt): name="opt_level_{opt_level}_bidirectional_{bidirectional}", ) def test_coupling_map_transpile_with_backendv2(self, opt_level, bidirectional): - backend = FakeBackend5QV2(bidirectional) + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2(bidirectional) qr = QuantumRegister(2) circ = QuantumCircuit(qr) circ.append(random_unitary(4, seed=1), [0, 1]) @@ -656,7 +659,9 @@ def test_coupling_unequal_duration_with_backendv2(self, opt_level, bidirectional qr = QuantumRegister(2) circ = QuantumCircuit(qr) circ.append(random_unitary(4, seed=1), [1, 0]) - backend = FakeBackend5QV2(bidirectional) + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackend5QV2(bidirectional) tqc = transpile( circ, backend=backend, @@ -681,7 +686,9 @@ def test_non_overlapping_kak_gates_with_backendv2(self, opt_level): qr = QuantumRegister(2) circ = QuantumCircuit(qr) circ.append(random_unitary(4, seed=1), [1, 0]) - backend = FakeBackendV2() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeBackendV2() tqc = transpile( circ, backend=backend, @@ -699,7 +706,9 @@ def test_fractional_cx_with_backendv2(self): qr = QuantumRegister(2) circ = QuantumCircuit(qr) circ.append(random_unitary(4, seed=1), [0, 1]) - backend = FakeMumbaiFractionalCX() + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=DeprecationWarning) + backend = FakeMumbaiFractionalCX() synth_pass = UnitarySynthesis(target=backend.target) tqc = synth_pass(circ) tqc_index = {qubit: index for index, qubit in enumerate(tqc.qubits)} diff --git a/test/python/visualization/test_gate_map.py b/test/python/visualization/test_gate_map.py index c4b7c3aabf32..01dcf28f28cc 100644 --- a/test/python/visualization/test_gate_map.py +++ b/test/python/visualization/test_gate_map.py @@ -12,6 +12,7 @@ """A test for visualizing device coupling maps""" import unittest +import warnings from io import BytesIO from ddt import ddt, data @@ -46,13 +47,15 @@ class TestGateMap(QiskitVisualizationTestCase): """visual tests for plot_gate_map""" - backends = list( - filter( - lambda x: not x.configuration().simulator - and x.configuration().num_qubits in range(5, 21), - FakeProvider().backends(), + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + backends = list( + filter( + lambda x: not x.configuration().simulator + and x.configuration().num_qubits in range(5, 21), + FakeProvider().backends(), + ) ) - ) @data(*backends) @unittest.skipIf(not optionals.HAS_MATPLOTLIB, "matplotlib not available.")