deq.noise.inject_noise (inject_si1000 / inject_biased) silently skips all instructions inside single-line REPEAT blocks

Open
#309 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, rust
Domain
backend

Research direction

Start by reading deq/deq/noise/injector.py around lines 107–109, where lines containing braces are skipped. Reproduce the issue with the single-line and multi-line REPEAT examples using deq.circuit.parser.parse and inject_si1000; check the same path for inject_biased. Done means both forms inject noise inside the loop and the resulting source still parses.

Written by the indexing model from the issue text.

Description

Describe the bug
deq.noise.inject_noise (and therefore inject_si1000 and inject_biased) silently skips noise injection for every instruction inside a single-line REPEAT N { ... } block, even though single-line REPEAT blocks are valid .deq syntax accepted by deq.circuit.parser.parse.

By contrast, writing the exact same REPEAT block across multiple lines injects the expected noise inside the loop body.

To Reproduce

  1. Install or import deq from main (19a8853b7611d147d49d8ab46f817d7d9efc76ce, deq 0.7.0).
  2. Run the following script:
from deq.circuit.parser import parse
from deq.noise import inject_si1000

single_line = "GADGET G {\n  REPEAT 2 { X 0 }\n  M 0\n}\n"
multi_line = "GADGET G {\n  REPEAT 2 {\n    X 0\n  }\n  M 0\n}\n"

# Both are valid .deq circuits accepted by the parser:
parse(single_line)
parse(multi_line)

print("=== single-line REPEAT ===")
print(inject_si1000(single_line, p=0.01))

print("=== multi-line REPEAT ===")
print(inject_si1000(multi_line, p=0.01))

Observed output:

=== single-line REPEAT ===
GADGET G {
  REPEAT 2 { X 0 }
  M(0.01) 0
}

=== multi-line REPEAT ===
GADGET G {
  REPEAT 2 {
    X 0
    DEPOLARIZE1(0.01) 0
  }
  M(0.01) 0
}

No DEPOLARIZE1(0.01) 0 is emitted for X 0 inside REPEAT 2 { X 0 }.

Expected behavior
Single-line REPEAT N { ... } blocks inside a GADGET should receive the same noise injection as their multi-line equivalent (e.g., expanding the block and emitting DEPOLARIZE1(0.01) 0 after X 0 inside REPEAT 2 { ... }), and the resulting .deq source should continue to parse via deq.circuit.parser.parse.

System environment (please complete the following information):

  • OS: Windows 11
  • Architecture: x86_64
  • Python or Rust versions, as appropriate: Python 3.12.14 (deq 0.7.0, commit 19a8853b7611d147d49d8ab46f817d7d9efc76ce; pure-Python deq.noise module, no Rust toolchain required to reproduce)

Additional context
In deq/deq/noise/injector.py (lines 107–109), any line containing an opening or closing brace is appended unchanged and skipped immediately:

if opens or closes:
    result.append(line)
    continue

For REPEAT 2 { X 0 }, opens == 1 and closes == 1, so inject_noise appends the raw line verbatim without inspecting the inner body. The same code path affects all NoiseEmitter callers (inject_si1000, inject_biased).

Suggested minimal fix (expanding single-line brace blocks inside GADGETs)
diff --git a/deq/deq/noise/injector.py b/deq/deq/noise/injector.py
--- a/deq/deq/noise/injector.py
+++ b/deq/deq/noise/injector.py
@@ -105,6 +105,24 @@ def inject_noise(text: str, emitter: NoiseEmitter) -> str:
                 block_stack.pop()
 
         if opens or closes:
+            if in_gadget and opens == 1 and closes == 1:
+                code_part = strip_comment(line)
+                open_idx = code_part.find("{")
+                close_idx = code_part.rfind("}")
+                inner = code_part[open_idx + 1 : close_idx].strip()
+                if inner:
+                    orig_indent = line[: len(line) - len(line.lstrip(" \t"))]
+                    ending = line[len(line.rstrip("\n\r")) :] or "\n"
+                    head = line[:open_idx].rstrip()
+                    tail = line[close_idx + 1 :]
+                    inner_noisy = inject_noise(
+                        f"GADGET _ {{\n{orig_indent}    {inner}\n}}\n", emitter
+                    ).splitlines(keepends=True)[1:-1]
+                    result.append(f"{head} {{\n")
+                    result.extend(inner_noisy)
+                    result.append(f"{orig_indent}}}{tail if tail else ending}")
+                    continue
             result.append(line)
             continue
Dominant language
Rust
Stars
69
Forks
25
Avg merge
2d 22h
Merged PRs (30d)
48

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/qdk-ec

All issues in microsoft/qdk-ec

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.