Skip to content

Commit

Permalink
Deprecate provider-specific fake backends,FakeProvider class and rela…
Browse files Browse the repository at this point in the history
…ted 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 <kevin@hart.mn>

* 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 <kevin@hart.mn>
  • Loading branch information
ElePT and kevinhartman authored Jan 30, 2024
1 parent 155a790 commit 3ca9882
Show file tree
Hide file tree
Showing 24 changed files with 291 additions and 48 deletions.
15 changes: 13 additions & 2 deletions qiskit/providers/fake_provider/fake_backend.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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()
Expand Down
22 changes: 22 additions & 0 deletions qiskit/providers/fake_provider/fake_backend_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion qiskit/providers/fake_provider/fake_job.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,13 +19,20 @@

from qiskit.providers import JobV1
from qiskit.providers.jobstatus import JobStatus
from qiskit.utils.deprecation import deprecate_func


class FakeJob(JobV1):
"""Fake simulator job"""

_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
Expand Down
8 changes: 8 additions & 0 deletions qiskit/providers/fake_provider/fake_mumbai_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 29 additions & 1 deletion qiskit/providers/fake_provider/fake_provider.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
20 changes: 19 additions & 1 deletion qiskit/providers/fake_provider/fake_pulse_backend.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.
# (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
Expand All @@ -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

Expand All @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion qiskit/providers/fake_provider/fake_qasm_backend.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.
# (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
Expand All @@ -16,6 +16,7 @@

import json
import os
import warnings

from qiskit.exceptions import QiskitError
from qiskit.providers.models import BackendProperties, QasmBackendConfiguration
Expand All @@ -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"""
Expand Down
7 changes: 7 additions & 0 deletions qiskit/providers/fake_provider/fake_qasm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
"""

from qiskit.providers.models import GateConfig, QasmBackendConfiguration
from qiskit.utils.deprecation import deprecate_func

from .fake_backend import FakeBackend


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",
Expand Down
9 changes: 8 additions & 1 deletion qiskit/providers/fake_provider/fake_qobj.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,13 +23,20 @@
QasmQobjExperiment,
QasmQobjConfig,
)
from qiskit.utils.deprecation import deprecate_func

from .fake_qasm_simulator import FakeQasmSimulator


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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions qiskit/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand All @@ -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):
Expand Down
Loading

0 comments on commit 3ca9882

Please sign in to comment.