Skip to content

Commit

Permalink
Deprecate fake provider class and device-specific fake backends. Leav…
Browse files Browse the repository at this point in the history
…e fake backend base classes and special fake backends for testing purposes.
  • Loading branch information
ElePT committed Jan 29, 2024
1 parent d71222f commit c5532d0
Show file tree
Hide file tree
Showing 23 changed files with 280 additions and 52 deletions.
9 changes: 9 additions & 0 deletions qiskit/providers/fake_provider/fake_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ class FakeBackendV2(BackendV2):

def __init__(self):
"""FakeBackendV2 initializer."""
warnings.warn(
message="Device-specific fake backends 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. "
"You should migrate your code to use "
"`from qiskit_ibm_runtime.fake_provider import FakeExample` "
"instead of `from qiskit.providers.fake_provider import FakeExample`.",
category=DeprecationWarning,
stacklevel=3,
)
self._conf_dict = self._get_conf_dict_from_json()
self._props_dict = None
self._defs_dict = None
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="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="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="Qiskit 1.0",
package_name="qiskit",
)
def __init__(self):
super().__init__(
None,
Expand Down
7 changes: 7 additions & 0 deletions qiskit/providers/fake_provider/fake_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 instead.",
since="0.46.0",
removal_timeline="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="Qiskit 1.0",
package_name="qiskit",
)
def __init__(self):
super().__init__(
name="FakeMumbaiFractionalCX",
Expand Down
28 changes: 27 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, 2023.
#
# 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. "
"You should migrate your code to "
"use `from qiskit_ibm_runtime.fake_provider import FakeProviderExample` "
"instead of `from qiskit.providers.fake_provider import FakeProviderExample`.",
since="0.46.0",
removal_timeline="Qiskit 1.0",
package_name="qiskit",
)
def __init__(self):
self.fake_provider = FakeProvider()

Expand Down Expand Up @@ -86,6 +96,14 @@ 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. "
"You should migrate your code to "
"use `from qiskit_ibm_runtime.fake_provider import FakeProviderExample` "
"instead of `from qiskit.providers.fake_provider import FakeProviderExample`.",
since="0.46.0",
package_name="qiskit",
)
def __init__(self):
self._backends = [
FakeAlmadenV2(),
Expand Down Expand Up @@ -160,6 +178,14 @@ 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. "
"You should migrate your code to "
"use `from qiskit_ibm_runtime.fake_provider import FakeProviderExample` "
"instead of `from qiskit.providers.fake_provider import FakeProviderExample`.",
since="0.46.0",
package_name="qiskit",
)
def __init__(self):
self._backends = [
FakeAlmaden(),
Expand Down
13 changes: 13 additions & 0 deletions qiskit/providers/fake_provider/fake_pulse_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from qiskit.exceptions import QiskitError
from qiskit.providers.models import PulseBackendConfiguration, PulseDefaults
import warnings

from .fake_qasm_backend import FakeQasmBackend
from .utils.json_decoder import decode_pulse_defaults
Expand All @@ -26,6 +27,18 @@ class FakePulseBackend(FakeQasmBackend):

defs_filename = None

def __init__(self):
warnings.warn(
message="Device-specific fake backends 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. "
"You should migrate your code to use "
"`from qiskit_ibm_runtime.fake_provider import FakeExample` "
"instead of `from qiskit.providers.fake_provider import FakeExample`.",
category=DeprecationWarning,
stacklevel=3,
)
super().__init__()

def defaults(self):
"""Returns a snapshot of device defaults"""
if not self._defaults:
Expand Down
12 changes: 11 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, 2023.
#
# 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 @@ -36,6 +37,15 @@ class FakeQasmBackend(FakeBackend):
backend_name = None

def __init__(self):
warnings.warn(
message="Device-specific fake backends 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. "
"You should migrate your code to use "
"`from qiskit_ibm_runtime.fake_provider import FakeExample` "
"instead of `from qiskit.providers.fake_provider import FakeExample`.",
category=DeprecationWarning,
stacklevel=3,
)
configuration = self._get_conf_from_json()
self._defaults = None
self._properties = None
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="Qiskit 1.0",
package_name="qiskit",
)
def __init__(self):
configuration = QasmBackendConfiguration(
backend_name="fake_qasm_simulator",
Expand Down
7 changes: 7 additions & 0 deletions qiskit/providers/fake_provider/fake_qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 instead.",
since="0.46.0",
removal_timeline="Qiskit 1.0",
package_name="qiskit",
)
def __init__(self):
qobj_id = "test_id"
config = QasmQobjConfig(shots=1024, memory_slots=1)
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"Device-specific fake backends have been migrated to the `qiskit_ibm_runtime` package.*"
warnings.filterwarnings(
"ignore", category=DeprecationWarning, message=ignore_fake_backend_message
)


class FullQiskitTestCase(QiskitTestCase):
Expand Down
50 changes: 50 additions & 0 deletions releasenotes/notes/deprecate-fake-backends-4dd275cf9a30d41f.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
deprecations:
- |
The core functionality of the :mod:`qiskit.providers.fake_provider` module has been migrated to
the new ``qiskit_ibm_runtime.fake_provider`` module. 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`
* any fake backend contained in :class:`qiskit.providers.fake_provider.backends`
(accesible 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
4 changes: 3 additions & 1 deletion test/python/algorithms/test_backendv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

0 comments on commit c5532d0

Please sign in to comment.