Skip to content

Commit 34e9679

Browse files
authored
Stim codegen with annotation (#570)
This PR address Status #522
1 parent c7892d1 commit 34e9679

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

src/bloqade/stim/emit/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
from . import impls as impls
12
from .stim_str import FuncEmit as FuncEmit, EmitStimMain as EmitStimMain

src/bloqade/stim/emit/impls.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from kirin.emit import EmitStrFrame
2+
from kirin.interp import MethodTable, impl
3+
from kirin.dialects.debug import Info, dialect
4+
5+
from bloqade.stim.emit.stim_str import EmitStimMain
6+
7+
8+
@dialect.register(key="emit.stim")
9+
class EmitStimDebugMethods(MethodTable):
10+
11+
@impl(Info)
12+
def info(self, emit: EmitStimMain, frame: EmitStrFrame, stmt: Info):
13+
14+
msg: str = frame.get(stmt.msg)
15+
emit.writeln(frame, f"# {msg}")
16+
17+
return ()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from kirin.dialects import debug
2+
3+
from bloqade import stim
4+
5+
from .base import codegen
6+
7+
8+
def test_debug():
9+
10+
@stim.main
11+
def test_debug_main():
12+
debug.info("debug message")
13+
14+
test_debug_main.print()
15+
out = codegen(test_debug_main)
16+
assert out.strip() == "# debug message"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
# debug message
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
3+
from kirin import ir
4+
from kirin.dialects import py, debug
5+
6+
from bloqade.squin import kernel
7+
from bloqade.stim.emit import EmitStimMain
8+
from bloqade.stim.passes import SquinToStimPass
9+
10+
11+
# Taken gratuitously from Kai's unit test
12+
def codegen(mt: ir.Method):
13+
# method should not have any arguments!
14+
emit = EmitStimMain()
15+
emit.initialize()
16+
emit.run(mt=mt, args=())
17+
return emit.get_output()
18+
19+
20+
def as_int(value: int):
21+
return py.constant.Constant(value=value)
22+
23+
24+
def as_float(value: float):
25+
return py.constant.Constant(value=value)
26+
27+
28+
def load_reference_program(filename):
29+
path = os.path.join(
30+
os.path.dirname(__file__), "stim_reference_programs", "debug", filename
31+
)
32+
with open(path, "r") as f:
33+
return f.read()
34+
35+
36+
def test_info():
37+
@kernel
38+
def test():
39+
debug.info("debug message")
40+
return
41+
42+
SquinToStimPass(test.dialects)(test)
43+
base_stim_prog = load_reference_program("debug.stim")
44+
assert codegen(test) == base_stim_prog.rstrip()

0 commit comments

Comments
 (0)