Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bloqade/qasm2/parse/lowering.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def loads(
with state.frame(
[stmt],
globals=globals,
finalize_next=False,
) as frame:
try:
self.visit(state, stmt)
Expand Down Expand Up @@ -398,7 +399,7 @@ def visit_BinOp(self, state: lowering.State[ast.Node], node: ast.BinOp):
def visit_UnaryOp(self, state: lowering.State[ast.Node], node: ast.UnaryOp):
if node.op == "-":
stmt = expr.Neg(value=state.lower(node.operand).expect_one())
return stmt.result
return state.current_frame.push(stmt).result
else:
return state.lower(node.operand).expect_one()

Expand Down
38 changes: 38 additions & 0 deletions test/qasm2/test_lowering.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tempfile
import textwrap

from kirin import ir
from kirin.dialects import func

from bloqade import qasm2
Expand Down Expand Up @@ -42,3 +43,40 @@ def test_loadfile():
(ret := kernel.callable_region.blocks[0].last_stmt), func.Return
)
assert ret.value.type.is_equal(qasm2.types.CRegType)


def test_negative_lowering():

mwe = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
rz(-0.2) q[0];
"""

entry = QASM2(qasm2.main).loads(mwe, "entry", returns="q")

body = ir.Region(
ir.Block(
[
(size := qasm2.expr.ConstInt(value=1)),
(qreg := qasm2.core.QRegNew(n_qubits=size.result)),
(phi := qasm2.expr.ConstFloat(value=0.2)),
(theta := qasm2.expr.Neg(phi.result)),
(idx := qasm2.expr.ConstInt(value=0)),
(qubit := qasm2.core.QRegGet(qreg.result, idx.result)),
(qasm2.uop.RZ(qubit.result, theta.result)),
(func.Return(qreg.result)),
]
)
)

code = func.Function(
sym_name="entry",
signature=func.Signature((), qasm2.QRegType),
body=body,
)

code.print()

assert entry.code.is_structurally_equal(code)