Skip to content

Commit

Permalink
add reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed May 5, 2021
1 parent 89830ec commit 41bd2b0
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/YaoCompiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export @device,
AnyReg,
# reexport YaoLocations
Locations,
CtrlLocations
CtrlLocations,
# reflection
@yao_code_lowered,
@yao_code_typed

using MLStyle
using YaoAPI
Expand Down Expand Up @@ -61,6 +64,7 @@ include("compiler/printing.jl")
include("compiler/intrinsics.jl")
include("compiler/syntax.jl")
include("compiler/interp.jl")
include("compiler/reflection.jl")

include("codegen/llvmopt.jl")
include("codegen/native.jl")
Expand Down
78 changes: 78 additions & 0 deletions src/compiler/reflection.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
macro yao_code_lowered(ex)
esc(yao_code_lowered_m(ex))
end

macro yao_code_lowered(ctrl, ex)
esc(yao_code_lowered_m(ctrl, ex))
end

macro yao_code_typed(ex)
esc(yao_code_typed_m(ex))
end

macro yao_code_typed(ctrl, ex)
esc(yao_code_typed_m(ctrl, ex))
end

function yao_code_lowered_m(ex)
@match ex begin
# apply(reg, gate, locs)
:($locs => $gate) => quote
Base.code_lowered(
$Intrinsics.apply,
($YaoCompiler.AnyReg, typeof($gate), typeof($Locations($(locs))))
)
end
# apply(reg, gate)
_ => quote
Base.code_lowered($Intrinsics.apply, ($YaoCompiler.AnyReg, typeof($ex)))
end
end
end

function yao_code_lowered_m(ctrl, ex)
@match ex begin
# apply(reg, gate, locs, ctrl)
:($locs => $gate) => quote
Base.code_lowered(
$Intrinsics.apply,
($YaoCompiler.AnyReg, typeof($gate), typeof($Locations($(locs))), typeof($CtrlLocations($ctrl)))
)
end
_ => error("expect a locs => gate expression")
end
end

function yao_code_typed_m(ex)
@match ex begin
# apply(reg, gate, locs)
:($locs => $gate) => quote
$Base.code_typed(
$Intrinsics.apply,
($YaoCompiler.AnyReg, typeof($gate), typeof($Locations($(locs))));
interp=$(YaoInterpreter())
)
end
# apply(reg, gate)
_ => quote
$Base.code_typed(
$Intrinsics.apply, ($YaoCompiler.AnyReg, typeof($ex));
interp=$(YaoInterpreter()),
)
end
end
end

function yao_code_typed_m(ctrl, ex)
@match ex begin
# apply(reg, gate, locs, ctrl)
:($locs => $gate) => quote
Base.code_typed(
$Intrinsics.apply,
($YaoCompiler.AnyReg, typeof($gate), typeof($Locations($(locs))), typeof($CtrlLocations($ctrl)));
interp=$(YaoInterpreter()),
)
end
_ => error("expect a locs => gate expression")
end
end
4 changes: 3 additions & 1 deletion test/interp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ end
return ret
end

ci, type = @yao_code_typed(test_pure_quantum())[1]

op = test_pure_quantum()
interp = YaoInterpreter()
ci, type = code_typed(Intrinsics.apply, (AnyReg, typeof(op)); interp)[1]
ci, type = @yao_code_typed(op)[1]

@test_codeinfo ci begin
Expr(
Expand Down

0 comments on commit 41bd2b0

Please sign in to comment.