Skip to content

Commit 720d448

Browse files
committed
Adding FromRanges (#7)
* adding FromRanges statement * adding FromRanges statement * adding test * adding missing test
1 parent ab13918 commit 720d448

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

src/bloqade/geometry/dialects/grid/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .concrete import GridInterpreter as GridInterpreter
1818
from .stmts import (
1919
FromPositions as FromPositions,
20+
FromRanges as FromRanges,
2021
Get as Get,
2122
GetSubGrid as GetSubGrid,
2223
GetXBounds as GetXBounds,

src/bloqade/geometry/dialects/grid/concrete.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,40 @@ def from_positions(
2323
),
2424
)
2525

26+
@impl(stmts.FromRanges)
27+
def from_ranges(
28+
self,
29+
interp: Interpreter,
30+
frame: Frame,
31+
stmt: stmts.FromRanges,
32+
):
33+
x_positions = list(
34+
map(
35+
float,
36+
range(
37+
frame.get(stmt.x_start),
38+
frame.get(stmt.x_stop),
39+
frame.get(stmt.x_step),
40+
),
41+
)
42+
)
43+
y_positions = list(
44+
map(
45+
float,
46+
range(
47+
frame.get(stmt.y_start),
48+
frame.get(stmt.y_stop),
49+
frame.get(stmt.y_step),
50+
),
51+
)
52+
)
53+
return (
54+
Grid.from_positions(
55+
x_positions=x_positions,
56+
y_positions=y_positions,
57+
),
58+
)
59+
2660
@impl(stmts.New)
2761
def new(
2862
self,

src/bloqade/geometry/dialects/grid/stmts.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ class FromPositions(ir.Statement):
2121
result: ir.ResultValue = info.result(GridType[NumX, NumY])
2222

2323

24+
@statement(dialect=dialect)
25+
class FromRanges(ir.Statement):
26+
name = "from_ranges"
27+
traits = frozenset({ir.Pure(), lowering.FromPythonCall()})
28+
29+
x_start: ir.SSAValue = info.argument(types.Int)
30+
x_stop: ir.SSAValue = info.argument(types.Int)
31+
x_step: ir.SSAValue = info.argument(types.Int)
32+
y_start: ir.SSAValue = info.argument(types.Int)
33+
y_stop: ir.SSAValue = info.argument(types.Int)
34+
y_step: ir.SSAValue = info.argument(types.Int)
35+
36+
2437
@statement(dialect=dialect)
2538
class New(ir.Statement):
2639
name = "new"

test/grid/test_concrete.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ def test_new(self):
5050
assert len(result) == 1
5151
assert expected_grid_obj.is_equal(result[0])
5252

53+
def test_from_ranges(self):
54+
x_start = 1
55+
x_stop = 5
56+
x_step = 1
57+
y_start = 2
58+
y_stop = 6
59+
y_step = 1
60+
61+
expected_grid_obj = grid.Grid.from_positions(
62+
[1.0, 2.0, 3.0, 4.0], [2.0, 3.0, 4.0, 5.0]
63+
)
64+
65+
result = self.run_stmt(
66+
grid.FromRanges, x_start, x_stop, x_step, y_start, y_stop, y_step
67+
)
68+
69+
assert isinstance(result, tuple)
70+
assert len(result) == 1
71+
assert expected_grid_obj.is_equal(result[0])
72+
5373
@pytest.mark.parametrize(
5474
("stmt_type", "method_name", "args"),
5575
[
@@ -62,6 +82,7 @@ def test_new(self):
6282
(grid.Scale, "scale", (1.0, 2.0)),
6383
(grid.Repeat, "repeat", (1, 2, 0.5, 1.0)),
6484
(grid.GetSubGrid, "get_view", (ilist.IList((0,)), ilist.IList((1,)))),
85+
(grid.Shape, "shape", ()),
6586
],
6687
)
6788
def test_template(self, stmt_type, method_name, args):

0 commit comments

Comments
 (0)