[Stim] Comment-only line before a REPEAT block's closing brace fails to parse

Open Beginner friendly
#4,312 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
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, rust
Domain
compilers

Research direction

The parser lives in source/compiler/stim_compiler/src/parser.rs, and the issue points at parse_block, which checks for Close(Brace) before skipping newlines. Reproduce with the Python snippet in the issue, which should fail with ExpectedToken on close(brace). Done when that snippet compiles and the comment-only line before the closing brace is accepted inside a REPEAT block.

Written by the indexing model from the issue text.

Description

bug needs triage

Describe the bug

qdk.stim fails to parse a REPEAT block when a comment-only line comes directly before the closing }. Stim accepts this program. A comment at the end of an instruction line (H 0 # comment) works, and so does a comment-only line elsewhere in the block.

To Reproduce

import sys
import qdk.stim

src = """R 0
REPEAT 2 {
    H 0
    # comment
}
M 0
"""
try:
    qdk.stim.compile(src)
except Exception as e:
    print(e)
    print("REPRODUCED")
    sys.exit(1)
print("NOT REPRODUCED")

This fails with Qdk.Stim.Parser.ExpectedToken: expected instruction_name, found close(brace), followed by UnexpectedEof.

Expected behavior

The program compiles, as it does in Stim (stim.Circuit(src) accepts it).

Additional context

The likely cause is in source/compiler/stim_compiler/src/parser.rs. The lexer skips the comment and then emits another Newline token. parse_block checks for Close(Brace) before skipping newlines, so it calls parse_item, which skips the newline, finds } and fails. Skipping newlines in parse_block before the Close(Brace) check should fix it.

Found by differential fuzzing against Stim 1.16.0.

Dominant language
Rust
Stars
1k
Forks
226
Avg merge
2d 9h
Merged PRs (30d)
72

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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

All issues in microsoft/qdk

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.