Skip to content

Commit

Permalink
fix insert_spider!
Browse files Browse the repository at this point in the history
  • Loading branch information
exAClior committed Sep 21, 2023
1 parent 229a200 commit 3e644b5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
47 changes: 35 additions & 12 deletions src/planar_multigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ Base.copy(g::PlanarMultigraph) = PlanarMultigraph(
)

function Base.:(==)(pmg1::PlanarMultigraph{T}, pmg2::PlanarMultigraph{T}) where {T<:Integer}
function print_nonoverlaping(dict1, dict2)
# Print elements in dict1 not in dict2
println("Elements in dict1 not in dict2:")
for (key, value) in dict1
if !haskey(dict2, key) || dict2[key] != value
println("Key: ", key, ", Value: ", value)
end
end

println()

# Print elements in dict2 not in dict1
println("Elements in dict2 not in dict1:")
for (key, value) in dict2
if !haskey(dict1, key) || dict1[key] != value
println("Key: ", key, ", Value: ", value)
end
end

end
if nv(pmg1) != nv(pmg2)
println("nv")
println(nv(pmg1))
Expand All @@ -138,25 +158,27 @@ function Base.:(==)(pmg1::PlanarMultigraph{T}, pmg2::PlanarMultigraph{T}) where
return false

Check warning on line 158 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L155-L158

Added lines #L155 - L158 were not covered by tests
end

# could be relaxed, idx might be different but content needs to be the same for HalfEdges
if pmg1.half_edges != pmg2.half_edges
println("HalfEdges")
print_nonoverlaping(pmg1.half_edges, pmg2.half_edges)

Check warning on line 163 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L162-L163

Added lines #L162 - L163 were not covered by tests
return false
end

if pmg1.next != pmg2.next
println("next")
println(pmg1.next)
println(pmg2.next)
print_nonoverlaping(pmg1.next, pmg2.next)
return false
end

if pmg1.twin != pmg2.twin
println("twin")
println(pmg1.twin)
println(pmg2.twin)
print_nonoverlaping(pmg1.twin, pmg2.twin)

Check warning on line 175 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L174-L175

Added lines #L174 - L175 were not covered by tests
return false
end

if pmg1.he2f != pmg2.he2f
println("he2f")
println(pmg1.he2f)
println(pmg2.he2f)
print_nonoverlaping(pmg1.he2f, pmg2.he2f)

Check warning on line 181 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L180-L181

Added lines #L180 - L181 were not covered by tests
return false
end

Expand Down Expand Up @@ -508,6 +530,7 @@ function split_vertex!(pmg::PlanarMultigraph{T}, h::T, g::T) where {T<:Integer}
gn = next(pmg, g)
hn = next(pmg, h)

tg = twin(pmg, g)
th = twin(pmg, h)

he_vec = trace_orbit(he -> σ_inv(pmg, he), th; rev = false)
Expand All @@ -525,13 +548,13 @@ function split_vertex!(pmg::PlanarMultigraph{T}, h::T, g::T) where {T<:Integer}
hes_id, _ = create_edge!(pmg, v2, v1)

for he in he_vec
set_dst!(pmg, he, v2)
(he == th) && break
set_dst!(pmg, twin(pmg, he), v2)
(he == tg) && break
end

set_next!(pmg, [h, g, hes_id...], [hes_id..., hn, gn])
set_face!(pmg, hes_id[1], hf; both = true)
set_face!(pmg, hes_id[2], gf; both = true)
set_next!(pmg, [g, h, hes_id...], [hes_id..., gn, hn])
set_face!(pmg, hes_id[1], gf; both = true)
set_face!(pmg, hes_id[2], hf; both = true)

return hes_id[1]
end
Expand Down
6 changes: 4 additions & 2 deletions src/zw_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,11 @@ function add_spider!(
end

"""
insert_spider!(zwd, he1,he2, spider)
insert_spider!(zwd, he1, spider)
Insert a spider `spider` with appropriate parameter on the half-edge `he1`.
Insert a spider `spider` with appropriate parameter on the half-edge prior to `he1`.
v1 <- he1 - v2 becomes
v1 <- he1 - v2 <- he_new - v_new
"""
function insert_spider!(
zwd::ZWDiagram{T,P},
Expand Down
16 changes: 8 additions & 8 deletions test/planar_multigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ end
pmg2 = PlanarMultigraph(
Dict(1 => 1, 2 => 3, 3 => 4, 4 => 5),
Dict(
1 => HalfEdge(1, 2),
2 => HalfEdge(2, 1),
1 => HalfEdge(1, 4),
2 => HalfEdge(4, 1),
3 => HalfEdge(2, 3),
4 => HalfEdge(3, 2),
5 => HalfEdge(4, 2),
Expand Down Expand Up @@ -516,8 +516,8 @@ end
4 => HalfEdge(3, 2),
5 => HalfEdge(3, 4),
6 => HalfEdge(4, 3),
7 => HalfEdge(4, 2),
8 => HalfEdge(2, 4),
7 => HalfEdge(2, 4),
8 => HalfEdge(4, 2),
),
Dict(0 => 1, 1 => 8),
Dict(1 => 0, 2 => 0, 3 => 1, 4 => 0, 5 => 1, 6 => 0, 7 => 0, 8 => 1),
Expand Down Expand Up @@ -764,10 +764,10 @@ end
Dict(
1 => HalfEdge(1, 2),
2 => HalfEdge(2, 1),
3 => HalfEdge(1, 2),
4 => HalfEdge(2, 1),
5 => HalfEdge(1, 2),
6 => HalfEdge(2, 1),
3 => HalfEdge(2, 1),
4 => HalfEdge(1, 2),
5 => HalfEdge(2, 1),
6 => HalfEdge(1, 2),
),
Dict(0 => 1, 1 => 3, 2 => 5),
Dict(1 => 0, 2 => 1, 4 => 1, 3 => 2, 6 => 2, 5 => 0),
Expand Down
16 changes: 9 additions & 7 deletions test/zw_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ using ZXCalculus.ZW:
get_output_idx,
add_power!,
add_global_phase!,
insert_spider!
insert_spider!,
neighbors
using ZXCalculus: trace_vertex
using ZXCalculus.ZXW: Parameter

@testset "utils" begin
Expand Down Expand Up @@ -65,7 +67,7 @@ end
zw = ZWDiagram(3)

pmg2 = PlanarMultigraph(
Dict(1 => 1, 2 => 16, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 2),
Dict(1 => 1, 7 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 2 => 15),
Dict(
1 => HalfEdge(1, 7),
2 => HalfEdge(7, 1),
Expand All @@ -84,7 +86,7 @@ end
15 => HalfEdge(7, 2),
16 => HalfEdge(2, 7),
),
Dict(0 => 2, 1 => 1, 3 => 2),
Dict(0 => 2, 1 => 1, 3 => 3),
Dict(
1 => 1,
16 => 0,
Expand Down Expand Up @@ -145,9 +147,9 @@ end
[0],
)

println(zw)
insert_spider!(zw, 2, ZW.binZ(Parameter(Val(:Factor), 2.0)))
insert_spider!(zw, 12, ZW.binZ(Parameter(Val(:Factor), 2.0)))
@test zw.pmg == pmg2
println(zw)
println(neighbors(zw.pmg))

set_phase!(zw, 7, Parameter(Val(:PiUnit), 1))
@test zw.st[7] == ZW.binZ(Parameter(Val(:PiUnit), 1))
end

0 comments on commit 3e644b5

Please sign in to comment.