Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ruff config #315

Merged
merged 12 commits into from
Apr 10, 2024
24 changes: 16 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ unsafe-fixes = true

[tool.ruff.lint]
extend-select = [
"A", # flake8-builtins
# "ANN", # flake8-annotations
"A", # flake8-builtins
"ANN", # flake8-annotations
"ARG", # flake8-unused-arguments
"ASYNC", # flake8-async
"B", "B904", # flake8-bugbear
Expand All @@ -144,8 +144,9 @@ extend-select = [
"I", # isort
"ICN", # flake8-import-conventions
"ISC", # flake8-implicit-str-concat
# "N", # flake8-naming
# "NPY", # numpy
"LOG", # flake8-logging-format
"N", # flake8-naming
"NPY", # numpy
"PERF", # perflint
"PGH", # pygrep-hooks
"PIE", # flake8-pie
Expand All @@ -157,28 +158,35 @@ extend-select = [
"RET", # flake8-return
"RSE", # flake8-raise
"RUF", # Ruff-specific
# "S", # flake8-bandit
"SLF", # flake8-self
"SLOT", # flake8-slots
"SIM", # flake8-simplify
# "T20", # flake8-print
"TCH", # flake8-type-checking
"TID", # flake8-tidy-imports
"TRY", # tryceratops
"UP", # pyupgrade
"YTT", # flake8-2020
]
extend-ignore = [
"PLR", # Design related pylint codes
"ISC001", # Added because of ruff-format warning
"ANN101", # Missing type annotation for self in method
"ANN102", # Missing type annotation for cls in classmethod
"ISC001", # Conflicts with formatter
"E501", # Line too long (Black is enough)
"PLR", # Design related pylint codes
"S101", # Use of assert detected
]
flake8-unused-arguments.ignore-variadic-names = true
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"*.pyi" = ["D"] # pydocstyle
"*.ipynb" = [
"D", # pydocstyle
"E402", # Allow imports to appear anywhere in Jupyter notebooks
"I002", # Allow missing `from __future__ import annotations` import
]

[tool.ruff.pydocstyle]
[tool.ruff.lint.pydocstyle]
convention = "google"
9 changes: 4 additions & 5 deletions src/mqt/bench/benchmark_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ def get_benchmark(
Returns:
Quantum Circuit Object representing the benchmark with the selected options, either as Qiskit::QuantumCircuit or Pytket::Circuit object (depending on the chosen compiler---while the algorithm level is always provided using Qiskit)
"""

if "gate_set_name" in kwargs:
msg = "gate_set_name is deprecated and will be removed in a future release. Use provider_name instead."
warn(msg, DeprecationWarning, stacklevel=2)
Expand Down Expand Up @@ -466,18 +465,18 @@ def timeout_watcher(
timeout: int,
args: list[Any] | int | tuple[int, str] | str,
) -> bool | QuantumCircuit | Circuit:
class TimeoutException(Exception): # Custom exception class
class TimeoutExceptionError(Exception): # Custom exception class
pass

def timeout_handler(_signum: Any, _frame: Any) -> None: # Custom signal handler
raise TimeoutException
def timeout_handler(_signum: int, _frame: Any) -> None: # noqa: ANN401
raise TimeoutExceptionError

# Change the behavior of SIGALRM
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(timeout)
try:
res = func(*args) if isinstance(args, tuple | list) else func(args)
except TimeoutException:
except TimeoutExceptionError:
print(
"Calculation/Generation exceeded timeout limit for ",
func.__name__,
Expand Down
4 changes: 1 addition & 3 deletions src/mqt/bench/benchmarks/ae.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
def create_circuit(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit implementing Quantum Amplitude Estimation.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""

ae = AmplitudeEstimation(
num_eval_qubits=num_qubits - 1, # -1 because of the to be estimated qubit
)
Expand Down Expand Up @@ -47,7 +46,6 @@ def power(self, power: float, _matrix_power: bool = True) -> QuantumCircuit:

def get_estimation_problem() -> EstimationProblem:
"""Returns a estimation problem instance for a fixed p value."""

p = 0.2

"""A circuit representing the Bernoulli A operator."""
Expand Down
9 changes: 4 additions & 5 deletions src/mqt/bench/benchmarks/dj.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
oracle_qc = QuantumCircuit(n + 1)

if case == "balanced":
np.random.seed(10)
rng = np.random.default_rng(10)
b_str = ""
for _ in range(n):
b = np.random.randint(0, 2)
b = rng.integers(0, 2)
b_str = b_str + str(b)

for qubit in range(len(b_str)):
Expand All @@ -29,7 +29,7 @@
oracle_qc.x(qubit)

if case == "constant":
output = np.random.randint(2)
output = rng.integers(2)
Fixed Show fixed Hide fixed
if output == 1:
oracle_qc.x(n)

Expand Down Expand Up @@ -62,11 +62,10 @@
def create_circuit(n: int, balanced: bool = True) -> QuantumCircuit:
"""Returns a quantum circuit implementing the Deutsch-Josza algorithm.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
balanced -- True for a balanced and False for a constant oracle
"""

oracle_mode = "balanced" if balanced else "constant"
n = n - 1 # because of ancilla qubit
oracle_gate = dj_oracle(oracle_mode, n)
Expand Down
3 changes: 1 addition & 2 deletions src/mqt/bench/benchmarks/ghz.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
def create_circuit(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit implementing the GHZ state.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""

q = QuantumRegister(num_qubits, "q")
qc = QuantumCircuit(q, name="ghz")
qc.h(q[-1])
Expand Down
3 changes: 1 addition & 2 deletions src/mqt/bench/benchmarks/graphstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
def create_circuit(num_qubits: int, degree: int = 2) -> QuantumCircuit:
"""Returns a quantum circuit implementing a graph state.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
degree -- number of edges per node
"""

q = QuantumRegister(num_qubits, "q")
qc = QuantumCircuit(q, name="graphstate")

Expand Down
3 changes: 1 addition & 2 deletions src/mqt/bench/benchmarks/grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
def create_circuit(num_qubits: int, ancillary_mode: str = "noancilla") -> QuantumCircuit:
"""Returns a quantum circuit implementing Grover's algorithm.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
ancillary_mode -- defining the decomposition scheme
"""

num_qubits = num_qubits - 1 # -1 because of the flag qubit
q = QuantumRegister(num_qubits, "q")
flag = AncillaRegister(1, "flag")
Expand Down
3 changes: 1 addition & 2 deletions src/mqt/bench/benchmarks/qaoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ def create_circuit(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit implementing the Quantum Approximation Optimization Algorithm for a specific max-cut
example.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""

qp = get_examplary_max_cut_qp(num_qubits)
assert isinstance(qp, QuadraticProgram)

Expand Down
3 changes: 1 addition & 2 deletions src/mqt/bench/benchmarks/qft.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
def create_circuit(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit implementing the Quantum Fourier Transform algorithm.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""

q = QuantumRegister(num_qubits, "q")
c = ClassicalRegister(num_qubits, "c")
qc = QuantumCircuit(q, c, name="qft")
Expand Down
3 changes: 1 addition & 2 deletions src/mqt/bench/benchmarks/qftentangled.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
def create_circuit(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit implementing the Quantum Fourier Transform algorithm using entangled qubits.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""

q = QuantumRegister(num_qubits, "q")
qc = QuantumCircuit(q)
qc.h(q[-1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
def create_circuit(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit of QAOA applied to a specific portfolio optimization task.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""

# set number of assets (= number of qubits)
num_assets = num_qubits

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
def create_circuit(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit of VQE applied to a specific portfolio optimization task.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""

# set number of assets (= number of qubits)
num_assets = num_qubits

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create_circuit(num_uncertainty_qubits: int = 5) -> QuantumCircuit:
"""Returns a quantum circuit of Iterative Amplitude Estimation applied to a problem instance of
pricing call options.

Keyword arguments:
Keyword Arguments:
num_uncertainty_qubits -- number of qubits to measure uncertainty
"""
# parameters for considered random distribution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def create_circuit(num_uncertainty_qubits: int = 5) -> QuantumCircuit:
"""Returns a quantum circuit of Iterative Amplitude Estimation applied to a problem instance of pricing put options.

Keyword arguments:
Keyword Arguments:
num_uncertainty_qubits -- number of qubits to measure uncertainty
"""
# parameters for considered random distribution
Expand Down
6 changes: 4 additions & 2 deletions src/mqt/bench/benchmarks/qiskit_application_ml/qnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
def create_circuit(num_qubits: int) -> QuantumCircuit:
"""Returns a quantum circuit implementing a Quantum Neural Network (QNN) with a ZZ FeatureMap and a RealAmplitudes ansatz.

Keyword arguments:
Keyword Arguments:
num_qubits -- number of qubits of the returned quantum circuit
"""
feature_map = ZZFeatureMap(feature_dimension=num_qubits)
ansatz = RealAmplitudes(num_qubits=num_qubits, reps=1)

qc = QuantumCircuit(num_qubits)
feature_map = feature_map.assign_parameters([1 for _ in range(feature_map.num_parameters)])
ansatz = ansatz.assign_parameters(np.random.rand(ansatz.num_parameters))

rng = np.random.default_rng(10)
ansatz = ansatz.assign_parameters(rng.random(ansatz.num_parameters) * 2 * np.pi)
qc.compose(feature_map, inplace=True)
qc.compose(ansatz, inplace=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def create_circuit(choice: str) -> QuantumCircuit:
"""Returns a quantum circuit implementing Ground State Estimation.

Keyword arguments:
Keyword Arguments:
molecule -- Molecule for which the ground state shall be estimated.
"""
molecule = get_molecule(choice)
Expand Down
Loading
Loading