Skip to content

Commit

Permalink
improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
exAClior committed Oct 6, 2023
1 parent dc51a7f commit 1336b2c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/parameter.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using .ZXW:Parameter, PiUnit, Factor
using .ZXW: Parameter, PiUnit, Factor

"""
Parameter
Expand Down
118 changes: 73 additions & 45 deletions src/planar_multigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,68 +122,96 @@ function Base.:(==)(pmg1::PlanarMultigraph{T}, pmg2::PlanarMultigraph{T}) where
return pmg_equiv(pmg1, pmg2, false)
end

function pmg_equiv(
pmg1::PlanarMultigraph{T},
pmg2::PlanarMultigraph{T},
verbose::Bool,
) 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
"""
print_nonoverlaping(dict1, dict2)
Helper function to print nonoverlaping elements in two dictionaries.
"""
function print_nonoverlaping(dict1, dict2)

Check warning on line 130 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L130

Added line #L130 was not covered by tests
# 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)

Check warning on line 135 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L132-L135

Added lines #L132 - L135 were not covered by tests
end
end

Check warning on line 137 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L137

Added line #L137 was not covered by tests

println()
println()

Check warning on line 139 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L139

Added line #L139 was not covered by tests

# 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
# 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)

Check warning on line 145 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L142-L145

Added lines #L142 - L145 were not covered by tests
end
end

Check warning on line 147 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L147

Added line #L147 was not covered by tests

end

"""
equiv_v2he(pmg1::PlanarMultigraph, pmg2::PlanarMultigraph)
Checking if the vertex to half edge mapping is equivalent.
"""
function equiv_v2he(pmg1::PlanarMultigraph, pmg2::PlanarMultigraph)
if length(keys(pmg1.v2he)) != length(keys(pmg2.v2he))
return false
end
function equiv_v2he(pmg1::PlanarMultigraph, pmg2::PlanarMultigraph)
if length(keys(pmg1.v2he)) != length(keys(pmg2.v2he))

for (v, _) in pmg1.v2he
if !haskey(pmg2.v2he, v)
return false

Check warning on line 163 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L163

Added line #L163 was not covered by tests
end

for (v, _) in pmg1.v2he
if !haskey(pmg2.v2he, v)
return false
end
if pmg2.v2he[v] trace_vertex(pmg1, v)
return false
end
if pmg2.v2he[v] trace_vertex(pmg1, v)
return false

Check warning on line 166 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L166

Added line #L166 was not covered by tests
end
return true
end
if !equiv_v2he(pmg1, pmg2)
if verbose
println("v2he")
print_nonoverlaping(pmg1.v2he, pmg2.v2he)
end
return true
end

"""
equiv_f2he(pmg1::PlanarMultigraph, pmg2::PlanarMultigraph)
Checking if the face to half edge mapping is equivalent.
"""
function equiv_f2he(pmg1::PlanarMultigraph, pmg2::PlanarMultigraph)
if length(keys(pmg1.f2he)) != length(keys(pmg2.f2he))
return false
end

function equiv_f2he(pmg1::PlanarMultigraph, pmg2::PlanarMultigraph)
if length(keys(pmg1.f2he)) != length(keys(pmg2.f2he))
for (f_id, _) in pmg1.f2he
if !haskey(pmg2.f2he, f_id)
return false

Check warning on line 184 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L184

Added line #L184 was not covered by tests
end
if pmg2.f2he[f_id] trace_face(pmg1, f_id)
return false

Check warning on line 187 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L187

Added line #L187 was not covered by tests
end
end
return true
end

"""
pmg_equiv(
pmg1::PlanarMultigraph{T},
pmg2::PlanarMultigraph{T},
verbose::Bool,
) where {T<:Integer}
Checking if two PlanarMultigraphs are equivalent.
for (f_id, _) in pmg1.f2he
if !haskey(pmg2.f2he, f_id)
return false
end
if pmg2.f2he[f_id] trace_face(pmg1, f_id)
return false
end
If verbose is true, then print out the nonoverlaping elements in the two.
"""
function pmg_equiv(
pmg1::PlanarMultigraph{T},
pmg2::PlanarMultigraph{T},
verbose::Bool,
) where {T<:Integer}
if !equiv_v2he(pmg1, pmg2)
if verbose
println("v2he")
print_nonoverlaping(pmg1.v2he, pmg2.v2he)

Check warning on line 212 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L210-L212

Added lines #L210 - L212 were not covered by tests
end
return true
return false

Check warning on line 214 in src/planar_multigraph.jl

View check run for this annotation

Codecov / codecov/patch

src/planar_multigraph.jl#L214

Added line #L214 was not covered by tests
end

if !equiv_f2he(pmg1, pmg2)
Expand Down
19 changes: 13 additions & 6 deletions src/zw_diagram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,23 @@ end
ZWDiagram(

Check warning on line 42 in src/zw_diagram.jl

View check run for this annotation

Codecov / codecov/patch

src/zw_diagram.jl#L42

Added line #L42 was not covered by tests
pmg::PlanarMultigraph{T},
st::Dict{T,ZWSpiderType},
P::Type = Rational,
) where {T} = ZWDiagram{T,P}(pmg, st)
scalar::Scalar{P},
) where {T,P} = ZWDiagram{T,P}(pmg, st, scalar)

Check warning on line 46 in src/zw_diagram.jl

View check run for this annotation

Codecov / codecov/patch

src/zw_diagram.jl#L46

Added line #L46 was not covered by tests

ZWDiagram(
pmg::PlanarMultigraph{T},
st::Vector{ZWSpiderType},
P::Type = Rational,
) where {T} = ZWDiagram{T,P}(pmg, Dict(zip(sort!(vertices(pmg)), st)))
scalar::Scalar{P},
) where {T,P} = ZWDiagram{T,P}(pmg, Dict(zip(sort!(vertices(pmg)), st)), scalar)

function ZWDiagram(nbits::T) where {T<:Integer}
"""
ZWDiagram(::Type{P}, nbits::T) where {T<:Integer}
Create a ZW-diagram with `nbits` input and output qubits.
Scalar is parameterized to be `P`.
"""
function ZWDiagram(nbits::T, ::Type{P} = Rational) where {T<:Integer,P}
nbits < 1 && error("We need to have at least one qubit in the system!")

half_edges = Dict{T,HalfEdge}()
Expand Down Expand Up @@ -172,7 +179,7 @@ function ZWDiagram(nbits::T) where {T<:Integer}

st = [i % 2 == 1 ? Input(div(i, 2) + 1) : Output(div(i, 2)) for i = 1:2*nbits]

return ZWDiagram(pmg, st)
return ZWDiagram(pmg, st, Scalar{P}())
end

Base.copy(zwd::ZWDiagram{T,P}) where {T,P} = ZWDiagram{T,P}(

Check warning on line 185 in src/zw_diagram.jl

View check run for this annotation

Codecov / codecov/patch

src/zw_diagram.jl#L185

Added line #L185 was not covered by tests
Expand Down
4 changes: 2 additions & 2 deletions src/zw_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function parameter(zwd::ZWDiagram{T,P}, v::T) where {T<:Integer,P}
binZ(p) => p
monoZ(p) => p
Input(q) || Output(q) => q
_ => Parameter(Val(:PiUnit), 0)
_ => nothing
end
end

Expand Down Expand Up @@ -181,7 +181,7 @@ end
add_spider!(zwd, spider, connect = [])
Add a new spider `spider` with appropriate parameter
connected to the half edges`connect`.
connected to the half edges `connect`.
Had to make halfedge class 1 citizen because there will be ambiguity
Consider A to B and there are multiple edges to A and from A to B
Expand Down
4 changes: 3 additions & 1 deletion test/zw_diagram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ using ZXCalculus.ZW: ZWDiagram, Input, Output
@test zx1.inputs == [1]
@test zx1.outputs == [2]
@test zx1.pmg == pmg1
@test zx1.scalar == Scalar{Rational}()

zx2 = ZWDiagram(2)
zx2 = ZWDiagram(2, Float64)
pmg2 = PlanarMultigraph(
Dict([1 => 1, 2 => 2, 3 => 3, 4 => 4]),
Dict([
Expand Down Expand Up @@ -48,6 +49,7 @@ using ZXCalculus.ZW: ZWDiagram, Input, Output
@test zx2.outputs == [2, 4]
@test zx2.pmg == pmg2
@test zx2.pmg.half_edges == pmg2.half_edges
@test zx2.scalar == Scalar{Float64}()

zx3 = ZWDiagram(3)

Expand Down

0 comments on commit 1336b2c

Please sign in to comment.