Skip to content

Commit

Permalink
infra: onboard to use ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Coull committed Oct 8, 2024
1 parent 132d462 commit a91d1f4
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 135 deletions.
10 changes: 9 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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


26 changes: 0 additions & 26 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@
],
extras_require={
"test": [
"black",
"botocore",
"flake8<=5.0.4",
"isort",
"jsonschema==3.2.0",
"pre-commit",
"pylint",
"pytest",
"pytest-cov",
"pytest-rerunfailures",
"pytest-xdist[psutil]",
"ruff",
"sphinx",
"sphinx-rtd-theme",
"sphinxcontrib-apidoc",
Expand Down
1 change: 1 addition & 0 deletions src/autoqasm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/autoqasm/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions src/autoqasm/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

"""Errors raised in the AutoQASM build process."""


from __future__ import annotations


Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/autoqasm/instructions/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion src/autoqasm/instructions/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 15 additions & 11 deletions src/autoqasm/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/autoqasm/operators/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/autoqasm/operators/typecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


"""Operators for int cast statements."""

from __future__ import annotations

from typing import Any
Expand Down
3 changes: 1 addition & 2 deletions src/autoqasm/program/pragmas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions src/autoqasm/program/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions src/autoqasm/pulse/pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/autoqasm/simulator/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/autoqasm/simulator/native_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions src/autoqasm/simulator/program_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/autoqasm/simulator/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions src/autoqasm/simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/autoqasm/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions test/unit_tests/autoqasm/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions test/unit_tests/autoqasm/test_gate_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/unit_tests/autoqasm/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading

0 comments on commit a91d1f4

Please sign in to comment.