diff --git a/docs/fundamentals/embedding.ipynb b/docs/fundamentals/embedding.ipynb index f861d2d51..563decc0d 100644 --- a/docs/fundamentals/embedding.ipynb +++ b/docs/fundamentals/embedding.ipynb @@ -346,18 +346,7 @@ "id": "27", "metadata": {}, "outputs": [], - "source": [ - "n=6\n", - "graph_2 = DataGraph.circle(n)\n", - "for i,e in enumerate(graph_2.edge_weights):\n", - " graph_2.edge_weights[e]= np.random.rand()\n", - "\n", - "embedded_graph_2 = embedder.embed(graph_2)\n", - "\n", - "fig, axs = plt.subplots(1, 2)\n", - "graph_2.draw(ax=axs[0])\n", - "embedded_graph_2.draw(ax=axs[1])" - ] + "source": "n=6\ngraph_2 = DataGraph.circle(n)\nfor e in graph_2.edge_weights:\n graph_2.edge_weights[e]= np.random.rand()\n\nembedded_graph_2 = embedder.embed(graph_2)\n\nfig, axs = plt.subplots(1, 2)\ngraph_2.draw(ax=axs[0])\nembedded_graph_2.draw(ax=axs[1])" }, { "cell_type": "markdown", diff --git a/pyproject.toml b/pyproject.toml index cc025fb0c..b6b959b8e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -107,10 +107,25 @@ exclude_also = [ line-length = 100 [tool.ruff.lint] -select = ["E", "F", "I", "Q"] +select = [ + "E", # pycodestyle + "F", # Pyflakes + "Q", # flake8-quotes + "UP", # pyupgrade + "B", # flake8-bugbear + "SIM", # flake8-simplify + "I", # isort +] +ignore = [ + "B905", # `zip()` without an explicit `strict=` parameter + "B008", # `np.random.default_rng` in argument defaults + "B018", # Found useless expression + "B024", # abstract base class with no abstract methods or properties + "B904", # raise exceptions with `raise ... from err` or `raise ... from None` + "SIM102", # Use a single `if` statement instead of nested `if` statements + "SIM211", # Use `not ...` instead of `False if ... else True` +] isort.required-imports = ["from __future__ import annotations"] -mccabe.max-complexity = 15 -flake8-quotes.docstring-quotes = "double" [tool.ruff.lint.per-file-ignores] "__init__.py" = ["F401", "F403"] diff --git a/qoolqit/devices/device.py b/qoolqit/devices/device.py index 0afe879b7..8529921e0 100644 --- a/qoolqit/devices/device.py +++ b/qoolqit/devices/device.py @@ -1,8 +1,8 @@ from __future__ import annotations import math +from collections.abc import Callable from dataclasses import replace -from typing import Callable, Optional import pulser from pulser.backend.remote import RemoteConnection @@ -62,7 +62,7 @@ class Device: def __init__( self, pulser_device: BaseDevice, - default_converter: Optional[UnitConverter] = None, + default_converter: UnitConverter | None = None, ) -> None: if not isinstance(pulser_device, BaseDevice): diff --git a/qoolqit/embedding/algorithms/blade/_distances_constraints_calculator.py b/qoolqit/embedding/algorithms/blade/_distances_constraints_calculator.py index 5bfda39d2..aff7e9268 100644 --- a/qoolqit/embedding/algorithms/blade/_distances_constraints_calculator.py +++ b/qoolqit/embedding/algorithms/blade/_distances_constraints_calculator.py @@ -1,7 +1,7 @@ from __future__ import annotations import dataclasses -from typing import Any, Optional +from typing import Any import numpy as np @@ -94,7 +94,7 @@ def __post_init__(self) -> None: def compute_scaling_min_max( self, positions: np.ndarray, step_cursor: float, draw_differences: bool = False - ) -> tuple[float, Optional[float], Optional[float]]: + ) -> tuple[float, float | None, float | None]: """Computes the scaling and the new minimum and maximum distances. Computes the best scaling factor on the positions and the new diff --git a/qoolqit/embedding/algorithms/blade/_force.py b/qoolqit/embedding/algorithms/blade/_force.py index 7b6cbe778..c57e3914c 100644 --- a/qoolqit/embedding/algorithms/blade/_force.py +++ b/qoolqit/embedding/algorithms/blade/_force.py @@ -92,7 +92,7 @@ def _fit_to_dims(self, a: np.ndarray) -> np.ndarray: def regulated( self, regulation_cursor: float = 0, - ) -> "Force": + ) -> Force: min_temperature = np.min(self.maximum_temperatures) if min_temperature != np.inf: diff --git a/qoolqit/embedding/algorithms/blade/_helpers.py b/qoolqit/embedding/algorithms/blade/_helpers.py index d77ad10ee..cd07324a7 100644 --- a/qoolqit/embedding/algorithms/blade/_helpers.py +++ b/qoolqit/embedding/algorithms/blade/_helpers.py @@ -9,7 +9,7 @@ def _format_return(input: Value, ret: np.ndarray, *, format: FormatType) -> Value: - if isinstance(input, float) or isinstance(input, int): + if isinstance(input, (float, int)): return float(ret) if input.ndim == 1: diff --git a/qoolqit/embedding/algorithms/blade/_qubo_mapper.py b/qoolqit/embedding/algorithms/blade/_qubo_mapper.py index 53b531e39..f319468e8 100644 --- a/qoolqit/embedding/algorithms/blade/_qubo_mapper.py +++ b/qoolqit/embedding/algorithms/blade/_qubo_mapper.py @@ -1,11 +1,11 @@ from __future__ import annotations -from typing import Mapping, Sequence, Union +from collections.abc import Mapping, Sequence import networkx as nx import numpy as np -NodeId = Union[str, int] +NodeId = str | int class Qubo: diff --git a/qoolqit/embedding/algorithms/blade/blade.py b/qoolqit/embedding/algorithms/blade/blade.py index 086e5b753..156c01d92 100644 --- a/qoolqit/embedding/algorithms/blade/blade.py +++ b/qoolqit/embedding/algorithms/blade/blade.py @@ -1,8 +1,9 @@ from __future__ import annotations import logging +from collections.abc import Callable from dataclasses import InitVar, dataclass -from typing import Callable, Final +from typing import Final import networkx as nx import numpy as np diff --git a/qoolqit/embedding/algorithms/blade/drawing.py b/qoolqit/embedding/algorithms/blade/drawing.py index bd2620df3..9dd43e766 100644 --- a/qoolqit/embedding/algorithms/blade/drawing.py +++ b/qoolqit/embedding/algorithms/blade/drawing.py @@ -23,7 +23,7 @@ def eformat(f: Any) -> str: if 1 <= abs(f) < 1000: return f"{np.round(f, decimals=1)}" - elif 0.01 <= abs(f): + elif abs(f) >= 0.01: return f"{np.round(f, decimals=2)}" if f == 0: return "0" diff --git a/qoolqit/embedding/base_embedder.py b/qoolqit/embedding/base_embedder.py index 976378046..6fc810ee5 100644 --- a/qoolqit/embedding/base_embedder.py +++ b/qoolqit/embedding/base_embedder.py @@ -2,8 +2,9 @@ import inspect from abc import ABC, abstractmethod +from collections.abc import Callable from dataclasses import asdict, dataclass -from typing import Callable, Generic, TypeVar +from typing import Generic, TypeVar @dataclass diff --git a/qoolqit/execution/sequence_compiler.py b/qoolqit/execution/sequence_compiler.py index c8c3d4e38..fa7abbf9f 100644 --- a/qoolqit/execution/sequence_compiler.py +++ b/qoolqit/execution/sequence_compiler.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Callable +from collections.abc import Callable from pulser.sequence.sequence import Sequence as PulserSequence diff --git a/qoolqit/graphs/base_graph.py b/qoolqit/graphs/base_graph.py index f5999ab2c..9d698602d 100644 --- a/qoolqit/graphs/base_graph.py +++ b/qoolqit/graphs/base_graph.py @@ -96,7 +96,7 @@ def from_nx(cls, g: nx.Graph) -> BaseGraph: num_edges = len(g.edges) # validate node attributes - for name, data in g.nodes.data(): + for _name, data in g.nodes.data(): unexpected_keys = set(data) - {"weight", "pos"} if unexpected_keys: raise ValueError(f"{unexpected_keys} not allowed in node attributes.") @@ -126,7 +126,7 @@ def from_nx(cls, g: nx.Graph) -> BaseGraph: ) # validate edge attributes - for u, v, data in g.edges.data(): + for _u, _v, data in g.edges.data(): unexpected_keys = set(data) - {"weight"} if unexpected_keys: raise ValueError(f"{unexpected_keys} not allowed in edge attributes.") diff --git a/qoolqit/graphs/utils.py b/qoolqit/graphs/utils.py index 4f8d94a9c..5ab3234bc 100644 --- a/qoolqit/graphs/utils.py +++ b/qoolqit/graphs/utils.py @@ -1,9 +1,9 @@ from __future__ import annotations import random +from collections.abc import Iterable from itertools import product from math import dist, hypot -from typing import Iterable import numpy as np diff --git a/qoolqit/program.py b/qoolqit/program.py index 4946dd613..e2d042066 100644 --- a/qoolqit/program.py +++ b/qoolqit/program.py @@ -36,7 +36,7 @@ def __init__( raise TypeError("`drive` must be of type Drive.") if drive.dmm is not None: dmm_weights = drive.dmm.weights - for qid in dmm_weights.keys(): + for qid in dmm_weights: if qid not in register.qubits: raise ValueError( "In this QuantumProgram, the drive's detuning modulator map (DMM) " diff --git a/tests/conftest.py b/tests/conftest.py index ac01e9b87..545bb72e4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ from __future__ import annotations +from collections.abc import Callable, Generator from random import uniform -from typing import Callable, Generator import matplotlib diff --git a/tests/test_compilation/test_default.py b/tests/test_compilation/test_default.py index 61424c34d..21a675466 100644 --- a/tests/test_compilation/test_default.py +++ b/tests/test_compilation/test_default.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Callable +from collections.abc import Callable import numpy as np import pytest diff --git a/tests/test_compilation/test_max_energy.py b/tests/test_compilation/test_max_energy.py index 232f65652..9df600f3c 100644 --- a/tests/test_compilation/test_max_energy.py +++ b/tests/test_compilation/test_max_energy.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Callable +from collections.abc import Callable import numpy as np import pytest diff --git a/tests/test_devices.py b/tests/test_devices.py index 96aa39c99..2c953e095 100644 --- a/tests/test_devices.py +++ b/tests/test_devices.py @@ -74,9 +74,9 @@ def test_device_init_and_units(device: Device) -> None: device.reset_converter() TIME_NEW, ENERGY_NEW, DISTANCE_NEW = device.converter.factors - assert TIME_ORIG == pytest.approx(TIME_NEW) - assert ENERGY_ORIG == pytest.approx(ENERGY_NEW) - assert DISTANCE_ORIG == pytest.approx(DISTANCE_NEW) + assert pytest.approx(TIME_NEW) == TIME_ORIG + assert pytest.approx(ENERGY_NEW) == ENERGY_ORIG + assert pytest.approx(DISTANCE_NEW) == DISTANCE_ORIG def test_default_device_specs() -> None: diff --git a/tests/test_program.py b/tests/test_program.py index 46000088f..827018166 100644 --- a/tests/test_program.py +++ b/tests/test_program.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Callable +from collections.abc import Callable import pytest from pulser.sequence import Sequence as PulserSequence