Skip to content

Commit

Permalink
🐍 Update some of the Python configuration (#280)
Browse files Browse the repository at this point in the history
This PR adds a proper pytest and coverage configuration and also some
further `ruff` checks and corresponding auto-fixes.

---------

Signed-off-by: burgholzer <burgholzer@me.com>
Co-authored-by: Stefan Hillmich <stefan.hillmich@scch.at>
Co-authored-by: Stefan Hillmich <hillmich@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 22, 2023
1 parent 4416a6f commit c6f95af
Show file tree
Hide file tree
Showing 28 changed files with 126 additions and 64 deletions.
3 changes: 2 additions & 1 deletion mqt/ddsim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from mqt.ddsim.pyddsim import ( # noqa: I001
from __future__ import annotations # noqa: I001
from mqt.ddsim.pyddsim import (
CircuitSimulator,
ConstructionMode,
HybridCircuitSimulator,
Expand Down
7 changes: 3 additions & 4 deletions mqt/ddsim/error.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""
Exception for errors raised by DDSIM simulator.
"""
"""Exception for errors raised by DDSIM simulator."""
from __future__ import annotations

from qiskit import QiskitError


class DDSIMError(QiskitError):
"""Class for errors raised by the DDSIM simulator."""

def __init__(self, *message):
def __init__(self, *message) -> None:
"""Set the error message."""
super().__init__(*message)
9 changes: 5 additions & 4 deletions mqt/ddsim/hybridqasmsimulator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Backend for DDSIM Hybrid Schrodinger-Feynman Simulator."""
from __future__ import annotations

import logging
import time
import uuid
import warnings
from math import log2
from typing import List, Union

from qiskit import QiskitError, QuantumCircuit
from qiskit.compiler import assemble
Expand All @@ -23,7 +23,7 @@


class HybridQasmSimulatorBackend(BackendV1):
"""Python interface to MQT DDSIM Hybrid Schrodinger-Feynman Simulator"""
"""Python interface to MQT DDSIM Hybrid Schrodinger-Feynman Simulator."""

SHOW_STATE_VECTOR = False

Expand All @@ -37,7 +37,7 @@ def _default_options(cls) -> Options:
nthreads=local_hardware_info()["cpus"],
)

def __init__(self, configuration=None, provider=None):
def __init__(self, configuration=None, provider=None) -> None:
conf = {
"backend_name": "hybrid_qasm_simulator",
"backend_version": __version__,
Expand Down Expand Up @@ -90,7 +90,7 @@ def __init__(self, configuration=None, provider=None):
}
super().__init__(configuration=configuration or BackendConfiguration.from_dict(conf), provider=provider)

def run(self, quantum_circuits: Union[QuantumCircuit, List[QuantumCircuit]], **options):
def run(self, quantum_circuits: QuantumCircuit | list[QuantumCircuit], **options):
if isinstance(quantum_circuits, (QasmQobj, PulseQobj)):
msg = "QasmQobj and PulseQobj are not supported."
raise QiskitError(msg)
Expand Down Expand Up @@ -189,6 +189,7 @@ def _validate(self, _quantum_circuit):

def status(self):
"""Return backend status.
Returns:
BackendStatus: the status of the backend.
"""
Expand Down
5 changes: 3 additions & 2 deletions mqt/ddsim/hybridstatevectorsimulator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Backend for DDSIM Hybrid Schrodinger-Feynman Simulator."""
from __future__ import annotations

import logging
from math import log2
Expand All @@ -13,11 +14,11 @@


class HybridStatevectorSimulatorBackend(HybridQasmSimulatorBackend):
"""Python interface to MQT DDSIM Hybrid Schrodinger-Feynman Simulator"""
"""Python interface to MQT DDSIM Hybrid Schrodinger-Feynman Simulator."""

SHOW_STATE_VECTOR = True

def __init__(self, configuration=None, provider=None):
def __init__(self, configuration=None, provider=None) -> None:
conf = {
"backend_name": "hybrid_statevector_simulator",
"backend_version": __version__,
Expand Down
13 changes: 8 additions & 5 deletions mqt/ddsim/job.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import functools
import logging
from concurrent import futures
Expand All @@ -8,12 +10,12 @@


def requires_submit(func):
"""
Decorator to ensure that a submit has been performed before
"""Decorator to ensure that a submit has been performed before
calling the method.
Args:
func (callable): test function to be decorated.
Returns:
callable: the decorated function.
"""
Expand All @@ -37,7 +39,7 @@ class DDSIMJob(JobV1):

_executor = futures.ThreadPoolExecutor(max_workers=1)

def __init__(self, backend, job_id, fn, qobj_experiment, **args):
def __init__(self, backend, job_id, fn, qobj_experiment, **args) -> None:
super().__init__(backend, job_id)
self._fn = fn
self.qobj_experiment = qobj_experiment
Expand All @@ -63,10 +65,11 @@ def result(self, timeout=None):
# pylint: disable=arguments-differ
"""Get job result. The behavior is the same as the underlying
concurrent Future objects,
https://docs.python.org/3/library/concurrent.futures.html#future-objects
https://docs.python.org/3/library/concurrent.futures.html#future-objects.
Args:
timeout (float): number of seconds to wait for results.
Returns:
qiskit.Result: Result object
Raises:
Expand All @@ -81,7 +84,7 @@ def cancel(self):

@requires_submit
def status(self) -> JobStatus:
"""Gets the status of the job by querying the Python's future
"""Gets the status of the job by querying the Python's future.
Returns:
JobStatus: The current JobStatus
Expand Down
9 changes: 5 additions & 4 deletions mqt/ddsim/pathqasmsimulator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Backend for DDSIM Task-Based Simulator."""
from __future__ import annotations

import logging
import pathlib
import time
import uuid
import warnings
from typing import List, Union

from qiskit import QiskitError, QuantumCircuit
from qiskit.compiler import assemble
Expand Down Expand Up @@ -109,7 +109,7 @@ def get_simulation_path(


class PathQasmSimulatorBackend(BackendV1):
"""Python interface to MQT DDSIM Simulation Path Framework"""
"""Python interface to MQT DDSIM Simulation Path Framework."""

SHOW_STATE_VECTOR = False

Expand All @@ -131,7 +131,7 @@ def _default_options(cls) -> Options:
cotengra_dump_path=True,
)

def __init__(self, configuration=None, provider=None):
def __init__(self, configuration=None, provider=None) -> None:
conf = {
"backend_name": "path_sim_qasm_simulator",
"backend_version": __version__,
Expand Down Expand Up @@ -203,7 +203,7 @@ def __init__(self, configuration=None, provider=None):
}
super().__init__(configuration=configuration or BackendConfiguration.from_dict(conf), provider=provider)

def run(self, quantum_circuits: Union[QuantumCircuit, List[QuantumCircuit]], **options):
def run(self, quantum_circuits: QuantumCircuit | list[QuantumCircuit], **options):
if isinstance(quantum_circuits, (QasmQobj, PulseQobj)):
msg = "QasmQobj and PulseQobj are not supported."
raise QiskitError(msg)
Expand Down Expand Up @@ -310,6 +310,7 @@ def _validate(self, _quantum_circuit):

def status(self):
"""Return backend status.
Returns:
BackendStatus: the status of the backend.
"""
Expand Down
5 changes: 3 additions & 2 deletions mqt/ddsim/pathstatevectorsimulator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Backend for DDSIM."""
from __future__ import annotations

import logging

Expand All @@ -11,11 +12,11 @@


class PathStatevectorSimulatorBackend(PathQasmSimulatorBackend):
"""Python interface to MQT DDSIM Simulation Path Framework"""
"""Python interface to MQT DDSIM Simulation Path Framework."""

SHOW_STATE_VECTOR = True

def __init__(self, configuration=None, provider=None):
def __init__(self, configuration=None, provider=None) -> None:
conf = {
"backend_name": "path_sim_statevector_simulator",
"backend_version": __version__,
Expand Down
6 changes: 4 additions & 2 deletions mqt/ddsim/provider.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from qiskit.providers import ProviderV1
from qiskit.providers.providerutils import filter_backends

Expand All @@ -13,7 +15,7 @@
class DDSIMProvider(ProviderV1):
_BACKENDS = None

def __init__(self):
def __init__(self) -> None:
if DDSIMProvider._BACKENDS is None:
DDSIMProvider._BACKENDS = [
("qasm_simulator", QasmSimulatorBackend, None, None),
Expand Down Expand Up @@ -43,5 +45,5 @@ def backends(self, name=None, filters=None, **kwargs):
backends.append(backend_cls(**opts))
return filter_backends(backends, filters=filters, **kwargs)

def __str__(self):
def __str__(self) -> str:
return "DDSIMProvider"
11 changes: 6 additions & 5 deletions mqt/ddsim/qasmsimulator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Backend for DDSIM."""
from __future__ import annotations

import logging
import time
import uuid
import warnings
from typing import Dict, List, Union

from qiskit import QiskitError, QuantumCircuit
from qiskit.compiler import assemble
Expand All @@ -20,7 +20,7 @@


class QasmSimulatorBackend(BackendV1):
"""Python interface to MQT DDSIM"""
"""Python interface to MQT DDSIM."""

SHOW_STATE_VECTOR = False

Expand All @@ -35,7 +35,7 @@ def _default_options(cls) -> Options:
approximation_strategy="fidelity",
)

def __init__(self, configuration=None, provider=None):
def __init__(self, configuration=None, provider=None) -> None:
conf = {
"backend_name": "qasm_simulator",
"backend_version": __version__,
Expand Down Expand Up @@ -106,7 +106,7 @@ def __init__(self, configuration=None, provider=None):
}
super().__init__(configuration=configuration or BackendConfiguration.from_dict(conf), provider=provider)

def run(self, quantum_circuits: Union[QuantumCircuit, List[QuantumCircuit]], **options) -> DDSIMJob:
def run(self, quantum_circuits: QuantumCircuit | list[QuantumCircuit], **options) -> DDSIMJob:
if isinstance(quantum_circuits, (QasmQobj, PulseQobj)):
msg = "QasmQobj and PulseQobj are not supported."
raise QiskitError(msg)
Expand Down Expand Up @@ -147,7 +147,7 @@ def _run_job(self, job_id, qobj_instance: Qobj, **options) -> Result:
}
return Result.from_dict(result)

def run_experiment(self, qobj_experiment: QasmQobjExperiment, **options) -> Dict:
def run_experiment(self, qobj_experiment: QasmQobjExperiment, **options) -> dict:
start_time = time.time()
approximation_step_fidelity = options.get("approximation_step_fidelity", 1.0)
approximation_steps = options.get("approximation_steps", 1)
Expand Down Expand Up @@ -184,6 +184,7 @@ def _validate(self, _quantum_circuit):

def status(self) -> BackendStatus:
"""Return backend status.
Returns:
BackendStatus: the status of the backend.
"""
Expand Down
5 changes: 3 additions & 2 deletions mqt/ddsim/statevectorsimulator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Backend for DDSIM."""
from __future__ import annotations

import logging

Expand All @@ -11,11 +12,11 @@


class StatevectorSimulatorBackend(QasmSimulatorBackend):
"""Python interface to MQT DDSIM"""
"""Python interface to MQT DDSIM."""

SHOW_STATE_VECTOR = True

def __init__(self, configuration=None, provider=None):
def __init__(self, configuration=None, provider=None) -> None:
conf = {
"backend_name": "statevector_simulator",
"backend_version": __version__,
Expand Down
8 changes: 4 additions & 4 deletions mqt/ddsim/unitarysimulator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Backend for DDSIM Unitary Simulator."""
from __future__ import annotations

import logging
import time
import uuid
import warnings
from math import log2, sqrt
from typing import List, Union

import numpy as np
from qiskit import QiskitError, QuantumCircuit
Expand All @@ -26,7 +26,7 @@
class UnitarySimulatorBackend(BackendV1):
"""Decision diagram-based unitary simulator."""

def __init__(self, configuration=None, provider=None, **fields):
def __init__(self, configuration=None, provider=None, **fields) -> None:
conf = {
"backend_name": "unitary_simulator",
"backend_version": __version__,
Expand Down Expand Up @@ -102,7 +102,7 @@ def __init__(self, configuration=None, provider=None, **fields):
def _default_options(cls):
return Options(shots=1, mode="recursive", parameter_binds=None)

def run(self, quantum_circuits: Union[QuantumCircuit, List[QuantumCircuit]], **options):
def run(self, quantum_circuits: QuantumCircuit | list[QuantumCircuit], **options):
if isinstance(quantum_circuits, (QasmQobj, PulseQobj)):
msg = "QasmQobj and PulseQobj are not supported."
raise QiskitError(msg)
Expand Down Expand Up @@ -185,7 +185,7 @@ def _validate(self, qobj):
"""Semantic validations of the qobj which cannot be done via schemas.
Some of these may later move to backend schemas.
1. No shots
2. No measurements in the middle
2. No measurements in the middle.
"""
n_qubits = qobj.config.n_qubits
max_qubits = self.configuration().n_qubits
Expand Down
Loading

0 comments on commit c6f95af

Please sign in to comment.