Commit 7e05f4c
fix: reject barrier as an OpenQASM 2 conditional body (#339)
* test: cover barrier as an OpenQASM 2 conditional body
The QASM 2 grammar admits only a <qop> as the body of an `if`:
<if> := if ( <id> == <nninteger> ) <qop>
<qop> := <uop> | measure ... | reset ...
`barrier` is a separate production, so `if(m==1) barrier q;` is not a
valid QASM 2 program. pyqasm accepts it and emits it unchanged, which
downstream QASM 2 parsers reject.
Tests assert the rejection, alongside cases pinning down what must keep
validating: every operation that *is* a <qop> in a conditional body, and
an unconditional barrier.
* fix: reject barrier as an OpenQASM 2 conditional body
`Qasm2Module._filter_statements` checked only the type of each top-level
statement, so nothing inspected what a `BranchingStatement` carried in
its body. A conditional barrier passed validation and was serialized
verbatim as `if (m == 1) barrier q[0], q[1];`, which QASM 2 parsers
reject — barrier is not a <qop>, and only a <qop> may follow an `if`.
Filtering now recurses into conditional bodies (both blocks, at any
depth) and raises a ValidationError naming the offending statement.
* test: cover non-qop conditional bodies, make nested test actually nest
Review feedback on #339.
`test_conditional_barrier_rejected_when_nested` claimed to exercise the
recursive descent but its program had no nesting -- it duplicated the
`barrier q;` parametrized case, so dropping the recursion left every
test green. It now nests (`if(m==1) if(m==0) barrier q;`), verified to
fail when the recursion is removed.
Adds failing tests for the wider hole behind that one: filtering
blacklists `barrier` alone, so any other non-qop body the parser accepts
validates cleanly and is emitted as invalid QASM 2. `delay` and `box`
bodies both do this today -- neither has any QASM 2 syntax, and `delay`
is even rejected at the top level by the existing whitelist.
* fix: whitelist the QASM 2 <qop> production for conditional bodies
Review feedback on #339.
Filtering blacklisted `barrier` alone, so every other non-qop body the
parser accepts still validated and was emitted verbatim. `delay` and
`box` bodies both did this -- neither has any QASM 2 syntax, and `delay`
is already rejected by the top-level whitelist, so a conditional was the
one place it could slip through.
Conditional bodies are now checked against the <qop> production itself
(QuantumGate, QuantumMeasurementStatement, QuantumReset), with branching
statements recursed into as before. Barrier keeps its specific message;
anything else is named by node type.
* review: hoist qop constant, name keywords, report global phase honestly
- _qop_statements becomes a module-level _QOP_STATEMENTS; it is a fixed grammar
production, not per-instance state (L1)
- non-qop bodies report the keyword the user wrote via _NON_QOP_KEYWORDS, so a
delay body says 'delay' rather than 'statement of type DelayInstruction' (L2)
- QuantumPhase gets its own message naming global phase and the rzz/rxx
decomposition that introduces it, instead of an AST class name; the
underlying gphase gap is tracked in #351 (M1 mitigation)
---------
Co-authored-by: TheGupta2012 <harshit.11235@gmail.com>1 parent 8a4eac2 commit 7e05f4c
3 files changed
Lines changed: 144 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
30 | 46 | | |
31 | 47 | | |
32 | 48 | | |
| |||
60 | 76 | | |
61 | 77 | | |
62 | 78 | | |
| 79 | + | |
| 80 | + | |
63 | 81 | | |
64 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
65 | 117 | | |
66 | 118 | | |
67 | 119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
0 commit comments