/
dmrg.jl
336 lines (291 loc) · 10.4 KB
/
dmrg.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#function IndexSet_ignore_missing(is::Union{Index,Nothing}...)
# return IndexSet(filter(i -> i isa Index, is))
#end
function permute(
M::AbstractMPS, ::Tuple{typeof(linkind),typeof(siteinds),typeof(linkind)}
)::typeof(M)
M̃ = typeof(M)(length(M))
for n in 1:length(M)
lₙ₋₁ = linkind(M, n - 1)
lₙ = linkind(M, n)
s⃗ₙ = sort(Tuple(siteinds(M, n)); by=plev)
M̃[n] = permute(M[n], filter(!isnothing, (lₙ₋₁, s⃗ₙ..., lₙ)))
end
set_ortho_lims!(M̃, ortho_lims(M))
return M̃
end
"""
dmrg(H::MPO,psi0::MPS,sweeps::Sweeps;kwargs...)
Use the density matrix renormalization group (DMRG) algorithm
to optimize a matrix product state (MPS) such that it is the
eigenvector of lowest eigenvalue of a Hermitian matrix H,
represented as a matrix product operator (MPO).
The MPS `psi0` is used to initialize the MPS to be optimized,
and the `sweeps` object determines the parameters used to
control the DMRG algorithm.
Returns:
* `energy::Float64` - eigenvalue of the optimized MPS
* `psi::MPS` - optimized MPS
Optional keyword arguments:
* `outputlevel::Int = 1` - larger outputlevel values make DMRG print more information and 0 means no output
* `observer` - object implementing the [Observer](@ref observer) interface which can perform measurements and stop DMRG early
* `write_when_maxdim_exceeds::Int` - when the allowed maxdim exceeds this value, begin saving tensors to disk to free memory in large calculations
"""
function dmrg(H::MPO, psi0::MPS, sweeps::Sweeps; kwargs...)::Tuple{Number,MPS}
check_hascommoninds(siteinds, H, psi0)
check_hascommoninds(siteinds, H, psi0')
# Permute the indices to have a better memory layout
# and minimize permutations
H = permute(H, (linkind, siteinds, linkind))
PH = ProjMPO(H)
return dmrg(PH, psi0, sweeps; kwargs...)
end
"""
dmrg(Hs::Vector{MPO},psi0::MPS,sweeps::Sweeps;kwargs...)
Use the density matrix renormalization group (DMRG) algorithm
to optimize a matrix product state (MPS) such that it is the
eigenvector of lowest eigenvalue of a Hermitian matrix H.
The MPS `psi0` is used to initialize the MPS to be optimized,
and the `sweeps` object determines the parameters used to
control the DMRG algorithm.
This version of `dmrg` accepts a representation of H as a
Vector of MPOs, Hs = [H1,H2,H3,...] such that H is defined
as H = H1+H2+H3+...
Note that this sum of MPOs is not actually computed; rather
the set of MPOs [H1,H2,H3,..] is efficiently looped over at
each step of the DMRG algorithm when optimizing the MPS.
Returns:
* `energy::Float64` - eigenvalue of the optimized MPS
* `psi::MPS` - optimized MPS
"""
function dmrg(Hs::Vector{MPO}, psi0::MPS, sweeps::Sweeps; kwargs...)::Tuple{Number,MPS}
for H in Hs
check_hascommoninds(siteinds, H, psi0)
check_hascommoninds(siteinds, H, psi0')
end
Hs .= permute.(Hs, Ref((linkind, siteinds, linkind)))
PHS = ProjMPOSum(Hs)
return dmrg(PHS, psi0, sweeps; kwargs...)
end
"""
dmrg(H::MPO,Ms::Vector{MPS},psi0::MPS,sweeps::Sweeps;kwargs...)
Use the density matrix renormalization group (DMRG) algorithm
to optimize a matrix product state (MPS) such that it is the
eigenvector of lowest eigenvalue of a Hermitian matrix H,
subject to the constraint that the MPS is orthogonal to each
of the MPS provided in the Vector `Ms`. The orthogonality
constraint is approximately enforced by adding to H terms of
the form w|M1><M1| + w|M2><M2| + ... where Ms=[M1,M2,...] and
w is the "weight" parameter, which can be adjusted through the
optional `weight` keyword argument.
The MPS `psi0` is used to initialize the MPS to be optimized,
and the `sweeps` object determines the parameters used to
control the DMRG algorithm.
Returns:
* `energy::Float64` - eigenvalue of the optimized MPS
* `psi::MPS` - optimized MPS
"""
function dmrg(
H::MPO, Ms::Vector{MPS}, psi0::MPS, sweeps::Sweeps; kwargs...
)::Tuple{Number,MPS}
check_hascommoninds(siteinds, H, psi0)
check_hascommoninds(siteinds, H, psi0')
for M in Ms
check_hascommoninds(siteinds, M, psi0)
end
H = permute(H, (linkind, siteinds, linkind))
Ms .= permute.(Ms, Ref((linkind, siteinds, linkind)))
weight = get(kwargs, :weight, 1.0)
PMM = ProjMPO_MPS(H, Ms; weight=weight)
return dmrg(PMM, psi0, sweeps; kwargs...)
end
function dmrg(PH, psi0::MPS, sweeps::Sweeps; kwargs...)::Tuple{Number,MPS}
if length(psi0) == 1
error(
"`dmrg` currently does not support system sizes of 1. You can diagonalize the MPO tensor directly with tools like `LinearAlgebra.eigen`, `KrylovKit.eigsolve`, etc.",
)
end
@debug_check begin
# Debug level checks
# Enable with ITensors.enable_debug_checks()
checkflux(psi0)
checkflux(PH)
end
which_decomp::Union{String,Nothing} = get(kwargs, :which_decomp, nothing)
svd_alg::String = get(kwargs, :svd_alg, "divide_and_conquer")
obs = get(kwargs, :observer, NoObserver())
outputlevel::Int = get(kwargs, :outputlevel, 1)
write_when_maxdim_exceeds::Union{Int,Nothing} = get(
kwargs, :write_when_maxdim_exceeds, nothing
)
# eigsolve kwargs
eigsolve_tol::Float64 = get(kwargs, :eigsolve_tol, 1e-14)
eigsolve_krylovdim::Int = get(kwargs, :eigsolve_krylovdim, 3)
eigsolve_maxiter::Int = get(kwargs, :eigsolve_maxiter, 1)
eigsolve_verbosity::Int = get(kwargs, :eigsolve_verbosity, 0)
# TODO: add support for non-Hermitian DMRG
ishermitian::Bool = get(kwargs, :ishermitian, true)
# TODO: add support for targeting other states with DMRG
# (such as the state with the largest eigenvalue)
# get(kwargs, :eigsolve_which_eigenvalue, :SR)
eigsolve_which_eigenvalue::Symbol = :SR
# TODO: use this as preferred syntax for passing arguments
# to eigsolve
#default_eigsolve_args = (tol = 1e-14, krylovdim = 3, maxiter = 1,
# verbosity = 0, ishermitian = true,
# which_eigenvalue = :SR)
#eigsolve = get(kwargs, :eigsolve, default_eigsolve_args)
# Keyword argument deprecations
if haskey(kwargs, :maxiter)
error("""maxiter keyword has been replaced by eigsolve_krylovdim.
Note: compared to the C++ version of ITensor,
setting eigsolve_krylovdim 3 is the same as setting
a maxiter of 2.""")
end
if haskey(kwargs, :errgoal)
error("errgoal keyword has been replaced by eigsolve_tol.")
end
if haskey(kwargs, :quiet)
error("quiet keyword has been replaced by outputlevel")
end
psi = copy(psi0)
N = length(psi)
if !isortho(psi) || orthocenter(psi) != 1
orthogonalize!(psi, 1)
end
@assert isortho(psi) && orthocenter(psi) == 1
position!(PH, psi, 1)
energy = 0.0
for sw in 1:nsweep(sweeps)
sw_time = @elapsed begin
maxtruncerr = 0.0
if !isnothing(write_when_maxdim_exceeds) &&
maxdim(sweeps, sw) > write_when_maxdim_exceeds
if outputlevel >= 2
println(
"write_when_maxdim_exceeds = $write_when_maxdim_exceeds and maxdim(sweeps, sw) = $(maxdim(sweeps, sw)), writing environment tensors to disk",
)
end
PH = disk(PH)
end
for (b, ha) in sweepnext(N)
@debug_check begin
checkflux(psi)
checkflux(PH)
end
@timeit_debug timer "dmrg: position!" begin
position!(PH, psi, b)
end
@debug_check begin
checkflux(psi)
checkflux(PH)
end
@timeit_debug timer "dmrg: psi[b]*psi[b+1]" begin
phi = psi[b] * psi[b + 1]
end
@timeit_debug timer "dmrg: eigsolve" begin
vals, vecs = eigsolve(
PH,
phi,
1,
eigsolve_which_eigenvalue;
ishermitian=ishermitian,
tol=eigsolve_tol,
krylovdim=eigsolve_krylovdim,
maxiter=eigsolve_maxiter,
)
end
energy::Number = vals[1]
phi::ITensor = vecs[1]
ortho = ha == 1 ? "left" : "right"
drho = nothing
if noise(sweeps, sw) > 0.0
@timeit_debug timer "dmrg: noiseterm" begin
# Use noise term when determining new MPS basis
drho = noise(sweeps, sw) * noiseterm(PH, phi, ortho)
end
end
@debug_check begin
checkflux(phi)
end
@timeit_debug timer "dmrg: replacebond!" begin
spec = replacebond!(
psi,
b,
phi;
maxdim=maxdim(sweeps, sw),
mindim=mindim(sweeps, sw),
cutoff=cutoff(sweeps, sw),
eigen_perturbation=drho,
ortho=ortho,
normalize=true,
which_decomp=which_decomp,
svd_alg=svd_alg,
)
end
maxtruncerr = max(maxtruncerr, spec.truncerr)
@debug_check begin
checkflux(psi)
checkflux(PH)
end
if outputlevel >= 2
@printf(
"Sweep %d, half %d, bond (%d,%d) energy=%.12f\n", sw, ha, b, b + 1, energy
)
@printf(
" Truncated using cutoff=%.1E maxdim=%d mindim=%d\n",
cutoff(sweeps, sw),
maxdim(sweeps, sw),
mindim(sweeps, sw)
)
@printf(
" Trunc. err=%.2E, bond dimension %d\n", spec.truncerr, dim(linkind(psi, b))
)
flush(stdout)
end
sweep_is_done = (b == 1 && ha == 2)
measure!(
obs;
energy=energy,
psi=psi,
bond=b,
sweep=sw,
half_sweep=ha,
spec=spec,
outputlevel=outputlevel,
sweep_is_done=sweep_is_done,
)
end
end
if outputlevel >= 1
@printf(
"After sweep %d energy=%.12f maxlinkdim=%d maxerr=%.2E time=%.3f\n",
sw,
energy,
maxlinkdim(psi),
maxtruncerr,
sw_time
)
flush(stdout)
end
isdone = checkdone!(obs; energy=energy, psi=psi, sweep=sw, outputlevel=outputlevel)
isdone && break
end
return (energy, psi)
end
function _dmrg_sweeps(;
nsweeps, maxdim=typemax(Int), mindim=1, cutoff=1E-8, noise=0.0, kwargs...
)
sweeps = Sweeps(nsweeps)
setmaxdim!(sweeps, maxdim...)
setmindim!(sweeps, mindim...)
setcutoff!(sweeps, cutoff...)
setnoise!(sweeps, noise...)
return sweeps
end
function dmrg(x1, x2, psi0::MPS; kwargs...)
return dmrg(x1, x2, psi0, _dmrg_sweeps(; kwargs...); kwargs...)
end
function dmrg(x1, psi0::MPS; kwargs...)
return dmrg(x1, psi0, _dmrg_sweeps(; kwargs...); kwargs...)
end