Skip to content

Commit

Permalink
add pi rule for ZXW Calculus
Browse files Browse the repository at this point in the history
  • Loading branch information
exAClior committed Dec 5, 2023
1 parent bef0cb5 commit 0590887
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/ZXW/zxw_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ function Base.match(::Rule{:b1}, zxwd::ZXWDiagram{T,P}) where {T,P}
return matches
end

function Base.match(::Rule{:pi}, zxwd::ZXWDiagram{T, P}) where {T, P}
matches = Match{T}[]
for v1 in spiders(zxwd)
degree(zxwd, v1) != 2 && continue
for v2 in neighbors(zxwd, v1)
res = @match (spider_type(zxwd,v1), spider_type(zxwd,v2)) begin
(X(p1), Z(p2)) && if p1 == one(P) end => Match{T}([v1, v2])
_ => nothing
end
isnothing(res) || push!(matches, res)
end
end
return matches
end

struct CalcRule{L} <: AbstractRule
var::Symbol
end
Expand Down Expand Up @@ -152,6 +167,22 @@ function rewrite!(::Rule{:b1}, zxwd::ZXWDiagram{T,P}, vs::Vector{T}) where {T,P}
return zxwd
end

function rewrite!(r::Rule{:pi}, zxwd::ZXWDiagram{T, P}, vs::Vector{T}) where {T, P}
v1, v2 = vs
add_global_phase!(zxwd, parameter(zxwd, v2))
set_phase!(zxwd, v2, -parameter(zxwd, v2))
nb = neighbors(zxwd, v2, count_mul = true)
for v3 in nb
# TODO
v3 != v1 && insert_spider!(zxwd, v2, v3, X(parameter(zxwd, v1)))
end
if neighbors(zxwd, v1) != [v2]
add_edge!(zxwd, neighbors(zxwd, v1))
rem_spider!(zxwd, v1)
end
return zxwd
end

function rewrite!(::CalcRule{:diff}, zxwd::ZXWDiagram{T,P}, vs::Vector{T}) where {T,P}
zero_loc = findfirst(x -> x == zero(T), vs)

Expand Down Expand Up @@ -392,3 +423,14 @@ function check_rule(r::CalcRule{:int}, zxwd::ZXWDiagram{T,P}, vs::Vector{T}) whe
end
return true
end

function check_rule(r::Rule{:pi}, zxwd::ZXWDiagram{T, P}, vs::Vector{T}) where {T, P}
v1, v2 = vs
(has_vertex(zxwd.mg, v1) && has_vertex(zxwd.mg, v2)) || return false
(degree(zxwd, v1)) == 2 || return false

return @match (spider_type(zxwd, v1), spider_type(zxwd, v2)) begin
(X(p1), Z(p2)) && if p1 == one(P) end => true
_ => false
end
end

0 comments on commit 0590887

Please sign in to comment.