-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
conversion between YaoIR and ZXDiagram for pure quantum #41
Conversation
ChenZhao44
commented
Jun 30, 2020
•
edited
edited
- YaoIR to ZXDiagram
- ZXDiagram to YaoIR
src/compiler/zx_calculus.jl
Outdated
return ir | ||
end | ||
|
||
function to_YaoIR(circ::ZXDiagram{T, P}) where {T, P} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could just define a constructor of YaoIR
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function only generates an IR not a YaoIR. It should be renamed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but you reused some code from YaoIR constructor. It'd be better to make this either a constructor of YaoIR
or a constructor for IR
. to_<node>
convention is used specifically for the parser side in YaoLang.
src/compiler/zx_calculus.jl
Outdated
return | ||
end | ||
lowered_ast = Meta.lower(@__MODULE__, ex) | ||
ir = mark_quantum(IRTools.IR(lowered_ast.args[], 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could segment fault, if you don't update slots.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mark quantum is not necessary here if you construct the YaoIR
object body directly from an empty IR
object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty IR
has no register and return. So I use these codes for generating them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can push!
these two argument as a statement and branch to IR
. This will cause a lot extra work and make the compilation slow.
src/compiler/zx_calculus.jl
Outdated
$(Expr(:meta, :register, :new, gensym(:register))) | ||
return | ||
end | ||
lowered_ast = Meta.lower(@__MODULE__, ex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't have to go through the parser again. you can use IR()
to construct an empty IR object directly, and then insert SSA statements.
test/compiler/zx_calculus.jl
Outdated
@@ -1,6 +1,7 @@ | |||
using YaoLang | |||
using YaoLang.Compiler | |||
using YaoLang.Compiler: device_m, enable_timings, timings | |||
using YaoLang.Compiler: to_ZX_diagram, to_YaoIR, optimize!, clifford_simplify! | |||
using IRTools | |||
|
|||
include("../../../ZXCalculus/script/zx_plot.jl") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remember to remove this.
Project.toml
Outdated
@@ -6,6 +6,7 @@ version = "0.2.1" | |||
[deps] | |||
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" | |||
IRTools = "7869d1d1-7146-5819-86e3-90919afe41df" | |||
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we only need push_gate!
functions from ZXCalculus, shouldn't we only need the dependency of ZXCalculus here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we move what should be inside ZXCalculus first, or updating it would require a 2nd major version update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LightGraphs is needed because we need neighbors(zxd, v)
here.
src/compiler/zx_calculus.jl
Outdated
for q = 1:nqubit | ||
if frontier_v[q] <= length(lo.spider_seq[q]) | ||
v = lo.spider_seq[q][frontier_v[q]] | ||
nb = neighbors(circ, v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just export neighbors
from ZXCalculus.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or simply use ZXCalculus.neighbors
src/compiler/zx_calculus.jl
Outdated
@@ -1,14 +1,72 @@ | |||
using ZXCalculus | |||
using ZXCalculus, LightGraphs | |||
using ZXCalculus: qubit_loc | |||
|
|||
function optimize!(ir::YaoIR) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optimize!
should use a compiler context to dispatch optimize methods, since zx calculus won't be the only optimization strategy and can be disabled in a low optimization context. I'll rewrite this part later.
@device optimize=[:zx_clifford, :zx_teleport] function circuit()
end |
@@ -0,0 +1,167 @@ | |||
using ZXCalculus | |||
using ZXCalculus: qubit_loc, clifford_simplification | |||
import IRTools: IR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use import
, if you need to overload a function write it explicitly IRTools.IR
end | ||
new_ir = YaoIR(ir.mod, ir.name, ir.args, ir.whereparams, IR(circ), | ||
ir.quantum_blocks, ir.pure_quantum, ir.qasm_compatible) | ||
return new_ir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.