Skip to content

Commit 8a99695

Browse files
committed
polish docstrings
1 parent 1ead3b8 commit 8a99695

9 files changed

Lines changed: 147 additions & 39 deletions

File tree

src/ZX/circuit_extraction.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
using DocStringExtensions
2+
13
"""
2-
ancilla_extraction(zxg::ZXGraph) -> ZXDiagram
4+
$(TYPEDSIGNATURES)
5+
6+
Extract a quantum circuit from a general ZX-graph even without a gflow.
7+
8+
This function can handle ZX-graphs that don't have a generalised flow (gflow),
9+
and will introduce post-selection operators as needed.
310
4-
Extract a quantum circuit from a general `ZXGraph` even without a gflow.
5-
It will introduce post-selection operators.
11+
Returns a `ZXDiagram` representing the extracted circuit.
612
"""
713
function ancilla_extraction(zxg::Union{ZXGraph, ZXCircuit})
814
nzxg = copy(zxg)
@@ -172,9 +178,14 @@ function update_frontier_ancilla!(frontiers, nzxg, gads, qubit_map, unextracts,
172178
end
173179

174180
"""
175-
circuit_extraction(zxg::ZXGraph)
181+
$(TYPEDSIGNATURES)
182+
183+
Extract a quantum circuit from a graph-like ZX-diagram.
184+
185+
This is the main circuit extraction algorithm that converts a ZX-diagram
186+
in graph-like form into an equivalent quantum circuit.
176187
177-
Extract circuit from a graph-like ZX-diagram.
188+
Returns a `ZXDiagram` representing the extracted circuit.
178189
"""
179190
function circuit_extraction(zxg::Union{ZXGraph{T, P}, ZXCircuit{T, P}}) where {T, P}
180191
nzxg = copy(zxg)

src/ZX/equality.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
using DocStringExtensions
2+
13
"""
2-
verify_equality(zxd_1::ZXDiagram, zxd_2::ZXDiagram)
4+
$(TYPEDSIGNATURES)
5+
6+
Check the equivalence of two different ZX-diagrams.
7+
8+
This function concatenates the first diagram with the dagger of the second,
9+
then performs full reduction. If the result contains only bare wires, the
10+
diagrams are equivalent.
311
4-
checks the equivalence of two different ZXDiagrams
12+
Returns `true` if the diagrams are equivalent, `false` otherwise.
513
"""
614
function verify_equality(zxd_1::ZXDiagram, zxd_2::ZXDiagram)
715
merged_diagram = copy(zxd_1)

src/ZX/implementations/zx_circuit/type.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1+
using DocStringExtensions
2+
3+
"""
4+
$(TYPEDEF)
5+
6+
This is the type for representing ZX-circuits with explicit circuit structure.
7+
8+
ZXCircuit separates the graph representation (ZXGraph) from circuit semantics
9+
(ordered inputs/outputs and layout). This design enables efficient graph-based
10+
simplification while maintaining circuit structure for extraction and visualization.
11+
12+
# Fields
13+
$(TYPEDFIELDS)
14+
"""
115
struct ZXCircuit{T, P} <: AbstractZXCircuit{T, P}
16+
"The underlying ZXGraph representation"
217
zx_graph::ZXGraph{T, P}
18+
"Ordered input spider vertices"
319
inputs::Vector{T}
20+
"Ordered output spider vertices"
421
outputs::Vector{T}
22+
"Layout information for visualization"
523
layout::ZXLayout{T}
624

7-
# maps a vertex id to its master id and scalar multiplier
25+
"Maps vertex id to its master id and scalar multiplier for phase tracking"
826
phase_ids::Dict{T, Tuple{T, Int}}
27+
"Reference to master circuit for phase tracking (optional)"
928
master::Union{Nothing, ZXCircuit{T, P}}
1029
end
1130

src/ZX/implementations/zx_diagram/type.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using DocStringExtensions
2+
13
"""
2-
ZXDiagram{T, P}
4+
$(TYPEDEF)
35
46
This is the type for representing ZX-diagrams.
57
@@ -10,18 +12,29 @@ This is the type for representing ZX-diagrams.
1012
1113
`ZXCircuit` provides the same functionality with better separation of concerns
1214
and more efficient graph-based simplification algorithms.
15+
16+
# Fields
17+
$(TYPEDFIELDS)
1318
"""
1419
struct ZXDiagram{T <: Integer, P <: AbstractPhase} <: AbstractZXCircuit{T, P}
20+
"The underlying multigraph structure"
1521
mg::Multigraph{T}
1622

23+
"Spider types indexed by vertex"
1724
st::Dict{T, SpiderType.SType}
25+
"Phases of spiders indexed by vertex"
1826
ps::Dict{T, P}
1927

28+
"Layout information for visualization"
2029
layout::ZXLayout{T}
30+
"Maps vertex id to its master id and scalar multiplier"
2131
phase_ids::Dict{T, Tuple{T, Int}}
2232

33+
"Global scalar factor"
2334
scalar::Scalar{P}
35+
"Ordered input spider vertices"
2436
inputs::Vector{T}
37+
"Ordered output spider vertices"
2538
outputs::Vector{T}
2639

2740
function ZXDiagram{T, P}(

src/ZX/implementations/zx_graph/type.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1+
using DocStringExtensions
2+
13
"""
2-
ZXGraph{T, P}
4+
$(TYPEDEF)
35
46
This is the type for representing the graph-like ZX-diagrams.
57
68
A pure graph representation without circuit structure assumptions.
79
Spiders of type In/Out can exist but are treated as searchable vertices,
810
not as ordered inputs/outputs.
11+
12+
# Fields
13+
$(TYPEDFIELDS)
914
"""
1015
struct ZXGraph{T <: Integer, P <: AbstractPhase} <: AbstractZXDiagram{T, P}
16+
"The underlying multigraph structure"
1117
mg::Multigraph{T}
18+
"Phases of spiders indexed by vertex"
1219
ps::Dict{T, P}
20+
"Spider types indexed by vertex"
1321
st::Dict{T, SpiderType.SType}
22+
"Edge types indexed by sorted vertex pairs"
1423
et::Dict{Tuple{T, T}, EdgeType.EType}
24+
"Global scalar factor"
1525
scalar::Scalar{P}
1626
end
1727

src/ZX/phase_teleportation.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
using DocStringExtensions
2+
13
"""
2-
phase_teleportation(zxd)
4+
$(TYPEDSIGNATURES)
5+
6+
Reduce T-count of `zxd` with the phase teleportation algorithms in [arXiv:1903.10477](https://arxiv.org/abs/1903.10477).
7+
8+
This optimization technique reduces the number of non-Clifford (T) gates in the circuit
9+
by teleporting phases through the diagram.
310
4-
Reducing T-count of `zxd` with the algorithms in [arXiv:1903.10477](https://arxiv.org/abs/1903.10477).
11+
Returns a ZX-diagram with reduced T-count.
512
"""
613
function phase_teleportation(cir::ZXDiagram{T, P}) where {T, P}
714
zxg = ZXCircuit(cir; track_phase=true, normalize=true)

src/ZX/rules/interface.jl

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using DocStringExtensions
2+
13
abstract type AbstractRule end
24

35
"""
@@ -32,27 +34,35 @@ struct Rule{L} <: AbstractRule end
3234
Rule(r::Symbol) = Rule{r}()
3335

3436
"""
35-
Match{T<:Integer}
37+
$(TYPEDEF)
38+
39+
A struct for saving matched vertices from rule matching.
3640
37-
A struct for saving matched vertices.
41+
# Fields
42+
$(TYPEDFIELDS)
3843
"""
3944
struct Match{T <: Integer}
45+
"Vector of vertex identifiers that match a rule pattern"
4046
vertices::Vector{T}
4147
end
4248

4349
"""
44-
match(r, zxd)
50+
$(TYPEDSIGNATURES)
51+
52+
Find all vertices in ZX-diagram `zxd` that match the pattern of rule `r`.
4553
46-
Returns all matched vertices, which will be stored in struct `Match`, for rule `r`
47-
in a ZX-diagram `zxd`.
54+
Returns a vector of `Match` objects containing the matched vertex sets.
4855
"""
4956
Base.match(r::AbstractRule, ::AbstractZXDiagram{T, P}) where {T, P} = error("match not implemented for rule $(r)")
5057

5158
"""
52-
rewrite!(r, zxd, matches)
59+
$(TYPEDSIGNATURES)
60+
61+
Rewrite a ZX-diagram `zxd` with rule `r` for all vertices in `matches`.
62+
63+
The `matches` parameter can be a vector of `Match` objects or a single `Match` instance.
5364
54-
Rewrite a ZX-diagram `zxd` with rule `r` for all vertices in `matches`. `matches`
55-
can be a vector of `Match` or just an instance of `Match`.
65+
Returns the modified ZX-diagram.
5666
"""
5767
function rewrite!(r::AbstractRule, zxd::AbstractZXDiagram{T, P}, matches::Vector{Match{T}}) where {T, P}
5868
for each in matches
@@ -70,18 +80,27 @@ function rewrite!(r::AbstractRule, zxd::AbstractZXDiagram{T, P}, matched::Match{
7080
end
7181

7282
"""
73-
rewrite!(r, zxd, vs)
83+
$(TYPEDSIGNATURES)
7484
75-
Rewrite a ZX-diagram `zxd` with rule `r` for vertices `vs`.
85+
Rewrite a ZX-diagram `zxd` with rule `r` for the specific vertices `vs`.
86+
87+
This is the core rewriting function that must be implemented for each rule.
88+
89+
Returns the modified ZX-diagram.
7690
"""
7791
function rewrite!(r::AbstractRule, ::AbstractZXDiagram{T, P}, ::Vector{T}) where {T, P}
7892
return error("rewrite! not implemented for rule $(r)!")
7993
end
8094

8195
"""
82-
check_rule(r, zxd, vs)
96+
$(TYPEDSIGNATURES)
8397
8498
Check whether the vertices `vs` in ZX-diagram `zxd` still match the rule `r`.
99+
100+
This is used to verify that a previously matched pattern is still valid before rewriting,
101+
as the diagram may have changed since the match was found.
102+
103+
Returns `true` if the vertices still match the rule pattern, `false` otherwise.
85104
"""
86105
function check_rule(r::AbstractRule, ::AbstractZXDiagram{T, P}, ::Vector{T}) where {T, P}
87106
return error("check_rule not implemented for rule $(r)!")

src/ZX/simplify.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
using DocStringExtensions
2+
13
const MAX_ITERATION = Ref{Int}(1000)
24

35
"""
4-
replace!(r, zxd)
6+
$(TYPEDSIGNATURES)
57
68
Match and replace with the rule `r`.
9+
10+
Returns the modified ZX-diagram.
711
"""
812
function Base.replace!(r::AbstractRule, zxd::AbstractZXDiagram)
913
matches = match(r, zxd)
@@ -12,9 +16,13 @@ function Base.replace!(r::AbstractRule, zxd::AbstractZXDiagram)
1216
end
1317

1418
"""
15-
simplify!(r, zxd)
19+
$(TYPEDSIGNATURES)
1620
1721
Simplify `zxd` with the rule `r`.
22+
23+
Repeatedly applies the rule until no more matches are found or MAX_ITERATION is reached.
24+
25+
Returns the simplified ZX-diagram.
1826
"""
1927
function simplify!(r::AbstractRule, zxd::AbstractZXDiagram)
2028
i = 1
@@ -39,9 +47,14 @@ function to_z_form!(zxg::Union{ZXGraph, ZXCircuit})
3947
end
4048

4149
"""
42-
clifford_simplification(zxd)
50+
$(TYPEDSIGNATURES)
51+
52+
Simplify `zxd` with the Clifford simplification algorithms in [arXiv:1902.03178](https://arxiv.org/abs/1902.03178).
53+
54+
This applies a sequence of local complementation and pivot rules to simplify the ZX-diagram
55+
while preserving Clifford structure.
4356
44-
Simplify `zxd` with the algorithms in [arXiv:1902.03178](https://arxiv.org/abs/1902.03178).
57+
Returns the simplified ZX-diagram.
4558
"""
4659
function clifford_simplification(circ::ZXDiagram)
4760
zxg = ZXCircuit(circ; track_phase=true, normalize=true)

src/ZX/types/zx_layout.jl

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
using DocStringExtensions
2+
13
"""
2-
ZXLayout
4+
$(TYPEDEF)
5+
6+
A struct for the layout information of ZX-circuits.
37
4-
A struct for the layout information of `ZXDiagram` and `ZXGraph`.
8+
# Fields
9+
$(TYPEDFIELDS)
510
"""
611
struct ZXLayout{T <: Integer}
12+
"Number of qubits in the circuit"
713
nbits::Int
14+
"Mapping from spider vertices to qubit locations"
815
spider_q::Dict{T, Rational{Int}}
16+
"Mapping from spider vertices to column positions"
917
spider_col::Dict{T, Rational{Int}}
1018
end
1119

@@ -24,43 +32,43 @@ end
2432
nqubits(layout::ZXLayout) = layout.nbits
2533

2634
"""
27-
qubit_loc(layout, v)
35+
$(TYPEDSIGNATURES)
2836
29-
Return the qubit number corresponding to the spider `v`.
37+
Return the qubit number corresponding to the spider `v`, or `nothing` if not in layout.
3038
"""
3139
qubit_loc(layout::ZXLayout{T}, v::T) where T = get(layout.spider_q, v, nothing)
3240

3341
"""
34-
column_loc(layout, v)
42+
$(TYPEDSIGNATURES)
3543
36-
Return the column number corresponding to the spider `v`.
44+
Return the column number corresponding to the spider `v`, or `nothing` if not in layout.
3745
"""
3846
column_loc(layout::ZXLayout{T}, v::T) where T = get(layout.spider_col, v, nothing)
3947

4048
"""
41-
set_qubit!(layout, v, q)
49+
$(TYPEDSIGNATURES)
4250
43-
Set the qubit number of the spider `v`.
51+
Set the qubit number of the spider `v` to `q`.
4452
"""
4553
function set_qubit!(layout::ZXLayout{T}, v::T, q) where T
4654
layout.spider_q[v] = q
4755
return layout
4856
end
4957

5058
"""
51-
set_qubit!(layout, v, q)
59+
$(TYPEDSIGNATURES)
5260
53-
Set the column number of the spider `v`.
61+
Set the column number of the spider `v` to `col`.
5462
"""
5563
function set_column!(layout::ZXLayout{T}, v::T, col) where T
5664
layout.spider_col[v] = col
5765
return layout
5866
end
5967

6068
"""
61-
set_loc!(layout, v, q, col)
69+
$(TYPEDSIGNATURES)
6270
63-
Set the location of the spider `v`.
71+
Set both the qubit and column location of the spider `v`.
6472
"""
6573
function set_loc!(layout::ZXLayout{T}, v::T, q, col) where T
6674
set_qubit!(layout, v, q)

0 commit comments

Comments
 (0)