/
quick-start.html
96 lines (95 loc) · 22.1 KB
/
quick-start.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Quick Start · Documentation | Yao</title><meta name="title" content="Quick Start · Documentation | Yao"/><meta property="og:title" content="Quick Start · Documentation | Yao"/><meta property="twitter:title" content="Quick Start · Documentation | Yao"/><meta name="description" content="Documentation for Documentation | Yao."/><meta property="og:description" content="Documentation for Documentation | Yao."/><meta property="twitter:description" content="Documentation for Documentation | Yao."/><script data-outdated-warner src="assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.050/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.8/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL="."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="assets/documenter.js"></script><script src="search_index.js"></script><script src="siteinfo.js"></script><script src="../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="assets/themeswap.js"></script><link href="assets/indigo.css" rel="stylesheet" type="text/css"/><link href="https://yaoquantum.org/assets/favicon-light.ico" rel="icon" type="image/x-icon"/></head><body><div id="documenter"><nav class="docs-sidebar"><a class="docs-logo" href="index.html"><img src="assets/logo.png" alt="Documentation | Yao logo"/></a><div class="docs-package-name"><span class="docs-autofit"><a href="index.html">Documentation | Yao</a></span></div><button class="docs-search-query input is-rounded is-small is-clickable my-2 mx-auto py-1 px-2" id="documenter-search-query">Search docs (Ctrl + /)</button><ul class="docs-menu"><li><a class="tocitem" href="index.html">Home</a></li><li class="is-active"><a class="tocitem" href="quick-start.html">Quick Start</a><ul class="internal"><li><a class="tocitem" href="#Create-a-quantum-register/state"><span>Create a quantum register/state</span></a></li><li><a class="tocitem" href="#Create-quantum-circuit"><span>Create quantum circuit</span></a></li><li><a class="tocitem" href="#Create-Hamiltonian"><span>Create Hamiltonian</span></a></li><li><a class="tocitem" href="#Differentiating-a-quantum-circuit"><span>Differentiating a quantum circuit</span></a></li><li><a class="tocitem" href="#Plot-quantum-circuits"><span>Plot quantum circuits</span></a></li></ul></li><li><span class="tocitem">Manual</span><ul><li><a class="tocitem" href="man/registers.html">Quantum Registers</a></li><li><a class="tocitem" href="man/blocks.html">Blocks</a></li><li><a class="tocitem" href="man/symbolic.html">Symbolic Computation</a></li><li><a class="tocitem" href="man/cuda.html">CUDA extension - CuYao</a></li><li><a class="tocitem" href="man/plot.html">Quantum Circuit Visualization</a></li><li><a class="tocitem" href="man/automatic_differentiation.html">Automatic Differentiation</a></li><li><a class="tocitem" href="man/yao2einsum.html">Tensor network backend</a></li><li><a class="tocitem" href="man/simplification.html">Simplification</a></li><li><a class="tocitem" href="man/bitbasis.html">BitBasis</a></li></ul></li><li><span class="tocitem">Examples</span><ul><li><a class="tocitem" href="generated/examples/1.prepare-ghz-state/index.html">Prepare Greenberger–Horne–Zeilinger state with Quantum Circuit</a></li><li><a class="tocitem" href="generated/examples/2.qft-phase-estimation/index.html">Quantum Fourier Transformation and Phase Estimation</a></li><li><a class="tocitem" href="generated/examples/3.grover-search/index.html">Grover Search</a></li><li><a class="tocitem" href="generated/examples/4.shor-algorithm/index.html">Shor's Algorithm</a></li><li><a class="tocitem" href="generated/examples/5.shor-9-code/index.html">Shor's 9 qubit code</a></li><li><a class="tocitem" href="generated/examples/6.quantum-circuit-born-machine/index.html">Quantum Circuit Born Machine</a></li><li><a class="tocitem" href="generated/examples/7.variation-quantum-eigen-solver/index.html">Variational Quantum Eigen Solver</a></li><li><a class="tocitem" href="generated/examples/8.riemannian-gradient-flow/index.html">Riemannian gradient flow optimizer</a></li></ul></li><li><a class="tocitem" href="performancetips.html">Performance Tips</a></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><a class="docs-sidebar-button docs-navbar-link fa-solid fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href="quick-start.html">Quick Start</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href="quick-start.html">Quick Start</a></li></ul></nav><div class="docs-right"><a class="docs-navbar-link" href="https://github.com/QuantumBFS/Yao.jl" title="View the repository on GitHub"><span class="docs-icon fa-brands"></span><span class="docs-label is-hidden-touch">GitHub</span></a><a class="docs-navbar-link" href="https://github.com/QuantumBFS/Yao.jl/blob/master/docs/src/quick-start.md" title="Edit source on GitHub"><span class="docs-icon fa-solid"></span></a><a class="docs-settings-button docs-navbar-link fa-solid fa-gear" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-article-toggle-button fa-solid fa-chevron-up" id="documenter-article-toggle-button" href="javascript:;" title="Collapse all docstrings"></a></div></header><article class="content" id="documenter-page"><h1 id="Quick-Start"><a class="docs-heading-anchor" href="#Quick-Start">Quick Start</a><a id="Quick-Start-1"></a><a class="docs-heading-anchor-permalink" href="#Quick-Start" title="Permalink"></a></h1><p>In this quick start, we list several common use cases for Yao before you go deeper into the manual.</p><h2 id="Create-a-quantum-register/state"><a class="docs-heading-anchor" href="#Create-a-quantum-register/state">Create a quantum register/state</a><a id="Create-a-quantum-register/state-1"></a><a class="docs-heading-anchor-permalink" href="#Create-a-quantum-register/state" title="Permalink"></a></h2><p>A register is an object that describes a device with an internal state. See <a href="man/registers.html#registers">Registers</a> for more details. Yao use registers to represent quantum states. The most common register is the <a href="man/registers.html#YaoArrayRegister.ArrayReg"><code>ArrayReg</code></a>, you can create it by feeding a state vector to it, e.g</p><pre><code class="language-julia-repl hljs" style="display:block;">julia> using Yao</code><code class="nohighlight hljs ansi" style="display:block;"></code><br/><code class="language-julia-repl hljs" style="display:block;">julia> ArrayReg(randn(ComplexF64, 2^3)) # a random unnormalized 3-qubit state</code><code class="nohighlight hljs ansi" style="display:block;">ArrayReg{2, ComplexF64, Array...}
active qubits: 3/3
nlevel: 2</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> zero_state(5) # |00000⟩</code><code class="nohighlight hljs ansi" style="display:block;">ArrayReg{2, ComplexF64, Array...}
active qubits: 5/5
nlevel: 2</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> rand_state(5) # a random state</code><code class="nohighlight hljs ansi" style="display:block;">ArrayReg{2, ComplexF64, Array...}
active qubits: 5/5
nlevel: 2</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> product_state(bit"10100") # |10100⟩</code><code class="nohighlight hljs ansi" style="display:block;">ArrayReg{2, ComplexF64, Array...}
active qubits: 5/5
nlevel: 2</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> ghz_state(5) # (|00000⟩ + |11111⟩)/√2</code><code class="nohighlight hljs ansi" style="display:block;">ArrayReg{2, ComplexF64, Array...}
active qubits: 5/5
nlevel: 2</code></pre><p>the internal quantum state can be accessed via <a href="man/registers.html#YaoArrayRegister.statevec"><code>statevec</code></a> method</p><pre><code class="language-julia-repl hljs" style="display:block;">julia> statevec(ghz_state(2))</code><code class="nohighlight hljs ansi" style="display:block;">4-element Vector{ComplexF64}:
0.7071067811865476 - 0.0im
0.0 + 0.0im
0.0 + 0.0im
0.7071067811865476 - 0.0im</code></pre><p>for more functionalities about registers please refer to the manual of <a href="man/registers.html#registers">Registers</a>.</p><h2 id="Create-quantum-circuit"><a class="docs-heading-anchor" href="#Create-quantum-circuit">Create quantum circuit</a><a id="Create-quantum-circuit-1"></a><a class="docs-heading-anchor-permalink" href="#Create-quantum-circuit" title="Permalink"></a></h2><p>Yao introduces an abstract representation for linear maps, called "block"s, which can be used to represent quantum circuits, Hamiltonians, and other quantum operations. The following code creates a 2-qubit circuit</p><pre><code class="language-julia-repl hljs" style="display:block;">julia> chain(2, put(1=>H), put(2=>X))</code><code class="nohighlight hljs ansi" style="display:block;"><span class="sgr36">nqubits: 2</span>
<span class="sgr34"><span class="sgr1">chain</span></span>
├─ <span class="sgr36"><span class="sgr1">put on (1)</span></span>
│ └─ H
└─ <span class="sgr36"><span class="sgr1">put on (2)</span></span>
└─ X</code></pre><p>where <code>H</code> gate is at 1st qubit, <code>X</code> gate is at 2nd qubit. A more advanced example is the quantum Fourier transform circuit</p><pre><code class="language-julia-repl hljs" style="display:block;">julia> A(i, j) = control(i, j=>shift(2π/(1<<(i-j+1)))) # a cphase gate</code><code class="nohighlight hljs ansi" style="display:block;">A (generic function with 1 method)</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> B(n, k) = chain(n, j==k ? put(k=>H) : A(j, k) for j in k:n)</code><code class="nohighlight hljs ansi" style="display:block;">B (generic function with 1 method)</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> qft(n) = chain(B(n, k) for k in 1:n)</code><code class="nohighlight hljs ansi" style="display:block;">qft (generic function with 1 method)</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> circuit = qft(3) # a 3-qubit QFT circuit</code><code class="nohighlight hljs ansi" style="display:block;"><span class="sgr36">nqubits: 3</span>
<span class="sgr34"><span class="sgr1">chain</span></span>
├─ <span class="sgr34"><span class="sgr1">chain</span></span>
│ ├─ <span class="sgr36"><span class="sgr1">put on (1)</span></span>
│ │ └─ H
│ ├─ <span class="sgr31"><span class="sgr1">control(2)</span></span>
│ │ └─ <span class="sgr37"><span class="sgr1">(1,)</span></span> shift(1.5707963267948966)
│ └─ <span class="sgr31"><span class="sgr1">control(3)</span></span>
│ └─ <span class="sgr37"><span class="sgr1">(1,)</span></span> shift(0.7853981633974483)
├─ <span class="sgr34"><span class="sgr1">chain</span></span>
│ ├─ <span class="sgr36"><span class="sgr1">put on (2)</span></span>
│ │ └─ H
│ └─ <span class="sgr31"><span class="sgr1">control(3)</span></span>
│ └─ <span class="sgr37"><span class="sgr1">(2,)</span></span> shift(1.5707963267948966)
└─ <span class="sgr34"><span class="sgr1">chain</span></span>
└─ <span class="sgr36"><span class="sgr1">put on (3)</span></span>
└─ H</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> mat(circuit) # the matrix representation of the circuit</code><code class="nohighlight hljs ansi" style="display:block;">8×8 SparseMatrixCSC{ComplexF64, Int64} with 64 stored entries:
0.353553+0.0im 0.353553+0.0im … 0.353553+0.0im
0.353553+0.0im -0.353553+0.0im 0.25-0.25im
0.353553+0.0im 0.353553+0.0im -2.16489e-17-0.353553im
0.353553+0.0im -0.353553+0.0im -0.25-0.25im
0.353553+0.0im 0.353553+0.0im -0.353553+0.0im
0.353553+0.0im -0.353553+0.0im … -0.25+0.25im
0.353553+0.0im 0.353553+0.0im 2.16489e-17+0.353553im
0.353553+0.0im -0.353553+0.0im 0.25+0.25im</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> apply!(zero_state(3), circuit) # apply the circuit to a zero state</code><code class="nohighlight hljs ansi" style="display:block;">ArrayReg{2, ComplexF64, Array...}
active qubits: 3/3
nlevel: 2</code></pre><p>More details about available blocks can be found in the manual of <a href="@ref blocks">Blocks</a>.</p><h2 id="Create-Hamiltonian"><a class="docs-heading-anchor" href="#Create-Hamiltonian">Create Hamiltonian</a><a id="Create-Hamiltonian-1"></a><a class="docs-heading-anchor-permalink" href="#Create-Hamiltonian" title="Permalink"></a></h2><p>We can create a simple Ising Hamiltonian on 1D chain as following</p><pre><code class="language-julia-repl hljs" style="display:block;">julia> h = sum([kron(5, i=>Z, mod1(i+1, 5)=>Z) for i in 1:5]) # a 5-qubit Ising Hamiltonian</code><code class="nohighlight hljs ansi" style="display:block;"><span class="sgr36">nqubits: 5</span>
<span class="sgr31"><span class="sgr1">+</span></span>
├─ <span class="sgr36"><span class="sgr1">kron</span></span>
│ ├─ <span class="sgr37"><span class="sgr1">1</span></span>=>Z
│ └─ <span class="sgr37"><span class="sgr1">2</span></span>=>Z
├─ <span class="sgr36"><span class="sgr1">kron</span></span>
│ ├─ <span class="sgr37"><span class="sgr1">2</span></span>=>Z
│ └─ <span class="sgr37"><span class="sgr1">3</span></span>=>Z
├─ <span class="sgr36"><span class="sgr1">kron</span></span>
│ ├─ <span class="sgr37"><span class="sgr1">3</span></span>=>Z
│ └─ <span class="sgr37"><span class="sgr1">4</span></span>=>Z
├─ <span class="sgr36"><span class="sgr1">kron</span></span>
│ ├─ <span class="sgr37"><span class="sgr1">4</span></span>=>Z
│ └─ <span class="sgr37"><span class="sgr1">5</span></span>=>Z
└─ <span class="sgr36"><span class="sgr1">kron</span></span>
├─ <span class="sgr37"><span class="sgr1">1</span></span>=>Z
└─ <span class="sgr37"><span class="sgr1">5</span></span>=>Z</code><br/><code class="language-julia-repl hljs" style="display:block;">julia> mat(h) # the matrix representation of the Hamiltonian</code><code class="nohighlight hljs ansi" style="display:block;">32×32 Diagonal{ComplexF64, Vector{ComplexF64}}:
5.0+0.0im ⋅ ⋅ … ⋅ ⋅ ⋅
⋅ 1.0+0.0im ⋅ ⋅ ⋅ ⋅
⋅ ⋅ 1.0+0.0im ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ … ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋮ ⋱ ⋮
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ … ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ 1.0+0.0im ⋅ ⋅
⋅ ⋅ ⋅ … ⋅ 1.0+0.0im ⋅
⋅ ⋅ ⋅ ⋅ ⋅ 5.0-0.0im</code></pre><h2 id="Differentiating-a-quantum-circuit"><a class="docs-heading-anchor" href="#Differentiating-a-quantum-circuit">Differentiating a quantum circuit</a><a id="Differentiating-a-quantum-circuit-1"></a><a class="docs-heading-anchor-permalink" href="#Differentiating-a-quantum-circuit" title="Permalink"></a></h2><p>Yao has its own automatic differentiation rule implemented, this allows one obtain gradients of a loss function by simply putting a <code>'</code> mark following <a href="man/blocks.html#YaoAPI.expect-Tuple{AbstractBlock, AbstractRegister}"><code>expect</code></a> or <a href="man/registers.html#YaoAPI.fidelity"><code>fidelity</code></a>, e.g</p><p>To obtain the gradient of the quantum Fourier transform circuit with respect to its parameters, one can use the following code</p><pre><code class="language-julia-repl hljs" style="display:block;">julia> grad_state, grad_circuit_params = expect'(kron(X, X, I2) + kron(I2, X, X), zero_state(3)=>qft(3))</code><code class="nohighlight hljs ansi" style="display:block;">ArrayReg{2, ComplexF64, Array...}
active qubits: 3/3
nlevel: 2 => Any[0.0, 0.0, 0.0]</code></pre><p>where <code>kron(X, X, I2) + kron(I2, X, X)</code> is the target Hamiltonian, <code>zero_state(3)</code> is the initial state, <code>qft(3)</code> is the quantum Fourier transform circuit. The return value is a vector, each corresponding to the gradient of the loss function with respect to a parameter in the circuit. The list of parameters can be obtained by <a href="man/blocks.html#YaoAPI.parameters-Tuple{AbstractBlock}"><code>parameters</code></a> function.</p><pre><code class="language-julia-repl hljs" style="display:block;">julia> parameters(qft(3))</code><code class="nohighlight hljs ansi" style="display:block;">3-element Vector{Float64}:
1.5707963267948966
0.7853981633974483
1.5707963267948966</code></pre><p>To obtain the gradient of the fidelity between a state parameterized by a quantum circuit and a target state, one can use the following code</p><pre><code class="language-julia-repl hljs" style="display:block;">julia> ((grad_state1, grad_circuit1), grad_state2) = fidelity'(zero_state(3)=>qft(3), ghz_state(3))</code><code class="nohighlight hljs ansi" style="display:block;">(ArrayReg{2, ComplexF64, Array...}
active qubits: 3/3
nlevel: 2 => Any[0.0, 0.0, 0.0], ArrayReg{2, ComplexF64, Array...}
active qubits: 3/3
nlevel: 2)</code></pre><p>where <code>zero_state(3)</code> is the initial state, <code>qft(3)</code> is the quantum Fourier transform circuit, <code>ghz_state(3)</code> is the target state.</p><p>The automatic differentiation functionality can also be accessed by interfacing with the machine learning libraries <a href="https://github.com/FluxML/Zygote.jl"><code>Zygote</code></a>.</p><h2 id="Plot-quantum-circuits"><a class="docs-heading-anchor" href="#Plot-quantum-circuits">Plot quantum circuits</a><a id="Plot-quantum-circuits-1"></a><a class="docs-heading-anchor-permalink" href="#Plot-quantum-circuits" title="Permalink"></a></h2><p>The component package <code>YaoPlots</code> provides plotting for quantum circuits and ZX diagrams. You can use it to visualize your quantum circuits in <a href="https://code.visualstudio.com/"><code>VSCode</code></a>, <a href="https://jupyter.org/"><code>Jupyter</code></a> notebook or <a href="https://github.com/fonsp/Pluto.jl"><code>Pluto</code></a> notebook.</p><pre><code class="language-julia hljs">using Yao.EasyBuild, Yao.YaoPlots
using Compose
# show a qft circuit
vizcircuit(qft_circuit(5))</code></pre><img src="quick-start-5613231c.svg" alt="Example block output"/><p>More details about the plotting can be found in the manual: <a href="man/plot.html#Quantum-Circuit-Visualization">Quantum Circuit Visualization</a>.</p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="index.html">« Home</a><a class="docs-footer-nextpage" href="man/registers.html">Quantum Registers »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.4.0 on <span class="colophon-date" title="Sunday 21 April 2024 16:01">Sunday 21 April 2024</span>. Using Julia version 1.10.2.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>