From 823c623d21c1d1a31e779ed588454f8b2db2415e Mon Sep 17 00:00:00 2001 From: Stan van der Linde Date: Fri, 29 Nov 2024 15:28:02 +0100 Subject: [PATCH] Style --- docs_files/make_docs.py | 6 ++---- qgym/envs/routing/routing.py | 2 +- qgym/envs/scheduling/machine_properties.py | 2 +- qgym/envs/scheduling/scheduling.py | 2 +- qgym/envs/scheduling/scheduling_rewarders.py | 2 +- qgym/utils/input_parsing.py | 2 +- qgym/wrappers/initial_mapping.py | 7 ++----- qgym/wrappers/routing.py | 3 ++- .../initial_mapping/test_initial_mapping_env.py | 1 - tests/generators/test_circuit.py | 3 --- tests/generators/test_graph.py | 2 -- tests/generators/test_interaction.py | 2 -- tests/generators/test_qiskit_circuit.py | 1 - tests/utils/test_input_parsing.py | 1 - tests/utils/test_qiskit_utils.py | 5 ----- tests/wrappers/test_initial_mapping.py | 2 -- tests/wrappers/test_routing.py | 13 +++++-------- 17 files changed, 16 insertions(+), 40 deletions(-) diff --git a/docs_files/make_docs.py b/docs_files/make_docs.py index fce7285b..7e3978e2 100644 --- a/docs_files/make_docs.py +++ b/docs_files/make_docs.py @@ -3,7 +3,7 @@ import sphinx.cmd.build as sphinx_build import sphinx.ext.apidoc as sphinx_apidoc -sphinx_apiddoc_args =[ +sphinx_apiddoc_args = [ "-o=docs_build", "--templatedir=docs_files/templates", "--force", @@ -14,11 +14,9 @@ "qgym", ] -sphinx_build_args = ["docs_build","docs"] +sphinx_build_args = ["docs_build", "docs"] sphinx_apidoc.main(sphinx_apiddoc_args) sphinx_build.main(sphinx_build_args) shutil.rmtree("docs_build") - - diff --git a/qgym/envs/routing/routing.py b/qgym/envs/routing/routing.py index 0b63ce19..86ad6131 100644 --- a/qgym/envs/routing/routing.py +++ b/qgym/envs/routing/routing.py @@ -75,7 +75,7 @@ A valid action is an integer in the domain [0, n_connections]. The values 0 to n_connections-1 represent an added SWAP gate. The value of n_connections indicates that the agents wants to surpass the current gate and move to the next gate. - + Illegal actions will not be executed. An action is considered illegal when the agent want to surpass a gate that cannot be executed with the current mapping. diff --git a/qgym/envs/scheduling/machine_properties.py b/qgym/envs/scheduling/machine_properties.py index 9ca02ff6..7ede02ad 100644 --- a/qgym/envs/scheduling/machine_properties.py +++ b/qgym/envs/scheduling/machine_properties.py @@ -206,7 +206,7 @@ def not_in_same_cycle(self) -> dict[str, set[str]] | dict[int, set[int]]: @staticmethod def _check_machine_properties_mapping( - machine_properties: Mapping[str, Any] + machine_properties: Mapping[str, Any], ) -> dict[str, Any]: """Check if the given machine properties ``Mapping`` is a valid descriptions of the machine properties and returns a ``Dict`` to easily initialize a diff --git a/qgym/envs/scheduling/scheduling.py b/qgym/envs/scheduling/scheduling.py index 54fde327..ad7b6c19 100644 --- a/qgym/envs/scheduling/scheduling.py +++ b/qgym/envs/scheduling/scheduling.py @@ -306,7 +306,7 @@ def get_circuit(self, mode: str = "human") -> list[Gate]: @staticmethod def _parse_machine_properties( - machine_properties: Mapping[str, Any] | str | MachineProperties + machine_properties: Mapping[str, Any] | str | MachineProperties, ) -> MachineProperties: """ Parse the machine_properties given by the user and return a diff --git a/qgym/envs/scheduling/scheduling_rewarders.py b/qgym/envs/scheduling/scheduling_rewarders.py index 5f9ad138..4c620d84 100644 --- a/qgym/envs/scheduling/scheduling_rewarders.py +++ b/qgym/envs/scheduling/scheduling_rewarders.py @@ -15,7 +15,7 @@ schedule_gate_bonus: = 3, ) - After initialization, the rewarders can be given to the + After initialization, the rewarders can be given to the :class:`~qgym.envs.Scheduling` environment. .. note:: diff --git a/qgym/utils/input_parsing.py b/qgym/utils/input_parsing.py index 2286e8a1..09002090 100644 --- a/qgym/utils/input_parsing.py +++ b/qgym/utils/input_parsing.py @@ -65,7 +65,7 @@ def parse_visualiser( def parse_connection_graph( - graph: nx.Graph | ArrayLike | list[int] | tuple[int, ...] + graph: nx.Graph | ArrayLike | list[int] | tuple[int, ...], ) -> nx.Graph: """Parse the user input (given in ``__init__``) to create a connection graph. diff --git a/qgym/wrappers/initial_mapping.py b/qgym/wrappers/initial_mapping.py index 31f570fb..b21735c8 100644 --- a/qgym/wrappers/initial_mapping.py +++ b/qgym/wrappers/initial_mapping.py @@ -8,12 +8,9 @@ from numpy.typing import NDArray from qiskit import QuantumCircuit from qiskit.dagcircuit import DAGCircuit -from qiskit.transpiler import Layout, AnalysisPass +from qiskit.transpiler import AnalysisPass, Layout -from qgym.utils.qiskit_utils import ( - get_interaction_graph, - parse_circuit, -) +from qgym.utils.qiskit_utils import get_interaction_graph, parse_circuit if TYPE_CHECKING: from stable_baselines3.common.base_class import BaseAlgorithm diff --git a/qgym/wrappers/routing.py b/qgym/wrappers/routing.py index 4fb53358..54f0d108 100644 --- a/qgym/wrappers/routing.py +++ b/qgym/wrappers/routing.py @@ -91,7 +91,8 @@ def compute_routing(self, circuit: QuantumCircuit | DAGCircuit) -> DAGCircuit: raise ValueError(msg) return insert_swaps_in_circuit( - circuit, self.env._state.swap_gates_inserted # type: ignore[attr-defined] + circuit, + self.env._state.swap_gates_inserted, # type: ignore[attr-defined] ) diff --git a/tests/envs/initial_mapping/test_initial_mapping_env.py b/tests/envs/initial_mapping/test_initial_mapping_env.py index 10d870f9..6d105410 100644 --- a/tests/envs/initial_mapping/test_initial_mapping_env.py +++ b/tests/envs/initial_mapping/test_initial_mapping_env.py @@ -30,7 +30,6 @@ def small_env(small_graph: nx.Graph) -> InitialMapping: class TestEnvironment: - def test_validity(self, small_env: InitialMapping) -> None: # todo: maybe switch this to the gymnasium env checker check_env(small_env, warn=True) diff --git a/tests/generators/test_circuit.py b/tests/generators/test_circuit.py index 2ac41c76..1d916a41 100644 --- a/tests/generators/test_circuit.py +++ b/tests/generators/test_circuit.py @@ -22,7 +22,6 @@ def test_abc() -> None: class TestNullCircuitGenerator: - @pytest.fixture(name="generator") def null_circuit_generator_fixture(self) -> NullCircuitGenerator: return NullCircuitGenerator() @@ -54,7 +53,6 @@ def test_iter(self, generator: NullCircuitGenerator) -> None: class TestBasicCircuitGenerator: - @pytest.fixture(name="simple_generator") def basic_circuit_generator_fixture(self) -> BasicCircuitGenerator: generator = BasicCircuitGenerator(seed=42) @@ -142,7 +140,6 @@ def test_seed(self) -> None: class TestWorkshopCircuitGenerator: - @pytest.fixture(name="simple_generator") def workshop_circuit_generator_fixture(self) -> WorkshopCircuitGenerator: generator = WorkshopCircuitGenerator(seed=42) diff --git a/tests/generators/test_graph.py b/tests/generators/test_graph.py index b8866f1e..1c177f5f 100644 --- a/tests/generators/test_graph.py +++ b/tests/generators/test_graph.py @@ -18,7 +18,6 @@ def test_abc() -> None: class TestNullGraphGenerator: - @pytest.fixture(name="generator") def null_graph_generator_fixture(self) -> NullGraphGenerator: generator = NullGraphGenerator() @@ -51,7 +50,6 @@ def test_iter(self, generator: NullGraphGenerator) -> None: class TestBasicGraphGenerator: - @pytest.fixture(name="simple_generator") def basic_graph_generator_fixture(self) -> BasicGraphGenerator: generator = BasicGraphGenerator() diff --git a/tests/generators/test_interaction.py b/tests/generators/test_interaction.py index f001fa5e..7e9c1d3e 100644 --- a/tests/generators/test_interaction.py +++ b/tests/generators/test_interaction.py @@ -19,7 +19,6 @@ def test_abc() -> None: class TestNullInteractionGenerator: - @pytest.fixture(name="generator") def null_interaction_generator_fixture(self) -> NullInteractionGenerator: return NullInteractionGenerator() @@ -53,7 +52,6 @@ def test_iter(self, generator: NullInteractionGenerator) -> None: class TestBasicGraphGenerator: - @pytest.fixture(name="simple_generator") def null_interaction_generator_fixture(self) -> BasicInteractionGenerator: generator = BasicInteractionGenerator() diff --git a/tests/generators/test_qiskit_circuit.py b/tests/generators/test_qiskit_circuit.py index 281b0f59..370428f2 100644 --- a/tests/generators/test_qiskit_circuit.py +++ b/tests/generators/test_qiskit_circuit.py @@ -6,7 +6,6 @@ class TestMaxCutQAOAGenerator: - @pytest.mark.parametrize("n_nodes", range(1, 6)) def test_iterating(self, n_nodes: int) -> None: generator = MaxCutQAOAGenerator(n_nodes, 0.5, seed=42) diff --git a/tests/utils/test_input_parsing.py b/tests/utils/test_input_parsing.py index aa721b88..d5617b85 100644 --- a/tests/utils/test_input_parsing.py +++ b/tests/utils/test_input_parsing.py @@ -69,7 +69,6 @@ def test_negative(self) -> None: class TestParseSeed: - def test_none(self) -> None: rng = parse_seed(None) assert isinstance(rng, Generator) diff --git a/tests/utils/test_qiskit_utils.py b/tests/utils/test_qiskit_utils.py index 225b1b5f..d8a2fef8 100644 --- a/tests/utils/test_qiskit_utils.py +++ b/tests/utils/test_qiskit_utils.py @@ -20,7 +20,6 @@ class TestParseCircuit: - def test_quantum_circuit(self) -> None: circuit = QuantumCircuit(1) dag_circuit = parse_circuit(circuit) @@ -34,7 +33,6 @@ def test_dag_circuit(self) -> None: class TestGetInteractionGraph: - @pytest.mark.parametrize("circuit_size", range(6)) def test_empty_circuit(self, circuit_size: int) -> None: circuit = QuantumCircuit(circuit_size) @@ -64,7 +62,6 @@ def test_multi_qubit_value_error(self) -> None: class TestGetInteractionCircuit: - @pytest.mark.parametrize("circuit_size", range(1, 6)) def test_empty_circuit(self, circuit_size: int) -> None: circuit = QuantumCircuit(circuit_size) @@ -111,14 +108,12 @@ def test_multi_qubit_value_error(self) -> None: ids=["no registers", "multiple registers", "wrong name"], ) def test_non_physical_circuit_error(self, circuit: QuantumCircuit) -> None: - expected_msg = "Interaction circuits are defined for physical circuits only" with pytest.raises(ValueError, match=expected_msg): get_interaction_circuit(circuit) class TestAddSwapsToCircuit: - def test_multiple_swaps(self) -> None: """ Input: Expected Output: diff --git a/tests/wrappers/test_initial_mapping.py b/tests/wrappers/test_initial_mapping.py index 8f55ee02..97959a44 100644 --- a/tests/wrappers/test_initial_mapping.py +++ b/tests/wrappers/test_initial_mapping.py @@ -13,7 +13,6 @@ class TestQiskitMapperWrapper: - @pytest.fixture(name="circuit") def circuit_fixture(self) -> QuantumCircuit: """Create a circuit with a cycle of cx gates.""" @@ -59,7 +58,6 @@ def test_sabre_layout( class TestAgentMapperWrapper: - @pytest.fixture(name="circuit") def circuit_fixture(self) -> QuantumCircuit: """Create a circuit with a line of cx gates.""" diff --git a/tests/wrappers/test_routing.py b/tests/wrappers/test_routing.py index f3707f79..8b1ef492 100644 --- a/tests/wrappers/test_routing.py +++ b/tests/wrappers/test_routing.py @@ -4,18 +4,16 @@ import numpy as np import pytest from qiskit import QuantumCircuit -from qiskit.transpiler import CouplingMap -from qiskit.transpiler.passes import SabreSwap, BasicSwap -from stable_baselines3 import PPO from qiskit.converters import dag_to_circuit -from qgym.envs.routing import BasicRewarder, Routing -from qgym.wrappers.routing import QiskitRoutingWrapper, AgentRoutingWrapper +from qiskit.transpiler import CouplingMap, Layout +from qiskit.transpiler.passes import BasicSwap, SabreSwap +from stable_baselines3 import PPO -from qiskit.transpiler import Layout +from qgym.envs.routing import BasicRewarder, Routing +from qgym.wrappers.routing import AgentRoutingWrapper, QiskitRoutingWrapper class TestQiskitRoutingWrapper: - @pytest.fixture(name="circuit") def circuit_fixture(self) -> QuantumCircuit: """Create a circuit with a cycle of cx gates.""" @@ -58,7 +56,6 @@ def test_sabre_swap( class TestAgentMapperWrapper: - @pytest.fixture(name="circuit") def circuit_fixture(self) -> QuantumCircuit: """Create a circuit with a cycle of cx gates."""