munich-quantum-toolkit / munich-quantum-toolkit/debugger
🐛 Register-reference validation is incomplete in the parser
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 21
- Forks
- 7
- Avg merge
- 9h 28m
- Merged PRs (30d)
- 42
Description
🤖 AI text below 🤖
Problem statement
The parser has three related gaps in how it validates register references and comparison values:
-
Well-formed identifier checking. Neither
validateTargets(for qubit targets in gate calls) norparseBitRegisterRef(for classical bits inifconditions) rejects malformed register names on lexical grounds. Inputs likec2](dangling]) or@#(not an identifier at all) slip through the shape check. On the qubit side the eventualdefinedRegisterslookup rejects them indirectly with an "unknown register" error; on the classical side there is no lookup, so the error surfaces even later in the DD backend. -
Classical bit reference against declared registers.
validateTargetsverifies that qubit targets exist indefinedRegistersand that the bit index fits within the register size. Nothing analogous runs for the register mentioned in anifcondition:if (nonexistent == 1)andif (c[999] == 1)(withcreg c[3]) both parse without complaint. -
expectedValuewithin the register's representable range.if (c == 999)oncreg c[3](3 bits, values 0 to 7) is impossible to satisfy but the parser accepts it. The comparison always evaluates to false at runtime.
Surfaced during the review of PR #463.
Proposed solution
Add the three missing checks and share them where the same code path applies to qubits and classical bits:
- Extract a small
isValidIdentifierhelper (letter or underscore followed by letters, digits, or underscores). Use it invalidateTargets(before thedefinedRegisterslookup) and inparseBitRegisterRef(before returning the reference). - Extend
preprocessCodeso it also runs an existence + bounds check on the register mentioned in the condition of anif, using the samedefinedRegistersmap already available there. The exact placement (insideparseClassicConditionExpressionor a separatevalidateClassicConditionpass) is an implementation detail. - Reject
expectedValuevalues that exceed what the referenced register can hold (whole register or single bit).
Out of scope
- Reserved-word checking (
qreg,creg,measure, etc.). Separate semantic concern. - Compound expressions in the condition (out of scope of the parser as a whole today).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing preprocessCode and the mentioned entry points validateTargets, parseBitRegisterRef, and parseClassicConditionExpression, using definedRegisters to understand the existing qubit checks. Add identifier, register existence, index-bounds, and expectedValue range validation for both relevant paths. Done means malformed references and impossible comparisons are rejected while the listed out-of-scope cases remain unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 66/100