Skip to content

Commit 6869301

Browse files
authored
Missing push statement to frame (#186)
1 parent 5a81a6a commit 6869301

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/bloqade/qasm2/parse/lowering.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def loads(
4545
with state.frame(
4646
[stmt],
4747
globals=globals,
48+
finalize_next=False,
4849
) as frame:
4950
try:
5051
self.visit(state, stmt)
@@ -398,7 +399,7 @@ def visit_BinOp(self, state: lowering.State[ast.Node], node: ast.BinOp):
398399
def visit_UnaryOp(self, state: lowering.State[ast.Node], node: ast.UnaryOp):
399400
if node.op == "-":
400401
stmt = expr.Neg(value=state.lower(node.operand).expect_one())
401-
return stmt.result
402+
return state.current_frame.push(stmt).result
402403
else:
403404
return state.lower(node.operand).expect_one()
404405

test/qasm2/test_lowering.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import tempfile
33
import textwrap
44

5+
from kirin import ir
56
from kirin.dialects import func
67

78
from bloqade import qasm2
@@ -42,3 +43,40 @@ def test_loadfile():
4243
(ret := kernel.callable_region.blocks[0].last_stmt), func.Return
4344
)
4445
assert ret.value.type.is_equal(qasm2.types.CRegType)
46+
47+
48+
def test_negative_lowering():
49+
50+
mwe = """
51+
OPENQASM 2.0;
52+
include "qelib1.inc";
53+
qreg q[1];
54+
rz(-0.2) q[0];
55+
"""
56+
57+
entry = QASM2(qasm2.main).loads(mwe, "entry", returns="q")
58+
59+
body = ir.Region(
60+
ir.Block(
61+
[
62+
(size := qasm2.expr.ConstInt(value=1)),
63+
(qreg := qasm2.core.QRegNew(n_qubits=size.result)),
64+
(phi := qasm2.expr.ConstFloat(value=0.2)),
65+
(theta := qasm2.expr.Neg(phi.result)),
66+
(idx := qasm2.expr.ConstInt(value=0)),
67+
(qubit := qasm2.core.QRegGet(qreg.result, idx.result)),
68+
(qasm2.uop.RZ(qubit.result, theta.result)),
69+
(func.Return(qreg.result)),
70+
]
71+
)
72+
)
73+
74+
code = func.Function(
75+
sym_name="entry",
76+
signature=func.Signature((), qasm2.QRegType),
77+
body=body,
78+
)
79+
80+
code.print()
81+
82+
assert entry.code.is_structurally_equal(code)

0 commit comments

Comments
 (0)