Skip to content

Commit 14f89ba

Browse files
authored
Fix bug in qasm2 custom gate emit (#202)
* fix emit bug * lint
1 parent d044d1b commit 14f89ba

File tree

4 files changed

+11
-32
lines changed

4 files changed

+11
-32
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import Literal
22

33
from kirin import interp
4-
from kirin.emit.exceptions import EmitError
54

65
from bloqade.qasm2.parse import ast
76
from bloqade.qasm2.types import QubitType
@@ -19,17 +18,18 @@ def emit_func(
1918
self, emit: EmitQASM2Gate, frame: EmitQASM2Frame, stmt: stmts.GateFunction
2019
):
2120

22-
args, cparams, qparams = [], [], []
23-
for arg in stmt.body.blocks[0].args[1:]:
24-
name = frame.get_typed(arg, ast.Name)
25-
args.append(name)
26-
if not isinstance(name, ast.Name):
27-
raise EmitError("expected ast.Name")
21+
args: list[ast.Node] = []
22+
cparams, qparams = [], []
23+
for arg in stmt.body.blocks[0].args:
24+
assert arg.name is not None
25+
26+
args.append(ast.Name(id=arg.name))
2827
if arg.type.is_subseteq(QubitType):
29-
qparams.append(name.id)
28+
qparams.append(arg.name)
3029
else:
31-
cparams.append(name.id)
32-
emit.run_ssacfg_region(frame, stmt.body, args)
30+
cparams.append(arg.name)
31+
32+
emit.run_ssacfg_region(frame, stmt.body, tuple(args))
3333
emit.output = ast.Gate(
3434
name=stmt.sym_name,
3535
cparams=cparams,

src/bloqade/qasm2/emit/gate.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,3 @@ def emit_err(self, emit: EmitQASM2Gate, frame: EmitQASM2Frame, stmt):
8686
@interp.impl(func.ConstantNone)
8787
def ignore(self, emit: EmitQASM2Gate, frame: EmitQASM2Frame, stmt):
8888
return ()
89-
90-
@interp.impl(func.Function)
91-
def emit_func(
92-
self, emit: EmitQASM2Gate, frame: EmitQASM2Frame, stmt: func.Function
93-
):
94-
args_ssa = stmt.args
95-
print(stmt.args)
96-
emit.run_ssacfg_region(frame, stmt.body, frame.get_values(args_ssa))
97-
98-
cparams, qparams = [], []
99-
for arg in args_ssa:
100-
if arg.type.is_subseteq(QubitType):
101-
qparams.append(frame.get(arg))
102-
else:
103-
cparams.append(frame.get(arg))
104-
105-
emit.output = ast.Gate(stmt.sym_name, cparams, qparams, frame.body)
106-
return ()

src/bloqade/qasm2/emit/target.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def emit(self, entry: ir.Method) -> ast.MainProgram:
101101

102102
Py2QASM(entry.dialects)(entry)
103103
target_main = EmitQASM2Main(self.main_target)
104-
target_main.run(entry, tuple(ast.Name(name) for name in entry.arg_names[1:]))
104+
target_main.run(entry, ())
105105

106106
main_program = target_main.output
107107
assert main_program is not None, f"failed to emit {entry.sym_name}"

test/qasm2/emit/test_qasm2.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import pytest
2-
31
from bloqade import qasm2
42

53

6-
@pytest.mark.skip(reason="broken gate emit!")
74
def test_qasm2_custom_gate():
85
@qasm2.gate
96
def custom_gate(a: qasm2.Qubit, b: qasm2.Qubit):

0 commit comments

Comments
 (0)