Skip to content

Commit 833023c

Browse files
committed
added toffoli as an example
1 parent 01d2321 commit 833023c

2 files changed

Lines changed: 95 additions & 1 deletion

File tree

docs/make.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ makedocs(;
3434
modules=[GroverAlgorithm],
3535
pages=[
3636
"Home" => "index.md",
37-
"Getting Started" => "getting_started.md",
37+
"Getting Started" => "example/getting_started.md",
38+
"example" => [
39+
"toffoli" => "example/toffoli.md"
40+
],
3841
#"Core Concepts" => [
3942
# "Quantum Gates and Circuits" => "structures.md",
4043
# "Initial States" => "initialstates.md",

docs/src/example/toffoli.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
```@meta
2+
EditURL = "../../../example/toffoli.jl"
3+
```
4+
5+
# Toffoli Gate Example
6+
Here we define the Toffoli gate (CCNOT) using the GroverAlgorithm package
7+
This is an example of how to use the package.
8+
Toffoli gate is available in ITensorMPS.jl as a built-in gate.
9+
Let's create a Toffoli gate and check its action on the basis states.
10+
11+
## Using the built-in Toffoli gate
12+
In ITensorMPS.jl, the Toffoli gate is available as a built-in three-qubit gate. We can simply add it to our quantum circuit and execute it.
13+
14+
````@example toffoli
15+
using Printf
16+
using GroverAlgorithm
17+
using ITensors, ITensorMPS
18+
19+
circuit = QuantumCircuit(3)
20+
add_gate!(circuit, ThreeQubitGate(1, 2, 3, :Toffoli))
21+
22+
sites = siteinds("Qubit", 3)
23+
psi = execute_circuit(circuit, sites)
24+
counts = measure(psi, Sampling(100))
25+
println("結果: ", counts)
26+
````
27+
28+
Toffoli gate acts as follows for the basis states:
29+
30+
````@example toffoli
31+
function test_actions(circuit)
32+
println("Test of Toffoli Gate")
33+
println("=" ^ 50)
34+
println("Input | Output")
35+
println("-" ^ 50)
36+
for a in 0:1, b in 0:1, c in 0:1
37+
initial = AbstractInitialState[ProductState([string(a), string(b), string(c)])]
38+
set_state!(circuit, initial)
39+
40+
sites = siteinds("Qubit", 3)
41+
psi = execute_circuit(circuit, sites)
42+
43+
counts = measure(psi, Sampling(1))
44+
output = collect(keys(counts))[1]
45+
@printf("|%d%d%d⟩ → |%s⟩\n", a, b, c, output)
46+
end
47+
end
48+
49+
test_actions(circuit)
50+
````
51+
52+
## Toffoli gate decomposition
53+
Then, Lets's construct the Toffoli gate using the standard decomposition into CNOT and single-qubit gates,
54+
and verify that it produces the same results as the built-in Toffoli gate.
55+
56+
````@example toffoli
57+
function toffoli_decomposed(circuit)
58+
add_gate!(circuit, SingleQubitGate(3, :H))
59+
add_gate!(circuit, ControlledGate(2, 3, :CX))
60+
add_gate!(circuit, SingleQubitGate(3, :Tdag))
61+
add_gate!(circuit, ControlledGate(1, 3, :CX))
62+
add_gate!(circuit, SingleQubitGate(3, :T))
63+
add_gate!(circuit, ControlledGate(2, 3, :CX))
64+
add_gate!(circuit, SingleQubitGate(2, :Tdag))
65+
add_gate!(circuit, SingleQubitGate(3, :Tdag))
66+
add_gate!(circuit, ControlledGate(1, 3, :CX))
67+
add_gate!(circuit, ControlledGate(1, 2, :CX))
68+
add_gate!(circuit, SingleQubitGate(1, :T))
69+
add_gate!(circuit, SingleQubitGate(2, :Tdag))
70+
add_gate!(circuit, SingleQubitGate(3, :T))
71+
add_gate!(circuit, ControlledGate(1, 2, :CX))
72+
add_gate!(circuit, SingleQubitGate(2, :S))
73+
add_gate!(circuit, SingleQubitGate(3, :H))
74+
return circuit
75+
end
76+
circuit_decomposed = QuantumCircuit(3)
77+
toffoli_decomposed(circuit_decomposed)
78+
79+
tp = to_tikz_picture(circuit_decomposed)
80+
````
81+
82+
Let's test the action of the decomposed Toffoli gate on the basis states to verify that it behaves the same as the built-in Toffoli gate.
83+
84+
````@example toffoli
85+
test_actions(circuit_decomposed)
86+
````
87+
88+
---
89+
90+
*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*
91+

0 commit comments

Comments
 (0)