Skip to content

Commit 5c497d5

Browse files
GiggleLiuRogerluo
authored andcommitted
Fix trait (#11)
* monor fix * seperate YaoExtensions * new H2 atoms * update docs * update Grover search
1 parent af61ed7 commit 5c497d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+574
-1636
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.jl.*.cov
33
*.jl.mem
44

5+
build/
56
docs/build/
67
docs/site/
78
docs/src/tutorial/

Project.toml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@ version = "0.1.0"
44

55
[deps]
66
BitBasis = "50ba71b6-fa0f-514d-ae9a-0916efc90dcf"
7-
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
8-
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
97
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
10-
LuxurySparse = "d05aeea4-b7d4-55ac-b691-9e7fabb07ba2"
11-
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
12-
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
13-
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
8+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
149
Yao = "5872b779-8223-5990-8dd0-5abbb0748c8c"
1510
YaoArrayRegister = "e600142f-9330-5003-8abb-0ebd767abc51"
1611
YaoBlocks = "418bc28f-b43b-5e0b-a6e7-61bbc1a2c1df"
12+
YaoExtensions = "7a06699c-c960-11e9-3c98-9f78548b5f0f"
1713

1814
[compat]
19-
julia = ">=1.0.0"
2015
Yao = ">=0.4.0"
16+
julia = ">=1.0.0"
2117

2218
[extras]
23-
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
24-
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
2519
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2620

2721
[targets]
28-
test = ["Test", "OrdinaryDiffEq"]
22+
test = ["Test"]

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
A curated implementation of quantum algorithms with [Yao.jl](https://github.com/QuantumBFS/Yao.jl)
99

10+
*Note*: part of functionalities has been moved to [YaoExtensions](https://github.com/QuantumBFS/YaoExtensions.jl).
11+
1012
## Installation
1113

1214
QuAlgorithmZoo.jl is not registered yet, please use the following command:
@@ -19,22 +21,26 @@ Disclaimer: **this package is still under development and needs further polish.*
1921

2022
## Contents
2123

22-
- [x] Quantum Circuit Born Machine
24+
- [x] [QFT](https://github.com/QuantumBFS/YaoExtensions.jl)
2325
- [x] Grover search
24-
- [x] HHL
25-
- [x] QFT
2626
- [x] Phase Estimation
27-
- [x] QuGAN
28-
- [x] QCBM
29-
- [x] Hamiltonian Solver
30-
- [x] QAOA
31-
- [x] Variational quantum eigensolver
32-
- [x] QuODE
27+
- [x] Imaginary Time Evolution Quantum Eigensolver
28+
- [x] Variational Quantum Eigensolver
3329
- [x] Hadamard Test
3430
- [x] State Overlap Algorithms
3531
- [x] Quantum SVD
32+
33+
In examples folder, you will find
34+
35+
- [x] HHL
36+
- [x] QAOA
37+
- [x] Quantum Circuit Born Machine
38+
- [x] QuGAN
3639
- [x] Shor
3740

41+
- [x] [QuODE](https://github.com/QuantumBFS/QuDiffEq.jl)
42+
- [x] [TensorNetwork Inspired Circuits](https://github.com/GiggleLiu/QuantumPEPS.jl)
43+
3844
## License
3945

4046
QuAlgorithmZoo.jl is released under Apache License 2.0.

docs/Project.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[deps]
2+
BitBasis = "50ba71b6-fa0f-514d-ae9a-0916efc90dcf"
3+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
4+
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
5+
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
6+
KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
7+
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
8+
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
9+
Weave = "44d3d7a6-8a23-5bf8-98c5-b353f8df5ec9"
10+
YaoArrayRegister = "e600142f-9330-5003-8abb-0ebd767abc51"
11+
YaoBase = "a8f54c17-34bc-5a9d-b050-f522fe3f755f"
12+
YaoBlocks = "418bc28f-b43b-5e0b-a6e7-61bbc1a2c1df"

docs/make.jl

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,56 @@ add Documenter Literate Plots Yao
66

77
using Documenter, Literate, QuAlgorithmZoo
88

9+
const Examples = ["Grover", "VQE", "Shor"]
10+
911
const PATH = (
1012
tutorial = joinpath(@__DIR__, "src/tutorial"),
1113
examples = joinpath(@__DIR__, "..", "examples")
1214
)
1315

14-
function process_literate_scripts(;excludes=["make.jl", "README.md"])
16+
function process_literate_scripts()
1517
TUTORIALS = []
16-
for (root, dirs, files) in walkdir(PATH.examples)
17-
for file in files
18-
file in excludes && continue
19-
filepath = joinpath(root, file)
20-
Literate.markdown(filepath, PATH.tutorial)
21-
22-
filename, _ = splitext(file)
23-
mdfile = join([filename, ".md"])
24-
# TODO: use PATH.tutorial rather then manual path
25-
push!(TUTORIALS, relpath(joinpath("tutorial", mdfile)))
26-
end
18+
for token in Examples
19+
file = "$token.jl"
20+
filepath = joinpath(PATH.examples, token, file)
21+
Literate.markdown(filepath, PATH.tutorial)
22+
23+
filename, _ = splitext(file)
24+
mdfile = join([filename, ".md"])
25+
# TODO: use PATH.tutorial rather then manual path
26+
push!(TUTORIALS, relpath(joinpath("tutorial", mdfile)))
2727
end
2828
TUTORIALS
2929
end
3030

31-
const TUTORIALS = process_literate_scripts()
32-
3331
#-----------------------------------------------
3432

35-
makedocs(
36-
modules = [QuAlgorithmZoo],
37-
clean = false,
38-
format = :html,
39-
sitename = "Quantum Algorithm Zoo",
40-
linkcheck = !("skiplinks" in ARGS),
41-
analytics = "UA-89508993-1",
42-
pages = [
43-
"Home" => "index.md",
44-
"Tutorial" => TUTORIALS,
45-
"Manual" => Any[
46-
"man/zoo.md",
33+
function generate(islocal::Bool="local" in ARGS)
34+
makedocs(
35+
modules = [QuAlgorithmZoo],
36+
clean = false,
37+
format = :html,
38+
sitename = "Quantum Algorithm Zoo",
39+
linkcheck = !("skiplinks" in ARGS),
40+
analytics = "UA-89508993-1",
41+
pages = [
42+
"Home" => "index.md",
43+
"Algorithms" => process_literate_scripts(),
44+
"Manual" => Any[
45+
"man/zoo.md",
46+
],
4747
],
48-
],
49-
html_prettyurls = !("local" in ARGS),
50-
html_canonical = "https://quantumbfs.github.io/QuAlgorithmZoo.jl/latest/",
51-
)
48+
html_prettyurls = !islocal,
49+
html_canonical = "https://quantumbfs.github.io/QuAlgorithmZoo.jl/latest/",
50+
)
51+
52+
deploydocs(
53+
repo = "github.com/QuantumBFS/QuAlgorithmZoo.jl.git",
54+
target = "build",
55+
julia = "1.0",
56+
deps = nothing,
57+
make = nothing,
58+
)
59+
end
5260

53-
deploydocs(
54-
repo = "github.com/QuantumBFS/QuAlgorithmZoo.jl.git",
55-
target = "build",
56-
julia = "1.0",
57-
deps = nothing,
58-
make = nothing,
59-
)
61+
generate(true)

docs/src/index.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ A curated implementation of quantum algorithms with [Yao.jl](https://github.com/
99
## Tutorial
1010
```@contents
1111
Pages = [
12-
"tutorial/Grover.md",
13-
"tutorial/QCBM.md",
14-
"tutorial/QuGAN.md",
12+
"tutorial/VQE.md",
13+
"tutorial/Shor.md",
1514
]
1615
Depth = 1
1716
```

docs/src/man/zoo.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44
Modules = [QuAlgorithmZoo]
55
Order = [:module, :constant, :type, :macro, :function]
66
```
7+
8+
```@autodocs
9+
Modules = [QuAlgorithmZoo.NumberTheory]
10+
Order = [:module, :constant, :type, :macro, :function]
11+
```

examples/Grover.jl

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

examples/Grover/Grover.jl

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# # [Grover Search](@id Grover)
2+
using Yao
3+
using YaoExtensions: variational_circuit
4+
using LinearAlgebra
5+
6+
# ## Grover Step
7+
# A single grover step is consist of applying oracle circuit and reflection circuit.
8+
# The `reflection_circuit` function takes the wave function generator `U` as the input and returns `U|0><0|U'`.
9+
function grover_step!(reg::AbstractRegister, oracle, U::AbstractBlock)
10+
apply!(reg |> oracle, reflect_circuit(U))
11+
end
12+
13+
function reflect_circuit(gen::AbstractBlock{N}) where N
14+
reflect0 = control(N, -collect(1:N-1), N=>-Z)
15+
chain(gen', reflect0, gen)
16+
end
17+
18+
# Compute the propotion of target states to estimate the number of iterations,
19+
# which requires computing the output state.
20+
function solution_state(oracle, gen::AbstractBlock{N}) where N
21+
reg= zero_state(N) |> gen
22+
reg.state[real.(statevec(ArrayReg(ones(ComplexF64, 1<<N)) |> oracle)) .> 0] .= 0
23+
normalize!(reg)
24+
end
25+
26+
function num_grover_step(oracle, gen::AbstractBlock{N}) where N
27+
reg = zero_state(N) |> gen
28+
ratio = abs2(solution_state(oracle, gen)'*reg)
29+
Int(round(pi/4/sqrt(ratio)))-1
30+
end
31+
32+
# #### Run
33+
# First, we define the problem by an oracle, it finds bit string `bit"000001100100"`.
34+
num_bit = 12
35+
oracle = matblock(Diagonal((v = ones(ComplexF64, 1<<num_bit); v[Int(bit"000001100100")+1]*=-1; v)))
36+
37+
# then solve the above problem
38+
gen = repeat(num_bit, H, 1:num_bit)
39+
reg = zero_state(num_bit) |> gen
40+
41+
target_state = solution_state(oracle, gen)
42+
43+
for i = 1:num_grover_step(oracle, gen)
44+
grover_step!(reg, oracle, gen)
45+
overlap = abs(reg'*target_state)
46+
println("step $(i-1), overlap = $overlap")
47+
end
48+
49+
# ## Rejection Sampling
50+
# In practise, it is often not possible to determine the number of iterations before actual running.
51+
# we can use rejection sampling technique to avoid estimating the number of grover steps.
52+
53+
using Random; Random.seed!(2) #src
54+
55+
# In a single try, we `apply` the grover algorithm for `nstep` times.
56+
function single_try(oracle, gen::AbstractBlock{N}, nstep::Int; nbatch::Int) where N
57+
reg = zero_state(N+1; nbatch=nshot)
58+
focus!(reg, 1:N) do r
59+
r |> gen
60+
for i = 1:nstep
61+
grover_step!(r, oracle, gen)
62+
end
63+
return r
64+
end
65+
reg |> checker
66+
res = measure_remove!(reg, (N+1))
67+
return res, reg
68+
end
69+
70+
# After running the grover search, we have a checker program that flips the ancilla qubit
71+
# if the output is the desired value, we assume the checker program can be implemented in polynomial time.
72+
# to gaurante the output is correct.
73+
# We contruct a checker "program", if the result is correct, flip the ancilla qubit
74+
ctrl = -collect(1:num_bit); ctrl[[3,6,7]] *= -1
75+
checker = control(num_bit+1,ctrl, num_bit+1=>X)
76+
77+
# The register is batched, with batch dimension `nshot`.
78+
# [`focus!`](@ref Yao.focus!) views the first 1-N qubts as system.
79+
# For a batched register, [`measure_remove!`](@ref Yao.measure_remove!)
80+
# returns a vector of bitstring as output.
81+
82+
# #### Run
83+
maxtry = 100
84+
nshot = 3
85+
86+
for nstep = 0:maxtry
87+
println("number of iter = $nstep")
88+
res, reg = single_try(oracle, gen, nstep; nbatch=3)
89+
90+
## success!
91+
if any(==(1), res)
92+
overlap_final = viewbatch(reg, findfirst(==(1), res))'*target_state
93+
println("success, overlap = $(overlap_final)")
94+
break
95+
end
96+
end
97+
98+
# The final state has an overlap of `1` with the target state.
99+
100+
# ## Amplitude Amplification
101+
# Given a circuit to generate a state,
102+
# now we want to project out the subspace with [1,3,5,8,9,11,12] fixed to 1 and [4,6] fixed to 0.
103+
# We can construct an oracle
104+
evidense = [1, 3, -4, 5, -6, 8, 9, 11, 12]
105+
function inference_oracle(nbit::Int, locs::Vector{Int})
106+
control(nbit, locs[1:end-1], abs(locs[end]) => (locs[end]>0 ? Z : -Z))
107+
end
108+
oracle = inference_oracle(nqubits(reg), evidense)
109+
110+
# We use a variational circuit generator defined in `YaoExtensions`
111+
gen = dispatch!(variational_circuit(num_bit), :random)
112+
reg = zero_state(num_bit) |> gen
113+
114+
# #### Run
115+
solution = solution_state(oracle, gen)
116+
for i = 1:num_grover_step(oracle, gen)
117+
grover_step!(reg, oracle, gen)
118+
println("step $(i-1), overlap = $(abs(reg'*solution))")
119+
end

0 commit comments

Comments
 (0)