/
update.jl
216 lines (203 loc) · 7.08 KB
/
update.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import Random: shuffle
@generated function myshuffle(xs)
canShuffleEmpty = try
shuffle([])
true
catch
false
end
if canShuffleEmpty
:(shuffle(xs))
else
:(length(xs)>0 ? shuffle(xs) : xs)
end
end
@generated function myshuffle(rng, xs)
canShuffleEmpty = try
shuffle([])
true
catch
false
end
if canShuffleEmpty
:(shuffle(rng, xs))
else
:(length(xs)>0 ? shuffle(rng, xs) : xs)
end
end
@doc """
loop_update!(model, param::Parameter)
loop_update!(model, T::Real,
Jz::AbstractArray,
Jxy::AbstractArray,
Gamma:AbstractArray)
Updates spin configuration by loop algorithm
under the temperature `T = param["T"]` and coupling constants `Jz, Jxy` and transverse field `Gamma`
"""
@inline function loop_update!(model::QuantumXXZ, param::Parameter)
p = convert_parameter(model, param)
return loop_update!(model, p...)
end
function loop_update!(model::QuantumXXZ, T::Real,
Jzs::AbstractArray, Jxys::AbstractArray, Gs::AbstractArray)
rng = model.rng
lo_types = [LET_FMLink, LET_AFLink, LET_Vertex, LET_Cross]
nsites = numsites(model)
S2 = model.S2
nspins = nsites*S2
nbonds = numbonds(model)
nst = numsitetypes(model)
nbt = numbondtypes(model)
weights = zeros(4*nbt+nst)
for i in 1:nbt
nb = numbonds(model,i)*S2*S2
z = nb*Jzs[i]
x = nb*abs(Jxys[i])
if z > x
## AntiFerroIsing like
weights[(4i-3):(4i)] .= 0.5*[0.0, z-x, x, 0.0]
elseif z < -x
## FerroIsing like
weights[(4i-3):(4i)] .= 0.5*[-z-x, 0.0, 0.0, x]
else
## XY like
weights[(4i-3):(4i)] .= 0.25*[0.0, 0.0, x+z, x-z]
end
end
for i in 1:nst
ns = numsites(model,i)*S2
weights[4nbt+i] = 0.5*ns*Gs[i]
end
accumulated_weights = cumsum(weights)
op_dt = T/accumulated_weights[end]
spins = model.spins[:]
currents = collect(1:nspins)
uf = UnionFind(nspins)
ops = LocalLoopOperator[]
iops = 1 # index of the next operator in the original string
t = randexp(rng)*op_dt
while t <= 1.0 || iops <= length(model.ops)
if iops > length(model.ops) || t < model.ops[iops].time
## INSERT
ot = searchsortedfirst(accumulated_weights, rand(rng)*accumulated_weights[end])
if ot <= 4nbt
## Bond
bt = ceil(Int, ot/4)
ot = mod1(ot,4)
lo_type = lo_types[ot]
b = bonds(model,bt)[rand(rng, 1:numbonds(model,bt))]
s1,s2 = source(b), target(b)
ss1, ss2 = rand(rng, 1:S2, 2)
if ifelse(spins[site2subspin(s1,ss1,S2)] == spins[site2subspin(s2,ss2,S2)],
lo_type == LET_FMLink || lo_type == LET_Cross,
lo_type == LET_AFLink || lo_type == LET_Vertex)
push!(ops, LocalLoopOperator(lo_type, t, nsites+b.id, (ss1, ss2)))
t += randexp(rng)*op_dt
else
t += randexp(rng)*op_dt
continue
end
else
## Site
st = ot-4nbt
s = sites(model,st)[rand(rng, 1:numsites(model,st))]
ss = rand(rng, 1:S2)
push!(ops, LocalLoopOperator(LET_Cut, t, s, (ss,ss)))
t += randexp(rng)*op_dt
end
else
op = model.ops[iops]
iops += 1
if op.isdiagonal
## REMOVE
continue
elseif op.let_type == LET_Cut
push!(ops, op)
else
push!(ops, op)
op = ops[end]
b = op.space-nsites
ss1,ss2 = op.subspace
bt = bondtype(model,b)
otype = ifelse(rand(rng)*(weights[4bt-1]+weights[4bt])<weights[4bt-1], LET_Vertex, LET_Cross)
op.let_type = otype
end
end
op = ops[end]
if op.let_type == LET_Cut
s = op.space
ss = op.subspace[1]
subspin = site2subspin(s,ss,S2)
op.bottom_id = currents[subspin]
c = addnode!(uf)
currents[subspin] = c
op.top_id = c
spins[subspin] *= ifelse(op.isdiagonal, 1, -1)
else
b = op.space - nsites
ss1,ss2 = op.subspace
s1 = source(model, b)
s2 = target(model, b)
subspin1 = site2subspin(s1,ss1,S2)
subspin2 = site2subspin(s2,ss2,S2)
if op.let_type == LET_FMLink || op.let_type == LET_AFLink
c = unify!(uf, currents[subspin1], currents[subspin2])
currents[subspin1] = currents[subspin2] = c
op.bottom_id = op.top_id = c
elseif op.let_type == LET_Cross
op.bottom_id = currents[subspin1]
op.top_id = currents[subspin2]
spins[subspin1], spins[subspin2] = spins[subspin2], spins[subspin1]
currents[subspin1], currents[subspin2] = currents[subspin2], currents[subspin1]
else # if op.let_type == LET_Vertex
unify!(uf, currents[subspin1], currents[subspin2])
op.bottom_id = currents[subspin1]
c = addnode!(uf)
op.top_id = c
currents[subspin1] = currents[subspin2] = c
spins[subspin1] *= ifelse(op.isdiagonal, 1, -1)
spins[subspin2] *= ifelse(op.isdiagonal, 1, -1)
end
end
end # of while loop
## PBC for imaginary time axis
subspin = 0
for s in 1:nsites
ups0 = zeros(Int,0)
ups1 = zeros(Int,0)
downs0 = zeros(Int,0)
downs1 = zeros(Int,0)
for ss in 1:S2
subspin += 1
push!(ifelse(spins[subspin]==1, ups0, downs0), subspin)
push!(ifelse(model.spins[subspin]==1, ups1, downs1), subspin)
end
@assert length(ups0) == length(ups1)
@assert length(downs0) == length(downs1)
for (u, u2) in zip(ups0, myshuffle(rng,ups1))
unify!(uf, u, currents[u2])
end
for (d, d2) in zip(downs0, myshuffle(rng,downs1))
unify!(uf, d, currents[d2])
end
end
model.ops = ops
nc = clusterize!(uf)
flips = rand(rng, [1,-1], nc)
for s in 1:nspins
model.spins[s] *= flips[clusterid(uf, s)]
end
for op in model.ops
if op.let_type == LET_Cross || op.let_type == LET_Vertex || op.let_type == LET_Cut
bid = clusterid(uf, op.bottom_id)
tid = clusterid(uf, op.top_id)
op.isdiagonal ⊻= (flips[bid] != flips[tid])
end
end
return uf
end
@gen_convert_parameter(QuantumXXZ, ("T", 1, 1.0),
("Jz", numbondtypes, 1.0),
("Jxy", numbondtypes, 1.0),
("Gamma", numsitetypes, 0.0),
)