Skip to content

Commit

Permalink
Remove qiskit_ibm_runtime import from MockIQBackend code
Browse files Browse the repository at this point in the history
The import was only used for subclassing
`qiskit_ibm_runtime.fake_provider.fake_backend.FakeBackendV2`. The code
now subclasses `qiskit.providers.BackendV2` directly.
  • Loading branch information
wshanks committed Feb 13, 2024
1 parent 4c54791 commit e96645d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
54 changes: 26 additions & 28 deletions qiskit_experiments/test/mock_iq_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,39 +35,35 @@
)


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()
# BackendV2Converter requires the description attribute
backend_v1._configuration.description = "A fake test backend with pulse defaults"

self.backend = BackendV2Converter(backend_v1, add_delay=True)
# See commented out defaults() method below
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
def target(self):
return self.backend.target

# 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <index>` is not a
required dependency of Qiskit Experiments.

0 comments on commit e96645d

Please sign in to comment.