/
sweeps.jl
266 lines (228 loc) · 6.34 KB
/
sweeps.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Printf: @printf
"""
A Sweeps objects holds information
about the various parameters controlling
a density matrix renormalization group (DMRG)
or similar matrix product state (MPS) calculation.
For a Sweeps object `sw` the available
parameters are:
- `nsweep(sw)` -- the number of sweeps to do
- `maxdim(sw,n)` -- maximum MPS bond dimension for sweep n
- `mindim(sw,n)` -- minimum MPS bond dimension for sweep n
- `cutoff(sw,n)` -- truncation error cutoff for sweep n
- `noise(sw,n)` -- noise term coefficient for sweep n
"""
mutable struct Sweeps
nsweep::Int
maxdim::Vector{Int}
cutoff::Vector{Float64}
mindim::Vector{Int}
noise::Vector{Float64}
function Sweeps(nsw::Int; maxdim=typemax(Int), cutoff=1E-16, mindim=1, noise=0.0)
sw = new(nsw, fill(typemax(Int), nsw), fill(1E-16, nsw), fill(1, nsw), fill(0.0, nsw))
setmaxdim!(sw, maxdim...)
setmindim!(sw, mindim...)
setcutoff!(sw, cutoff...)
setnoise!(sw, noise...)
return sw
end
end
Sweeps() = Sweeps(0)
"""
Sweeps(d::AbstractMatrix)
Sweeps(nsweep::Int, d::AbstractMatrix)
Make a sweeps object from a matrix of input values.
The first row should be strings that define which
variables are being set ("maxdim", "cutoff", "mindim",
and "noise").
If the number of sweeps are not specified, they
are determined from the size of the input matrix.
# Examples
```julia
julia > Sweeps(
[
"maxdim" "mindim" "cutoff" "noise"
50 10 1e-12 1E-7
100 20 1e-12 1E-8
200 20 1e-12 1E-10
400 20 1e-12 0
800 20 1e-12 1E-11
800 20 1e-12 0
],
)
Sweeps
1cutoff = 1.0E-12, maxdim = 50, mindim = 10, noise = 1.0E-07
2cutoff = 1.0E-12, maxdim = 100, mindim = 20, noise = 1.0E-08
3cutoff = 1.0E-12, maxdim = 200, mindim = 20, noise = 1.0E-10
4cutoff = 1.0E-12, maxdim = 400, mindim = 20, noise = 0.0E+00
5cutoff = 1.0E-12, maxdim = 800, mindim = 20, noise = 1.0E-11
6cutoff = 1.0E-12, maxdim = 800, mindim = 20, noise = 0.0E+00
```
"""
function Sweeps(nsw::Int, d::AbstractMatrix)
sw = Sweeps(nsw)
vars = d[1, :]
for (n, var) in enumerate(vars)
inputs = d[2:end, n]
if var == "maxdim"
maxdim!(sw, inputs...)
elseif var == "cutoff"
cutoff!(sw, inputs...)
elseif var == "mindim"
mindim!(sw, inputs...)
elseif var == "noise"
noise!(sw, float.(inputs)...)
else
error("Sweeps object does not have the field $var")
end
end
return sw
end
Sweeps(d::AbstractMatrix) = Sweeps(size(d, 1) - 1, d)
"""
nsweep(sw::Sweeps)
length(sw::Sweeps)
Obtain the number of sweeps parameterized
by this sweeps object.
"""
nsweep(sw::Sweeps)::Int = sw.nsweep
Base.length(sw::Sweeps)::Int = sw.nsweep
Base.isempty(sw::Sweeps)::Bool = (sw.nsweep == 0)
"""
maxdim(sw::Sweeps,n::Int)
Maximum MPS bond dimension allowed by the
Sweeps object `sw` during sweep `n`
"""
maxdim(sw::Sweeps, n::Int)::Int = sw.maxdim[n]
"""
mindim(sw::Sweeps,n::Int)
Minimum MPS bond dimension allowed by the
Sweeps object `sw` during sweep `n`
"""
mindim(sw::Sweeps, n::Int)::Int = sw.mindim[n]
"""
cutoff(sw::Sweeps,n::Int)
Truncation error cutoff setting of the
Sweeps object `sw` during sweep `n`
"""
cutoff(sw::Sweeps, n::Int)::Float64 = sw.cutoff[n]
"""
noise(sw::Sweeps,n::Int)
Noise term coefficient setting of the
Sweeps object `sw` during sweep `n`
"""
noise(sw::Sweeps, n::Int)::Float64 = sw.noise[n]
get_maxdims(sw::Sweeps) = sw.maxdim
get_mindims(sw::Sweeps) = sw.mindim
get_cutoffs(sw::Sweeps) = sw.cutoff
get_noises(sw::Sweeps) = sw.noise
"""
maxdim!(sw::Sweeps,maxdims::Int...)
Set the maximum MPS bond dimension for each
sweep by providing up to `nsweep(sw)` values.
If fewer values are provided, the last value
is repeated for the remaining sweeps.
"""
function setmaxdim!(sw::Sweeps, maxdims::Int...)::Nothing
mdims = collect(maxdims)
for i in 1:nsweep(sw)
sw.maxdim[i] = get(mdims, i, maxdims[end])
end
end
maxdim!(sw::Sweeps, maxdims::Int...) = setmaxdim!(sw, maxdims...)
"""
mindim!(sw::Sweeps,maxdims::Int...)
Set the minimum MPS bond dimension for each
sweep by providing up to `nsweep(sw)` values.
If fewer values are provided, the last value
is repeated for the remaining sweeps.
"""
function setmindim!(sw::Sweeps, mindims::Int...)::Nothing
mdims = collect(mindims)
for i in 1:nsweep(sw)
sw.mindim[i] = get(mdims, i, mindims[end])
end
end
mindim!(sw::Sweeps, mindims::Int...) = setmindim!(sw, mindims...)
"""
cutoff!(sw::Sweeps,maxdims::Int...)
Set the MPS truncation error used for each
sweep by providing up to `nsweep(sw)` values.
If fewer values are provided, the last value
is repeated for the remaining sweeps.
"""
function setcutoff!(sw::Sweeps, cutoffs::Real...)::Nothing
cuts = collect(cutoffs)
for i in 1:nsweep(sw)
sw.cutoff[i] = get(cuts, i, cutoffs[end])
end
end
cutoff!(sw::Sweeps, cutoffs::Real...) = setcutoff!(sw, cutoffs...)
"""
noise!(sw::Sweeps,maxdims::Int...)
Set the noise-term coefficient used for each
sweep by providing up to `nsweep(sw)` values.
If fewer values are provided, the last value
is repeated for the remaining sweeps.
"""
function setnoise!(sw::Sweeps, noises::Real...)::Nothing
nvals = collect(noises)
for i in 1:nsweep(sw)
sw.noise[i] = get(nvals, i, noises[end])
end
end
noise!(sw::Sweeps, noises::Real...) = setnoise!(sw, noises...)
function Base.show(io::IO, sw::Sweeps)
println(io, "Sweeps")
for n in 1:nsweep(sw)
@printf(
io,
"%d cutoff=%.1E, maxdim=%d, mindim=%d, noise=%.1E\n",
n,
cutoff(sw, n),
maxdim(sw, n),
mindim(sw, n),
noise(sw, n)
)
end
end
struct SweepNext
N::Int
ncenter::Int
end
"""
sweepnext(N::Int; ncenter::Int=2)
Returns an iterable object that evaluates
to tuples of the form `(b,ha)` where `b`
is the bond number and `ha` is the half-sweep
number. Takes an optional named argument
`ncenter` for use with an n-site MPS or DMRG
algorithm, with a default of 2-site.
"""
function sweepnext(N::Int; ncenter::Int=2)::SweepNext
if ncenter < 0
error("ncenter must be non-negative")
end
return SweepNext(N, ncenter)
end
function Base.iterate(sn::SweepNext, state=(0, 1))
b, ha = state
if ha == 1
inc = 1
bstop = sn.N - sn.ncenter + 2
else
inc = -1
bstop = 0
end
new_b = b + inc
new_ha = ha
done = false
if new_b == bstop
new_b -= inc
new_ha += 1
if ha == 2
return nothing
end
end
return ((new_b, new_ha), (new_b, new_ha))
end