Skip to content

Commit a1f66e0

Browse files
committed
ZToXRule
1 parent fd2026b commit a1f66e0

3 files changed

Lines changed: 28 additions & 31 deletions

File tree

src/ZX/ZX.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ include("implementations/zx_circuit/phase_tracking.jl")
6666
# Rules and algorithms
6767
export AbstractRule
6868
export Rule, Match
69-
export FusionRule, XToZRule, Identity1Rule, HBoxRule,
69+
export FusionRule, XToZRule, ZToXRule, Identity1Rule, HBoxRule,
7070
PiRule, CopyRule, BialgebraRule,
7171
LocalCompRule, Pivot1Rule, Pivot2Rule, Pivot3Rule,
7272
PivotBoundaryRule, PivotGadgetRule,

src/ZX/interfaces/calculus_interface.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ phase(circ::AbstractZXCircuit, v) = phase(base_zx_graph(circ), v)
5959

6060
# Spider manipulation
6161

62+
"""
63+
$(TYPEDSIGNATURES)
64+
65+
Set the spider type of vertex `v` to `st` in the ZX-diagram.
66+
"""
67+
set_spider_type!(::AbstractZXDiagram, v, st) = error("set_spider_type! not implemented")
68+
set_spider_type!(circ::AbstractZXCircuit, v, st) = set_spider_type!(base_zx_graph(circ), v, st)
69+
6270
"""
6371
$(TYPEDSIGNATURES)
6472

src/ZX/rules/color.jl

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
struct XToZRule <: AbstractRule end
2+
struct ZToXRule <: AbstractRule end
23

3-
function Base.match(::XToZRule, zxd::ZXDiagram{T, P}) where {T, P}
4+
Base.match(::XToZRule, zxd::ZXDiagram{T, P}) where {T, P} = match_spider_type(zxd, SpiderType.X)
5+
Base.match(::XToZRule, zxd::ZXGraph{T, P}) where {T, P} = match_spider_type(zxd, SpiderType.X)
6+
Base.match(::ZToXRule, zxd::ZXDiagram{T, P}) where {T, P} = match_spider_type(zxd, SpiderType.Z)
7+
Base.match(::ZToXRule, zxd::ZXGraph{T, P}) where {T, P} = match_spider_type(zxd, SpiderType.Z)
8+
function match_spider_type(zxd::AbstractZXDiagram{T, P}, st::SpiderType.SType) where {T, P}
49
matches = Match{T}[]
510
for v1 in spiders(zxd)
6-
if spider_type(zxd, v1) == SpiderType.X
11+
if spider_type(zxd, v1) == st
712
push!(matches, Match{T}([v1]))
813
end
914
end
1015
return matches
1116
end
1217

13-
function check_rule(::XToZRule, zxd::ZXDiagram{T, P}, vs::Vector{T}) where {T, P}
18+
check_rule(::XToZRule, zxd::ZXDiagram{T, P}, vs::Vector{T}) where {T, P} = check_spider_type(zxd, vs, SpiderType.X)
19+
check_rule(::XToZRule, zxg::ZXGraph{T, P}, vs::Vector{T}) where {T, P} = check_spider_type(zxg, vs, SpiderType.X)
20+
check_rule(::ZToXRule, zxd::ZXDiagram{T, P}, vs::Vector{T}) where {T, P} = check_spider_type(zxd, vs, SpiderType.Z)
21+
check_rule(::ZToXRule, zxg::ZXGraph{T, P}, vs::Vector{T}) where {T, P} = check_spider_type(zxg, vs, SpiderType.Z)
22+
23+
function check_spider_type(zxd::AbstractZXDiagram{T, P}, vs::Vector{T}, st::SpiderType.SType) where {T, P}
1424
@inbounds v1 = vs[1]
1525
has_vertex(zxd.mg, v1) || return false
16-
if spider_type(zxd, v1) == SpiderType.X
17-
return true
18-
end
19-
return false
26+
return spider_type(zxd, v1) == st
2027
end
2128

2229
function rewrite!(::XToZRule, zxd::ZXDiagram{T, P}, vs::Vector{T}) where {T, P}
@@ -30,32 +37,14 @@ function rewrite!(::XToZRule, zxd::ZXDiagram{T, P}, vs::Vector{T}) where {T, P}
3037
return zxd
3138
end
3239

33-
function Base.match(::XToZRule, zxg::ZXGraph{T, P}) where {T, P}
34-
matches = Match{T}[]
35-
for v1 in spiders(zxg)
36-
if spider_type(zxg, v1) == SpiderType.X
37-
push!(matches, Match{T}([v1]))
38-
end
39-
end
40-
return matches
41-
end
42-
43-
function check_rule(::XToZRule, zxg::ZXGraph{T, P}, vs::Vector{T}) where {T, P}
44-
@inbounds v1 = vs[1]
45-
has_vertex(zxg.mg, v1) || return false
46-
if spider_type(zxg, v1) == SpiderType.X
47-
return true
48-
end
49-
return false
50-
end
51-
52-
function rewrite!(::XToZRule, zxg::ZXGraph{T, P}, vs::Vector{T}) where {T, P}
40+
function rewrite!(::Union{XToZRule, ZToXRule}, zxg::ZXGraph{T, P}, vs::Vector{T}) where {T, P}
5341
@inbounds v1 = vs[1]
54-
set_spider_type!(zxg, v1, SpiderType.Z)
42+
st = spider_type(zxg, v1) == SpiderType.X ? SpiderType.Z : SpiderType.X
43+
set_spider_type!(zxg, v1, st)
5544
for v2 in neighbors(zxg, v1)
5645
if v2 != v1
57-
et = edge_type(zxg, v1, v2)
58-
set_edge_type!(zxg, v1, v2, et === EdgeType.SIM ? EdgeType.HAD : EdgeType.SIM)
46+
et = edge_type(zxg, v1, v2) === EdgeType.SIM ? EdgeType.HAD : EdgeType.SIM
47+
set_edge_type!(zxg, v1, v2, et)
5948
end
6049
end
6150
return zxg

0 commit comments

Comments
 (0)