Skip to content

Commit 953b703

Browse files
committed
adding dispatch.
1 parent fadefd4 commit 953b703

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

src/bloqade/ir/location/transform.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from beartype.vale import Is
44
from typing import Annotated
55
from beartype import beartype
6+
from plum import dispatch
67
import numpy as np
78
from bloqade.ir.scalar import cast
89

@@ -46,7 +47,7 @@ def scale(self, scale: ScalarType):
4647

4748
return ListOfLocations(location_list)
4849

49-
@beartype
50+
@dispatch
5051
def _add_position(
5152
self, position: Tuple[ScalarType, ScalarType], filling: Optional[bool] = None
5253
):
@@ -61,8 +62,8 @@ def _add_position(
6162

6263
return ListOfLocations(location_list)
6364

64-
@beartype
65-
def _add_position_list(
65+
@dispatch
66+
def _add_position( # noqa: F811
6667
self,
6768
position: List[Tuple[ScalarType, ScalarType]],
6869
filling: Optional[List[bool]] = None,
@@ -86,23 +87,23 @@ def _add_position_list(
8687

8788
return ListOfLocations(location_list)
8889

89-
@beartype
90-
def _add_numpy_position(
90+
@dispatch
91+
def _add_position( # noqa: F811
9192
self, position: PositionArray, filling: Optional[BoolArray] = None
9293
):
93-
return self._add_position_list(
94+
return self.add_position(
9495
list(map(tuple, position.tolist())),
9596
filling.tolist() if filling is not None else None,
9697
)
9798

9899
def add_position(
99100
self,
100101
position: Union[
102+
PositionArray,
101103
Tuple[ScalarType, ScalarType],
102104
List[Tuple[ScalarType, ScalarType]],
103-
PositionArray,
104105
],
105-
filling: Optional[Union[bool, list[bool], BoolArray]] = None,
106+
filling: Optional[Union[BoolArray, List[bool], bool]] = None,
106107
) -> "ListOfLocations":
107108
"""add a position or list of positions to existing atom arrangement.
108109
@@ -113,25 +114,15 @@ def add_position(
113114
position to add
114115
filling (bool | list[bool]
115116
| numpy.array with shape (n, ) | None, optional):
116-
filling of the added position(s). Defaults to None.
117+
filling of the added position(s). Defaults to None. if None, all
118+
positions are filled.
119+
117120
118121
Returns:
119122
ListOfLocations: new atom arrangement with added positions
120123
121124
"""
122-
if isinstance(position, tuple) and isinstance(filling, (bool, type(None))):
123-
return self._add_position(position, filling)
124-
elif isinstance(position, list) and isinstance(filling, (list, type(None))):
125-
return self._add_position_list(position, filling)
126-
elif isinstance(position, np.ndarray) and isinstance(
127-
filling, (np.ndarray, type(None))
128-
):
129-
return self._add_numpy_position(position, filling)
130-
else:
131-
raise TypeError(
132-
f"cannot interpret arguments, got {type(position)} "
133-
f"for position and {type(filling)} for filling"
134-
)
125+
return self._add_position(position, filling)
135126

136127
@beartype
137128
def apply_defect_count(

0 commit comments

Comments
 (0)