Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
ElePT committed Oct 4, 2023
1 parent 7e757e1 commit a6bafb0
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 51 deletions.
1 change: 0 additions & 1 deletion qiskit_ibm_provider/fake_provider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,3 @@

# Special fake backends for special testing purposes
from .fake_qasm_simulator import FakeQasmSimulator

Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
101 changes: 75 additions & 26 deletions qiskit_ibm_provider/fake_provider/fake_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -373,23 +386,29 @@ 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()

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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand All @@ -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",
Expand All @@ -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))
],
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion qiskit_ibm_provider/fake_provider/fake_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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}

Expand Down
9 changes: 7 additions & 2 deletions qiskit_ibm_provider/fake_provider/fake_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .backends import *
from .fake_qasm_simulator import FakeQasmSimulator


class FakeProviderFactory:
"""Fake provider factory class."""

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion qiskit_ibm_provider/fake_provider/fake_qasm_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 14 additions & 2 deletions qiskit_ibm_provider/fake_provider/fake_qasm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion qiskit_ibm_provider/fake_provider/fake_qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Loading

0 comments on commit a6bafb0

Please sign in to comment.