Skip to content

Commit f2c34cc

Browse files
committed
wip: fold DMRG3S/NoExpand into alg_gauge, single dispatch point
1 parent f0e3b74 commit f2c34cc

4 files changed

Lines changed: 29 additions & 26 deletions

File tree

‎src/MPSKit.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export time_evolve, timestep, timestep!, make_time_mpo
3838
export TDVP, TDVP2, WI, WII, TaylorCluster
3939
export changebonds, changebonds!
4040
export VUMPSSvdCut, OptimalExpand, SvdCut, RandExpand, SketchedExpand
41-
export post_expand!, NoExpand
41+
export NoExpand
4242
export NoiseSchedule, FunctionalSchedule, ExponentialDecay, Warmup, DMRG3S
4343
export propagator
4444
export DynamicalDMRG, NaiveInvert, Jeckelmann

‎src/algorithms/groundstate/dmrg.jl‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function local_update!(
7171
site, direction,
7272
ψ, O, alg::DMRG, envs,
7373
ϵ_global, ϵ_trunc, decay_rate,
74-
timeroutput
74+
alg_gauge, timeroutput
7575
)
7676
ϵ_local = calc_galerkin(site, ψ, O, ψ, envs)
7777

@@ -88,7 +88,7 @@ function local_update!(
8888
end
8989

9090
# 3. gauge
91-
ψ, ϵ_trunc = @timeit timeroutput "gauge" gauge!(ψ, site, direction, AC′, alg.alg_gauge; normalize = true)
91+
ψ, ϵ_trunc = @timeit timeroutput "gauge" gauge!(site, direction, ψ, O, envs, AC′, alg_gauge; normalize = true)
9292

9393
# 4. bookkeeping: measured contraction factor per matvec, kept a strict contraction in (0, 1)
9494
decay_rate = clamp((first(info.normres) / ϵ_local)^(1 / max(1, info.numops)), 1.0e-3, 0.999)
@@ -137,15 +137,15 @@ function DMRG2(;
137137
alg_eigsolve′ = alg_eigsolve isa NamedTuple ?
138138
Defaults.alg_eigsolve(; adaptive = true, alg_eigsolve...) : alg_eigsolve
139139
# two-site DMRG always truncates the enlarged bond back down, so the gauge is a truncated SVD
140-
alg_gauge = MatrixAlgebraKit.TruncatedAlgorithm(alg_svd, trscheme)
140+
alg_gauge = NoExpand(MatrixAlgebraKit.TruncatedAlgorithm(alg_svd, trscheme))
141141
return DMRG2(tol, maxiter, verbosity, alg_eigsolve′, alg_gauge, finalize)
142142
end
143143

144144
function local_update!(
145145
pos, direction,
146146
ψ, O, alg::DMRG2, envs,
147147
ϵ_global, ϵ_trunc, decay_rate,
148-
timeroutput
148+
alg_gauge, timeroutput
149149
)
150150
Heff = @timeit timeroutput "AC2_hamiltonian" AC2_hamiltonian(pos, ψ, O, ψ, envs)
151151

@@ -164,7 +164,7 @@ function local_update!(
164164

165165
# 2. gauge: truncated SVD split back into single-site tensors and install;
166166
# the discarded weight is the truncation error
167-
ψ, ϵ_trunc = @timeit timeroutput "gauge" gauge2!(ψ, pos, direction, newA2center, alg.alg_gauge; normalize = true)
167+
ψ, ϵ_trunc = @timeit timeroutput "gauge" gauge2!(site, direction, ψ, O, envs, newA2center, alg_gauge; normalize = true)
168168

169169
# 3. bookkeeping: measured contraction factor per matvec, kept a strict contraction in (0, 1)
170170
decay_rate = clamp((first(info.normres) / ϵ_local)^(1 / max(1, info.numops)), 1.0e-3, 0.999)
@@ -203,6 +203,7 @@ function find_groundstate!(
203203
LoggingExtras.withlevel(; alg.verbosity) do
204204
@infov 2 loginit!(log, ϵ_global, expectation_value(ψ, H, envs))
205205
for iter in 1:(alg.maxiter)
206+
alg_gauge = _update_alg_gauge(alg.alg_gauge, iter, ϵ_global)
206207
@timeit timeroutput "sweep" begin
207208
# left-to-right
208209
for pos in fwd
@@ -211,7 +212,7 @@ function find_groundstate!(
211212
pos, Val(:right),
212213
ψ, H, alg, envs,
213214
ϵ_global, ϵ_truncs[pos], decay_rates[pos],
214-
timeroutput
215+
alg_gauge, timeroutput
215216
)
216217
ϵ_global = maximum(ϵ_locals)
217218
end
@@ -223,7 +224,7 @@ function find_groundstate!(
223224
pos, Val(:left),
224225
ψ, H, alg, envs,
225226
ϵ_global, ϵ_truncs[pos], decay_rates[pos],
226-
timeroutput
227+
alg_gauge, timeroutput
227228
)
228229
ϵ_global = maximum(ϵ_locals)
229230
end

‎src/algorithms/post_expand/dmrg3s.jl‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,25 @@ end
4949

5050
(s::Warmup)(noise, iter, ϵ) = iter ≤ s.iters ? noise : zero(noise)
5151

52-
struct DMRG3S{N, S <: NoiseSchedule} <: Algorithm
52+
struct DMRG3S{N, S <: NoiseSchedule, A} <: Algorithm
5353
noise::N
5454
schedule::S
55+
alg_gauge::A
5556
end
5657

57-
function _update_post_expand(alg::DMRG3S, iter, ϵ)
58+
DMRG3S(noise, schedule) = DMRG3S(noise, schedule, nothing)
59+
60+
function _update_alg_gauge(alg::DMRG3S, iter, ϵ)
5861
noise = alg.schedule(alg.noise, iter, ϵ)
59-
return iszero(noise) ? NoExpand() : DMRG3S(noise, alg.schedule)
62+
return iszero(noise) ? NoExpand(alg.alg_gauge) : DMRG3S(noise, alg.schedule, alg.alg_gauge)
6063
end
6164

6265
function _get_combiner(::Type{T}, V1, V2) where {T}
6366
Vf = fuse(V1 ⊗ V2)
6467
return isomorphism(T, (V1 ⊗ V2) ← Vf), Vf
6568
end
6669

67-
function post_expand!(pos::Int, ::Val{:right}, ψ::AbstractFiniteMPS, H, alg::DMRG3S, envs, AC, alg_gauge; normalize = true)
70+
function gauge!(pos::Int, ::Val{:right}, ψ::AbstractFiniteMPS, H, envs, AC, alg::DMRG3S; normalize = true)
6871
El = leftenv(envs, pos, ψ)
6972
Hi = H[pos]
7073
α = alg.noise
@@ -76,17 +79,17 @@ function post_expand!(pos::Int, ::Val{:right}, ψ::AbstractFiniteMPS, H, alg::DM
7679

7780
AC_expanded = catdomain(AC, pert)
7881

79-
AL, C = left_gauge(AC_expanded, alg_gauge)
82+
AL, C, ϵ = left_gauge(AC_expanded, alg.alg_gauge)
8083
B = _transpose_tail(ψ.AR[pos + 1])
8184
AR = _transpose_front(catcodomain(B, zeros(T, Vpert ← domain(B))))
8285

8386
normalize && normalize!(C)
8487
ψ.AC[pos] = (AL, C)
8588
ψ.AC[pos + 1] = (C, AR)
86-
return ψ
89+
return ψ, ϵ
8790
end
8891

89-
function post_expand!(pos::Int, ::Val{:left}, ψ::AbstractFiniteMPS, H, alg::DMRG3S, envs, AC, alg_gauge; normalize = true)
92+
function gauge!(pos::Int, ::Val{:left}, ψ::AbstractFiniteMPS, H, envs, AC, alg::DMRG3S; normalize = true)
9093
Er = rightenv(envs, pos, ψ)
9194
Hi = H[pos]
9295
α = alg.noise
@@ -99,12 +102,12 @@ function post_expand!(pos::Int, ::Val{:left}, ψ::AbstractFiniteMPS, H, alg::DMR
99102
AC = _transpose_tail(AC)
100103
AC_expanded = catcodomain(AC, pert)
101104

102-
C, AR = right_gauge(AC_expanded, alg_gauge)
105+
C, AR, ϵ = right_gauge(AC_expanded, alg.alg_gauge)
103106
B = ψ.AL[pos - 1]
104107
AL = catdomain(B, zeros(T, codomain(B) ← Vpert))
105108

106109
normalize && normalize!(C)
107110
ψ.AC[pos] = (C, AR)
108111
ψ.AC[pos - 1] = (AL, C)
109-
return ψ
112+
return ψ, ϵ
110113
end
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
struct NoExpand <: Algorithm end
2-
3-
_update_post_expand(::NoExpand, iter, ϵ) = NoExpand()
4-
5-
function post_expand!(pos::Int, ::Val{:right}, ψ::AbstractFiniteMPS, H, ::NoExpand, envs, AC, alg_gauge; normalize::Bool = false)
6-
return left_gauge!(ψ, pos, AC, alg_gauge; normalize)
1+
struct NoExpand{A} <: Algorithm
2+
alg_gauge::A
73
end
84

9-
function post_expand!(pos::Int, ::Val{:left}, ψ::AbstractFiniteMPS, H, ::NoExpand, envs, AC, alg_gauge; normalize::Bool = false)
10-
return right_gauge!(ψ, pos, AC, alg_gauge; normalize)
11-
end
5+
_update_alg_gauge(alg::NoExpand, iter, ϵ) = alg
6+
7+
gauge!(pos::Int, direction, ψ::AbstractFiniteMPS, H, envs, AC, alg::NoExpand; normalize::Bool = false) =
8+
gauge!(ψ, pos, direction, AC, alg.alg_gauge; normalize)
9+
gauge2!(pos::Int, direction, ψ::AbstractFiniteMPS, H, envs, AC2, alg::NoExpand; normalize::Bool = false) =
10+
gauge2!(ψ, pos, direction, AC2, alg.alg_gauge; normalize)

0 commit comments

Comments
 (0)