Skip to content

Commit 57294ec

Browse files
committed
format environments
1 parent ecbac58 commit 57294ec

File tree

7 files changed

+201
-165
lines changed

7 files changed

+201
-165
lines changed

src/environments/abstract_envs.jl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,26 @@ end
6868
6969
Determine an appropriate algorithm for computing the environments, based on the given `kwargs...`.
7070
"""
71-
function environment_alg(::Union{InfiniteMPS,MultilineMPS},
72-
::Union{InfiniteMPO,MultilineMPO},
73-
::Union{InfiniteMPS,MultilineMPS};
74-
tol=Defaults.tol, maxiter=Defaults.maxiter,
75-
krylovdim=Defaults.krylovdim, verbosity=Defaults.VERBOSE_NONE,
76-
eager=true)
71+
function environment_alg(
72+
::Union{InfiniteMPS, MultilineMPS}, ::Union{InfiniteMPO, MultilineMPO},
73+
::Union{InfiniteMPS, MultilineMPS};
74+
tol = Defaults.tol, maxiter = Defaults.maxiter, krylovdim = Defaults.krylovdim,
75+
verbosity = Defaults.VERBOSE_NONE, eager = true
76+
)
7777
return Arnoldi(; tol, maxiter, krylovdim, verbosity, eager)
7878
end
79-
function environment_alg(below, ::InfiniteMPOHamiltonian, above;
80-
tol=Defaults.tol, maxiter=Defaults.maxiter,
81-
krylovdim=Defaults.krylovdim, verbosity=Defaults.VERBOSE_NONE)
79+
function environment_alg(
80+
below, ::InfiniteMPOHamiltonian, above;
81+
tol = Defaults.tol, maxiter = Defaults.maxiter, krylovdim = Defaults.krylovdim,
82+
verbosity = Defaults.VERBOSE_NONE
83+
)
8284
return GMRES(; tol, maxiter, krylovdim, verbosity)
8385
end
84-
function environment_alg(::Union{InfiniteQP,MultilineQP},
85-
::Union{InfiniteMPO,MultilineMPO},
86-
::Union{InfiniteQP,MultilineQP};
87-
tol=Defaults.tol, maxiter=Defaults.maxiter,
88-
krylovdim=Defaults.krylovdim, verbosity=Defaults.VERBOSE_NONE)
86+
function environment_alg(
87+
::Union{InfiniteQP, MultilineQP}, ::Union{InfiniteMPO, MultilineMPO},
88+
::Union{InfiniteQP, MultilineQP};
89+
tol = Defaults.tol, maxiter = Defaults.maxiter, krylovdim = Defaults.krylovdim,
90+
verbosity = Defaults.VERBOSE_NONE
91+
)
8992
return GMRES(; tol, maxiter, krylovdim, verbosity)
9093
end

src/environments/finite_envs.jl

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Environment manager for `FiniteMPS` and `WindowMPS`. This structure is responsable for automatically checking
55
if the queried environment is still correctly cached and if not recalculates.
66
"""
7-
struct FiniteEnvironments{A,B,C,D} <: AbstractMPSEnvironments
7+
struct FiniteEnvironments{A, B, C, D} <: AbstractMPSEnvironments
88
above::A
99

1010
operator::B #the operator
@@ -28,14 +28,17 @@ function environments(below, operator, above, leftstart, rightstart)
2828
rightenvs = [i == N ? rightstart : similar(rightstart) for i in 0:length(below)]
2929

3030
t = similar(below.AL[1])
31-
return FiniteEnvironments(above, operator, fill(t, length(below)),
32-
fill(t, length(below)),
33-
leftenvs,
34-
rightenvs)
31+
return FiniteEnvironments(
32+
above, operator, fill(t, length(below)),
33+
fill(t, length(below)),
34+
leftenvs,
35+
rightenvs
36+
)
3537
end
3638

37-
function environments(below::FiniteMPS{S}, O::Union{FiniteMPO,FiniteMPOHamiltonian},
38-
above=nothing) where {S}
39+
function environments(
40+
below::FiniteMPS{S}, O::Union{FiniteMPO, FiniteMPOHamiltonian}, above = nothing
41+
) where {S}
3942
Vl_bot = left_virtualspace(below, 1)
4043
Vl_mid = left_virtualspace(O, 1)
4144
Vl_top = isnothing(above) ? left_virtualspace(below, 1) : left_virtualspace(above, 1)
@@ -49,17 +52,18 @@ function environments(below::FiniteMPS{S}, O::Union{FiniteMPO,FiniteMPOHamiltoni
4952

5053
return environments(below, O, above, leftstart, rightstart)
5154
end
52-
function environments(below::WindowMPS, O::Union{InfiniteMPOHamiltonian,InfiniteMPO},
53-
above=nothing;
54-
lenvs=environments(below.left_gs, O),
55-
renvs=environments(below.right_gs, O))
55+
function environments(
56+
below::WindowMPS, O::Union{InfiniteMPOHamiltonian, InfiniteMPO}, above = nothing;
57+
lenvs = environments(below.left_gs, O),
58+
renvs = environments(below.right_gs, O)
59+
)
5660
leftstart = copy(lenvs.GLs[1])
5761
rightstart = copy(renvs.GRs[end])
5862

5963
return environments(below, O, above, leftstart, rightstart)
6064
end
6165

62-
function environments(below::S, above::S) where {S<:Union{FiniteMPS,WindowMPS}}
66+
function environments(below::S, above::S) where {S <: Union{FiniteMPS, WindowMPS}}
6367
S isa WindowMPS &&
6468
(above.left_gs == below.left_gs || throw(ArgumentError("left gs differs")))
6569
S isa WindowMPS &&
@@ -69,13 +73,15 @@ function environments(below::S, above::S) where {S<:Union{FiniteMPS,WindowMPS}}
6973
return environments(below, operator, above, l_LL(above), r_RR(above))
7074
end
7175

72-
function environments(state::Union{FiniteMPS,WindowMPS}, operator::ProjectionOperator)
76+
function environments(state::Union{FiniteMPS, WindowMPS}, operator::ProjectionOperator)
7377
@plansor leftstart[-1; -2 -3 -4] := l_LL(operator.ket)[-3; -4] *
74-
l_LL(operator.ket)[-1; -2]
78+
l_LL(operator.ket)[-1; -2]
7579
@plansor rightstart[-1; -2 -3 -4] := r_RR(operator.ket)[-1; -2] *
76-
r_RR(operator.ket)[-3; -4]
77-
return environments(state, fill(nothing, length(state)), operator.ket, leftstart,
78-
rightstart)
80+
r_RR(operator.ket)[-3; -4]
81+
return environments(
82+
state, fill(nothing, length(state)), operator.ket, leftstart,
83+
rightstart
84+
)
7985
end
8086

8187
#notify the cache that we updated in-place, so it should invalidate the dependencies
@@ -94,7 +100,7 @@ function rightenv(ca::FiniteEnvironments, ind, state)
94100
for j in a:-1:(ind + 1)
95101
above = isnothing(ca.above) ? state.AR[j] : ca.above.AR[j]
96102
ca.GRs[j] = TransferMatrix(above, ca.operator[j], state.AR[j]) *
97-
ca.GRs[j + 1]
103+
ca.GRs[j + 1]
98104
ca.rdependencies[j] = state.AR[j]
99105
end
100106
end
@@ -110,7 +116,7 @@ function leftenv(ca::FiniteEnvironments, ind, state)
110116
for j in a:(ind - 1)
111117
above = isnothing(ca.above) ? state.AL[j] : ca.above.AL[j]
112118
ca.GLs[j + 1] = ca.GLs[j] *
113-
TransferMatrix(above, ca.operator[j], state.AL[j])
119+
TransferMatrix(above, ca.operator[j], state.AL[j])
114120
ca.ldependencies[j] = state.AL[j]
115121
end
116122
end

src/environments/infinite_envs.jl

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ T_RR[i] * GRs[i] = λ GRs[i - 1]
88
```
99
where `T_LL` and `T_RR` are the (regularized) transfer matrix operators on a give site for `AL-O-AL` and `AR-O-AR` respectively.
1010
"""
11-
struct InfiniteEnvironments{V<:GenericMPSTensor} <: AbstractMPSEnvironments
11+
struct InfiniteEnvironments{V <: GenericMPSTensor} <: AbstractMPSEnvironments
1212
GLs::PeriodicVector{V}
1313
GRs::PeriodicVector{V}
1414
end
@@ -18,33 +18,41 @@ 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(below::InfiniteMPS,
22-
operator::Union{InfiniteMPO,InfiniteMPOHamiltonian},
23-
above::InfiniteMPS=below;
24-
kwargs...)
21+
function environments(
22+
below::InfiniteMPS, operator::Union{InfiniteMPO, InfiniteMPOHamiltonian},
23+
above::InfiniteMPS = below; kwargs...
24+
)
2525
GLs, GRs = initialize_environments(below, operator, above)
2626
envs = InfiniteEnvironments(GLs, GRs)
2727
return recalculate!(envs, below, operator, above; kwargs...)
2828
end
2929

30-
function issamespace(envs::InfiniteEnvironments, below::InfiniteMPS,
31-
operator::Union{InfiniteMPO,InfiniteMPOHamiltonian},
32-
above::InfiniteMPS)
30+
function issamespace(
31+
envs::InfiniteEnvironments, below::InfiniteMPS,
32+
operator::Union{InfiniteMPO, InfiniteMPOHamiltonian}, above::InfiniteMPS
33+
)
3334
L = check_length(below, operator, above)
3435
for i in 1:L
3536
space(envs.GLs[i]) ==
36-
(left_virtualspace(below, i) left_virtualspace(operator, i)'
37-
left_virtualspace(above, i)) || return false
37+
(
38+
left_virtualspace(below, i) left_virtualspace(operator, i)'
39+
left_virtualspace(above, i)
40+
) || return false
3841
space(envs.GRs[i]) ==
39-
(right_virtualspace(above, i) right_virtualspace(operator, i)
40-
right_virtualspace(below, i)) || return false
42+
(
43+
right_virtualspace(above, i) right_virtualspace(operator, i)
44+
right_virtualspace(below, i)
45+
) || return false
4146
end
4247
return true
4348
end
4449

45-
function recalculate!(envs::InfiniteEnvironments, below::InfiniteMPS,
46-
operator::Union{InfiniteMPO,InfiniteMPOHamiltonian},
47-
above::InfiniteMPS=below; kwargs...)
50+
function recalculate!(
51+
envs::InfiniteEnvironments, below::InfiniteMPS,
52+
operator::Union{InfiniteMPO, InfiniteMPOHamiltonian},
53+
above::InfiniteMPS = below;
54+
kwargs...
55+
)
4856
if !issamespace(envs, below, operator, above)
4957
# TODO: in-place initialization?
5058
GLs, GRs = initialize_environments(below, operator, above)
@@ -65,36 +73,43 @@ end
6573

6674
# InfiniteMPO environments
6775
# ------------------------
68-
function initialize_environments(below::InfiniteMPS, operator::InfiniteMPO,
69-
above::InfiniteMPS=below)
76+
function initialize_environments(
77+
below::InfiniteMPS, operator::InfiniteMPO, above::InfiniteMPS = below
78+
)
7079
L = check_length(below, operator, above)
7180
GLs = PeriodicVector([randomize!(allocate_GL(below, operator, above, i)) for i in 1:L])
7281
GRs = PeriodicVector([randomize!(allocate_GR(below, operator, above, i)) for i in 1:L])
7382
return GLs, GRs
7483
end
7584

76-
function compute_leftenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
77-
operator::InfiniteMPO, above::InfiniteMPS, alg)
85+
function compute_leftenvs!(
86+
envs::InfiniteEnvironments, below::InfiniteMPS,
87+
operator::InfiniteMPO, above::InfiniteMPS, alg
88+
)
7889
# compute eigenvector
7990
T = TransferMatrix(above.AL, operator, below.AL)
8091
λ, envs.GLs[1] = fixedpoint(flip(T), envs.GLs[1], :LM, alg)
8192
# push through unitcell
8293
for i in 2:length(operator)
8394
envs.GLs[i] = envs.GLs[i - 1] *
84-
TransferMatrix(above.AL[i - 1], operator[i - 1], below.AL[i - 1])
95+
TransferMatrix(above.AL[i - 1], operator[i - 1], below.AL[i - 1])
8596
end
8697
return λ, envs
8798
end
8899

89-
function compute_rightenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
90-
operator::InfiniteMPO, above::InfiniteMPS, alg)
100+
function compute_rightenvs!(
101+
envs::InfiniteEnvironments, below::InfiniteMPS, operator::InfiniteMPO,
102+
above::InfiniteMPS, alg
103+
)
91104
# compute eigenvector
92105
T = TransferMatrix(above.AR, operator, below.AR)
93106
λ, envs.GRs[end] = fixedpoint(T, envs.GRs[end], :LM, alg)
94107
# push through unitcell
95108
for i in reverse(1:(length(operator) - 1))
96-
envs.GRs[i] = TransferMatrix(above.AR[i + 1], operator[i + 1],
97-
below.AR[i + 1]) * envs.GRs[i + 1]
109+
envs.GRs[i] = TransferMatrix(
110+
above.AR[i + 1], operator[i + 1],
111+
below.AR[i + 1]
112+
) * envs.GRs[i + 1]
98113
end
99114
return λ, envs
100115
end
@@ -104,8 +119,10 @@ end
104119
# - normalize the left environment to have overlap 1
105120
# this avoids catastrophic blow-up of norms, while keeping the total normalized
106121
# and does not lead to issues for negative overlaps and real entries.
107-
function TensorKit.normalize!(envs::InfiniteEnvironments, below::InfiniteMPS,
108-
operator::InfiniteMPO, above::InfiniteMPS)
122+
function TensorKit.normalize!(
123+
envs::InfiniteEnvironments, below::InfiniteMPS, operator::InfiniteMPO,
124+
above::InfiniteMPS
125+
)
109126
for i in 1:length(operator)
110127
normalize!(envs.GRs[i])
111128
Hc = C_hamiltonian(i, below, operator, above, envs)
@@ -117,8 +134,10 @@ end
117134

118135
# InfiniteMPOHamiltonian environments
119136
# -----------------------------------
120-
function initialize_environments(below::InfiniteMPS, operator::InfiniteMPOHamiltonian,
121-
above::InfiniteMPS=below)
137+
function initialize_environments(
138+
below::InfiniteMPS, operator::InfiniteMPOHamiltonian,
139+
above::InfiniteMPS = below
140+
)
122141
L = check_length(above, operator, below)
123142
GLs = PeriodicVector([allocate_GL(below, operator, above, i) for i in 1:L])
124143
GRs = PeriodicVector([allocate_GR(below, operator, above, i) for i in 1:L])
@@ -146,8 +165,10 @@ function initialize_environments(below::InfiniteMPS, operator::InfiniteMPOHamilt
146165
return GLs, GRs
147166
end
148167

149-
function compute_leftenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
150-
operator::InfiniteMPOHamiltonian, above::InfiniteMPS, alg)
168+
function compute_leftenvs!(
169+
envs::InfiniteEnvironments, below::InfiniteMPS,
170+
operator::InfiniteMPOHamiltonian, above::InfiniteMPS, alg
171+
)
151172
L = check_length(below, above, operator)
152173
GLs = envs.GLs
153174
vsize = length(first(GLs))
@@ -181,8 +202,7 @@ function compute_leftenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
181202
# go through the unitcell, again subtracting fixpoints
182203
for site in 1:L
183204
@plansor GLs[site][i][-1 -2; -3] -= GLs[site][i][1 -2; 2] *
184-
r_LL(above, site - 1)[2; 1] *
185-
l_LL(above, site)[-1; -3]
205+
r_LL(above, site - 1)[2; 1] * l_LL(above, site)[-1; -3]
186206
end
187207

188208
else
@@ -200,20 +220,24 @@ function compute_leftenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
200220
return GLs
201221
end
202222

203-
function left_cyclethrough!(index::Int, GL, below::InfiniteMPS, H::InfiniteMPOHamiltonian,
204-
above::InfiniteMPS=below)
223+
function left_cyclethrough!(
224+
index::Int, GL, below::InfiniteMPS, H::InfiniteMPOHamiltonian,
225+
above::InfiniteMPS = below
226+
)
205227
# TODO: efficient transfer matrix slicing for large unitcells
206228
leftinds = 1:index
207229
for site in eachindex(GL)
208-
GL[site + 1][index] = GL[site][leftinds] * TransferMatrix(above.AL[site],
209-
H[site][leftinds, 1, 1, index],
210-
below.AL[site])
230+
GL[site + 1][index] = GL[site][leftinds] * TransferMatrix(
231+
above.AL[site], H[site][leftinds, 1, 1, index], below.AL[site]
232+
)
211233
end
212234
return GL
213235
end
214236

215-
function compute_rightenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
216-
operator::InfiniteMPOHamiltonian, above::InfiniteMPS, alg)
237+
function compute_rightenvs!(
238+
envs::InfiniteEnvironments, below::InfiniteMPS,
239+
operator::InfiniteMPOHamiltonian, above::InfiniteMPS, alg
240+
)
217241
L = check_length(above, operator, below)
218242
GRs = envs.GRs
219243
vsize = length(last(GRs))
@@ -248,8 +272,7 @@ function compute_rightenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
248272
# go through the unitcell, again subtracting fixpoints
249273
for site in 1:L
250274
@plansor GRs[site][i][-1 -2; -3] -= GRs[site][i][1 -2; 2] *
251-
l_RR(above, site + 1)[2; 1] *
252-
r_RR(above, site)[-1; -3]
275+
l_RR(above, site + 1)[2; 1] * r_RR(above, site)[-1; -3]
253276
end
254277
else
255278
if !isemptylevel(operator, i)
@@ -267,22 +290,25 @@ function compute_rightenvs!(envs::InfiniteEnvironments, below::InfiniteMPS,
267290
return GRs
268291
end
269292

270-
function right_cyclethrough!(index::Int, GR, below::InfiniteMPS,
271-
operator::InfiniteMPOHamiltonian,
272-
above::InfiniteMPS=below)
293+
function right_cyclethrough!(
294+
index::Int, GR, below::InfiniteMPS, operator::InfiniteMPOHamiltonian,
295+
above::InfiniteMPS = below
296+
)
273297
# TODO: efficient transfer matrix slicing for large unitcells
274298
for site in reverse(eachindex(GR))
275299
rightinds = index:length(GR[site])
276-
GR[site - 1][index] = TransferMatrix(above.AR[site],
277-
operator[site][index, 1, 1, rightinds],
278-
below.AR[site]) * GR[site][rightinds]
300+
GR[site - 1][index] = TransferMatrix(
301+
above.AR[site], operator[site][index, 1, 1, rightinds], below.AR[site]
302+
) * GR[site][rightinds]
279303
end
280304
return GR
281305
end
282306

283307
# no normalization necessary -- for consistant interface
284-
function TensorKit.normalize!(envs::InfiniteEnvironments, below::InfiniteMPS,
285-
operator::InfiniteMPOHamiltonian, above::InfiniteMPS)
308+
function TensorKit.normalize!(
309+
envs::InfiniteEnvironments, below::InfiniteMPS,
310+
operator::InfiniteMPOHamiltonian, above::InfiniteMPS
311+
)
286312
return envs
287313
end
288314

src/environments/lazylincocache.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
struct LazyLincoCache{A<:LinearCombination,C<:Tuple} <: AbstractMPSEnvironments
1+
struct LazyLincoCache{A <: LinearCombination, C <: Tuple} <: AbstractMPSEnvironments
22
operator::A
33
envs::C
44
end
55

66
function environments(state, H::LinearCombination)
77
return LazyLincoCache(H, broadcast(o -> environments(state, o), H.opps))
8-
end;
8+
end

0 commit comments

Comments
 (0)