`deq.noise.inject_si1000` drops target qubits on `CX[tag] 0 1` and produces invalid `M(p)[tag]` syntax on `M[tag] 0 1`

Open Beginner friendly
#303 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

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

Research direction

Read deq/deq/noise/common.py at split_two_qubit_gate_targets and deq/deq/noise/injector.py at inject_noise. Reproduce both cases from the issue, then run the deq unit tests. Done means tagged two-qubit gates retain both targets, tagged measurements put the probability after the tag, and the resulting circuit parses cleanly.

Written by the indexing model from the issue text.

Description

Description

Two Stim [tag] bugs in deq.noise:

  1. split_two_qubit_gate_targets checks if t before stripping [...] tags, leaving an empty string "" in index 0 for CX[entangle] 0 1. Pair chunking then emits malformed DEPOLARIZE2(0.01) 0 and silently drops qubit 1.
  2. inject_noise prepends ({prob}) directly before after_clean on measurements, rewriting M[ro] 0 1 as invalid M(0.01)[ro] 0 1 instead of M[ro](0.01) 0 1.

Steps to Reproduce

Run on main (60dd1134c53d1a7dc792516014fa1a021aa10a61), Python 3.12.14:

# Trigger 1 (2Q [tag] qubit drop):
from deq.noise import inject_si1000

src = "CODE C [[1,1,1]] {\n  LOGICAL X0 Z0\n}\nGADGET G {\n  R 0 1\n  CX[entangle] 0 1\n  M 0 1\n}\n"
noisy = inject_si1000(src, p=0.01)
assert "DEPOLARIZE2(0.01) 0 1" in noisy, f"Corrupted 2Q noise:\n{noisy}"
print("OK")

# Trigger 2 (M [tag](prob) ordering):
from deq.circuit.parser import parse
from deq.noise import inject_si1000

src = "CODE C [[1,1,1]] {\n  LOGICAL X0 Z0\n}\nGADGET G {\n  R 0 1\n  M[ro] 0 1\n}\n"
noisy = inject_si1000(src, p=0.01)
assert "M[ro](0.01) 0 1" in noisy, f"Wrong tag/noise order:\n{noisy}"
parse(noisy)
print("OK")

Actual Behavior

On CX[entangle] 0 1:
  DEPOLARIZE2(0.01)  0  # qubit 1 dropped
On M[ro] 0 1:
  M(0.01)[ro] 0 1       # deq.circuit.parser.parse raises SyntaxError

Expected Behavior

CX[entangle] 0 1 should get DEPOLARIZE2(0.01) 0 1, and M[ro] 0 1 should become M[ro](0.01) 0 1 and parse cleanly via deq.circuit.parser.parse.

Root Cause & Suggested Minimal Fix

  • Location: deq/deq/noise/common.py#L79-L83, deq/deq/noise/injector.py#L158-L167
  • Details: common.py keeps empty tokens produced by regex tag stripping; injector.py inserts ({prob}) before [tag] instead of immediately after ].
Suggested minimal diff (verified against `deq` unit tests)
diff --git a/deq/deq/noise/common.py b/deq/deq/noise/common.py
index db910ba..3a49806 100644
--- a/deq/deq/noise/common.py
+++ b/deq/deq/noise/common.py
@@ -79,7 +79,8 @@ def split_two_qubit_gate_targets(
             return tok, True
         return re.sub(r"\[[^\]]*\]", "", tok), False
 
-    processed = [_process(t) for t in raw_tokens if t]
+    # Drop empty tokens produced when stripping bracketed Stim tags like [tag]
+    processed = [res for t in raw_tokens if (res := _process(t))[0]]
 
     qubit_pairs: list[str] = []
     single_qubits: list[str] = []
diff --git a/deq/deq/noise/injector.py b/deq/deq/noise/injector.py
index f54cba5..fc18eb9 100644
--- a/deq/deq/noise/injector.py
+++ b/deq/deq/noise/injector.py
@@ -158,7 +158,13 @@ def inject_noise(text: str, emitter: NoiseEmitter) -> str:
 
         if measure_prob is not None:
             after_clean = strip_parens(after) if "(" in after else after
-            line = f"{indent}{m.group('name')}({fmt_prob(measure_prob)}){after_clean}{ending}"
+            stripped = after_clean.lstrip()
+            if stripped.startswith("[") and "]" in stripped:
+                ws = after_clean[: len(after_clean) - len(stripped)]
+                tag_end = stripped.index("]") + 1
+                line = f"{indent}{m.group('name')}{ws}{stripped[:tag_end]}({fmt_prob(measure_prob)}){stripped[tag_end:]}{ending}"
+            else:
+                line = f"{indent}{m.group('name')}({fmt_prob(measure_prob)}){after_clean}{ending}"
 
         # --- The (possibly modified) instruction ---
         result.append(line)
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.