diff --git a/qiskit_ibm_provider/fake_provider/__init__.py b/qiskit_ibm_provider/fake_provider/__init__.py index e2ae5a1ff..70acc4371 100644 --- a/qiskit_ibm_provider/fake_provider/__init__.py +++ b/qiskit_ibm_provider/fake_provider/__init__.py @@ -251,4 +251,3 @@ # Special fake backends for special testing purposes from .fake_qasm_simulator import FakeQasmSimulator - diff --git a/qiskit_ibm_provider/fake_provider/backends/melbourne/fake_melbourne.py b/qiskit_ibm_provider/fake_provider/backends/melbourne/fake_melbourne.py index 530492b31..c7c559141 100644 --- a/qiskit_ibm_provider/fake_provider/backends/melbourne/fake_melbourne.py +++ b/qiskit_ibm_provider/fake_provider/backends/melbourne/fake_melbourne.py @@ -17,7 +17,11 @@ import os import json -from qiskit.providers.models import GateConfig, QasmBackendConfiguration, BackendProperties +from qiskit.providers.models import ( + GateConfig, + QasmBackendConfiguration, + BackendProperties, +) from qiskit_ibm_provider.fake_provider.fake_backend import FakeBackend from qiskit_ibm_provider.fake_provider import fake_backend @@ -86,6 +90,6 @@ def properties(self): """Returns a snapshot of device properties""" dirname = os.path.dirname(__file__) filename = "props_melbourne.json" - with open(os.path.join(dirname, filename)) as f_prop: + with open(os.path.join(dirname, filename), encoding="utf-8") as f_prop: props = json.load(f_prop) return BackendProperties.from_dict(props) diff --git a/qiskit_ibm_provider/fake_provider/backends/poughkeepsie/fake_poughkeepsie.py b/qiskit_ibm_provider/fake_provider/backends/poughkeepsie/fake_poughkeepsie.py index 920f6f590..ed47f4096 100644 --- a/qiskit_ibm_provider/fake_provider/backends/poughkeepsie/fake_poughkeepsie.py +++ b/qiskit_ibm_provider/fake_provider/backends/poughkeepsie/fake_poughkeepsie.py @@ -17,7 +17,11 @@ import os import json -from qiskit.providers.models import GateConfig, QasmBackendConfiguration, BackendProperties +from qiskit.providers.models import ( + GateConfig, + QasmBackendConfiguration, + BackendProperties, +) from qiskit_ibm_provider.fake_provider.fake_backend import FakeBackend from qiskit_ibm_provider.fake_provider import fake_backend @@ -119,6 +123,6 @@ def properties(self): """Returns a snapshot of device properties""" dirname = os.path.dirname(__file__) filename = "props_poughkeepsie.json" - with open(os.path.join(dirname, filename)) as f_prop: + with open(os.path.join(dirname, filename), encoding="utf-8") as f_prop: props = json.load(f_prop) return BackendProperties.from_dict(props) diff --git a/qiskit_ibm_provider/fake_provider/backends/tenerife/fake_tenerife.py b/qiskit_ibm_provider/fake_provider/backends/tenerife/fake_tenerife.py index 969ec854e..2d49e8a4b 100644 --- a/qiskit_ibm_provider/fake_provider/backends/tenerife/fake_tenerife.py +++ b/qiskit_ibm_provider/fake_provider/backends/tenerife/fake_tenerife.py @@ -17,7 +17,11 @@ import os import json -from qiskit.providers.models import GateConfig, QasmBackendConfiguration, BackendProperties +from qiskit.providers.models import ( + GateConfig, + QasmBackendConfiguration, + BackendProperties, +) from qiskit_ibm_provider.fake_provider.fake_backend import FakeBackend @@ -59,6 +63,6 @@ def properties(self): """Returns a snapshot of device properties as recorded on 8/30/19.""" dirname = os.path.dirname(__file__) filename = "props_tenerife.json" - with open(os.path.join(dirname, filename)) as f_prop: + with open(os.path.join(dirname, filename), encoding="utf-8") as f_prop: props = json.load(f_prop) return BackendProperties.from_dict(props) diff --git a/qiskit_ibm_provider/fake_provider/backends/tokyo/fake_tokyo.py b/qiskit_ibm_provider/fake_provider/backends/tokyo/fake_tokyo.py index c012685ec..586702e1d 100644 --- a/qiskit_ibm_provider/fake_provider/backends/tokyo/fake_tokyo.py +++ b/qiskit_ibm_provider/fake_provider/backends/tokyo/fake_tokyo.py @@ -17,7 +17,11 @@ import os import json -from qiskit.providers.models import GateConfig, QasmBackendConfiguration, BackendProperties +from qiskit.providers.models import ( + GateConfig, + QasmBackendConfiguration, + BackendProperties, +) from qiskit_ibm_provider.fake_provider.fake_backend import FakeBackend @@ -132,6 +136,6 @@ def properties(self): """Returns a snapshot of device properties as recorded on 8/30/19.""" dirname = os.path.dirname(__file__) filename = "props_tokyo.json" - with open(os.path.join(dirname, filename)) as f_prop: + with open(os.path.join(dirname, filename), encoding="utf-8") as f_prop: props = json.load(f_prop) return BackendProperties.from_dict(props) diff --git a/qiskit_ibm_provider/fake_provider/fake_backend.py b/qiskit_ibm_provider/fake_provider/fake_backend.py index d4c33f329..6a132117a 100644 --- a/qiskit_ibm_provider/fake_provider/fake_backend.py +++ b/qiskit_ibm_provider/fake_provider/fake_backend.py @@ -25,7 +25,11 @@ from typing import List, Iterable from qiskit import circuit -from qiskit.providers.models import BackendProperties, BackendConfiguration, PulseDefaults +from qiskit.providers.models import ( + BackendProperties, + BackendConfiguration, + PulseDefaults, +) from qiskit.providers import BackendV2, BackendV1 from qiskit import pulse from qiskit.exceptions import QiskitError @@ -118,7 +122,9 @@ def _parse_channels(self, channels): def _setup_sim(self): if _optionals.HAS_AER: - from qiskit_aer import AerSimulator # pylint: disable=import-outside-toplevel + from qiskit_aer import ( # pylint: disable=import-outside-toplevel + AerSimulator, + ) self.sim = AerSimulator() if self.target and self._props_dict: @@ -200,7 +206,9 @@ def _default_options(cls): default values set """ if _optionals.HAS_AER: - from qiskit_aer import AerSimulator # pylint: disable=import-outside-toplevel + from qiskit_aer import ( # pylint: disable=import-outside-toplevel + AerSimulator, + ) return AerSimulator._default_options() else: @@ -339,7 +347,10 @@ def run(self, run_input, **options): pulse_job = False elif isinstance(circuits, list): if circuits: - if all(isinstance(x, (pulse.Schedule, pulse.ScheduleBlock)) for x in circuits): + if all( + isinstance(x, (pulse.Schedule, pulse.ScheduleBlock)) + for x in circuits + ): pulse_job = True elif all(isinstance(x, circuit.QuantumCircuit) for x in circuits): pulse_job = False @@ -349,7 +360,9 @@ def run(self, run_input, **options): "QuantumCircuit, Schedule, or a list of either" % circuits ) if pulse_job: # pulse job - raise QiskitError("Pulse simulation is currently not supported for V2 fake backends.") + raise QiskitError( + "Pulse simulation is currently not supported for V2 fake backends." + ) # circuit job if not _optionals.HAS_AER: warnings.warn("Aer not found using BasicAer and no noise", RuntimeWarning) @@ -373,15 +386,21 @@ def _get_noise_model_from_backend_v2( This is a temporary fix until qiskit-aer supports building noise model from a BackendV2 object. """ - from qiskit.circuit import Delay # pylint: disable=import-outside-toplevel - from qiskit.providers.exceptions import BackendPropertyError # pylint: disable=import-outside-toplevel - from qiskit_aer.noise import NoiseModel # pylint: disable=import-outside-toplevel - from qiskit_aer.noise.device.models import ( - _excited_population, # pylint: disable=import-outside-toplevel - basic_device_gate_errors, # pylint: disable=import-outside-toplevel - basic_device_readout_errors, # pylint: disable=import-outside-toplevel + from qiskit.circuit import Delay # pylint: disable=import-outside-toplevel + from qiskit.providers.exceptions import ( # pylint: disable=import-outside-toplevel + BackendPropertyError, + ) + from qiskit_aer.noise import ( # pylint: disable=import-outside-toplevel + NoiseModel, + ) + from qiskit_aer.noise.device.models import ( # pylint: disable=import-outside-toplevel + _excited_population, + basic_device_gate_errors, + basic_device_readout_errors, + ) + from qiskit_aer.noise.passes import ( # pylint: disable=import-outside-toplevel + RelaxationNoisePass, ) - from qiskit_aer.noise.passes import RelaxationNoisePass # pylint: disable=import-outside-toplevel if self._props_dict is None: self._set_props_dict_from_json() @@ -389,7 +408,7 @@ def _get_noise_model_from_backend_v2( properties = BackendProperties.from_dict(self._props_dict) basis_gates = self.operation_names num_qubits = self.num_qubits - dt = self.dt + dt = self.dt # pylint: disable=invalid-name noise_model = NoiseModel(basis_gates=basis_gates) @@ -419,7 +438,9 @@ def _get_noise_model_from_backend_v2( # Add delay errors via RelaxationNoise pass try: excited_state_populations = [ - _excited_population(freq=properties.frequency(q), temperature=temperature) + _excited_population( + freq=properties.frequency(q), temperature=temperature + ) for q in range(num_qubits) ] except BackendPropertyError: @@ -458,8 +479,10 @@ def __init__(self, configuration, time_alive=10): def _setup_sim(self): if _optionals.HAS_AER: - import qiskit_aer as aer - from qiskit_aer.noise import NoiseModel + import qiskit_aer as aer # pylint: disable=import-outside-toplevel + from qiskit_aer.noise import ( # pylint: disable=import-outside-toplevel + NoiseModel, + ) self.sim = aer.AerSimulator() if self.properties(): @@ -484,8 +507,18 @@ def properties(self): "last_update_date": "2000-01-01 00:00:00Z", "qubits": [ [ - {"date": "2000-01-01 00:00:00Z", "name": "T1", "unit": "\u00b5s", "value": 0.0}, - {"date": "2000-01-01 00:00:00Z", "name": "T2", "unit": "\u00b5s", "value": 0.0}, + { + "date": "2000-01-01 00:00:00Z", + "name": "T1", + "unit": "\u00b5s", + "value": 0.0, + }, + { + "date": "2000-01-01 00:00:00Z", + "name": "T2", + "unit": "\u00b5s", + "value": 0.0, + }, { "date": "2000-01-01 00:00:00Z", "name": "frequency", @@ -498,7 +531,12 @@ def properties(self): "unit": "", "value": 0.0, }, - {"date": "2000-01-01 00:00:00Z", "name": "operational", "unit": "", "value": 1}, + { + "date": "2000-01-01 00:00:00Z", + "name": "operational", + "unit": "", + "value": 1, + }, ] for _ in range(len(unique_qubits)) ], @@ -526,7 +564,9 @@ def properties(self): @classmethod def _default_options(cls): if _optionals.HAS_AER: - from qiskit_aer import QasmSimulator + from qiskit_aer import ( # pylint: disable=import-outside-toplevel + QasmSimulator, + ) return QasmSimulator._default_options() else: @@ -542,7 +582,10 @@ def run(self, run_input, **kwargs): pulse_job = False elif isinstance(circuits, list): if circuits: - if all(isinstance(x, (pulse.Schedule, pulse.ScheduleBlock)) for x in circuits): + if all( + isinstance(x, (pulse.Schedule, pulse.ScheduleBlock)) + for x in circuits + ): pulse_job = True elif all(isinstance(x, circuit.QuantumCircuit) for x in circuits): pulse_job = False @@ -553,19 +596,25 @@ def run(self, run_input, **kwargs): ) if pulse_job: if _optionals.HAS_AER: - import qiskit_aer as aer - from qiskit_aer.pulse import PulseSystemModel + import qiskit_aer as aer # pylint: disable=import-outside-toplevel + from qiskit_aer.pulse import ( # pylint: disable=import-outside-toplevel + PulseSystemModel, + ) system_model = PulseSystemModel.from_backend(self) sim = aer.Aer.get_backend("pulse_simulator") job = sim.run(circuits, system_model=system_model, **kwargs) else: - raise QiskitError("Unable to run pulse schedules without qiskit-aer installed") + raise QiskitError( + "Unable to run pulse schedules without qiskit-aer installed" + ) else: if self.sim is None: self._setup_sim() if not _optionals.HAS_AER: - warnings.warn("Aer not found using BasicAer and no noise", RuntimeWarning) + warnings.warn( + "Aer not found using BasicAer and no noise", RuntimeWarning + ) self.sim._options = self._options job = self.sim.run(circuits, **kwargs) return job diff --git a/qiskit_ibm_provider/fake_provider/fake_job.py b/qiskit_ibm_provider/fake_provider/fake_job.py index dbf60abfa..35e96a9c1 100644 --- a/qiskit_ibm_provider/fake_provider/fake_job.py +++ b/qiskit_ibm_provider/fake_provider/fake_job.py @@ -19,6 +19,7 @@ from qiskit.providers import JobV1 from qiskit.providers.jobstatus import JobStatus +from qiskit_ibm_provider.exceptions import IBMError class FakeJob(JobV1): @@ -54,7 +55,7 @@ def status(self): elif self._error: _status = JobStatus.ERROR else: - raise Exception(f"Unexpected state of {self.__class__.__name__}") + raise IBMError(f"Unexpected state of {self.__class__.__name__}") _status_msg = None return {"status": _status, "status_msg": _status_msg} diff --git a/qiskit_ibm_provider/fake_provider/fake_provider.py b/qiskit_ibm_provider/fake_provider/fake_provider.py index 5a5244e09..a0d7585c8 100644 --- a/qiskit_ibm_provider/fake_provider/fake_provider.py +++ b/qiskit_ibm_provider/fake_provider/fake_provider.py @@ -22,6 +22,7 @@ from .backends import * from .fake_qasm_simulator import FakeQasmSimulator + class FakeProviderFactory: """Fake provider factory class.""" @@ -72,7 +73,9 @@ class FakeProviderForBackendV2(ProviderV1): def get_backend(self, name=None, **kwargs): backend = self._backends[0] if name: - filtered_backends = [backend for backend in self._backends if backend.name() == name] + filtered_backends = [ + backend for backend in self._backends if backend.name() == name + ] if not filtered_backends: raise QiskitBackendNotFoundError() @@ -146,7 +149,9 @@ class FakeProvider(ProviderV1): def get_backend(self, name=None, **kwargs): backend = self._backends[0] if name: - filtered_backends = [backend for backend in self._backends if backend.name() == name] + filtered_backends = [ + backend for backend in self._backends if backend.name() == name + ] if not filtered_backends: raise QiskitBackendNotFoundError() diff --git a/qiskit_ibm_provider/fake_provider/fake_qasm_backend.py b/qiskit_ibm_provider/fake_provider/fake_qasm_backend.py index d350e3a20..1f8713b2c 100644 --- a/qiskit_ibm_provider/fake_provider/fake_qasm_backend.py +++ b/qiskit_ibm_provider/fake_provider/fake_qasm_backend.py @@ -64,7 +64,7 @@ def _set_props_from_json(self): self._properties = BackendProperties.from_dict(props) def _load_json(self, filename): - with open(os.path.join(self.dirname, filename)) as f_json: + with open(os.path.join(self.dirname, filename), encoding="utf-8") as f_json: the_json = json.load(f_json) return the_json diff --git a/qiskit_ibm_provider/fake_provider/fake_qasm_simulator.py b/qiskit_ibm_provider/fake_provider/fake_qasm_simulator.py index e7210d0cb..cfa0497f6 100644 --- a/qiskit_ibm_provider/fake_provider/fake_qasm_simulator.py +++ b/qiskit_ibm_provider/fake_provider/fake_qasm_simulator.py @@ -38,8 +38,20 @@ def __init__(self): gates=[GateConfig(name="TODO", parameters=[], qasm_def="TODO")], dt=1.3333, dtm=10.5, - qubit_lo_range=[[4.95, 5.05], [4.95, 5.05], [4.95, 5.05], [4.95, 5.05], [4.95, 5.05]], - meas_lo_range=[[6.65, 6.75], [6.65, 6.75], [6.65, 6.75], [6.65, 6.75], [6.65, 6.75]], + qubit_lo_range=[ + [4.95, 5.05], + [4.95, 5.05], + [4.95, 5.05], + [4.95, 5.05], + [4.95, 5.05], + ], + meas_lo_range=[ + [6.65, 6.75], + [6.65, 6.75], + [6.65, 6.75], + [6.65, 6.75], + [6.65, 6.75], + ], ) super().__init__(configuration) diff --git a/qiskit_ibm_provider/fake_provider/fake_qobj.py b/qiskit_ibm_provider/fake_provider/fake_qobj.py index 71bd1d96b..c0f33d6e5 100644 --- a/qiskit_ibm_provider/fake_provider/fake_qobj.py +++ b/qiskit_ibm_provider/fake_provider/fake_qobj.py @@ -41,4 +41,6 @@ def __init__(self): config=QasmQobjExperimentConfig(seed=123456), ) ] - super().__init__(qobj_id=qobj_id, config=config, experiments=experiments, header=header) + super().__init__( + qobj_id=qobj_id, config=config, experiments=experiments, header=header + ) diff --git a/qiskit_ibm_provider/fake_provider/utils/backend_converter.py b/qiskit_ibm_provider/fake_provider/utils/backend_converter.py index 758a294a5..998b44cc4 100644 --- a/qiskit_ibm_provider/fake_provider/utils/backend_converter.py +++ b/qiskit_ibm_provider/fake_provider/utils/backend_converter.py @@ -27,7 +27,9 @@ from qiskit.providers.models.pulsedefaults import PulseDefaults -def convert_to_target(conf_dict: dict, props_dict: dict = None, defs_dict: dict = None) -> Target: +def convert_to_target( + conf_dict: dict, props_dict: dict = None, defs_dict: dict = None +) -> Target: """Uses configuration, properties and pulse defaults dicts to construct and return Target class. """ @@ -43,7 +45,9 @@ def convert_to_target(conf_dict: dict, props_dict: dict = None, defs_dict: dict qubit_props = None if props_dict: qubit_props = qubit_props_from_props(props_dict) - target = Target(qubit_properties=qubit_props, concurrent_measurements=conf_dict.get("meas_map")) + target = Target( + qubit_properties=qubit_props, concurrent_measurements=conf_dict.get("meas_map") + ) # Parse from properties if it exsits if props_dict is not None: # Parse instructions @@ -98,14 +102,16 @@ def convert_to_target(conf_dict: dict, props_dict: dict = None, defs_dict: dict measure_props = {(n,): None for n in range(conf_dict["n_qubits"])} target.add_instruction(Measure(), measure_props) # parse global configuration properties - dt = conf_dict.get("dt") + dt = conf_dict.get("dt") # pylint: disable=invalid-name if dt: target.dt = dt * 1e-9 if "timing_constraints" in conf_dict: target.granularity = conf_dict["timing_constraints"].get("granularity") target.min_length = conf_dict["timing_constraints"].get("min_length") target.pulse_alignment = conf_dict["timing_constraints"].get("pulse_alignment") - target.acquire_alignment = conf_dict["timing_constraints"].get("acquire_alignment") + target.acquire_alignment = conf_dict["timing_constraints"].get( + "acquire_alignment" + ) # If pulse defaults exists use that as the source of truth if defs_dict is not None: # TODO remove the usage of PulseDefaults as it will be deprecated in the future @@ -141,10 +147,16 @@ def qubit_props_from_props(properties: dict) -> list: qubit_properties = {} for prop_dict in qubit: if prop_dict["name"] == "T1": - qubit_properties["t1"] = apply_prefix(prop_dict["value"], prop_dict["unit"]) + qubit_properties["t1"] = apply_prefix( + prop_dict["value"], prop_dict["unit"] + ) elif prop_dict["name"] == "T2": - qubit_properties["t2"] = apply_prefix(prop_dict["value"], prop_dict["unit"]) + qubit_properties["t2"] = apply_prefix( + prop_dict["value"], prop_dict["unit"] + ) elif prop_dict["name"] == "frequency": - qubit_properties["frequency"] = apply_prefix(prop_dict["value"], prop_dict["unit"]) + qubit_properties["frequency"] = apply_prefix( + prop_dict["value"], prop_dict["unit"] + ) qubit_props.append(QubitProperties(**qubit_properties)) return qubit_props diff --git a/qiskit_ibm_provider/fake_provider/utils/json_decoder.py b/qiskit_ibm_provider/fake_provider/utils/json_decoder.py index 557674915..e40035466 100644 --- a/qiskit_ibm_provider/fake_provider/utils/json_decoder.py +++ b/qiskit_ibm_provider/fake_provider/utils/json_decoder.py @@ -40,7 +40,9 @@ def decode_backend_properties(properties: Dict) -> None: Args: properties: A ``BackendProperties`` in dictionary format. """ - properties["last_update_date"] = dateutil.parser.isoparse(properties["last_update_date"]) + properties["last_update_date"] = dateutil.parser.isoparse( + properties["last_update_date"] + ) for qubit in properties["qubits"]: for nduv in qubit: nduv["date"] = dateutil.parser.isoparse(nduv["date"]) @@ -106,4 +108,6 @@ def _decode_pulse_qobj_instr(pulse_qobj_instr: Dict) -> None: if "val" in pulse_qobj_instr: pulse_qobj_instr["val"] = _to_complex(pulse_qobj_instr["val"]) if "parameters" in pulse_qobj_instr and "amp" in pulse_qobj_instr["parameters"]: - pulse_qobj_instr["parameters"]["amp"] = _to_complex(pulse_qobj_instr["parameters"]["amp"]) + pulse_qobj_instr["parameters"]["amp"] = _to_complex( + pulse_qobj_instr["parameters"]["amp"] + ) diff --git a/test/unit/fake_provider/test_fake_backends.py b/test/unit/fake_provider/test_fake_backends.py index 1b45684e7..4473dcfb0 100644 --- a/test/unit/fake_provider/test_fake_backends.py +++ b/test/unit/fake_provider/test_fake_backends.py @@ -31,6 +31,7 @@ def get_test_circuit(): qc.measure(qr[1], cr[1]) return qc + class FakeBackendsTest(QiskitTestCase): """fake backends test."""