Skip to content

Error(s) with code-gen of if-statements #157

Open
3 of 4 issues completed
Open
3 of 4 issues completed
@cduck

Description

@cduck

Bug report by https://github.com/randyshee/yquantum/blob/main/2025/team_solutions/charlie_and_delta/bug_report.ipynb

There are several related errors with the QASM2 if-statements that are emitted by Bloqade that cause parse errors for Qiskit.

If-statement with one basic operation in it works:

from bloqade import qasm2
from bloqade.qasm2.emit import QASM2 # the QASM2 target
from bloqade.qasm2.parse import pprint # the QASM2 pretty printer
from bloqade.qasm2.parse import pprint, spprint, Console
from qiskit import QuantumCircuit

circ_nested = None
del circ_nested

@qasm2.extended
def circ_nested():
    qr = qasm2.qreg(4)
    cr = qasm2.creg(4)

    qasm2.measure(qr, cr)
    
    if cr == 0:
        # Different errors depending on the code in this block:
        qasm2.x(qr[3])

    return qr

target = QASM2()
ast = target.emit(circ_nested)
qasm = spprint(ast, console=Console(no_color=True, force_jupyter=False, force_interactive=False, force_terminal=False))
print(qasm)
circuit = QuantumCircuit.from_qasm_str(qasm)

Two doesn't work:

    if cr == 0:
        # Different errors depending on the code in this block:
        qasm2.x(qr[3])
        qasm2.x(qr[3])
OPENQASM 2.0;
include "qelib1.inc";
qreg qr[4];
creg cr[4];
measure qr -> cr;
if (cr == 0) {
  x qr[3];
  x qr[3];
}
-----
QASM2ParseError: '<input>:6,13: needed a gate application, measurement or reset, but instead saw {'

The error message makes it clear that the if-statement body is restricted.

Sub-issues

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingloweringqasm2qasm2 related issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions