Skip to content

Commit 9769869

Browse files
Roger-luoweinbe58
authored andcommitted
fix lowering
1 parent d17c395 commit 9769869

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/bloqade/qasm2/dialects/expr/lowering.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def lower_Assign(self, state: lowering.State, node: ast.Assign):
3636
raise lowering.BuildError(f"unsupported target syntax {target_syntax}")
3737

3838
def lower_Expr(self, state: lowering.State, node: ast.Expr):
39-
return state.lower(node.value)
39+
return state.parent.visit(state, node.value)
4040

4141
def lower_Constant(self, state: lowering.State, node: ast.Constant):
4242
if isinstance(node.value, int):

src/bloqade/qasm2/dialects/inline.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ def lower(
3030
if isinstance(text, ast.Constant) and isinstance(text.value, str):
3131
value = text.value
3232
elif isinstance(text, ast.Name) and isinstance(text.ctx, ast.Load):
33-
value = state.get_global(text.id).expect(str)
33+
value = state.get_global(text).expect(str)
3434
else:
3535
raise lowering.BuildError(
3636
"InlineQASM takes a string literal or global string"
3737
)
3838

3939
raw = textwrap.dedent(value)
4040
qasm_lowering = QASM2(state.parent.dialects)
41-
qasm_lowering.run(loads(raw))
41+
code = qasm_lowering.run(loads(raw))
42+
code.print()
4243

4344

4445
# NOTE: this is a dummy statement that won't appear in IR.

src/bloqade/qasm2/parse/lowering.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def run(
2121
self,
2222
stmt: ast.Node,
2323
*,
24+
state: lowering.State | None = None,
2425
source: str | None = None,
2526
globals: dict[str, Any] | None = None,
2627
file: str | None = None,
@@ -29,7 +30,7 @@ def run(
2930
compactify: bool = True,
3031
) -> ir.Statement:
3132
# TODO: add source info
32-
state = lowering.State(
33+
state = state or lowering.State(
3334
self,
3435
file=file,
3536
lineno_offset=lineno_offset,

test/qasm2/test_inline.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,27 @@ def qasm2_inline_code():
8888
qasm2.inline(lines)
8989

9090
qasm2_inline_code.print()
91+
92+
93+
lines = textwrap.dedent(
94+
"""
95+
OPENQASM 2.0;
96+
97+
qreg q[2];
98+
creg c[2];
99+
100+
h q[0];
101+
CX q[0], q[1];
102+
barrier q[0], q[1];
103+
CX q[0], q[1];
104+
rx(pi/2) q[0];
105+
"""
106+
)
107+
108+
109+
@qasm2.main.add(inline)
110+
def qasm2_inline_code():
111+
qasm2.inline(lines)
112+
113+
114+
qasm2_inline_code.print()

0 commit comments

Comments
 (0)