Qiskit / openqasm3_parser

Parser and semantic analyzer for the OpenQASM3 language
Apache License 2.0
10 stars 11 forks source link

If statements should support single statement `then` and `else` #228

Open idavis opened 2 weeks ago

idavis commented 2 weeks ago

From the spec:

The statement if ( bool ) <true-body> branches to program if the Boolean evaluates to true
and may optionally be followed by else <false-body>. Both true-body and false-body can
be a single statement terminated by a semicolon, or a program block of several statements { stmt1; stmt2; }.

grammar: ifStatement: IF LPAREN expression RPAREN if_body=statementOrScope (ELSE else_body=statementOrScope)?;

if-then example:

include "stdgates.inc";
qubit q;
if (0 == 1) z q;

output:

Error: [  x QASM3 Parse Error: expected a block
   ,-[test:4:20]
 3 |         qubit q;
 4 |         if (0 == 1) z q;
   :                    ^
 5 |     
   `----
]

if-then-else example:

include "stdgates.inc";
qubit q;
if (0 == 1) z q; else x q;

output:

Error: [  x QASM3 Parse Error: expected a block
   ,-[test:4:20]
 3 |         qubit q;
 4 |         if (0 == 1) z q; else x q;
   :                    ^
 5 |     
   `----
,   x QASM3 Parse Error: stmt: expected expression, type declaration, or let
  | statement
   ,-[test:4:26]
 3 |         qubit q;
 4 |         if (0 == 1) z q; else x q;
   :                          ^
 5 |     
   `----
]
jakelishman commented 2 weeks ago

Just to warn you because you're opening a lot of issues: this parser is/was experimental, and is not this second under active development. We reprioritised our efforts earlier in the year, and this parser is one of the things that got temporarily deprioritised, before we'd finished making it production ready.

We do intend to pick up development on it again, probably in the new year, but right now, there's nobody from the Qiskit team actively working on it.