Skip to content

Commit 41bd2b0

Browse files
committed
add reflection
1 parent 89830ec commit 41bd2b0

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

src/YaoCompiler.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export @device,
2323
AnyReg,
2424
# reexport YaoLocations
2525
Locations,
26-
CtrlLocations
26+
CtrlLocations,
27+
# reflection
28+
@yao_code_lowered,
29+
@yao_code_typed
2730

2831
using MLStyle
2932
using YaoAPI
@@ -61,6 +64,7 @@ include("compiler/printing.jl")
6164
include("compiler/intrinsics.jl")
6265
include("compiler/syntax.jl")
6366
include("compiler/interp.jl")
67+
include("compiler/reflection.jl")
6468

6569
include("codegen/llvmopt.jl")
6670
include("codegen/native.jl")

src/compiler/reflection.jl

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
macro yao_code_lowered(ex)
2+
esc(yao_code_lowered_m(ex))
3+
end
4+
5+
macro yao_code_lowered(ctrl, ex)
6+
esc(yao_code_lowered_m(ctrl, ex))
7+
end
8+
9+
macro yao_code_typed(ex)
10+
esc(yao_code_typed_m(ex))
11+
end
12+
13+
macro yao_code_typed(ctrl, ex)
14+
esc(yao_code_typed_m(ctrl, ex))
15+
end
16+
17+
function yao_code_lowered_m(ex)
18+
@match ex begin
19+
# apply(reg, gate, locs)
20+
:($locs => $gate) => quote
21+
Base.code_lowered(
22+
$Intrinsics.apply,
23+
($YaoCompiler.AnyReg, typeof($gate), typeof($Locations($(locs))))
24+
)
25+
end
26+
# apply(reg, gate)
27+
_ => quote
28+
Base.code_lowered($Intrinsics.apply, ($YaoCompiler.AnyReg, typeof($ex)))
29+
end
30+
end
31+
end
32+
33+
function yao_code_lowered_m(ctrl, ex)
34+
@match ex begin
35+
# apply(reg, gate, locs, ctrl)
36+
:($locs => $gate) => quote
37+
Base.code_lowered(
38+
$Intrinsics.apply,
39+
($YaoCompiler.AnyReg, typeof($gate), typeof($Locations($(locs))), typeof($CtrlLocations($ctrl)))
40+
)
41+
end
42+
_ => error("expect a locs => gate expression")
43+
end
44+
end
45+
46+
function yao_code_typed_m(ex)
47+
@match ex begin
48+
# apply(reg, gate, locs)
49+
:($locs => $gate) => quote
50+
$Base.code_typed(
51+
$Intrinsics.apply,
52+
($YaoCompiler.AnyReg, typeof($gate), typeof($Locations($(locs))));
53+
interp=$(YaoInterpreter())
54+
)
55+
end
56+
# apply(reg, gate)
57+
_ => quote
58+
$Base.code_typed(
59+
$Intrinsics.apply, ($YaoCompiler.AnyReg, typeof($ex));
60+
interp=$(YaoInterpreter()),
61+
)
62+
end
63+
end
64+
end
65+
66+
function yao_code_typed_m(ctrl, ex)
67+
@match ex begin
68+
# apply(reg, gate, locs, ctrl)
69+
:($locs => $gate) => quote
70+
Base.code_typed(
71+
$Intrinsics.apply,
72+
($YaoCompiler.AnyReg, typeof($gate), typeof($Locations($(locs))), typeof($CtrlLocations($ctrl)));
73+
interp=$(YaoInterpreter()),
74+
)
75+
end
76+
_ => error("expect a locs => gate expression")
77+
end
78+
end

test/interp.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ end
2828
return ret
2929
end
3030

31+
ci, type = @yao_code_typed(test_pure_quantum())[1]
32+
3133
op = test_pure_quantum()
3234
interp = YaoInterpreter()
33-
ci, type = code_typed(Intrinsics.apply, (AnyReg, typeof(op)); interp)[1]
35+
ci, type = @yao_code_typed(op)[1]
3436

3537
@test_codeinfo ci begin
3638
Expr(

0 commit comments

Comments
 (0)