From ea945da002b3557c5efb57619a7605e64193c93f Mon Sep 17 00:00:00 2001 From: Will Shanks Date: Tue, 13 Feb 2024 12:18:03 -0500 Subject: [PATCH] Remove `qiskit_ibm_runtime` import from `MockIQBackend` code The import was only used for subclassing `qiskit_ibm_runtime.fake_provider.fake_backend.FakeBackendV2`. The code now subclasses `qiskit.providers.BackendV2` directly. --- qiskit_experiments/test/mock_iq_backend.py | 59 ++++++++----------- ...thout-qiskit-runtime-20d2bf9edb48312d.yaml | 7 +++ 2 files changed, 31 insertions(+), 35 deletions(-) create mode 100644 releasenotes/notes/mock-iq-backend-without-qiskit-runtime-20d2bf9edb48312d.yaml diff --git a/qiskit_experiments/test/mock_iq_backend.py b/qiskit_experiments/test/mock_iq_backend.py index 8626af8aac..db5d81d882 100644 --- a/qiskit_experiments/test/mock_iq_backend.py +++ b/qiskit_experiments/test/mock_iq_backend.py @@ -11,16 +11,18 @@ # that they have been altered from the originals. """A mock IQ backend for testing.""" +import datetime from abc import abstractmethod from typing import Sequence, List, Tuple, Dict, Union, Any + import numpy as np from qiskit import QuantumCircuit from qiskit.circuit.library import XGate, SXGate from qiskit.result import Result +from qiskit.providers import BackendV2, BackendV2Converter, Provider from qiskit.providers.fake_provider import FakeOpenPulse2Q from qiskit.qobj.utils import MeasLevel -from qiskit_ibm_runtime.fake_provider.fake_backend import FakeBackendV2 from qiskit_experiments.exceptions import QiskitError from qiskit_experiments.framework import Options @@ -33,46 +35,33 @@ ) -class FakeOpenPulse2QV2(FakeBackendV2): - """BackendV2 version of FakeOpenPulse2Q""" +class FakeOpenPulse2QV2(BackendV2): + """BackendV2 conversion of qiskit.providers.fake_provider.FakeOpenPulse2Q""" + + def __init__( + self, + provider: Provider = None, + name: str = None, + description: str = None, + online_date: datetime.datetime = None, + backend_version: str = None, + **fields, + ): + super().__init__(provider, name, description, online_date, backend_version, **fields) - def __init__(self): - # FakeOpenPulse2Q has its data hard-coded rather than in json files. We - # prepopulate the dicts so that FakeBackendV2 does not try to load them - # from files. - # - # We have to use a hack to populate _conf_dict backend_v1 = FakeOpenPulse2Q() - self._conf_dict_real = backend_v1._configuration.to_dict() - super().__init__() - self._props_dict = backend_v1._properties.to_dict() - self._defs_dict = backend_v1._defaults.to_dict() + backend_v1._configuration.description = "A fake test backend with pulse defaults" + + self.backend = BackendV2Converter(backend_v1, add_delay=True) self._defaults = backend_v1._defaults - # Workaround a bug in FakeOpenPulse2Q. It defines u1 on qubit 1 in the - # cmd_def in the defaults json file but not in the gates in the - # properties instance. The code FakeBackendV2 uses to build the Target - # assumes these two are consistent. - u1_0 = next(g for g in self._props_dict["gates"] if g["gate"] == "u1") - self._props_dict["gates"].append(u1_0.copy()) - self._props_dict["gates"][-1]["qubits"] = [1] + @property + def max_circuits(self): + return 300 @property - def _conf_dict(self): - # FakeBackendV2 sets this in __init__. As a hack, we use this property - # to prevent it from overriding our values. - return self._conf_dict_real - - @_conf_dict.setter - def _conf_dict(self, value): - pass - - # This method is not defined in the base class as we would like to avoid - # relying on it as much as necessary. Individual tests should add it when - # necessary. - # def defaults(self): - # """Pulse defaults""" - # return self._defaults + def target(self): + return self.backend.target class MockRestlessBackend(FakeOpenPulse2QV2): diff --git a/releasenotes/notes/mock-iq-backend-without-qiskit-runtime-20d2bf9edb48312d.yaml b/releasenotes/notes/mock-iq-backend-without-qiskit-runtime-20d2bf9edb48312d.yaml new file mode 100644 index 0000000000..71eadd9fb0 --- /dev/null +++ b/releasenotes/notes/mock-iq-backend-without-qiskit-runtime-20d2bf9edb48312d.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + :class:`.MockIQBackend` was refactored so that it does not import + ``qiskit_ibm_runtime`` since + :external+qiskit_ibm_runtime:doc:`qiskit-ibm-runtime ` is not a + required dependency of Qiskit Experiments.