From 507d49b2a9fca723e6c13cbdb476185920e88eea Mon Sep 17 00:00:00 2001 From: kaihsin Date: Mon, 27 Oct 2025 10:08:16 -0400 Subject: [PATCH 1/3] add annotation codegen for stim --- src/bloqade/stim/emit/__init__.py | 1 + src/bloqade/stim/emit/impls.py | 17 +++++++++++++++++ test/stim/dialects/stim/emit/test_stim_debug.py | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 src/bloqade/stim/emit/impls.py create mode 100644 test/stim/dialects/stim/emit/test_stim_debug.py diff --git a/src/bloqade/stim/emit/__init__.py b/src/bloqade/stim/emit/__init__.py index b7d8c569b..90dfcdf6c 100644 --- a/src/bloqade/stim/emit/__init__.py +++ b/src/bloqade/stim/emit/__init__.py @@ -1 +1,2 @@ +from . import impls as impls from .stim_str import FuncEmit as FuncEmit, EmitStimMain as EmitStimMain diff --git a/src/bloqade/stim/emit/impls.py b/src/bloqade/stim/emit/impls.py new file mode 100644 index 000000000..743ee4c73 --- /dev/null +++ b/src/bloqade/stim/emit/impls.py @@ -0,0 +1,17 @@ +from kirin.emit import EmitStrFrame +from kirin.interp import MethodTable, impl +from kirin.dialects.debug import Info, dialect + +from bloqade.stim.emit.stim_str import EmitStimMain + + +@dialect.register(key="emit.stim") +class EmitStimDebugMethods(MethodTable): + + @impl(Info) + def info(self, emit: EmitStimMain, frame: EmitStrFrame, stmt: Info): + + msg: str = frame.get(stmt.msg) + emit.writeln(frame, f"#{msg}") + + return () diff --git a/test/stim/dialects/stim/emit/test_stim_debug.py b/test/stim/dialects/stim/emit/test_stim_debug.py new file mode 100644 index 000000000..a3c9aafa1 --- /dev/null +++ b/test/stim/dialects/stim/emit/test_stim_debug.py @@ -0,0 +1,16 @@ +from kirin.dialects import debug + +from bloqade import stim + +from .base import codegen + + +def test_debug(): + + @stim.main + def test_debug_main(): + debug.info("debug message") + + test_debug_main.print() + out = codegen(test_debug_main) + assert out.strip() == "#debug message" From 5126e6d9fb2fba7af314522019692b03cb5fd2f0 Mon Sep 17 00:00:00 2001 From: kaihsin Date: Mon, 27 Oct 2025 10:30:01 -0400 Subject: [PATCH 2/3] add tests --- src/bloqade/stim/emit/impls.py | 2 +- .../dialects/stim/emit/test_stim_debug.py | 2 +- test/stim/passes/test_squin_debug_to_stim.py | 44 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 test/stim/passes/test_squin_debug_to_stim.py diff --git a/src/bloqade/stim/emit/impls.py b/src/bloqade/stim/emit/impls.py index 743ee4c73..83a0c8671 100644 --- a/src/bloqade/stim/emit/impls.py +++ b/src/bloqade/stim/emit/impls.py @@ -12,6 +12,6 @@ class EmitStimDebugMethods(MethodTable): def info(self, emit: EmitStimMain, frame: EmitStrFrame, stmt: Info): msg: str = frame.get(stmt.msg) - emit.writeln(frame, f"#{msg}") + emit.writeln(frame, f"# {msg}") return () diff --git a/test/stim/dialects/stim/emit/test_stim_debug.py b/test/stim/dialects/stim/emit/test_stim_debug.py index a3c9aafa1..b056e5402 100644 --- a/test/stim/dialects/stim/emit/test_stim_debug.py +++ b/test/stim/dialects/stim/emit/test_stim_debug.py @@ -13,4 +13,4 @@ def test_debug_main(): test_debug_main.print() out = codegen(test_debug_main) - assert out.strip() == "#debug message" + assert out.strip() == "# debug message" diff --git a/test/stim/passes/test_squin_debug_to_stim.py b/test/stim/passes/test_squin_debug_to_stim.py new file mode 100644 index 000000000..7319bc47d --- /dev/null +++ b/test/stim/passes/test_squin_debug_to_stim.py @@ -0,0 +1,44 @@ +import os + +from kirin import ir +from kirin.dialects import py, debug + +from bloqade.squin import kernel +from bloqade.stim.emit import EmitStimMain +from bloqade.stim.passes import SquinToStimPass + + +# Taken gratuitously from Kai's unit test +def codegen(mt: ir.Method): + # method should not have any arguments! + emit = EmitStimMain() + emit.initialize() + emit.run(mt=mt, args=()) + return emit.get_output() + + +def as_int(value: int): + return py.constant.Constant(value=value) + + +def as_float(value: float): + return py.constant.Constant(value=value) + + +def load_reference_program(filename): + path = os.path.join( + os.path.dirname(__file__), "stim_reference_programs", "debug", filename + ) + with open(path, "r") as f: + return f.read() + + +def test_info(): + @kernel + def test(): + debug.info("debug message") + return + + SquinToStimPass(test.dialects)(test) + base_stim_prog = load_reference_program("debug.stim") + assert codegen(test) == base_stim_prog.rstrip() From ac09434d6c2f7013b15fa63d020e4885c0ce8889 Mon Sep 17 00:00:00 2001 From: kaihsin Date: Mon, 27 Oct 2025 11:40:43 -0400 Subject: [PATCH 3/3] add stim file --- test/stim/passes/stim_reference_programs/debug/debug.stim | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 test/stim/passes/stim_reference_programs/debug/debug.stim diff --git a/test/stim/passes/stim_reference_programs/debug/debug.stim b/test/stim/passes/stim_reference_programs/debug/debug.stim new file mode 100644 index 000000000..7479e928f --- /dev/null +++ b/test/stim/passes/stim_reference_programs/debug/debug.stim @@ -0,0 +1,2 @@ + +# debug message