From a91d1f4b29a816f5aa8d60cfd341ab25a6895523 Mon Sep 17 00:00:00 2001 From: Coull Date: Tue, 8 Oct 2024 14:55:57 -0700 Subject: [PATCH 1/3] infra: onboard to use ruff --- pyproject.toml | 10 ++- setup.cfg | 26 ------- setup.py | 4 +- src/autoqasm/__init__.py | 1 + src/autoqasm/api.py | 4 +- src/autoqasm/errors.py | 5 +- src/autoqasm/instructions/gates.py | 2 +- src/autoqasm/instructions/instructions.py | 2 +- src/autoqasm/operators/__init__.py | 26 ++++--- src/autoqasm/operators/arithmetic.py | 2 +- src/autoqasm/operators/typecast.py | 1 + src/autoqasm/program/pragmas.py | 3 +- src/autoqasm/program/program.py | 12 ++-- src/autoqasm/pulse/pulse.py | 8 +-- src/autoqasm/simulator/conversion.py | 1 + src/autoqasm/simulator/native_interpreter.py | 6 +- src/autoqasm/simulator/program_context.py | 6 +- src/autoqasm/simulator/simulation.py | 4 +- src/autoqasm/simulator/simulator.py | 7 +- src/autoqasm/types/types.py | 4 +- test/unit_tests/autoqasm/test_devices.py | 3 +- .../autoqasm/test_gate_decorator.py | 3 +- test/unit_tests/autoqasm/test_operators.py | 4 +- tox.ini | 68 +++++-------------- 24 files changed, 77 insertions(+), 135 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index aa4949aa..7b2a1448 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,2 +1,10 @@ -[tool.black] +[tool.ruff] +target-version = "py39" line-length = 100 +lint.isort = { known-first-party = [ + "braket", +] } +lint.extend-select = ["I"] +lint.preview = true + + diff --git a/setup.cfg b/setup.cfg index d75c4f03..2c3421db 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,29 +13,3 @@ filterwarnings= # Ref: https://github.com/pytest-dev/pytest-cov/issues/557 ignore:The --rsyncdir command line argument and rsyncdirs config variable are deprecated.:DeprecationWarning -[isort] -line_length = 100 -multi_line_output = 3 -include_trailing_comma = true -profile = black - -[flake8] -ignore = - # not pep8, black adds whitespace before ':' - E203, - # not pep8, https://www.python.org/dev/peps/pep-0008/#pet-peeves - E231, - # not pep8, black adds line break before binary operator - W503, - # Google Python style is not RST until after processed by Napoleon - # See https://github.com/peterjc/flake8-rst-docstrings/issues/17 - RST201,RST203,RST301, -max_line_length = 100 -max-complexity = 10 -exclude = - __pycache__ - .tox - .git - bin - build - venv diff --git a/setup.py b/setup.py index 3fa424d1..8b201a3f 100644 --- a/setup.py +++ b/setup.py @@ -39,10 +39,7 @@ ], extras_require={ "test": [ - "black", "botocore", - "flake8<=5.0.4", - "isort", "jsonschema==3.2.0", "pre-commit", "pylint", @@ -50,6 +47,7 @@ "pytest-cov", "pytest-rerunfailures", "pytest-xdist[psutil]", + "ruff", "sphinx", "sphinx-rtd-theme", "sphinxcontrib-apidoc", diff --git a/src/autoqasm/__init__.py b/src/autoqasm/__init__.py index 113f36cf..41ddd7bd 100644 --- a/src/autoqasm/__init__.py +++ b/src/autoqasm/__init__.py @@ -43,6 +43,7 @@ def my_program(): result[0] = measure __qubits__[0]; result[1] = measure __qubits__[1]; """ + from . import errors, instructions, operators # noqa: F401 from .api import gate, gate_calibration, main, subroutine # noqa: F401 from .hybrid_job import hybrid_job # noqa: F401 diff --git a/src/autoqasm/api.py b/src/autoqasm/api.py index 9de784b5..69186e3f 100644 --- a/src/autoqasm/api.py +++ b/src/autoqasm/api.py @@ -610,8 +610,8 @@ def _get_gate_args(f: Callable) -> aq_program.GateArgs: if param.annotation == aq_instructions.QubitIdentifierType: gate_args.append_qubit(param.name) - elif param.annotation == float or any( - type_ == float for type_ in get_args(param.annotation) + elif isinstance(param.annotation, float) or any( + isinstance(type_, float) for type_ in get_args(param.annotation) ): gate_args.append_angle(param.name) else: diff --git a/src/autoqasm/errors.py b/src/autoqasm/errors.py index 2b259f8e..d052fa4d 100644 --- a/src/autoqasm/errors.py +++ b/src/autoqasm/errors.py @@ -13,7 +13,6 @@ """Errors raised in the AutoQASM build process.""" - from __future__ import annotations @@ -96,9 +95,7 @@ def __init__(self, true_type: type | None, false_type: type | None): self.message = """\ `if` clause resolves to {}, but `else` clause resolves to {}. \ Both the `if` and `else` clauses of an inline conditional expression \ -must resolve to the same type.""".format( - if_type, else_type - ) +must resolve to the same type.""".format(if_type, else_type) def __str__(self): return self.message diff --git a/src/autoqasm/instructions/gates.py b/src/autoqasm/instructions/gates.py index d858ac86..24c8aa79 100644 --- a/src/autoqasm/instructions/gates.py +++ b/src/autoqasm/instructions/gates.py @@ -17,10 +17,10 @@ from typing import Union import oqpy -from braket.circuits.free_parameter_expression import FreeParameterExpression from autoqasm.instructions.instructions import _qubit_instruction from autoqasm.types import QubitIdentifierType +from braket.circuits.free_parameter_expression import FreeParameterExpression GateParameterType = Union[float, FreeParameterExpression, oqpy._ClassicalVar] diff --git a/src/autoqasm/instructions/instructions.py b/src/autoqasm/instructions/instructions.py index 5a0024b8..3db70e4c 100644 --- a/src/autoqasm/instructions/instructions.py +++ b/src/autoqasm/instructions/instructions.py @@ -20,12 +20,12 @@ from typing import Any import oqpy -from braket.circuits.basis_state import BasisState, BasisStateInput from autoqasm import program as aq_program from autoqasm import types as aq_types from autoqasm.instructions.qubits import _qubit from autoqasm.types import QubitIdentifierType +from braket.circuits.basis_state import BasisState, BasisStateInput def _qubit_instruction( diff --git a/src/autoqasm/operators/__init__.py b/src/autoqasm/operators/__init__.py index 4486cf4b..171aa8ba 100644 --- a/src/autoqasm/operators/__init__.py +++ b/src/autoqasm/operators/__init__.py @@ -26,18 +26,22 @@ from .comparisons import gt_, gteq_, lt_, lteq_ # noqa: F401 from .conditional_expressions import if_exp # noqa: F401 from .control_flow import for_stmt, if_stmt, while_stmt # noqa: F401 -from .data_structures import ListPopOpts # noqa: F401 -from .data_structures import ListStackOpts # noqa: F401 -from .data_structures import list_append # noqa: F401 -from .data_structures import list_pop # noqa: F401 -from .data_structures import list_stack # noqa: F401 -from .data_structures import new_list # noqa: F401 +from .data_structures import ( + ListPopOpts, # noqa: F401 + ListStackOpts, # noqa: F401 + list_append, # noqa: F401 + list_pop, # noqa: F401 + list_stack, # noqa: F401 + new_list, # noqa: F401 +) from .exceptions import assert_stmt # noqa: F401 -from .logical import and_ # noqa: F401 -from .logical import eq # noqa: F401 -from .logical import not_ # noqa: F401 -from .logical import not_eq # noqa: F401 -from .logical import or_ # noqa: F401 +from .logical import ( + and_, # noqa: F401 + eq, # noqa: F401 + not_, # noqa: F401 + not_eq, # noqa: F401 + or_, # noqa: F401 +) from .return_statements import return_output_from_main # noqa: F401 from .slices import GetItemOpts, get_item, set_item # noqa: F401 from .typecast import int_ # noqa: F401 diff --git a/src/autoqasm/operators/arithmetic.py b/src/autoqasm/operators/arithmetic.py index 1decb7d2..baa9762a 100644 --- a/src/autoqasm/operators/arithmetic.py +++ b/src/autoqasm/operators/arithmetic.py @@ -11,7 +11,7 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. -"""Operators for arithmetic operators: // """ +"""Operators for arithmetic operators: //""" from __future__ import annotations diff --git a/src/autoqasm/operators/typecast.py b/src/autoqasm/operators/typecast.py index ef808c26..e2f59bf7 100644 --- a/src/autoqasm/operators/typecast.py +++ b/src/autoqasm/operators/typecast.py @@ -13,6 +13,7 @@ """Operators for int cast statements.""" + from __future__ import annotations from typing import Any diff --git a/src/autoqasm/program/pragmas.py b/src/autoqasm/program/pragmas.py index 49b31d27..4172ab9f 100644 --- a/src/autoqasm/program/pragmas.py +++ b/src/autoqasm/program/pragmas.py @@ -34,9 +34,8 @@ def pragma_example() -> None: from enum import Enum from typing import Iterable -from braket.device_schema import DeviceActionType - from autoqasm import errors, program +from braket.device_schema import DeviceActionType class PragmaType(str, Enum): diff --git a/src/autoqasm/program/program.py b/src/autoqasm/program/program.py index d833bfae..6eacaed2 100644 --- a/src/autoqasm/program/program.py +++ b/src/autoqasm/program/program.py @@ -25,12 +25,6 @@ import oqpy.base import pygments -from braket.aws.aws_device import AwsDevice -from braket.circuits.free_parameter_expression import FreeParameterExpression -from braket.circuits.serialization import IRType, SerializableProgram -from braket.device_schema import DeviceActionType -from braket.devices.device import Device -from braket.pulse.ast.qasm_parser import ast_to_qasm from openpulse import ast from openqasm_pygments import OpenQASM3Lexer from pygments.formatters.terminal import TerminalFormatter @@ -44,6 +38,12 @@ SerializationProperties, ) from autoqasm.types import QubitIdentifierType as Qubit +from braket.aws.aws_device import AwsDevice +from braket.circuits.free_parameter_expression import FreeParameterExpression +from braket.circuits.serialization import IRType, SerializableProgram +from braket.device_schema import DeviceActionType +from braket.devices.device import Device +from braket.pulse.ast.qasm_parser import ast_to_qasm # Create the thread-local object for the program conversion context. _local = threading.local() diff --git a/src/autoqasm/pulse/pulse.py b/src/autoqasm/pulse/pulse.py index 790eb9da..1a43cf72 100644 --- a/src/autoqasm/pulse/pulse.py +++ b/src/autoqasm/pulse/pulse.py @@ -17,6 +17,10 @@ from __future__ import annotations import oqpy + +from autoqasm import program as aq_program +from autoqasm.instructions.qubits import _get_physical_qubit_indices +from autoqasm.types import BitVar, QubitIdentifierType, is_qubit_identifier_type from braket.parametric import FreeParameterExpression from braket.parametric.free_parameter import FreeParameter from braket.pulse import PulseSequence @@ -25,10 +29,6 @@ from braket.pulse.waveforms import Waveform from braket.registers.qubit_set import QubitSet -from autoqasm import program as aq_program -from autoqasm.instructions.qubits import _get_physical_qubit_indices -from autoqasm.types import BitVar, QubitIdentifierType, is_qubit_identifier_type - def _pulse_instruction(name: str, frame: Frame, *args) -> None: """Define a pulse instruction. diff --git a/src/autoqasm/simulator/conversion.py b/src/autoqasm/simulator/conversion.py index 31981beb..a162e242 100644 --- a/src/autoqasm/simulator/conversion.py +++ b/src/autoqasm/simulator/conversion.py @@ -15,6 +15,7 @@ from typing import Any, Union import numpy as np + from braket.default_simulator.openqasm._helpers.casting import convert_bool_array_to_string from braket.default_simulator.openqasm.parser.openqasm_ast import ( ArrayLiteral, diff --git a/src/autoqasm/simulator/native_interpreter.py b/src/autoqasm/simulator/native_interpreter.py index 7da43073..2ab49cca 100644 --- a/src/autoqasm/simulator/native_interpreter.py +++ b/src/autoqasm/simulator/native_interpreter.py @@ -16,6 +16,9 @@ from logging import Logger from typing import Any, List, Optional, Union +from openqasm3.ast import IntegerLiteral + +from autoqasm.simulator.program_context import McmProgramContext from braket.default_simulator.openqasm._helpers.casting import cast_to, wrap_value_into_literal from braket.default_simulator.openqasm.interpreter import Interpreter from braket.default_simulator.openqasm.parser.openqasm_ast import ( @@ -34,9 +37,6 @@ ) from braket.default_simulator.openqasm.parser.openqasm_parser import parse from braket.default_simulator.simulation import Simulation -from openqasm3.ast import IntegerLiteral - -from autoqasm.simulator.program_context import McmProgramContext class NativeInterpreter(Interpreter): diff --git a/src/autoqasm/simulator/program_context.py b/src/autoqasm/simulator/program_context.py index 8b1f1dac..f48bf2cb 100644 --- a/src/autoqasm/simulator/program_context.py +++ b/src/autoqasm/simulator/program_context.py @@ -18,6 +18,9 @@ from typing import Optional, Union import numpy as np +from sympy import Integer + +from autoqasm.simulator.conversion import convert_to_output from braket.default_simulator.openqasm._helpers.arrays import ( convert_discrete_set_to_list, convert_range_def_to_slice, @@ -34,9 +37,6 @@ ) from braket.default_simulator.openqasm.program_context import ProgramContext, Table from braket.default_simulator.operation import GateOperation -from sympy import Integer - -from autoqasm.simulator.conversion import convert_to_output class QubitTable(Table): diff --git a/src/autoqasm/simulator/simulation.py b/src/autoqasm/simulator/simulation.py index 18b0fc40..ef53e721 100644 --- a/src/autoqasm/simulator/simulation.py +++ b/src/autoqasm/simulator/simulation.py @@ -12,12 +12,12 @@ # language governing permissions and limitations under the License. import numpy as np + +from autoqasm.simulator.linalg_utils import measurement_collapse_sv, measurement_sample from braket.default_simulator import StateVectorSimulation from braket.default_simulator.gate_operations import PauliX from braket.default_simulator.linalg_utils import marginal_probability -from autoqasm.simulator.linalg_utils import measurement_collapse_sv, measurement_sample - class Simulation(StateVectorSimulation): def add_qubits(self, num_qubits: int) -> None: diff --git a/src/autoqasm/simulator/simulator.py b/src/autoqasm/simulator/simulator.py index 60a9e985..470e01e8 100644 --- a/src/autoqasm/simulator/simulator.py +++ b/src/autoqasm/simulator/simulator.py @@ -11,15 +11,14 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. +from autoqasm.simulator.native_interpreter import NativeInterpreter +from autoqasm.simulator.program_context import McmProgramContext +from autoqasm.simulator.simulation import Simulation from braket.default_simulator import StateVectorSimulator from braket.ir.openqasm import Program as OpenQASMProgram from braket.task_result import AdditionalMetadata, TaskMetadata from braket.tasks import GateModelQuantumTaskResult -from autoqasm.simulator.native_interpreter import NativeInterpreter -from autoqasm.simulator.program_context import McmProgramContext -from autoqasm.simulator.simulation import Simulation - class McmSimulator(StateVectorSimulator): DEVICE_ID = "autoqasm" diff --git a/src/autoqasm/types/types.py b/src/autoqasm/types/types.py index 11bcf2c3..2b7ad0cc 100644 --- a/src/autoqasm/types/types.py +++ b/src/autoqasm/types/types.py @@ -21,11 +21,11 @@ import numpy as np import oqpy import oqpy.base -from braket.circuits import FreeParameterExpression -from braket.registers import Qubit from openpulse import ast from autoqasm import errors, program +from braket.circuits import FreeParameterExpression +from braket.registers import Qubit def is_qasm_type(val: Any) -> bool: diff --git a/test/unit_tests/autoqasm/test_devices.py b/test/unit_tests/autoqasm/test_devices.py index 674110c7..a4bb495a 100644 --- a/test/unit_tests/autoqasm/test_devices.py +++ b/test/unit_tests/autoqasm/test_devices.py @@ -11,8 +11,7 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. -"""AutoQASM tests exercising device-specific targeting functionality. -""" +"""AutoQASM tests exercising device-specific targeting functionality.""" import json from unittest.mock import Mock, patch diff --git a/test/unit_tests/autoqasm/test_gate_decorator.py b/test/unit_tests/autoqasm/test_gate_decorator.py index b6b934f9..1df310fd 100644 --- a/test/unit_tests/autoqasm/test_gate_decorator.py +++ b/test/unit_tests/autoqasm/test_gate_decorator.py @@ -11,8 +11,7 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. -"""AutoQASM tests exercising the @aq.gate decorator and related functionality. -""" +"""AutoQASM tests exercising the @aq.gate decorator and related functionality.""" import numpy as np import pytest diff --git a/test/unit_tests/autoqasm/test_operators.py b/test/unit_tests/autoqasm/test_operators.py index fc53cad1..1a78a80b 100644 --- a/test/unit_tests/autoqasm/test_operators.py +++ b/test/unit_tests/autoqasm/test_operators.py @@ -850,9 +850,7 @@ def test_control_flow(): expected = """OPENQASM 3.0; qubit[1] __qubits__; -{} __qubits__[0];""".format( - "h" if value else "x" - ) +{} __qubits__[0];""".format("h" if value else "x") assert test_control_flow.build().to_ir() == expected diff --git a/tox.ini b/tox.ini index 7aaa3d47..648f3a51 100644 --- a/tox.ini +++ b/tox.ini @@ -41,71 +41,35 @@ extras = test [testenv:linters] basepython = python3 -skip_install = true +# Remove this to check what versions are installed for the env. This stops running pip freeze. +list_dependencies_command = echo deps = - {[testenv:isort]deps} - {[testenv:black]deps} - {[testenv:flake8]deps} + {[testenv:ruff-format]deps} + {[testenv:ruff-check]deps} commands = - # isort MUST come before black as it will revert any changes made by black - {[testenv:isort]commands} - {[testenv:black]commands} - {[testenv:flake8]commands} + {[testenv:ruff-format]commands} + {[testenv:ruff-check]commands} # Read only linter env [testenv:linters_check] basepython = python3 -skip_install = true -deps = - {[testenv:isort_check]deps} - {[testenv:black_check]deps} - {[testenv:flake8]deps} -commands = - {[testenv:isort_check]commands} - {[testenv:black_check]commands} - {[testenv:flake8]commands} - -[testenv:flake8] -basepython = python3 -skip_install = true -deps = - flake8 - git+https://github.com/amazon-braket/amazon-braket-build-tools.git -commands = - flake8 --extend-exclude src {posargs} - flake8 --enable-extensions=BCS src {posargs} - -[testenv:isort] -basepython = python3 -skip_install = true -deps = - isort +extras = linters commands = - isort . {posargs} + {[testenv:ruff-check]commands} -[testenv:isort_check] +[testenv:ruff-check] basepython = python3 -skip_install = true -deps = - isort +extras = linters +deps = ruff commands = - isort . -c {posargs} + ruff check src {posargs} -[testenv:black] +[testenv:ruff-format] basepython = python3 -skip_install = true -deps = - black -commands = - black ./ {posargs} - -[testenv:black_check] -basepython = python3 -skip_install = true -deps = - black +extras = linters +deps = ruff commands = - black --check ./ {posargs} + ruff format . {posargs} [testenv:docs] basepython = python3 From 1882072aceb45d8f7521ff9b2995c282228d9ad7 Mon Sep 17 00:00:00 2001 From: Coull Date: Wed, 9 Oct 2024 08:57:27 -0700 Subject: [PATCH 2/3] add ruff to the env dependencies --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index 648f3a51..21dfb51a 100644 --- a/tox.ini +++ b/tox.ini @@ -53,6 +53,7 @@ commands = # Read only linter env [testenv:linters_check] basepython = python3 +deps = ruff extras = linters commands = {[testenv:ruff-check]commands} From 2ae0bf3eaa8592ff3d01bdc5bf151905824042af Mon Sep 17 00:00:00 2001 From: Coull Date: Thu, 31 Oct 2024 11:50:35 -0700 Subject: [PATCH 3/3] correct typing check --- src/autoqasm/api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/autoqasm/api.py b/src/autoqasm/api.py index 69186e3f..386c2799 100644 --- a/src/autoqasm/api.py +++ b/src/autoqasm/api.py @@ -610,8 +610,9 @@ def _get_gate_args(f: Callable) -> aq_program.GateArgs: if param.annotation == aq_instructions.QubitIdentifierType: gate_args.append_qubit(param.name) - elif isinstance(param.annotation, float) or any( - isinstance(type_, float) for type_ in get_args(param.annotation) + elif param.annotation == float or any( # noqa: E721 + type_ == float # noqa: E721 + for type_ in get_args(param.annotation) ): gate_args.append_angle(param.name) else: