|
| 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 | +``` |
0 commit comments