From a2b67973d64d22704ed29ef9f58708a13b2b1bad Mon Sep 17 00:00:00 2001 From: Rafael Haenel Date: Thu, 16 Oct 2025 11:57:32 -0700 Subject: [PATCH 1/3] Add debug logging to writeln method in EmitStimMain class --- src/bloqade/stim/emit/stim_str.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/bloqade/stim/emit/stim_str.py b/src/bloqade/stim/emit/stim_str.py index 640d97a1f..7a7cdaee5 100644 --- a/src/bloqade/stim/emit/stim_str.py +++ b/src/bloqade/stim/emit/stim_str.py @@ -43,6 +43,12 @@ def get_output(self) -> str: self.file.seek(0) return self.file.read() + def writeln(self, frame: EmitStrFrame, *args): + if self.debug: + self.newline(frame) + self.file.write(f"# v-- {frame.code.source or 'unknown source'}") + super().writeln(frame, *args) + @func.dialect.register(key="emit.stim") class FuncEmit(interp.MethodTable): From a048ec2a660bd967a55476b18e9487971384743b Mon Sep 17 00:00:00 2001 From: Rafael Haenel Date: Thu, 16 Oct 2025 12:21:40 -0700 Subject: [PATCH 2/3] Add unit test --- test/stim/emit/__init__.py | 0 test/stim/emit/test_stim_str.py | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/stim/emit/__init__.py create mode 100644 test/stim/emit/test_stim_str.py diff --git a/test/stim/emit/__init__.py b/test/stim/emit/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/test/stim/emit/test_stim_str.py b/test/stim/emit/test_stim_str.py new file mode 100644 index 000000000..20082caf7 --- /dev/null +++ b/test/stim/emit/test_stim_str.py @@ -0,0 +1,26 @@ +import pytest + +from bloqade import squin +from bloqade.squin import qubit, kernel +from bloqade.stim.emit import EmitStimMain +from bloqade.stim.passes import SquinToStimPass + + +@pytest.mark.parametrize("debug", [True, False]) +def test_debug_emit_with_source_info(debug: bool): + @kernel + def test(): + q = qubit.new(2) + squin.cx(q[0], q[1]) + + SquinToStimPass(test.dialects)(test) + + emit = EmitStimMain(debug=debug) + emit.initialize() + emit.run(mt=test, args=()) + output = emit.get_output() + + if debug: + assert "# v--" in output + else: + assert "# v--" not in output From 20bfb7485ba299e28233767e81ed788ab3445ebe Mon Sep 17 00:00:00 2001 From: Rafael Haenel Date: Tue, 21 Oct 2025 11:49:35 -0700 Subject: [PATCH 3/3] Writeln with correct source info with offset --- src/bloqade/stim/emit/stim_str.py | 16 ++++++++++++++-- test/stim/emit/test_stim_str.py | 12 ++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/bloqade/stim/emit/stim_str.py b/src/bloqade/stim/emit/stim_str.py index 7a7cdaee5..354f14f2c 100644 --- a/src/bloqade/stim/emit/stim_str.py +++ b/src/bloqade/stim/emit/stim_str.py @@ -27,6 +27,12 @@ def initialize(self): self.file.seek(0) return self + def run_method( + self, method: ir.Method, args: tuple[str, ...] + ) -> tuple[EmitStrFrame, str]: + self._current_method = method + return super().run_method(method, args) + def eval_stmt_fallback( self, frame: EmitStrFrame, stmt: ir.Statement ) -> tuple[str, ...]: @@ -34,6 +40,7 @@ def eval_stmt_fallback( def emit_block(self, frame: EmitStrFrame, block: ir.Block) -> str | None: for stmt in block.stmts: + frame.current_stmt = stmt result = self.eval_stmt(frame, stmt) if isinstance(result, tuple): frame.set_values(stmt.results, result) @@ -46,7 +53,13 @@ def get_output(self) -> str: def writeln(self, frame: EmitStrFrame, *args): if self.debug: self.newline(frame) - self.file.write(f"# v-- {frame.code.source or 'unknown source'}") + source = frame.current_stmt.source + if source is not None: + self.file.write( + f"# v-- {source.file}:{source.lineno + self._current_method.lineno_begin -1}" + ) + else: + self.file.write("# v-- unknown source") super().writeln(frame, *args) @@ -56,5 +69,4 @@ class FuncEmit(interp.MethodTable): @interp.impl(func.Function) def emit_func(self, emit: EmitStimMain, frame: EmitStrFrame, stmt: func.Function): _ = emit.run_ssacfg_region(frame, stmt.body, ()) - # emit.output = "\n".join(frame.body) return () diff --git a/test/stim/emit/test_stim_str.py b/test/stim/emit/test_stim_str.py index 20082caf7..67043a114 100644 --- a/test/stim/emit/test_stim_str.py +++ b/test/stim/emit/test_stim_str.py @@ -1,19 +1,14 @@ import pytest -from bloqade import squin -from bloqade.squin import qubit, kernel +from bloqade import stim from bloqade.stim.emit import EmitStimMain -from bloqade.stim.passes import SquinToStimPass @pytest.mark.parametrize("debug", [True, False]) def test_debug_emit_with_source_info(debug: bool): - @kernel + @stim.main def test(): - q = qubit.new(2) - squin.cx(q[0], q[1]) - - SquinToStimPass(test.dialects)(test) + stim.cx((0, 1), (2, 3)) emit = EmitStimMain(debug=debug) emit.initialize() @@ -22,5 +17,6 @@ def test(): if debug: assert "# v--" in output + assert ".py:" in output else: assert "# v--" not in output