Skip to content

Commit 08d205b

Browse files
Merge pull request #13 from sotashimozono/feature
Feature
2 parents 6f09aa1 + 7438abc commit 08d205b

8 files changed

Lines changed: 243 additions & 59 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GroverAlgorithm"
22
uuid = "480f6047-bde0-43c5-a5a8-f20cfea8349f"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
authors = ["sota <[email protected]>"]
55

66
[deps]

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ 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" => ["toffoli" => "example/toffoli.md"],
3839
#"Core Concepts" => [
3940
# "Quantum Gates and Circuits" => "structures.md",
4041
# "Initial States" => "initialstates.md",

docs/src/assets/custom.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
margin-bottom: 12px; /* 下の名前との距離 */
77
}
88

9-
article#documenter-page img[src$=".svg"][alt*="Example block"] {
9+
article#documenter-page img {
1010
display: block;
1111
margin-left: auto;
1212
margin-right: auto;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Getting Started: Your First Quantum Circuit
2+
3+
In this page, you will learn:
4+
5+
- How to construct quantum circuits on GroverAlgorithm.jl
6+
- How quantum circuits on GroverAlgorithm.jl works in ITensors and Quantikz.
7+
8+
## Install
9+
10+
In order to use visualize functions, `LaTeX` and `Quantikz` may be needed.
11+
12+
```julia
13+
using Pkg
14+
Pkg.add(url="https://github.com/sotashimozono/GroverAlgorithm.jl")
15+
Pkg.add("ITensors")
16+
Pkg.add("ITensorMPS")
17+
```
18+
19+
## Basics
20+
21+
In GroverAlgorithm.jl, use quantum circuits as follows:
22+
23+
1. Construct circuit - define circuit by adding desired gates.
24+
2. Simulation on ITensors.jl - calculate quantum states.
25+
3. Visualize circuits on Quantikz - generate circuits figure on LaTeX
26+
27+
## Example: Hadamard gate
28+
29+
Let's consider from easiest one.
30+
31+
### step1: Construct gate
32+
33+
```@example getting_started
34+
using GroverAlgorithm
35+
using ITensors, ITensorMPS
36+
37+
# 1 site qubit
38+
circuit = QuantumCircuit(1, AbstractQuantumGate[])
39+
40+
# add Hadamard gate
41+
add_gate!(circuit, SingleQubitGate(1, :H))
42+
43+
println("circuit information:")
44+
println(" number of qubits: ", circuit.nqubits)
45+
println(" number of gates: ", length(circuit.gates))
46+
```
47+
48+
### step2: Simulation on ITensors.jl
49+
50+
```@example getting_started
51+
using ITensors, ITensorMPS
52+
53+
# site index
54+
sites = siteinds("Qubit", 1)
55+
56+
# execute circuit
57+
psi = execute_circuit(circuit, sites)
58+
59+
# MPS info
60+
println("\nMPS (Matrix Product State):")
61+
println(psi)
62+
```
63+
64+
structure of MPS:
65+
66+
```@example getting_started
67+
println("amplitude of quantum states:")
68+
amp_0 = inner(psi, MPS(sites, ["0"]))
69+
amp_1 = inner(psi, MPS(sites, ["1"]))
70+
71+
println(" ⟨0|ψ⟩ = ", amp_0, " (probability: ", abs2(amp_0), ")")
72+
println(" ⟨1|ψ⟩ = ", amp_1, " (probability: ", abs2(amp_1), ")")
73+
```
74+
75+
this means the state $|\psi\rangle$ is superposition of $|0\rangle, |1\rangle$:
76+
$$\begin{aligned}
77+
|\psi\rangle = \frac{|0\rangle+|1\rangle}{\sqrt2}
78+
\end{aligned}$$
79+
80+
### step3: Visualization on Quantikz
81+
82+
```@example getting_started
83+
# generate LaTeX string of quantikz
84+
latex_code = to_quantikz(circuit)
85+
println("Quantikz LaTeX code:")
86+
println(latex_code)
87+
```
88+
89+
This LaTeX code generates following:
90+
91+
```@example getting_started
92+
to_tikz_picture(circuit)
93+
```
94+
95+
## Example2: Bell state(Entanglement)
96+
97+
Next, we construct 2-site qubit.
98+
99+
### step1: construct circuits
100+
101+
```@example getting_started
102+
circuit = QuantumCircuit(2, AbstractQuantumGate[])
103+
104+
add_gate!(circuit, SingleQubitGate(1, :H))
105+
add_gate!(circuit, ControlledGate(1, 2, :CNOT))
106+
107+
println("Bell state circuit:")
108+
println(" gate1: H on qubit 1")
109+
println(" gate2: CNOT (control: 1, target: 2)")
110+
```
111+
112+
### step2: Simulation on ITensors
113+
114+
```@example getting_started
115+
sites = siteinds("Qubit", 2)
116+
psi = execute_circuit(circuit, sites)
117+
118+
println("amplitude of Bell state:")
119+
for s in ["00", "01", "10", "11"]
120+
basis = MPS(sites, [string(c) for c in s])
121+
amp = inner(basis, psi)
122+
prob = abs2(amp)
123+
println(" ⟨$s|ψ⟩ = ", round(amp, digits=4), " (probability: ", round(prob, digits=4), ")")
124+
end
125+
```
126+
127+
### step3: Visualize circuit on Quantikz
128+
129+
```@example getting_started
130+
latex_code = to_quantikz(circuit)
131+
println(latex_code)
132+
133+
to_tikz_picture(circuit)
134+
```

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+

example/makedocs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Pkg.activate("../")
44
using Literate, GroverAlgorithm, ITensors, ITensorMPS
55

66
base_dir = pkgdir(GroverAlgorithm)
7-
output_dir = joinpath(base_dir, "docs", "src", "examples")
7+
output_dir = joinpath(base_dir, "docs", "src", "example")
88
mkpath(output_dir)
99

1010
Literate.markdown("toffoli.jl", output_dir; documenter=true)

example/toffoli.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
using Printf
2-
using GroverAlgorithm
3-
using ITensors, ITensorMPS
4-
1+
# # Toffoli Gate Example
52
# Here we define the Toffoli gate (CCNOT) using the GroverAlgorithm package
63
# This is an example of how to use the package.
74
# Toffoli gate is available in ITensorMPS.jl as a built-in gate.
85
# Let's create a Toffoli gate and check its action on the basis states.
6+
7+
# ## Using the built-in Toffoli gate
8+
# 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.
9+
10+
using Printf
11+
using GroverAlgorithm
12+
using ITensors, ITensorMPS
13+
914
circuit = QuantumCircuit(3)
1015
add_gate!(circuit, ThreeQubitGate(1, 2, 3, :Toffoli))
1116

@@ -33,10 +38,9 @@ function test_actions(circuit)
3338
end
3439
end
3540

36-
circuit = QuantumCircuit(3)
37-
add_gate!(circuit, ThreeQubitGate(1, 2, 3, :Toffoli))
3841
test_actions(circuit)
3942

43+
# ## Toffoli gate decomposition
4044
# Then, Lets's construct the Toffoli gate using the standard decomposition into CNOT and single-qubit gates,
4145
# and verify that it produces the same results as the built-in Toffoli gate.
4246
function toffoli_decomposed(circuit)
@@ -60,6 +64,8 @@ function toffoli_decomposed(circuit)
6064
end
6165
circuit_decomposed = QuantumCircuit(3)
6266
toffoli_decomposed(circuit_decomposed)
63-
test_actions(circuit_decomposed)
6467

6568
tp = to_tikz_picture(circuit_decomposed)
69+
70+
# 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.
71+
test_actions(circuit_decomposed)

example/toffoli.md

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)