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

Aperta
#309 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, rust
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Rust
Stelle
69
Fork
25
Merge medio
2g 22h
PR unite (30g)
48

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/qdk-ec

Tutte le issue di microsoft/qdk-ec

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.