Skip to content

Commit 57a2098

Browse files
authored
Improve docu and switch linter from black to ruff (#257)
1 parent 621be88 commit 57a2098

25 files changed

Lines changed: 319 additions & 116 deletions

.pre-commit-config.yaml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,6 @@ repos:
3535
- id: fix-ligatures
3636
- id: fix-smartquotes
3737

38-
# Run code formatting with Black
39-
- repo: https://github.com/psf/black-pre-commit-mirror
40-
rev: "23.10.1" # Keep in sync with blacken-docs
41-
hooks:
42-
- id: black-jupyter
43-
44-
# Also run Black on examples in the documentation
45-
- repo: https://github.com/asottile/blacken-docs
46-
rev: "1.16.0"
47-
hooks:
48-
- id: blacken-docs
49-
additional_dependencies:
50-
- black==23.10.1 # keep in sync with black hook
51-
5238
# Clean jupyter notebooks
5339
- repo: https://github.com/srstevenson/nb-clean
5440
rev: "3.1.0"
@@ -74,6 +60,16 @@ repos:
7460
hooks:
7561
- id: ruff
7662
args: ["--fix", "--show-fixes"]
63+
types_or: [python, pyi, jupyter]
64+
- id: ruff-format
65+
types_or: [python, pyi, jupyter]
66+
67+
# Also run Black on examples in the documentation
68+
- repo: https://github.com/adamchainz/blacken-docs
69+
rev: 1.16.0
70+
hooks:
71+
- id: blacken-docs
72+
additional_dependencies: [black==23.*]
7773

7874
- repo: https://github.com/pre-commit/mirrors-mypy
7975
rev: v1.6.1

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ from mqt.bench import get_benchmark
251251
qc = get_benchmark("dj", "alg", 5)
252252
```
253253

254+
Examples can be found in the [notebooks/examples.ipynb](notebooks/examples.ipynb) jupyter notebook.
255+
254256
### Locally hosting the MQT Bench Viewer
255257

256258
Additionally, this python package includes the same webserver used for the hosting of the

notebooks/examples.ipynb

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "a866359da8df0489",
6+
"metadata": {},
7+
"source": [
8+
"# MQT Bench Usage via Python Package"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"id": "ef302dc8f33466e4",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from mqt.bench import CompilerSettings, QiskitSettings, TKETSettings, get_benchmark"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"id": "ee984ededf8e10bc",
24+
"metadata": {},
25+
"source": [
26+
"## Algorithmic Level"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": null,
32+
"id": "8346993b51294feb",
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"qc_algorithmic_level = get_benchmark(benchmark_name=\"dj\", level=\"alg\", circuit_size=5)\n",
37+
"qc_algorithmic_level.draw(output=\"mpl\")"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"id": "3edc74a2bf18d67d",
43+
"metadata": {},
44+
"source": [
45+
"## Target-independent Level"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"id": "9482737dacfc40d",
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"qc_target_independent_level = get_benchmark(benchmark_name=\"dj\", level=\"indep\", circuit_size=5, compiler=\"qiskit\")\n",
56+
"qc_target_independent_level.draw(output=\"mpl\")"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"id": "f3c0ae43336dca9b",
62+
"metadata": {},
63+
"source": [
64+
"## Target-dependent Native Gates Level"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"id": "ca5ce053798f36e5",
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"compiler_settings = CompilerSettings(qiskit=QiskitSettings(optimization_level=2))\n",
75+
"qc_native_gates_level = get_benchmark(\n",
76+
" benchmark_name=\"dj\",\n",
77+
" level=\"nativegates\",\n",
78+
" circuit_size=5,\n",
79+
" compiler=\"qiskit\",\n",
80+
" compiler_settings=compiler_settings,\n",
81+
" gate_set_name=\"ionq\",\n",
82+
")\n",
83+
"qc_native_gates_level.draw(output=\"mpl\")"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"id": "d57921d7884bb4cc",
89+
"metadata": {},
90+
"source": [
91+
"## Target-dependent Mapped Level"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"id": "ddb7b07a146a8fd8",
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"from pytket.extensions.qiskit import tk_to_qiskit\n",
102+
"\n",
103+
"compiler_settings = CompilerSettings(tket=TKETSettings(placement=\"lineplacement\"))\n",
104+
"qc_mapped_level = get_benchmark(\n",
105+
" benchmark_name=\"dj\",\n",
106+
" level=\"mapped\",\n",
107+
" circuit_size=5,\n",
108+
" compiler=\"tket\",\n",
109+
" compiler_settings=compiler_settings,\n",
110+
" gate_set_name=\"ionq\",\n",
111+
" device_name=\"ionq_harmony\",\n",
112+
")\n",
113+
"tk_to_qiskit(qc_mapped_level).draw(output=\"mpl\")"
114+
]
115+
}
116+
],
117+
"metadata": {
118+
"kernelspec": {
119+
"display_name": "Python 3",
120+
"language": "python",
121+
"name": "python3"
122+
},
123+
"language_info": {
124+
"codemirror_mode": {
125+
"name": "ipython",
126+
"version": 2
127+
},
128+
"file_extension": ".py",
129+
"mimetype": "text/x-python",
130+
"name": "python",
131+
"nbconvert_exporter": "python",
132+
"pygments_lexer": "ipython2"
133+
}
134+
},
135+
"nbformat": 4,
136+
"nbformat_minor": 5
137+
}

pyproject.toml

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ show_missing = true
9696
skip_empty = true
9797
precision = 1
9898

99-
[tool.black]
100-
line-length = 120
101-
10299
[tool.mypy]
103100
mypy_path = "$MYPY_CONFIG_FILE_DIR/src"
104101
files = ["src", "tests", "setup.py"]
@@ -119,58 +116,61 @@ module = ["qiskit.*", "qiskit_finance.*", "joblib.*", "networkx.*", "pandas.*"]
119116
ignore_missing_imports = true
120117

121118
[tool.ruff]
122-
select = [
123-
"E", "F", "W", # flake8
119+
line-length = 120
120+
src = ["src"]
121+
unsafe-fixes = true
122+
123+
[tool.ruff.lint]
124+
extend-select = [
124125
"A", # flake8-builtins
125-
"B", "B904", # flake8-bugbear
126-
"I", # isort
126+
# "ANN", # flake8-annotations
127127
"ARG", # flake8-unused-arguments
128+
"ASYNC", # flake8-async
129+
"B", "B904", # flake8-bugbear
128130
"C4", # flake8-comprehensions
131+
# "D", # pydocstyle
129132
"EM", # flake8-errmsg
130133
"EXE", # flake8-executable
134+
"FA", # flake8-future-annotations
135+
"FLY", # flynt
136+
"I", # isort
131137
"ICN", # flake8-import-conventions
132138
"ISC", # flake8-implicit-str-concat
139+
# "N", # flake8-naming
140+
# "NPY", # numpy
141+
"PERF", # perflint
133142
"PGH", # pygrep-hooks
134143
"PIE", # flake8-pie
135144
"PL", # pylint
136145
"PT", # flake8-pytest-style
137146
"PTH", # flake8-use-pathlib
147+
"PYI", # flake8-pyi
138148
"Q", # flake8-quotes
139149
"RET", # flake8-return
150+
"RSE", # flake8-raise
140151
"RUF", # Ruff-specific
152+
"SLF", # flake8-self
153+
"SLOT", # flake8-slots
141154
"SIM", # flake8-simplify
142155
"TCH", # flake8-type-checking
156+
"TID", # flake8-tidy-imports
157+
"TRY", # tryceratops
143158
"UP", # pyupgrade
144159
"YTT", # flake8-2020
145160
]
146-
ignore = [
147-
"PLR2004", # Magic values
148-
"PLR0913", # Too many arguments
149-
"E501", # Line too long (Black is enough)
161+
extend-ignore = [
162+
"PLR", # Design related pylint codes
163+
"ISC001", # Added because of ruff-format warning
150164
]
151-
152-
# Exclude a variety of commonly ignored directories.
153-
exclude = [
154-
"__init__.py",
155-
".bzr",
156-
".direnv",
157-
".eggs",
158-
".git",
159-
".hg",
160-
".mypy_cache",
161-
".nox",
162-
".pants.d",
163-
".ruff_cache",
164-
".svn",
165-
".tox",
166-
".venv",
167-
"__pypackages__",
168-
"_build",
169-
"buck-out",
170-
"build",
171-
"dist",
172-
"node_modules",
173-
"venv",
165+
isort.required-imports = ["from __future__ import annotations"]
166+
167+
[tool.ruff.per-file-ignores]
168+
"*.pyi" = ["D"] # pydocstyle
169+
"*.ipynb" = [
170+
"D", # pydocstyle
171+
"E402", # Allow imports to appear anywhere in Jupyter notebooks
172+
"I002", # Allow missing `from __future__ import annotations` import
174173
]
175174

176-
line-length = 120
175+
[tool.ruff.pydocstyle]
176+
convention = "google"

setup.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/mqt/bench/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from __future__ import annotations
22

3+
from mqt.bench import qiskit_helper, tket_helper, utils
34
from mqt.bench.benchmark_generator import (
4-
generate,
5-
get_benchmark,
65
BenchmarkGenerator,
7-
timeout_watcher,
86
CompilerSettings,
97
QiskitSettings,
108
TKETSettings,
9+
generate,
10+
get_benchmark,
11+
timeout_watcher,
1112
)
12-
from mqt.bench import qiskit_helper, tket_helper, utils
1313

1414
__all__ = [
1515
"generate",

src/mqt/bench/benchmark_generator.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
from typing import TYPE_CHECKING, Any, Callable, Literal, TypedDict, overload
99

1010
from joblib import Parallel, delayed
11-
from mqt.bench import qiskit_helper, tket_helper, utils
1211
from qiskit import QuantumCircuit
1312

13+
from mqt.bench import qiskit_helper, tket_helper, utils
14+
1415
if TYPE_CHECKING: # pragma: no cover
1516
from types import ModuleType
1617

@@ -107,7 +108,11 @@ def define_benchmark_instances(self, benchmark: Benchmark) -> None:
107108
instances = range(benchmark["min_uncertainty"], benchmark["max_uncertainty"])
108109

109110
else:
110-
instances = range(benchmark["min_qubits"], benchmark["max_qubits"], benchmark["stepsize"])
111+
instances = range(
112+
benchmark["min_qubits"],
113+
benchmark["max_qubits"],
114+
benchmark["stepsize"],
115+
)
111116

112117
self.generate_all_benchmarks(lib, instances, file_precheck)
113118

@@ -121,7 +126,7 @@ def generate_all_benchmarks(
121126
self.generate_native_gates_levels(file_precheck, lib, parameter_space)
122127
self.generate_mapped_levels(file_precheck, lib, parameter_space)
123128

124-
def generate_mapped_levels( # noqa: PLR0912
129+
def generate_mapped_levels(
125130
self,
126131
file_precheck: bool,
127132
lib: ModuleType,
@@ -243,7 +248,9 @@ def generate_indep_levels(
243248
break
244249
assert isinstance(qc, QuantumCircuit)
245250
res = timeout_watcher(
246-
function, self.timeout, [qc, qc.num_qubits, file_precheck, False, self.qasm_output_path]
251+
function,
252+
self.timeout,
253+
[qc, qc.num_qubits, file_precheck, False, self.qasm_output_path],
247254
)
248255
if not res:
249256
break
@@ -291,7 +298,7 @@ def get_benchmark(
291298
...
292299

293300

294-
def get_benchmark( # noqa: PLR0911, PLR0912, PLR0915
301+
def get_benchmark(
295302
benchmark_name: str,
296303
level: str | int,
297304
circuit_size: int | None = None,
@@ -442,13 +449,15 @@ def generate(num_jobs: int = -1) -> None:
442449

443450

444451
def timeout_watcher(
445-
func: Callable[..., bool | QuantumCircuit], timeout: int, args: list[Any] | int | tuple[int, str] | str
452+
func: Callable[..., bool | QuantumCircuit],
453+
timeout: int,
454+
args: list[Any] | int | tuple[int, str] | str,
446455
) -> bool | QuantumCircuit | Circuit:
447456
class TimeoutException(Exception): # Custom exception class
448457
pass
449458

450459
def timeout_handler(_signum: Any, _frame: Any) -> None: # Custom signal handler
451-
raise TimeoutException()
460+
raise TimeoutException
452461

453462
# Change the behavior of SIGALRM
454463
signal.signal(signal.SIGALRM, timeout_handler)

0 commit comments

Comments
 (0)