Skip to content

Commit d30ef9e

Browse files
authored
Consistent environments signature: below, operator, above argument order (#268)
* Change calling syntax to `(below, operator, above=below)` for infinite MPS environments * Tweak test to cover previously broken case * Use consistent bond dimensions
1 parent ceda9d2 commit d30ef9e

File tree

7 files changed

+94
-92
lines changed

7 files changed

+94
-92
lines changed

src/algorithms/toolbox.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ function infinite_temperature_density_matrix(H::MPOHamiltonian)
3636
end
3737

3838
"""
39-
calc_galerkin(above, operator, below, envs)
40-
calc_galerkin(pos, above, operator, below, envs)
39+
calc_galerkin(below, operator, above, envs)
40+
calc_galerkin(pos, below, operator, above, envs)
4141
4242
Calculate the Galerkin error, which is the error between the solution of the original problem, and the solution of the problem projected on the tangent space.
4343
Concretely, this is the overlap of the current state with the single-site derivative, projected onto the nullspace of the current state:
@@ -46,25 +46,25 @@ Concretely, this is the overlap of the current state with the single-site deriva
4646
\\epsilon = |VL * (VL' * \\frac{above}{\\partial AC_{pos}})|
4747
```
4848
"""
49-
function calc_galerkin(pos::Int, above::Union{InfiniteMPS,FiniteMPS,WindowMPS}, operator,
50-
below, envs)
51-
AC´ = ∂∂AC(pos, above, operator, envs) * above.AC[pos]
49+
function calc_galerkin(pos::Int, below::Union{InfiniteMPS,FiniteMPS,WindowMPS}, operator,
50+
above, envs)
51+
AC´ = ∂∂AC(pos, below, operator, envs) * above.AC[pos]
5252
normalize!(AC´)
5353
out = add!(AC´, below.AL[pos] * below.AL[pos]' * AC´, -1)
5454
return norm(out)
5555
end
56-
function calc_galerkin(pos::CartesianIndex{2}, above::MultilineMPS, operator::MultilineMPO,
57-
below::MultilineMPS, envs::MultilineEnvironments)
56+
function calc_galerkin(pos::CartesianIndex{2}, below::MultilineMPS, operator::MultilineMPO,
57+
above::MultilineMPS, envs::MultilineEnvironments)
5858
row, col = pos.I
59-
return calc_galerkin(col, above[row], operator[row], below[row + 1], envs[row])
59+
return calc_galerkin(col, below[row + 1], operator[row], above[row], envs[row])
6060
end
61-
function calc_galerkin(above::Union{InfiniteMPS,FiniteMPS,WindowMPS}, operator,
62-
below, envs)
63-
return maximum(pos -> calc_galerkin(pos, above, operator, below, envs), 1:length(above))
61+
function calc_galerkin(below::Union{InfiniteMPS,FiniteMPS,WindowMPS}, operator,
62+
above, envs)
63+
return maximum(pos -> calc_galerkin(pos, below, operator, above, envs), 1:length(above))
6464
end
65-
function calc_galerkin(above::MultilineMPS, operator::MultilineMPO, below::MultilineMPS,
65+
function calc_galerkin(below::MultilineMPS, operator::MultilineMPO, above::MultilineMPS,
6666
envs::MultilineEnvironments)
67-
return maximum(pos -> calc_galerkin(pos, above, operator, below, envs),
67+
return maximum(pos -> calc_galerkin(pos, below, operator, above, envs),
6868
CartesianIndices(size(above)))
6969
end
7070

src/environments/abstract_envs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ end
6464
# Environment algorithms
6565
# ----------------------
6666
"""
67-
environment_alg(above, operator, below; kwargs...)
67+
environment_alg(below, operator, above; kwargs...)
6868
6969
Determine an appropriate algorithm for computing the environments, based on the given `kwargs...`.
7070
"""
@@ -76,7 +76,7 @@ function environment_alg(::Union{InfiniteMPS,MultilineMPS},
7676
eager=true)
7777
return Arnoldi(; tol, maxiter, krylovdim, verbosity, eager)
7878
end
79-
function environment_alg(above, ::InfiniteMPOHamiltonian, below;
79+
function environment_alg(below, ::InfiniteMPOHamiltonian, above;
8080
tol=Defaults.tol, maxiter=Defaults.maxiter,
8181
krylovdim=Defaults.krylovdim, verbosity=Defaults.VERBOSE_NONE)
8282
return GMRES(; tol, maxiter, krylovdim, verbosity)

src/environments/infinite_envs.jl

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ Base.length(envs::InfiniteEnvironments) = length(envs.GLs)
1818
leftenv(envs::InfiniteEnvironments, site::Int, state) = envs.GLs[site]
1919
rightenv(envs::InfiniteEnvironments, site::Int, state) = envs.GRs[site]
2020

21-
function environments(above::InfiniteMPS,
21+
function environments(below::InfiniteMPS,
2222
operator::Union{InfiniteMPO,InfiniteMPOHamiltonian},
23-
below::InfiniteMPS=above;
23+
above::InfiniteMPS=below;
2424
kwargs...)
25-
GLs, GRs = initialize_environments(above, operator, below)
25+
GLs, GRs = initialize_environments(below, operator, above)
2626
envs = InfiniteEnvironments(GLs, GRs)
27-
return recalculate!(envs, above, operator, below; kwargs...)
27+
return recalculate!(envs, below, operator, above; kwargs...)
2828
end
2929

30-
function issamespace(envs::InfiniteEnvironments, above::InfiniteMPS,
30+
function issamespace(envs::InfiniteEnvironments, below::InfiniteMPS,
3131
operator::Union{InfiniteMPO,InfiniteMPOHamiltonian},
32-
below::InfiniteMPS)
33-
L = check_length(above, operator, below)
32+
above::InfiniteMPS)
33+
L = check_length(below, operator, above)
3434
for i in 1:L
3535
space(envs.GLs[i]) ==
3636
(left_virtualspace(below, i) left_virtualspace(operator, i)'
@@ -42,39 +42,39 @@ function issamespace(envs::InfiniteEnvironments, above::InfiniteMPS,
4242
return true
4343
end
4444

45-
function recalculate!(envs::InfiniteEnvironments, above::InfiniteMPS,
45+
function recalculate!(envs::InfiniteEnvironments, below::InfiniteMPS,
4646
operator::Union{InfiniteMPO,InfiniteMPOHamiltonian},
47-
below::InfiniteMPS=above; kwargs...)
48-
if !issamespace(envs, above, operator, below)
47+
above::InfiniteMPS=below; kwargs...)
48+
if !issamespace(envs, below, operator, above)
4949
# TODO: in-place initialization?
50-
GLs, GRs = initialize_environments(above, operator, below)
50+
GLs, GRs = initialize_environments(below, operator, above)
5151
copy!(envs.GLs, GLs)
5252
copy!(envs.GRs, GRs)
5353
end
5454

55-
alg = environment_alg(above, operator, below; kwargs...)
55+
alg = environment_alg(below, operator, above; kwargs...)
5656

5757
@sync begin
58-
@spawn compute_leftenvs!(envs, above, operator, below, alg)
59-
@spawn compute_rightenvs!(envs, above, operator, below, alg)
58+
@spawn compute_leftenvs!(envs, below, operator, above, alg)
59+
@spawn compute_rightenvs!(envs, below, operator, above, alg)
6060
end
61-
normalize!(envs, above, operator, below)
61+
normalize!(envs, below, operator, above)
6262

6363
return envs
6464
end
6565

6666
# InfiniteMPO environments
6767
# ------------------------
68-
function initialize_environments(above::InfiniteMPS, operator::InfiniteMPO,
69-
below::InfiniteMPS=above)
70-
L = check_length(above, operator, below)
68+
function initialize_environments(below::InfiniteMPS, operator::InfiniteMPO,
69+
above::InfiniteMPS=below)
70+
L = check_length(below, operator, above)
7171
GLs = PeriodicVector([randomize!(allocate_GL(below, operator, above, i)) for i in 1:L])
7272
GRs = PeriodicVector([randomize!(allocate_GR(below, operator, above, i)) for i in 1:L])
7373
return GLs, GRs
7474
end
7575

76-
function compute_leftenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
77-
operator::InfiniteMPO, below::InfiniteMPS, alg)
76+
function compute_leftenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
77+
operator::InfiniteMPO, above::InfiniteMPS, alg)
7878
# compute eigenvector
7979
T = TransferMatrix(above.AL, operator, below.AL)
8080
λ, envs.GLs[1] = fixedpoint(flip(T), envs.GLs[1], :LM, alg)
@@ -86,8 +86,8 @@ function compute_leftenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
8686
return λ, envs
8787
end
8888

89-
function compute_rightenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
90-
operator::InfiniteMPO, below::InfiniteMPS, alg)
89+
function compute_rightenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
90+
operator::InfiniteMPO, above::InfiniteMPS, alg)
9191
# compute eigenvector
9292
T = TransferMatrix(above.AR, operator, below.AR)
9393
λ, envs.GRs[end] = fixedpoint(T, envs.GRs[end], :LM, alg)
@@ -99,8 +99,8 @@ function compute_rightenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
9999
return λ, envs
100100
end
101101

102-
function TensorKit.normalize!(envs::InfiniteEnvironments, above::InfiniteMPS,
103-
operator::InfiniteMPO, below::InfiniteMPS)
102+
function TensorKit.normalize!(envs::InfiniteEnvironments, below::InfiniteMPS,
103+
operator::InfiniteMPO, above::InfiniteMPS)
104104
for i in 1:length(operator)
105105
λ = dot(below.C[i], MPO_∂∂C(envs.GLs[i + 1], envs.GRs[i]) * above.C[i])
106106
scale!(envs.GLs[i + 1], inv(λ))
@@ -110,8 +110,8 @@ end
110110

111111
# InfiniteMPOHamiltonian environments
112112
# -----------------------------------
113-
function initialize_environments(above::InfiniteMPS, operator::InfiniteMPOHamiltonian,
114-
below::InfiniteMPS=above)
113+
function initialize_environments(below::InfiniteMPS, operator::InfiniteMPOHamiltonian,
114+
above::InfiniteMPS=below)
115115
L = check_length(above, operator, below)
116116
GLs = PeriodicVector([allocate_GL(below, operator, above, i) for i in 1:L])
117117
GRs = PeriodicVector([allocate_GR(below, operator, above, i) for i in 1:L])
@@ -139,9 +139,9 @@ function initialize_environments(above::InfiniteMPS, operator::InfiniteMPOHamilt
139139
return GLs, GRs
140140
end
141141

142-
function compute_leftenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
143-
operator::InfiniteMPOHamiltonian, below::InfiniteMPS, alg)
144-
L = check_length(above, operator, below)
142+
function compute_leftenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
143+
operator::InfiniteMPOHamiltonian, above::InfiniteMPS, alg)
144+
L = check_length(below, above, operator)
145145
GLs = envs.GLs
146146
vsize = length(first(GLs))
147147

@@ -156,20 +156,20 @@ function compute_leftenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
156156
# fill_data!(leftutil, one)
157157
# @plansor GL[1][1][-1 -2; -3] = ρ_left[-1; -3] * leftutil[-2]
158158

159-
(L > 1) && left_cyclethrough!(1, GLs, above, operator, below)
159+
(L > 1) && left_cyclethrough!(1, GLs, below, operator, above)
160160

161161
for i in 2:vsize
162162
prev = copy(GLs[1][i])
163163
zerovector!(GLs[1][i])
164-
left_cyclethrough!(i, GLs, above, operator, below)
164+
left_cyclethrough!(i, GLs, below, operator, above)
165165

166166
if isidentitylevel(operator, i) # identity matrices; do the hacky renormalization
167167
T = regularize(TransferMatrix(above.AL, below.AL), ρ_left, ρ_right)
168168
GLs[1][i], convhist = linsolve(flip(T), GLs[1][i], prev, alg, 1, -1)
169169
convhist.converged == 0 &&
170170
@warn "GL$i failed to converge: normres = $(convhist.normres)"
171171

172-
(L > 1) && left_cyclethrough!(i, GLs, above, operator, below)
172+
(L > 1) && left_cyclethrough!(i, GLs, below, operator, above)
173173

174174
# go through the unitcell, again subtracting fixpoints
175175
for site in 1:L
@@ -186,15 +186,15 @@ function compute_leftenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
186186
convhist.converged == 0 &&
187187
@warn "GL$i failed to converge: normres = $(convhist.normres)"
188188
end
189-
(L > 1) && left_cyclethrough!(i, GLs, above, operator, below)
189+
(L > 1) && left_cyclethrough!(i, GLs, below, operator, above)
190190
end
191191
end
192192

193193
return GLs
194194
end
195195

196-
function left_cyclethrough!(index::Int, GL, above::InfiniteMPS, H::InfiniteMPOHamiltonian,
197-
below::InfiniteMPS=above)
196+
function left_cyclethrough!(index::Int, GL, below::InfiniteMPS, H::InfiniteMPOHamiltonian,
197+
above::InfiniteMPS=below)
198198
# TODO: efficient transfer matrix slicing for large unitcells
199199
leftinds = 1:index
200200
for site in eachindex(GL)
@@ -205,8 +205,8 @@ function left_cyclethrough!(index::Int, GL, above::InfiniteMPS, H::InfiniteMPOHa
205205
return GL
206206
end
207207

208-
function compute_rightenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
209-
operator::InfiniteMPOHamiltonian, below::InfiniteMPS, alg)
208+
function compute_rightenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
209+
operator::InfiniteMPOHamiltonian, above::InfiniteMPS, alg)
210210
L = check_length(above, operator, below)
211211
GRs = envs.GRs
212212
vsize = length(last(GRs))
@@ -222,12 +222,12 @@ function compute_rightenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
222222
# fill_data!(rightutil, one)
223223
# @plansor GR[end][end][-1 -2; -3] = r_RR(state)[-1; -3] * rightutil[-2]
224224

225-
(L > 1) && right_cyclethrough!(vsize, GRs, above, operator, below) # populate other sites
225+
(L > 1) && right_cyclethrough!(vsize, GRs, below, operator, above) # populate other sites
226226

227227
for i in (vsize - 1):-1:1
228228
prev = copy(GRs[end][i])
229229
zerovector!(GRs[end][i])
230-
right_cyclethrough!(i, GRs, above, operator, below)
230+
right_cyclethrough!(i, GRs, below, operator, above)
231231

232232
if isidentitylevel(operator, i) # identity matrices; do the hacky renormalization
233233
# subtract fixpoints
@@ -236,7 +236,7 @@ function compute_rightenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
236236
convhist.converged == 0 &&
237237
@warn "GR$i failed to converge: normres = $(convhist.normres)"
238238

239-
L > 1 && right_cyclethrough!(i, GRs, above, operator, below)
239+
L > 1 && right_cyclethrough!(i, GRs, below, operator, above)
240240

241241
# go through the unitcell, again subtracting fixpoints
242242
for site in 1:L
@@ -253,16 +253,16 @@ function compute_rightenvs!(envs::InfiniteEnvironments, above::InfiniteMPS,
253253
@warn "GR$i failed to converge: normres = $(convhist.normres)"
254254
end
255255

256-
(L > 1) && right_cyclethrough!(i, GRs, above, operator, below)
256+
(L > 1) && right_cyclethrough!(i, GRs, below, operator, above)
257257
end
258258
end
259259

260260
return GRs
261261
end
262262

263-
function right_cyclethrough!(index::Int, GR, above::InfiniteMPS,
263+
function right_cyclethrough!(index::Int, GR, below::InfiniteMPS,
264264
operator::InfiniteMPOHamiltonian,
265-
below::InfiniteMPS)
265+
above::InfiniteMPS=below)
266266
# TODO: efficient transfer matrix slicing for large unitcells
267267
for site in reverse(eachindex(GR))
268268
rightinds = index:length(GR[site])
@@ -274,21 +274,21 @@ function right_cyclethrough!(index::Int, GR, above::InfiniteMPS,
274274
end
275275

276276
# no normalization necessary -- for consistant interface
277-
function TensorKit.normalize!(envs::InfiniteEnvironments, above::InfiniteMPS,
278-
operator::InfiniteMPOHamiltonian, below::InfiniteMPS)
277+
function TensorKit.normalize!(envs::InfiniteEnvironments, below::InfiniteMPS,
278+
operator::InfiniteMPOHamiltonian, above::InfiniteMPS)
279279
return envs
280280
end
281281

282282
# Transfer operations
283283
# -------------------
284284

285-
function transfer_leftenv!(envs::InfiniteEnvironments, above, operator, below, site::Int)
285+
function transfer_leftenv!(envs::InfiniteEnvironments, below, operator, above, site::Int)
286286
T = TransferMatrix(above.AL[site - 1], operator[site - 1], below.AL[site - 1])
287287
envs.GLs[site] = envs.GLs[site - 1] * T
288288
return envs
289289
end
290290

291-
function transfer_rightenv!(envs::InfiniteEnvironments, above, operator, below, site::Int)
291+
function transfer_rightenv!(envs::InfiniteEnvironments, below, operator, above, site::Int)
292292
T = TransferMatrix(above.AR[site + 1], operator[site + 1], below.AR[site + 1])
293293
envs.GRs[site] = T * envs.GRs[site + 1]
294294
return envs

src/environments/multiline_envs.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
const MultilineEnvironments{E<:AbstractMPSEnvironments} = Multiline{E}
22

3-
function environments(above::MultilineMPS, operator::MultilineMPO,
4-
below::MultilineMPS=above; kwargs...)
3+
function environments(below::MultilineMPS, operator::MultilineMPO,
4+
above::MultilineMPS=below; kwargs...)
55
(rows = size(above, 1)) == size(operator, 1) == size(below, 1) ||
66
throw(ArgumentError("Incompatible sizes"))
77
envs = map(1:rows) do row
8-
return environments(above[row], operator[row], below[row + 1]; kwargs...)
8+
return environments(below[row + 1], operator[row], above[row]; kwargs...)
99
end
1010
return Multiline(PeriodicVector(envs))
1111
end
1212

13-
function recalculate!(envs::MultilineEnvironments, above::MultilineMPS,
14-
operator::MultilineMPO, below::MultilineMPS=above; kwargs...)
13+
function recalculate!(envs::MultilineEnvironments, below::MultilineMPS,
14+
operator::MultilineMPO, above::MultilineMPS=below; kwargs...)
1515
(rows = size(above, 1)) == size(operator, 1) == size(below, 1) ||
1616
throw(ArgumentError("Incompatible sizes"))
1717
@threads for row in 1:rows
18-
recalculate!(envs[row], above[row], operator[row], below[row + 1]; kwargs...)
18+
recalculate!(envs[row], below[row + 1], operator[row], above[row]; kwargs...)
1919
end
2020
return envs
2121
end
2222
function recalculate!(envs::MultilineEnvironments, below, (operator, above)::Tuple;
2323
kwargs...)
24-
return recalculate!(envs, above, operator, below; kwargs...)
24+
return recalculate!(envs, below, operator, above; kwargs...)
2525
end
2626

27-
function TensorKit.normalize!(envs::MultilineEnvironments, above, operator, below)
28-
for row in 1:size(above, 1)
29-
normalize!(envs[row], above[row], operator[row], below[row + 1])
27+
function TensorKit.normalize!(envs::MultilineEnvironments, below, operator, above)
28+
for row in 1:size(below, 1)
29+
normalize!(envs[row], below[row + 1], operator[row], above[row])
3030
end
3131
return envs
3232
end
3333
function TensorKit.normalize!(envs::MultilineEnvironments, below, (operator, above))
3434
for row in 1:size(above, 1)
35-
normalize!(envs[row], above[row], operator[row], below[row + 1])
35+
normalize!(envs[row], below[row + 1], operator[row], above[row])
3636
end
3737
return envs
3838
end
@@ -44,22 +44,22 @@ function rightenv(envs::MultilineEnvironments, col::Int, state)
4444
return rightenv.(parent(envs), col, parent(state))
4545
end
4646

47-
function transfer_leftenv!(envs::MultilineEnvironments, above, operator, below, site::Int)
47+
function transfer_leftenv!(envs::MultilineEnvironments, below, operator, above, site::Int)
4848
for row in 1:size(above, 1)
49-
transfer_leftenv!(envs[row], above[row], operator[row], below[row + 1], site)
49+
transfer_leftenv!(envs[row], below[row + 1], operator[row], above[row], site)
5050
end
5151
return envs
5252
end
5353
function transfer_leftenv!(envs::MultilineEnvironments, below, (O, above)::Tuple, site::Int)
54-
return transfer_leftenv!(envs, above, O, below, site)
54+
return transfer_leftenv!(envs, below, O, above, site)
5555
end
56-
function transfer_rightenv!(envs::MultilineEnvironments, above, operator, below, site::Int)
56+
function transfer_rightenv!(envs::MultilineEnvironments, below, operator, above, site::Int)
5757
for row in 1:size(above, 1)
58-
transfer_rightenv!(envs[row], above[row], operator[row], below[row + 1], site)
58+
transfer_rightenv!(envs[row], below[row + 1], operator[row], above[row], site)
5959
end
6060
return envs
6161
end
6262
function transfer_rightenv!(envs::MultilineEnvironments, below, (O, above)::Tuple,
6363
site::Int)
64-
return transfer_rightenv!(envs, above, O, below, site)
64+
return transfer_rightenv!(envs, below, O, above, site)
6565
end

0 commit comments

Comments
 (0)