-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyao_tutorial.jl
69 lines (49 loc) · 1.45 KB
/
yao_tutorial.jl
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
Pkg.add Yao
Pkg.add StatsBase
using Yao
# circuit
# https://tutorials.yaoquantum.org/dev/generated/quick-start/1.prepare-ghz-state/assets/ghz4.png
circuit = chain(
4, # 4qubits
put(1=>X), # 1: Xgate
repeat(H, 2:4),
control(2, 1=>X), # 2:control-bit, 1:target-bit
control(4, 3=>X),
control(3, 1=>X),
control(4, 3=>X),
repeat(H, 1:4), # Hgate, 1:4-qubits
)
# explanation
put(4, 1=>X)
?=> # Construct a Pair object with type Pair{typeof(x), typeof(y)}
# Pair型:イテレートできる構造体みたいなやつ
put(4, (1,2)=>swap(2,1,2))
# 最初の引数はbit数?
# swap: swap(n, loc1, loc2)
# Create a n-qubit Swap gate which swap loc1 and loc2.
put(1=>X)
put(1=>X)(4)
repeat(H, 2:4)
control(4, 2, 1=>X)
control(2, 1=>X)
# (n -> control(n, 2, 1 => X))
# n qubits, CNT:2, TAR:1
typeof(circuit)
# 下2つは同じ?
zero_state(4)
ArrayReg{bit"0000"}
# ArrayRegistar
apply!(zero_state(4), circuit)
results = zero_state(4) |> circuit |> r->measure(r, nshots = 1000)
?|> # A |> B :: AをBに代入
typeof(results)
using StatsBase, Plots
# fit気になる
hist = fit(Histogram, Int.(results), 0:16)
# 0:16: 4bit なので、2^4 = 16
bar(hist.edges[1] .- 0.5, hist.weights, legend =:none)
# bar:棒グラフ
bar(hist.edges[1], hist.weights, legend =:none)
# hist.edges[1] .- 0.5::x軸の目盛りの位置ずらしてるっぽい
bar(hist.edges[1],legend =:none)
# hist.weightsがないと、添字をプロットし始める