/
dmrg.jl
450 lines (404 loc) · 13.7 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
using Adapt: adapt
using KrylovKit: eigsolve
using NDTensors: scalartype, timer
using Printf: @printf
using TupleTools: TupleTools
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⃗ₙ = TupleTools.sort(Tuple(siteinds(M, n)); by=plev)
M̃[n] = ITensors.permute(M[n], filter(!isnothing, (lₙ₋₁, s⃗ₙ..., lₙ)))
end
set_ortho_lims!(M̃, ortho_lims(M))
return M̃
end
function dmrg(H::MPO, psi0::MPS, sweeps::Sweeps; kwargs...)
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
function dmrg(Hs::Vector{MPO}, psi0::MPS, sweeps::Sweeps; kwargs...)
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
function dmrg(H::MPO, Ms::Vector{MPS}, psi0::MPS, sweeps::Sweeps; weight=true, kwargs...)
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)))
if weight <= 0
error(
"weight parameter should be > 0.0 in call to excited-state dmrg (value passed was weight=$weight)",
)
end
PMM = ProjMPO_MPS(H, Ms; weight)
return dmrg(PMM, psi0, sweeps; kwargs...)
end
using NDTensors.TypeParameterAccessors: unwrap_array_type
"""
dmrg(H::MPO, psi0::MPS; kwargs...)
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).
dmrg(Hs::Vector{MPO}, psi0::MPS; kwargs...)
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`.
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.
dmrg(H::MPO, Ms::Vector{MPS}, psi0::MPS; weight=1.0, kwargs...)
dmrg(H::MPO, Ms::Vector{MPS}, psi0::MPS, sweeps::Sweeps; weight=1.0, 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.
!!! note
`dmrg` will report the energy of the operator
`H + w|M1><M1| + w|M2><M2| + ...`, not the operator `H`.
If you want the expectation value of the MPS eigenstate
with respect to just `H`, you can compute it yourself with
an observer or after DMRG is run with `inner(psi', H, psi)`.
The MPS `psi0` is used to initialize the MPS to be optimized.
The number of sweeps of thd DMRG algorithm is controlled by
passing the `nsweeps` keyword argument. The keyword arguments
`maxdim`, `cutoff`, `noise`, and `mindim` can also be passed
to control the cost versus accuracy of the algorithm - see below
for details.
Alternatively the number of sweeps and accuracy parameters can
be passed through a `Sweeps` object, though this interface is
no longer preferred.
Returns:
- `energy::Number` - eigenvalue of the optimized MPS
- `psi::MPS` - optimized MPS
Keyword arguments:
- `nsweeps::Int` - number of "sweeps" of DMRG to perform
Optional keyword arguments:
- `maxdim` - integer or array of integers specifying the maximum size
allowed for the bond dimension or rank of the MPS being optimized.
- `cutoff` - float or array of floats specifying the truncation error cutoff
or threshold to use for truncating the bond dimension or rank of the MPS.
- `eigsolve_krylovdim::Int = 3` - maximum dimension of Krylov space used to
locally solve the eigenvalue problem. Try setting to a higher value if
convergence is slow or the Hamiltonian is close to a critical point. [^krylovkit]
- `eigsolve_tol::Number = 1e-14` - Krylov eigensolver tolerance. [^krylovkit]
- `eigsolve_maxiter::Int = 1` - number of times the Krylov subspace can be
rebuilt. [^krylovkit]
- `eigsolve_verbosity::Int = 0` - verbosity level of the Krylov solver.
Warning: enabling this will lead to a lot of outputs to the terminal. [^krylovkit]
- `ishermitian=true` - boolean specifying if dmrg should assume the MPO (or more
general linear operator) represents a Hermitian matrix. [^krylovkit]
- `noise` - float or array of floats specifying strength of the "noise term"
to use to aid convergence.
- `mindim` - integer or array of integers specifying the minimum size of the
bond dimension or rank, if possible.
- `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 RAM memory in large calculations
- `write_path::String = tempdir()` - path to use to save files to disk
(to save RAM) when maxdim exceeds the `write_when_maxdim_exceeds` option, if set
[^krylovkit]:
The `dmrg` function in `ITensors.jl` currently uses the `eigsolve`
function in `KrylovKit.jl` as the internal the eigensolver.
See the `KrylovKit.jl` documention on the `eigsolve` function for more details:
[KrylovKit.eigsolve](https://jutho.github.io/KrylovKit.jl/stable/man/eig/#KrylovKit.eigsolve).
"""
function dmrg(
PH,
psi0::MPS,
sweeps::Sweeps;
which_decomp=nothing,
svd_alg=nothing,
observer=NoObserver(),
outputlevel=1,
write_when_maxdim_exceeds=nothing,
write_path=tempdir(),
# eigsolve kwargs
eigsolve_tol=1e-14,
eigsolve_krylovdim=3,
eigsolve_maxiter=1,
eigsolve_verbosity=0,
eigsolve_which_eigenvalue=:SR,
ishermitian=true,
)
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
psi = copy(psi0)
N = length(psi)
if !isortho(psi) || orthocenter(psi) != 1
psi = orthogonalize!(PH, psi, 1)
end
@assert isortho(psi) && orthocenter(psi) == 1
if !isnothing(write_when_maxdim_exceeds)
if (maxlinkdim(psi) > write_when_maxdim_exceeds) ||
(maxdim(sweeps, 1) > write_when_maxdim_exceeds)
PH = disk(PH; path=write_path)
end
end
PH = 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(
"\nWriting environment tensors do disk (write_when_maxdim_exceeds = $write_when_maxdim_exceeds and maxdim(sweeps, sw) = $(maxdim(sweeps, sw))).\nFiles located at path=$write_path\n",
)
end
PH = disk(PH; path=write_path)
end
for (b, ha) in sweepnext(N)
@debug_check begin
checkflux(psi)
checkflux(PH)
end
@timeit_debug timer "dmrg: position!" begin
PH = 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,
tol=eigsolve_tol,
krylovdim=eigsolve_krylovdim,
maxiter=eigsolve_maxiter,
)
end
energy = vals[1]
## Right now there is a conversion problem in CUDA.jl where `UnifiedMemory` Arrays are being converted
## into `DeviceMemory`. This conversion line is here temporarily to fix that problem when it arises
## Adapt is only called when using CUDA backend. CPU will work as implemented previously.
## TODO this might be the only place we really need iscu if its not fixed.
phi = if NDTensors.iscu(phi) && NDTensors.iscu(vecs[1])
adapt(ITensors.set_eltype(unwrap_array_type(phi), eltype(vecs[1])), vecs[1])
else
vecs[1]
end
ortho = ha == 1 ? "left" : "right"
drho = nothing
if noise(sweeps, sw) > 0
@timeit_debug timer "dmrg: noiseterm" begin
# Use noise term when determining new MPS basis.
# This is used to preserve the element type of the MPS.
elt = real(scalartype(psi))
drho = elt(noise(sweeps, sw)) * noiseterm(PH, phi, ortho)
end
end
@debug_check begin
checkflux(phi)
end
@timeit_debug timer "dmrg: replacebond!" begin
spec = replacebond!(
PH,
psi,
b,
phi;
maxdim=maxdim(sweeps, sw),
mindim=mindim(sweeps, sw),
cutoff=cutoff(sweeps, sw),
eigen_perturbation=drho,
ortho,
normalize=true,
which_decomp,
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=%s\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!(
observer;
energy,
psi,
projected_operator=PH,
bond=b,
sweep=sw,
half_sweep=ha,
spec,
outputlevel,
sweep_is_done,
)
end
end
if outputlevel >= 1
@printf(
"After sweep %d energy=%s maxlinkdim=%d maxerr=%.2E time=%.3f\n",
sw,
energy,
maxlinkdim(psi),
maxtruncerr,
sw_time
)
flush(stdout)
end
isdone = checkdone!(observer; energy, psi, sweep=sw, outputlevel)
isdone && break
end
return (energy, psi)
end
function _dmrg_sweeps(;
nsweeps,
maxdim=default_maxdim(),
mindim=default_mindim(),
cutoff=default_cutoff(Float64),
noise=default_noise(),
)
sweeps = Sweeps(nsweeps)
setmaxdim!(sweeps, maxdim...)
setmindim!(sweeps, mindim...)
setcutoff!(sweeps, cutoff...)
setnoise!(sweeps, noise...)
return sweeps
end
function dmrg(
x1,
x2,
psi0::MPS;
nsweeps,
maxdim=default_maxdim(),
mindim=default_mindim(),
cutoff=default_cutoff(Float64),
noise=default_noise(),
kwargs...,
)
return dmrg(
x1, x2, psi0, _dmrg_sweeps(; nsweeps, maxdim, mindim, cutoff, noise); kwargs...
)
end
function dmrg(
x1,
psi0::MPS;
nsweeps,
maxdim=default_maxdim(),
mindim=default_mindim(),
cutoff=default_cutoff(Float64),
noise=default_noise(),
kwargs...,
)
return dmrg(x1, psi0, _dmrg_sweeps(; nsweeps, maxdim, mindim, cutoff, noise); kwargs...)
end
# Implementation of DMRG originally from ITensorTDVP package
function dmrg_solver(
f::typeof(eigsolve);
solver_which_eigenvalue,
ishermitian,
solver_tol,
solver_krylovdim,
solver_maxiter,
solver_verbosity,
)
function solver(H, t, psi0; current_time, outputlevel)
howmany = 1
which = solver_which_eigenvalue
vals, vecs, info = f(
H,
psi0,
howmany,
which;
ishermitian=default_ishermitian(),
tol=solver_tol,
krylovdim=solver_krylovdim,
maxiter=solver_maxiter,
verbosity=solver_verbosity,
)
psi = vecs[1]
return psi, info
end
return solver
end
function itensortdvp_dmrg(
H,
psi0::MPS;
ishermitian=default_ishermitian(),
solver_which_eigenvalue=default_solver_which_eigenvalue(eigsolve),
solver_tol=default_solver_tol(eigsolve),
solver_krylovdim=default_solver_krylovdim(eigsolve),
solver_maxiter=default_solver_maxiter(eigsolve),
solver_verbosity=default_solver_verbosity(),
kwargs...,
)
reverse_step = false
psi = alternating_update(
dmrg_solver(
eigsolve;
solver_which_eigenvalue,
ishermitian,
solver_tol,
solver_krylovdim,
solver_maxiter,
solver_verbosity,
),
H,
psi0;
reverse_step,
kwargs...,
)
return psi
end