Skip to content

Commit 47d4889

Browse files
committed
update docs
1 parent 45f97b4 commit 47d4889

File tree

4 files changed

+288
-267
lines changed

4 files changed

+288
-267
lines changed

docs/src/examples.md

Lines changed: 124 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ This example can be found in the appendix of [Clifford simplification](https://a
1010
using ZXCalculus
1111

1212
function generate_example_1()
13-
zxd = ZXDiagram(4)
14-
push_gate!(zxd, Val(:Z), 1, 3//2)
15-
push_gate!(zxd, Val(:H), 1)
16-
push_gate!(zxd, Val(:Z), 1, 1//2)
17-
push_gate!(zxd, Val(:H), 4)
18-
push_gate!(zxd, Val(:CZ), 4, 1)
19-
push_gate!(zxd, Val(:CNOT), 1, 4)
20-
push_gate!(zxd, Val(:H), 1)
21-
push_gate!(zxd, Val(:H), 4)
22-
push_gate!(zxd, Val(:Z), 1, 1//4)
23-
push_gate!(zxd, Val(:Z), 4, 3//2)
24-
push_gate!(zxd, Val(:X), 4, 1//1)
25-
push_gate!(zxd, Val(:H), 1)
26-
push_gate!(zxd, Val(:Z), 4, 1//2)
27-
push_gate!(zxd, Val(:X), 4, 1//1)
28-
push_gate!(zxd, Val(:Z), 2, 1//2)
29-
push_gate!(zxd, Val(:CNOT), 3, 2)
30-
push_gate!(zxd, Val(:H), 2)
31-
push_gate!(zxd, Val(:CNOT), 3, 2)
32-
push_gate!(zxd, Val(:Z), 2, 1//4)
33-
push_gate!(zxd, Val(:Z), 3, 1//2)
34-
push_gate!(zxd, Val(:H), 2)
35-
push_gate!(zxd, Val(:H), 3)
36-
push_gate!(zxd, Val(:Z), 3, 1//2)
37-
push_gate!(zxd, Val(:CNOT), 3, 2)
38-
39-
return zxd
13+
zxc = ZXCircuit(4)
14+
push_gate!(zxc, Val(:Z), 1, 3//2)
15+
push_gate!(zxc, Val(:H), 1)
16+
push_gate!(zxc, Val(:Z), 1, 1//2)
17+
push_gate!(zxc, Val(:H), 4)
18+
push_gate!(zxc, Val(:CZ), 4, 1)
19+
push_gate!(zxc, Val(:CNOT), 1, 4)
20+
push_gate!(zxc, Val(:H), 1)
21+
push_gate!(zxc, Val(:H), 4)
22+
push_gate!(zxc, Val(:Z), 1, 1//4)
23+
push_gate!(zxc, Val(:Z), 4, 3//2)
24+
push_gate!(zxc, Val(:X), 4, 1//1)
25+
push_gate!(zxc, Val(:H), 1)
26+
push_gate!(zxc, Val(:Z), 4, 1//2)
27+
push_gate!(zxc, Val(:X), 4, 1//1)
28+
push_gate!(zxc, Val(:Z), 2, 1//2)
29+
push_gate!(zxc, Val(:CNOT), 3, 2)
30+
push_gate!(zxc, Val(:H), 2)
31+
push_gate!(zxc, Val(:CNOT), 3, 2)
32+
push_gate!(zxc, Val(:Z), 2, 1//4)
33+
push_gate!(zxc, Val(:Z), 3, 1//2)
34+
push_gate!(zxc, Val(:H), 2)
35+
push_gate!(zxc, Val(:H), 3)
36+
push_gate!(zxc, Val(:Z), 3, 1//2)
37+
push_gate!(zxc, Val(:CNOT), 3, 2)
38+
39+
return zxc
4040
end
4141
ex1 = generate_example_1()
4242
```
@@ -46,17 +46,16 @@ using YaoPlots
4646
plot(ex1)
4747
```
4848
![the circuit of example 1](imgs/ex1.svg)
49-
To simplify `zxd`, one can simply use
49+
To simplify `ex1`, one can simply use
5050
```julia
5151
simplified_ex1 = clifford_simplification(ex1)
5252
```
53-
or explicitly use
53+
or explicitly apply the simplification rules:
5454
```julia
55-
zxg = ZXCircuit(ex1)
56-
simplify!(LocalCompRule(), zxg)
57-
simplify!(Pivot1Rule(), zxg)
58-
replace!(PivotBoundaryRule(), zxg)
59-
simplified_ex1 = circuit_extraction(zxg)
55+
simplify!(LocalCompRule(), ex1)
56+
simplify!(Pivot1Rule(), ex1)
57+
replace!(PivotBoundaryRule(), ex1)
58+
simplified_ex1 = circuit_extraction(ex1)
6059
```
6160
And we draw the simplified circuit.
6261
```julia
@@ -72,77 +71,77 @@ We first build up the circuit.
7271
```julia
7372
using ZXCalculus, YaoPlots
7473
function generate_example2()
75-
cir = ZXDiagram(5)
76-
push_gate!(cir, Val(:X), 5, 1//1)
77-
push_gate!(cir, Val(:H), 5)
78-
push_gate!(cir, Val(:Z), 5)
79-
push_gate!(cir, Val(:CNOT), 5, 4)
80-
push_gate!(cir, Val(:Z), 5, 7//4)
81-
push_gate!(cir, Val(:CNOT), 5, 1)
82-
push_gate!(cir, Val(:Z), 5, 1//4)
83-
push_gate!(cir, Val(:CNOT), 5, 4)
84-
push_gate!(cir, Val(:Z), 4, 1//4)
85-
push_gate!(cir, Val(:Z), 5, 7//4)
86-
push_gate!(cir, Val(:CNOT), 5, 1)
87-
push_gate!(cir, Val(:CNOT), 4, 1)
88-
push_gate!(cir, Val(:Z), 5, 1//4)
89-
push_gate!(cir, Val(:Z), 1, 1//4)
90-
push_gate!(cir, Val(:Z), 4, 7//4)
91-
push_gate!(cir, Val(:CNOT), 4, 1)
92-
push_gate!(cir, Val(:CNOT), 5, 4)
93-
push_gate!(cir, Val(:Z), 5, 7//4)
94-
push_gate!(cir, Val(:CNOT), 5, 3)
95-
push_gate!(cir, Val(:Z), 5, 1//4)
96-
push_gate!(cir, Val(:CNOT), 5, 4)
97-
push_gate!(cir, Val(:Z), 4, 1//4)
98-
push_gate!(cir, Val(:Z), 5, 7//4)
99-
push_gate!(cir, Val(:CNOT), 5, 3)
100-
push_gate!(cir, Val(:CNOT), 4, 3)
101-
push_gate!(cir, Val(:Z), 5, 1//4)
102-
push_gate!(cir, Val(:Z), 3, 1//4)
103-
push_gate!(cir, Val(:Z), 4, 7//4)
104-
push_gate!(cir, Val(:H), 5)
105-
push_gate!(cir, Val(:Z), 5)
106-
push_gate!(cir, Val(:CNOT), 4, 3)
107-
push_gate!(cir, Val(:CNOT), 5, 4)
108-
push_gate!(cir, Val(:H), 5)
109-
push_gate!(cir, Val(:Z), 5)
110-
push_gate!(cir, Val(:CNOT), 5, 3)
111-
push_gate!(cir, Val(:Z), 5, 7//4)
112-
push_gate!(cir, Val(:CNOT), 5, 2)
113-
push_gate!(cir, Val(:Z), 5, 1//4)
114-
push_gate!(cir, Val(:CNOT), 5, 3)
115-
push_gate!(cir, Val(:Z), 3, 1//4)
116-
push_gate!(cir, Val(:Z), 5, 7//4)
117-
push_gate!(cir, Val(:CNOT), 5, 2)
118-
push_gate!(cir, Val(:CNOT), 3, 2)
119-
push_gate!(cir, Val(:Z), 5, 1//4)
120-
push_gate!(cir, Val(:H), 5)
121-
push_gate!(cir, Val(:Z), 2, 1//4)
122-
push_gate!(cir, Val(:Z), 3, 7//4)
123-
push_gate!(cir, Val(:Z), 5)
124-
push_gate!(cir, Val(:CNOT), 3, 2)
125-
push_gate!(cir, Val(:CNOT), 5, 3)
126-
push_gate!(cir, Val(:H), 5)
127-
push_gate!(cir, Val(:Z), 5)
128-
push_gate!(cir, Val(:CNOT), 5, 2)
129-
push_gate!(cir, Val(:Z), 5, 7//4)
130-
push_gate!(cir, Val(:CNOT), 5, 1)
131-
push_gate!(cir, Val(:Z), 5, 1//4)
132-
push_gate!(cir, Val(:CNOT), 5, 2)
133-
push_gate!(cir, Val(:Z), 2, 1//4)
134-
push_gate!(cir, Val(:Z), 5, 7//4)
135-
push_gate!(cir, Val(:CNOT), 5, 1)
136-
push_gate!(cir, Val(:CNOT), 2, 1)
137-
push_gate!(cir, Val(:Z), 5, 1//4)
138-
push_gate!(cir, Val(:Z), 1, 1//4)
139-
push_gate!(cir, Val(:Z), 2, 7//4)
140-
push_gate!(cir, Val(:H), 5)
141-
push_gate!(cir, Val(:Z), 5)
142-
push_gate!(cir, Val(:CNOT), 2, 1)
143-
push_gate!(cir, Val(:CNOT), 5, 2)
144-
push_gate!(cir, Val(:CNOT), 5, 1)
145-
return cir
74+
zxc = ZXCircuit(5)
75+
push_gate!(zxc, Val(:X), 5, 1//1)
76+
push_gate!(zxc, Val(:H), 5)
77+
push_gate!(zxc, Val(:Z), 5)
78+
push_gate!(zxc, Val(:CNOT), 5, 4)
79+
push_gate!(zxc, Val(:Z), 5, 7//4)
80+
push_gate!(zxc, Val(:CNOT), 5, 1)
81+
push_gate!(zxc, Val(:Z), 5, 1//4)
82+
push_gate!(zxc, Val(:CNOT), 5, 4)
83+
push_gate!(zxc, Val(:Z), 4, 1//4)
84+
push_gate!(zxc, Val(:Z), 5, 7//4)
85+
push_gate!(zxc, Val(:CNOT), 5, 1)
86+
push_gate!(zxc, Val(:CNOT), 4, 1)
87+
push_gate!(zxc, Val(:Z), 5, 1//4)
88+
push_gate!(zxc, Val(:Z), 1, 1//4)
89+
push_gate!(zxc, Val(:Z), 4, 7//4)
90+
push_gate!(zxc, Val(:CNOT), 4, 1)
91+
push_gate!(zxc, Val(:CNOT), 5, 4)
92+
push_gate!(zxc, Val(:Z), 5, 7//4)
93+
push_gate!(zxc, Val(:CNOT), 5, 3)
94+
push_gate!(zxc, Val(:Z), 5, 1//4)
95+
push_gate!(zxc, Val(:CNOT), 5, 4)
96+
push_gate!(zxc, Val(:Z), 4, 1//4)
97+
push_gate!(zxc, Val(:Z), 5, 7//4)
98+
push_gate!(zxc, Val(:CNOT), 5, 3)
99+
push_gate!(zxc, Val(:CNOT), 4, 3)
100+
push_gate!(zxc, Val(:Z), 5, 1//4)
101+
push_gate!(zxc, Val(:Z), 3, 1//4)
102+
push_gate!(zxc, Val(:Z), 4, 7//4)
103+
push_gate!(zxc, Val(:H), 5)
104+
push_gate!(zxc, Val(:Z), 5)
105+
push_gate!(zxc, Val(:CNOT), 4, 3)
106+
push_gate!(zxc, Val(:CNOT), 5, 4)
107+
push_gate!(zxc, Val(:H), 5)
108+
push_gate!(zxc, Val(:Z), 5)
109+
push_gate!(zxc, Val(:CNOT), 5, 3)
110+
push_gate!(zxc, Val(:Z), 5, 7//4)
111+
push_gate!(zxc, Val(:CNOT), 5, 2)
112+
push_gate!(zxc, Val(:Z), 5, 1//4)
113+
push_gate!(zxc, Val(:CNOT), 5, 3)
114+
push_gate!(zxc, Val(:Z), 3, 1//4)
115+
push_gate!(zxc, Val(:Z), 5, 7//4)
116+
push_gate!(zxc, Val(:CNOT), 5, 2)
117+
push_gate!(zxc, Val(:CNOT), 3, 2)
118+
push_gate!(zxc, Val(:Z), 5, 1//4)
119+
push_gate!(zxc, Val(:H), 5)
120+
push_gate!(zxc, Val(:Z), 2, 1//4)
121+
push_gate!(zxc, Val(:Z), 3, 7//4)
122+
push_gate!(zxc, Val(:Z), 5)
123+
push_gate!(zxc, Val(:CNOT), 3, 2)
124+
push_gate!(zxc, Val(:CNOT), 5, 3)
125+
push_gate!(zxc, Val(:H), 5)
126+
push_gate!(zxc, Val(:Z), 5)
127+
push_gate!(zxc, Val(:CNOT), 5, 2)
128+
push_gate!(zxc, Val(:Z), 5, 7//4)
129+
push_gate!(zxc, Val(:CNOT), 5, 1)
130+
push_gate!(zxc, Val(:Z), 5, 1//4)
131+
push_gate!(zxc, Val(:CNOT), 5, 2)
132+
push_gate!(zxc, Val(:Z), 2, 1//4)
133+
push_gate!(zxc, Val(:Z), 5, 7//4)
134+
push_gate!(zxc, Val(:CNOT), 5, 1)
135+
push_gate!(zxc, Val(:CNOT), 2, 1)
136+
push_gate!(zxc, Val(:Z), 5, 1//4)
137+
push_gate!(zxc, Val(:Z), 1, 1//4)
138+
push_gate!(zxc, Val(:Z), 2, 7//4)
139+
push_gate!(zxc, Val(:H), 5)
140+
push_gate!(zxc, Val(:Z), 5)
141+
push_gate!(zxc, Val(:CNOT), 2, 1)
142+
push_gate!(zxc, Val(:CNOT), 5, 2)
143+
push_gate!(zxc, Val(:CNOT), 5, 1)
144+
return zxc
146145
end
147146
ex2 = generate_example2()
148147
plot(ex2)
@@ -166,25 +165,30 @@ we can see that the number of T gates has decreased from 28 to 8.
166165

167166
## Other usages
168167

169-
In the previous sections, we introduced how to use `ZXCalculus.jl` for ZX-diagrams which represent quantum circuits. Sometimes, one may wish to use it for general ZX-diagrams. It is possible.
168+
In the previous sections, we introduced how to use `ZXCalculus.jl` for quantum circuits using `ZXCircuit`. Sometimes, one may wish to work with the lower-level graph representation directly.
169+
170+
### Working with ZXGraph directly
171+
172+
For advanced users who need direct access to the graph structure, you can work with `ZXGraph`. However, for most use cases, `ZXCircuit` is the recommended interface.
170173

171-
One can create a `ZXDiagram` by building up its `Multigraph` and other information. For example,
172174
```julia
173175
using ZXCalculus, YaoPlots, Graphs
174-
g = Multigraph(6)
175-
add_edge!(g, 1, 2)
176-
add_edge!(g, 2, 3)
177-
add_edge!(g, 3, 4)
178-
add_edge!(g, 3, 5)
179-
add_edge!(g, 3, 6)
180-
ps = [0, 1, 1//2, 0, 0, 0]
181-
v_t = [SpiderType.In, SpiderType.X, SpiderType.Z, SpiderType.Out, SpiderType.Out, SpiderType.Out]
182-
zxd = ZXDiagram(g, v_t, ps)
176+
177+
# Create a ZXCircuit first
178+
zxc = ZXCircuit(3)
179+
push_gate!(zxc, Val(:H), 1)
180+
push_gate!(zxc, Val(:CNOT), 1, 2)
181+
182+
# Access the underlying ZXGraph if needed for low-level operations
183+
zxg = zxc.zxd # The internal ZXGraph
184+
185+
# Apply graph-based rules
186+
simplify!(LocalCompRule(), zxc)
183187
```
184-
Because the information of vertices locations of a general ZX-diagram is not provided, its plot will have a random layout.
185188

186-
We can manipulate `zxd` by using ZX-calculus [`Rule`](@ref)s.
189+
### Deprecated: ZXDiagram
190+
191+
**Note:** `ZXDiagram` is deprecated. For circuit-based operations, use `ZXCircuit` instead. If you have existing code using `ZXDiagram`, you can convert it:
187192
```julia
188-
matches = match(PiRule(), zxd)
189-
rewrite!(PiRule(), zxd, matches)
193+
zxc = ZXCircuit(old_zxd) # Convert deprecated ZXDiagram to ZXCircuit
190194
```

0 commit comments

Comments
 (0)