Skip to content
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

Merged
merged 24 commits into from Aug 30, 2020

Conversation

ChenZhao44
Copy link
Member

@ChenZhao44 ChenZhao44 commented Jun 30, 2020

  • YaoIR to ZXDiagram
  • ZXDiagram to YaoIR

@ChenZhao44 ChenZhao44 changed the title conversion from YaoIR to ZXDiagram for pure quantum conversion between YaoIR and ZXDiagram for pure quantum Jul 1, 2020
@ChenZhao44 ChenZhao44 requested a review from Roger-luo July 1, 2020 09:01
return ir
end

function to_YaoIR(circ::ZXDiagram{T, P}) where {T, P}
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

return
end
lowered_ast = Meta.lower(@__MODULE__, ex)
ir = mark_quantum(IRTools.IR(lowered_ast.args[], 0))
Copy link
Member

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.

Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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.

$(Expr(:meta, :register, :new, gensym(:register)))
return
end
lowered_ast = Meta.lower(@__MODULE__, ex)
Copy link
Member

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.

@@ -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")
Copy link
Member

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"
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

@ChenZhao44 ChenZhao44 Jul 2, 2020

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.

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)
Copy link
Member

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.

Copy link
Member

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

@@ -1,14 +1,72 @@
using ZXCalculus
using ZXCalculus, LightGraphs
using ZXCalculus: qubit_loc

function optimize!(ir::YaoIR)
Copy link
Member

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.

@Roger-luo
Copy link
Member

@device optimize=[:zx_clifford, :zx_teleport] function circuit()
end

@@ -0,0 +1,167 @@
using ZXCalculus
using ZXCalculus: qubit_loc, clifford_simplification
import IRTools: IR
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Roger-luo Roger-luo merged commit a279099 into master Aug 30, 2020
@Roger-luo Roger-luo deleted the simplification branch August 30, 2020 02:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants