-
Notifications
You must be signed in to change notification settings - Fork 0
/
ground.jl
202 lines (177 loc) · 6.37 KB
/
ground.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using Yao
using BitBasis
using LinearAlgebra
using Statistics
using QuAlgorithmZoo: Sequence
using Plots
using LsqFit
using FFTW
include("circuit.jl")
abstract type AbstractModel{D} end
nspin(model::AbstractModel) = prod(model.size)
struct Heisenberg{D} <: AbstractModel{D}
size::NTuple{D,Int}
periodic::Bool
Heisenberg(size::Int...;periodic::Bool) = new{length(size)}(size,periodic)
end
heisenberg_ij(nbit::Int, m::Int, n::Int = m + 1) = put(nbit,m=>X) * put(nbit,n=>X) + put(nbit,m=>Y) * put(nbit,n=>Y) + put(nbit,m=>Z) * put(nbit,n=>Z)
heisenberg_iijj(nbit::Int, m::Int, n::Int = m + 2) = put(nbit,m=>X) * put(nbit,n=>X) + put(nbit,m=>Y) * put(nbit,n=>Y) + put(nbit,m=>Z) * put(nbit,n=>Z)
function get_bonds(model::AbstractModel{1})
nbit, = model.size
[(i,i%nbit+1) for i in 1:(model.periodic ? nbit : nbit-1)]
end
function get_bonds(model::AbstractModel{2})
m,n = model.size
cis = LinearIndices(model.size)
bonds = Tuple{Int,Int,Float64}[]
for i = 1:m, j = 1:n
(i!=m || model.periodic) && push!(bonds,(cis[i,j],cis[i%m+1,j]))
(j!=n || model.periodic) && push!(bonds,(cis[i,j],cis[i,j%n+1]))
end
bonds
end
function hamiltionian(model::Heisenberg)
nbit = nspin(model)
sum(x->heisenberg_ij(nbit,x[1],x[2]),get_bonds(model))*0.25
end
function gensample(circuit,operator;nbatch=1024)
mblocks = collect_blocks(Measure,circuit)
for m in mblocks
m.operator = operator
end
reg = zero_state(nqubits(circuit);nbatch=nbatch)
reg |> circuit
mblocks
end
function ising_energy(circuit,bonds,basis;nbatch = nbatch)
mblocks = gensample(circuit,basis;nbatch = nbatch)
nspin = length(mblocks)
local eng = 0.0
for (a,b) in bonds
eng += mean(mblocks[a].results.*mblocks[b].results)
end
eng/=4
end
function energy(circuit,model::Heisenberg;nbatch = 1024)
bonds = get_bonds(model)
sum(basis->ising_energy(circuit,bonds,basis;nbatch = nbatch),[X,Y,Z])
end
function fidelity(circuit,VG)
psi = zero_state(nqubits(circuit))|>circuit
return abs(statevec(psi)'*VG)
end
#hei_model = Heisenberg(4;periodic = false)
function cos_fit_min(x::Array{Float64,1},y::Array{Float64,1})
# A = std(y);
# b = mean(y);
# w = 1.0;
# ph = 0.0;
# x0 = vec(x);
# p = [A,w,ph,b];
# fun(x0,p) = p[1].*sin.(p[2].*x0 .+ p[3]) .+ p[4];
# fit = curve_fit(fun,x0,y0,p);
# return fit.param[1] > 0 ? mod2pi(3/2*π - fit.param[3]) : mod2pi(π/2 - fit.param[3])
b = (y[1] + y[3])/2
ϕ = atan((y[1] - b)/(y[2] - b)) - x[1]
A = (y[1] - b)/sin(x[1] + ϕ)
if A < 0
A = -A
ϕ = mod2pi(ϕ+π)
else
ϕ = mod2pi(ϕ)
end
return A,ϕ,b
end
function optimalr(A,ϕ,b)
A = median(A)
ϕ = median(ϕ)
b = median(b)
#A = mean(A)
#ϕ = mean(ϕ)
#b = mean(b)
return mod2pi(3/2*π - ϕ)
end
function train(circuit, model; m = 5, VG = nothing, maxiter = 3, nbatch = 1024)
rots = Sequence(collect_blocks(RotationGate,circuit))
mcircuit = fcircuit(nqubits(circuit));
loss_history = Float64[]
for i in 0:maxiter
for (j,r) in enumerate(rots.blocks)
A = fill(0.0,m)
ϕ = fill(0.0,m)
b = fill(0.0,m)
para = parameters(r)[1] #parameters return a one-element arrary
for k in 1:m #m denotes the group of "three points"
E = Float64[] #energy array of three points
tmp = Float64[] #para array of three points
para += π/2 * (k-1)/m; #initial para of each group,i.e. the first para
dispatch!(r,para) #parameterized
para_tmp = copy(para); #tmp variable
for l in 1:3
push!(tmp,para_tmp)
push!(E,energy(circuit,model;nbatch=nbatch))
dispatch!(+,r,π/2);
para_tmp += π/2;
end
A[k],ϕ[k],b[k] = cos_fit_min(tmp,E);
end
dispatch!(r,optimalr(A,ϕ,b));
push!(loss_history,energy(circuit,model,nbatch=nbatch)/nspin(model));
print("Iter $i.$j, E/N = $(loss_history[end])")
if !(VG == nothing)
dispatch!(mcircuit,parameters(circuit))
fid = fidelity(mcircuit,VG)
#println(", fidelity = $fid, coefficients of sine function: A, ϕ, b: $A, $ϕ, $b")
println(", fidelity = $fid")
else
println()
end
end
#push!(loss_history,energy(circuit,model,nbatch=nbatch)/nspin(model));
#if i%10==0
#print("Iter $i, E/N = $(loss_history[end])")
#= if !(VG == nothing)
dispatch!(mcircuit,parameters(circuit))
fid = fidelity(mcircuit,VG)
println(", fidelity = $fid")
else
println()
end=#
#end
end
loss_history,circuit
end
function iscos(mycircuit,model,index = 2,m = 20,nbatch = 1024)
rots = Sequence(collect_blocks(RotationGate,mycircuit))
E = Float64[]
para = Float64[]
for i in 1:m
push!(E, energy(mycircuit,model,nbatch = nbatch))
push!(para,parameters(rots[index])[1])
dispatch!(+,rots[index],2.0*π/m)
end
A = std(E);
b = mean(E);
w = 1.0;
ph = 0.0;
x0 = vec(para);
y0 = vec(E);
p = [A,w,ph,b];
fun(x0,p) = p[1].*sin.(p[2].*x0 .+ p[3]) .+ p[4];
fit = curve_fit(fun,x0,y0,p);
return E,para,fit.param
end
#########################################################################
lattice_size = 6;
model = Heisenberg(lattice_size;periodic = false)
h = hamiltionian(model)
res = eigen(mat(h)|>Matrix)
EG = res.values[1]/nspin(model)
@show EG
VG = res.vectors[:,1]
mycircuit = dispatch!(tcircuit(lattice_size), :random);
npara = nparameters(mycircuit)
loss_history,mycircuit = train(mycircuit,model,m=5;VG = VG)
plot([0:npara*4-1],[loss_history,fill(EG,npara*4)],label = ["QMPS","Exact"],lw = 3,ylabel = "Energy")
E,para,coeffs = iscos(mycircuit,model,15)
plot(para,[E,map(x->coeffs[1].*sin(coeffs[2].*x+coeffs[3])+coeffs[4],para)])