Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions opensquirrel/circuit_builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from collections.abc import Callable
from copy import deepcopy
from functools import partial
from typing import Any
Expand All @@ -12,6 +11,8 @@
from opensquirrel.ir import IR, AsmDeclaration, Bit, BitLike, Instruction, Qubit, QubitLike
from opensquirrel.register_manager import BitRegister, QubitRegister, RegisterManager

_builder_dynamic_attributes = (*default_instruction_set, "asm")


class CircuitBuilder:
"""
Expand Down Expand Up @@ -42,8 +43,14 @@ def __init__(self, qubit_register_size: int, bit_register_size: int = 0) -> None
self.register_manager = RegisterManager(QubitRegister(qubit_register_size), BitRegister(bit_register_size))
self.ir = IR()

def __getattr__(self, attr: Any) -> Callable[..., Self]:
return partial(self._add_statement, attr)
def __dir__(self) -> list[str]:
return super().__dir__() + list(_builder_dynamic_attributes) # type: ignore

def __getattr__(self, attr: str) -> Any:
if attr in _builder_dynamic_attributes:
return partial(self._add_statement, attr)
# Default behaviour
return self.__getattribute__(attr)

def _check_qubit_out_of_bounds_access(self, qubit: QubitLike) -> None:
"""Throw error if qubit index is outside the qubit register range.
Expand Down
3 changes: 2 additions & 1 deletion opensquirrel/passes/router/general_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


class Router(ABC):
def __init__(self, **kwargs: Any) -> None: ...
def __init__(self, **kwargs: Any) -> None:
"""Generic router class"""

@abstractmethod
def route(self, ir: IR) -> IR:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_circuit_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_measure_index_error(self) -> None:

def test_unknown_instruction(self) -> None:
builder = CircuitBuilder(3)
with pytest.raises(ValueError, match="unknown instruction 'unknown'"):
with pytest.raises(AttributeError, match="has no attribute 'unknown'"):
builder.unknown(0)

def test_wrong_number_of_arguments(self) -> None:
Expand Down
Loading