Skip to content

Commit

Permalink
construct variance ZXWDiagram test
Browse files Browse the repository at this point in the history
  • Loading branch information
exAClior committed Jul 25, 2023
1 parent caabc29 commit 4216c94
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/ZXCalculus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export push_gate!, pushfirst_gate!, tcount, insert_wtrig!
export convert_to_chain, convert_to_zxd
export rewrite!, simplify!, clifford_simplification, full_reduction,
circuit_extraction, phase_teleportation
export substitute_variables!
export substitute_variables!, expval_circ!, diff_diagram!

include("parameter.jl")
include("phase.jl")
Expand Down
12 changes: 6 additions & 6 deletions src/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Take derivative of ZXWDiagram with respect to a parameter
Assuming Spiders have Parameter of type PiUnit which is parameterized purely by θ
"""
function diff_diagram(zxwd::ZXWDiagram{T,P}, θ::Symbol) where {T,P}
function diff_diagram!(zxwd::ZXWDiagram{T,P}, θ::Symbol) where {T,P}
vs_pos = symbol_vertices(zxwd, θ)
vs_neg = symbol_vertices(zxwd, θ; neg = true)

Expand All @@ -29,7 +29,7 @@ function diff_diagram(zxwd::ZXWDiagram{T,P}, θ::Symbol) where {T,P}
x_v = add_spider!(zxwd, X(Parameter(Val(:PiUnit), 1.0)), [v])
w_v = add_spider!(zxwd, D, [x_v])
frac_v = @match spider_type(zxwd, v).p begin
PiUnit(pu, _) => add_spider!(zxwd, Z(Parameter(Val(:Factor), π)), [w_v])
PiUnit(pu, _) => add_spider!(zxwd, Z(Parameter(Val(:PiUnit), 1.0)), [w_v])
Factor(f, _) => error("Only supports PiUnit differentiation")
_ => error("not a valid parameter")

Check warning on line 34 in src/diff.jl

View check run for this annotation

Codecov / codecov/patch

src/diff.jl#L33-L34

Added lines #L33 - L34 were not covered by tests
end
Expand All @@ -44,9 +44,9 @@ function diff_diagram(zxwd::ZXWDiagram{T,P}, θ::Symbol) where {T,P}
end

"""
Take derivative with of Circuit with expectation of Hamiltonian H.
Construct ZXW Diagram for representing the expectation value circuit
"""
function diff_expval!(zxwd::ZXWDiagram{T,P}, H::String, θ::Symbol) where {T,P}
function expval_circ!(zxwd::ZXWDiagram{T,P}, H::String) where {T,P}
# convert U to U H U^\dagger
zxwd_dag = dagger(zxwd)
for (i, h) in enumerate(H)
Expand All @@ -63,7 +63,7 @@ function diff_expval!(zxwd::ZXWDiagram{T,P}, H::String, θ::Symbol) where {T,P}
end
end
concat!(zxwd, zxwd_dag)
return diff_diagram(zxwd, θ)
return zxwd
end

"""
Expand Down Expand Up @@ -151,7 +151,7 @@ function integrate4!(zxwd::ZXWDiagram{T,P}, loca::T, locb::T, locc::T, locd::T)

locm = add_spider!(zxwd, X(Parameter(Val(:PiUnit), 0)), [loca, locb])
locm = add_spider!(zxwd, D, [locm])
locm = add_spider!(zxwd, X(Parameter(Val(:PiUnit), π)), [locm])
locm = add_spider!(zxwd, X(Parameter(Val(:PiUnit), 1.0)), [locm])
add_spider!(zxwd, X(Parameter(Val(:PiUnit), 0)), [locm, locc, locd])
return zxwd
end
Expand Down
82 changes: 40 additions & 42 deletions test/diff.jl
Original file line number Diff line number Diff line change
@@ -1,53 +1,16 @@
using ZXCalculus: insert_spider!
using ZXCalculus: symbol_vertices, diff_diagram, dagger, concat!, diff_expval!, integrate!
using ZXCalculus: symbol_vertices, diff_diagram!, dagger, concat!, expval_circ!, integrate!

# @testset "diff" begin
# zxwd = ZXWDiagram(3)
# insert_spider!(zxwd, 1, 2, Z(Parameter(Val(:PiUnit), :a)))
# insert_spider!(zxwd, 7, 2, Z(Parameter(Val(:PiUnit), :a)))
# insert_spider!(zxwd, 3, 4, X(Parameter(Val(:PiUnit), :b)))
# insert_spider!(zxwd, 5, 6, Z(Parameter(Val(:PiUnit), 0.3)))
# insert_spider!(zxwd, 8, 2, Z(Parameter(Val(:PiUnit), Expr(:call, :-, :a))))

# @test sort!(symbol_vertices(zxwd, :a)) == [7, 8]
# @test symbol_vertices(zxwd, :a; neg = true) == [11]
# @test symbol_vertices(zxwd, :b) == [9]
# @test symbol_vertices(zxwd, :c) == []

# diff_expval!(copy(zxwd), "ZZZ", :a)

# diff_zxwd = diff_diagram(copy(zxwd), :a)
# # originally 11 spiders, plus 12 spiders from differentiation
# @test length(vertices(diff_zxwd.mg)) == 23

# zxwd_dg = dagger(zxwd)
# @test spider_type(zxwd_dg, 7).p.pu == Expr(:call, :-, :a)
# @test sort!(symbol_vertices(zxwd_dg, :a; neg = true)) == [7, 8]

# concat!(zxwd, zxwd_dg)

# @test length(vertices(zxwd.mg)) == 16

# integrate!(zxwd, [7, 11])
# @test length(vertices(zxwd.mg)) == 16

# # abuse, cann't actually integrate like this
# integrate!(zxwd, [7, 8, 11, 9])
# @test length(vertices(zxwd.mg)) == 25

# end

@testset "Example 27 - 30" begin
@testset "Example 28" begin
zxwd = ZXWDiagram(2)
push_gate!(zxwd, Val(:H), 1)
push_gate!(zxwd, Val(:H), 2)

push_gate!(zxwd, Val(:CZ), 1, 2)
push_gate!(zxwd, Val(:X), 1, :a; autoconvert = false)
push_gate!(zxwd, Val(:X), 2, :b; autoconvert = false)
push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false)
push_gate!(zxwd, Val(:X), 2, :b; autoconvert=false)

# since :c is not in the circuit, this will return UHU^\dagger
exp_zxwd = diff_expval!(copy(zxwd), "ZZ", :c)
exp_zxwd = expval_circ!(copy(zxwd), "ZZ")

exp_zxwd_sub = substitute_variables!(copy(exp_zxwd), Dict(:a => 0.3, :b => 0.4))
exp_val = Matrix(exp_zxwd_sub)[1, 1]
Expand Down Expand Up @@ -80,3 +43,38 @@ using ZXCalculus: symbol_vertices, diff_diagram, dagger, concat!, diff_expval!,
@test exp_val exp_yao

end

@testset "Lemma 30" begin
# to calculate variance, we double the circuit
zxwd = ZXWDiagram(4)

push_gate!(zxwd, Val(:H), 1)
push_gate!(zxwd, Val(:H), 2)
push_gate!(zxwd, Val(:H), 3)
push_gate!(zxwd, Val(:H), 4)
push_gate!(zxwd, Val(:CZ), 1, 2)
push_gate!(zxwd, Val(:CZ), 3, 4)
push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false)
push_gate!(zxwd, Val(:X), 2, :b; autoconvert=false)
push_gate!(zxwd, Val(:X), 3, :a; autoconvert=false)
push_gate!(zxwd, Val(:X), 4, :c; autoconvert=false)

exp_zxwd = expval_circ!(copy(zxwd), "ZZZZ")
exp_zxwd = diff_diagram!(exp_zxwd, :b)
exp_zxwd = diff_diagram!(exp_zxwd, :c)


posb = ZXCalculus.symbol_vertices(exp_zxwd, :b; neg=false)[1]
posbminus = ZXCalculus.symbol_vertices(exp_zxwd, :b; neg=true)[1]

posc = ZXCalculus.symbol_vertices(exp_zxwd, :c; neg=false)[1]
poscminus = ZXCalculus.symbol_vertices(exp_zxwd, :c; neg=true)[1]

integrated_zxwd = ZXCalculus.integrate!(exp_zxwd, [posb, posbminus, posc, poscminus])


integrated_zxwd = substitute_variables!(integrated_zxwd, Dict(:a => 0.0))

integrated_mtx = Matrix(integrated_zxwd)

end

0 comments on commit 4216c94

Please sign in to comment.