Skip to content

Commit 077ee8b

Browse files
lkdvosclaude
andcommitted
Prepare effective Hamiltonian halves in FiniteEnvironments
FiniteEnvironments now stores the prepared left and right halves of the AC and AC2 effective Hamiltonians, with an interface `lefthalf`/`righthalf` on top of `leftenv`/`rightenv`. Halves are prepared lazily per kind and rebuilt when the environment they were prepared from is replaced, so every algorithm on finite environments reuses them and DMRGEnvironments, together with its wrapping in the DMRG driver, is removed. The storage types are derived from the environment and operator types. The Jordan MPO derivatives no longer require the bra and ket to be equal: the not-started left and finished right environments are kept as explicit boundary tensors when they are not identities. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8d9e4f7 commit 077ee8b

7 files changed

Lines changed: 474 additions & 409 deletions

File tree

‎src/MPSKit.jl‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ include("environments/multiline_envs.jl")
148148
include("environments/qp_envs.jl")
149149
include("environments/multiple_envs.jl")
150150
include("environments/lazylincocache.jl")
151-
include("environments/dmrg_envs.jl")
152151

153152
include("algorithms/fixedpoint.jl")
154153
include("algorithms/derivatives/derivatives.jl")

‎src/algorithms/derivatives/hamiltonian_derivatives.jl‎

Lines changed: 241 additions & 136 deletions
Large diffs are not rendered by default.

‎src/algorithms/derivatives/mpo_derivatives.jl‎

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ function AC_hamiltonian(
4747
site::Int, below, operator, above, envs; prepare::Bool = true,
4848
backend::AbstractBackend = DefaultBackend(), allocator = DefaultAllocator()
4949
)
50+
prepare && operator isa Union{FiniteMPO, InfiniteMPO} && has_halves(envs) &&
51+
return _AC_hamiltonian_from_halves(site, below, operator, envs, backend, allocator)
5052
O = isnothing(operator) ? nothing : operator[site]
5153
H_AC = MPO_AC_Hamiltonian(
5254
leftenv(envs, site, below), O, rightenv(envs, site, below), backend, allocator
@@ -57,6 +59,8 @@ function AC2_hamiltonian(
5759
site::Int, below, operator, above, envs; prepare::Bool = true,
5860
backend::AbstractBackend = DefaultBackend(), allocator = DefaultAllocator()
5961
)
62+
prepare && operator isa Union{FiniteMPO, InfiniteMPO} && has_halves(envs) &&
63+
return _AC2_hamiltonian_from_halves(site, below, operator, envs, backend, allocator)
6064
O1, O2 = isnothing(operator) ? (nothing, nothing) : (operator[site], operator[site + 1])
6165
H_AC2 = MPO_AC2_Hamiltonian(
6266
leftenv(envs, site, below), O1, O2, rightenv(envs, site + 1, below),
@@ -391,50 +395,48 @@ function prepare_operator!!(H::Multiline{<:_ToPrepare})
391395
return Multiline(map(prepare_operator!!, parent(H)))
392396
end
393397

394-
# Prepared halves for `DMRGEnvironments`
395-
# ---------------------------------------
396-
pair_data(O, GL) = nothing
398+
# Prepared halves for `FiniteEnvironments`
399+
# -----------------------------------------
400+
halfstorage(envs::FiniteEnvironments, ::typeof(AC_hamiltonian)) = envs.halves.AC
401+
halfstorage(envs::FiniteEnvironments, ::typeof(AC2_hamiltonian)) = envs.halves.AC2
397402

398-
_derivative_type(nsites) = nsites == 1 ? MPO_AC_Hamiltonian : MPO_AC2_Hamiltonian
399-
prepare_lefthalf(GL, O::AbstractMPO, pairs, i, nsites, backend, allocator) =
400-
left_half(_derivative_type(nsites), GL, O[i], backend, allocator)
401-
prepare_righthalf(GR, O::AbstractMPO, pairs, i, nsites, backend, allocator) =
402-
right_half(_derivative_type(nsites), GR, O[i], backend, allocator)
403+
_derivative_type(::typeof(AC_hamiltonian)) = MPO_AC_Hamiltonian
404+
_derivative_type(::typeof(AC2_hamiltonian)) = MPO_AC2_Hamiltonian
403405

404-
function AC_hamiltonian(
405-
site::Int, below, O::Union{FiniteMPO, InfiniteMPO}, above, envs::DMRGEnvironments;
406-
prepare::Bool = true, backend::AbstractBackend = envs.backend, allocator = envs.allocator,
407-
)
408-
GL, GR = leftenv(envs, site, below), rightenv(envs, site, below)
409-
H = MPO_AC_Hamiltonian(GL, O[site], GR, backend, allocator)
410-
prepare || return H
411-
envs.nsites == 1 || return prepare_operator!!(H)
412-
return prepared_operator_type(typeof(H))(lefthalf(envs, site, below), righthalf(envs, site, below), backend, allocator)
406+
# The types of the halves are those of the environments of the prepared operator.
407+
function halftypes(::typeof(AC_hamiltonian), ::Type{G}, ::Type{W}) where {G, W}
408+
P = prepared_operator_type(MPO_AC_Hamiltonian{G, W, G, DefaultBackend, DefaultAllocator})
409+
return fieldtype(P, :leftenv), fieldtype(P, :rightenv)
413410
end
414-
function AC2_hamiltonian(
415-
site::Int, below, O::Union{FiniteMPO, InfiniteMPO}, above, envs::DMRGEnvironments;
416-
prepare::Bool = true, backend::AbstractBackend = envs.backend, allocator = envs.allocator,
417-
)
418-
GL, GR = leftenv(envs, site, below), rightenv(envs, site + 1, below)
419-
H = MPO_AC2_Hamiltonian(GL, O[site], O[site + 1], GR, backend, allocator)
420-
prepare || return H
421-
envs.nsites == 2 || return prepare_operator!!(H)
422-
return prepared_operator_type(typeof(H))(lefthalf(envs, site, below), righthalf(envs, site + 1, below), backend, allocator)
423-
end
424-
425-
# Projections inside DMRG reuse prepared halves when they exist; otherwise, like the generic
426-
# projection routines, they apply an unprepared operator once.
427-
function AC_projection(
428-
site::Int, below, O::AbstractMPO, above, envs::DMRGEnvironments;
429-
backend::AbstractBackend = envs.backend, allocator = envs.allocator,
430-
)
431-
H = AC_hamiltonian(site, below, O, above, envs; prepare = envs.nsites == 1, backend, allocator)
432-
return H * above.AC[site]
411+
function halftypes(::typeof(AC2_hamiltonian), ::Type{G}, ::Type{W}) where {G, W}
412+
P = prepared_operator_type(MPO_AC2_Hamiltonian{G, W, W, G, DefaultBackend, DefaultAllocator})
413+
return fieldtype(P, :leftenv), fieldtype(P, :rightenv)
433414
end
434-
function AC2_projection(
435-
site::Int, below, O::AbstractMPO, above, envs::DMRGEnvironments;
436-
backend::AbstractBackend = envs.backend, allocator = envs.allocator, kwargs...,
437-
)
438-
H = AC2_hamiltonian(site, below, O, above, envs; prepare = envs.nsites == 2, backend, allocator)
439-
return H * AC2(above, site; kwargs...)
415+
416+
function environment_halves(O::Union{FiniteMPO, InfiniteMPO}, ::Type{G}, N) where {G}
417+
W = eltype(O)
418+
(G <: MPSTensor && W <: MPOTensor && isconcretetype(W)) || return nothing
419+
AC = PreparedHalves{halftypes(AC_hamiltonian, G, W)..., G}(N)
420+
AC2 = PreparedHalves{halftypes(AC2_hamiltonian, G, W)..., G}(N)
421+
return (; AC, AC2)
422+
end
423+
424+
prepare_lefthalf(kind, O::Union{FiniteMPO, InfiniteMPO}, envs, GL, site, backend, allocator) =
425+
left_half(_derivative_type(kind), GL, O[site], backend, allocator)
426+
prepare_righthalf(kind, O::Union{FiniteMPO, InfiniteMPO}, envs, GR, site, backend, allocator) =
427+
right_half(_derivative_type(kind), GR, O[site], backend, allocator)
428+
429+
function _AC_hamiltonian_from_halves(site, below, O::Union{FiniteMPO, InfiniteMPO}, envs, backend, allocator)
430+
G, W = eltype(envs.GLs), eltype(O)
431+
P = prepared_operator_type(MPO_AC_Hamiltonian{G, W, G, typeof(backend), typeof(allocator)})
432+
L = lefthalf(AC_hamiltonian, envs, site, below; backend, allocator)
433+
R = righthalf(AC_hamiltonian, envs, site, below; backend, allocator)
434+
return P(L, R, backend, allocator)
435+
end
436+
function _AC2_hamiltonian_from_halves(site, below, O::Union{FiniteMPO, InfiniteMPO}, envs, backend, allocator)
437+
G, W = eltype(envs.GLs), eltype(O)
438+
P = prepared_operator_type(MPO_AC2_Hamiltonian{G, W, W, G, typeof(backend), typeof(allocator)})
439+
L = lefthalf(AC2_hamiltonian, envs, site, below; backend, allocator)
440+
R = righthalf(AC2_hamiltonian, envs, site + 1, below; backend, allocator)
441+
return P(L, R, backend, allocator)
440442
end

‎src/algorithms/groundstate/dmrg.jl‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ function DMRGState(ψ, H, alg::Union{DMRG, DMRG2}, envs, allocator, timeroutput)
312312
Tr = real(scalartype(ψ))
313313
n = _num_updates(alg, ψ)
314314
local_errors = ones(Tr, n)
315-
envs = dmrg_environments(ψ, H, envs; alg.backend, allocator, nsites = alg isa DMRG ? 1 : 2)
316315
return DMRGState(
317316
ψ, H, envs, 0, maximum(local_errors), local_errors, zeros(Tr, n),
318317
zeros(n), timeroutput, allocator,
@@ -346,19 +345,14 @@ function Base.iterate(it::IterativeSolver{<:Union{DMRG, DMRG2}}, state::DMRGStat
346345
state = sweep!(it, state, Val(:right), iter)
347346
sweep!(it, state, Val(:left), iter)
348347
end
349-
envs = unwrap_environments(state.envs)
350-
ψ, finalized_envs = @timeit timeroutput "finalize" it.finalize(
351-
iter, state.mps, state.operator, envs
352-
)::Tuple{typeof(state.mps), typeof(envs)}
353-
dmrg_envs = finalized_envs === envs ? state.envs : dmrg_environments(
354-
ψ, state.operator, finalized_envs;
355-
it.alg.backend, state.allocator, nsites = it.alg isa DMRG ? 1 : 2,
356-
)
348+
ψ, envs = @timeit timeroutput "finalize" it.finalize(
349+
iter, state.mps, state.operator, state.envs
350+
)::Tuple{typeof(state.mps), typeof(state.envs)}
357351
it.state = DMRGState(
358-
ψ, state.operator, dmrg_envs, iter, state.ϵ, state.local_errors,
352+
ψ, state.operator, envs, iter, state.ϵ, state.local_errors,
359353
state.truncation_errors, state.decay_rates, timeroutput, state.allocator,
360354
)
361-
return (ψ, finalized_envs, state.ϵ), it.state
355+
return (ψ, envs, state.ϵ), it.state
362356
end
363357

364358
# Truncation sets the attainable floor of the Galerkin error.
@@ -393,7 +387,7 @@ function find_groundstate_sweep!(
393387
truncation_errors = _bond_truncation_errors(alg, state.truncation_errors),
394388
numiter = state.iter,
395389
)
396-
return state.mps, unwrap_environments(state.envs), info
390+
return state.mps, state.envs, info
397391
end
398392

399393
function find_groundstate(ψ, H, alg::Union{DMRG, DMRG2}, envs...; kwargs...)

‎src/environments/dmrg_envs.jl‎

Lines changed: 0 additions & 104 deletions
This file was deleted.

‎src/environments/finite_envs.jl‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Environment manager for `FiniteMPS` and `WindowMPS`. This structure is responsible 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, H} <: AbstractMPSEnvironments
88
above::A
99

1010
operator::B #the operator
@@ -14,17 +14,45 @@ struct FiniteEnvironments{A, B, C, D} <: AbstractMPSEnvironments
1414

1515
GLs::Vector{D}
1616
GRs::Vector{D}
17+
18+
halves::H # prepared halves of the effective Hamiltonians, see `lefthalf`
19+
end
20+
21+
"""
22+
PreparedHalves{L, R, G}
23+
24+
The prepared left and right halves of one kind of effective Hamiltonian at every site, each
25+
stored with the environment it was prepared from. Entries are `nothing` until first queried.
26+
"""
27+
struct PreparedHalves{L, R, G}
28+
left::Vector{Union{Nothing, L}}
29+
right::Vector{Union{Nothing, R}}
30+
lsources::Vector{Union{Nothing, G}}
31+
rsources::Vector{Union{Nothing, G}}
32+
end
33+
function PreparedHalves{L, R, G}(N::Int) where {L, R, G}
34+
unset(T) = fill!(Vector{Union{Nothing, T}}(undef, N), nothing)
35+
return PreparedHalves{L, R, G}(unset(L), unset(R), unset(G), unset(G))
1736
end
1837

38+
"""
39+
environment_halves(operator, G, N)
40+
41+
The storage for the prepared halves of the effective Hamiltonians of `operator` on `N` sites
42+
with environments of type `G`, or `nothing` if `operator` has no prepared halves.
43+
"""
44+
environment_halves(operator, G, N) = nothing
45+
1946
function initialize_environments(below::AbstractFiniteMPS, operator, above, leftstart, rightstart)
2047
above = above === below ? nothing : above
2148
N = length(below)
2249
leftenvs = [i == 0 ? leftstart : similar(leftstart) for i in 0:N]
2350
rightenvs = [i == N ? rightstart : similar(rightstart) for i in 0:N]
2451

2552
t = similar(below.AL[1])
53+
halves = environment_halves(operator, eltype(leftenvs), N)
2654
return FiniteEnvironments(
27-
above, operator, fill(t, N), fill(t, N), leftenvs, rightenvs
55+
above, operator, fill(t, N), fill(t, N), leftenvs, rightenvs, halves
2856
)
2957
end
3058

@@ -157,3 +185,42 @@ function leftenv(
157185

158186
return ca.GLs[ind]
159187
end
188+
189+
"""
190+
lefthalf(kind, envs, site, state; backend, allocator)
191+
righthalf(kind, envs, site, state; backend, allocator)
192+
193+
Return the prepared half of the effective Hamiltonian `kind` (`AC_hamiltonian` or
194+
`AC2_hamiltonian`) that depends only on the left (right) environment of `site` for `state`,
195+
where `site` is the first (last) site of the effective Hamiltonian.
196+
A half is prepared when first queried, and again whenever its environment has been replaced.
197+
198+
See also [`leftenv`](@ref) and [`rightenv`](@ref).
199+
"""
200+
function lefthalf(
201+
kind, envs::FiniteEnvironments, site::Int, state;
202+
backend::AbstractBackend = DefaultBackend(), allocator = DefaultAllocator()
203+
)
204+
GL = leftenv(envs, site, state; backend, allocator)
205+
halves = halfstorage(envs, kind)
206+
if halves.lsources[site] !== GL
207+
halves.left[site] = prepare_lefthalf(kind, envs.operator, envs, GL, site, backend, allocator)
208+
halves.lsources[site] = GL
209+
end
210+
return something(halves.left[site])
211+
end
212+
function righthalf(
213+
kind, envs::FiniteEnvironments, site::Int, state;
214+
backend::AbstractBackend = DefaultBackend(), allocator = DefaultAllocator()
215+
)
216+
GR = rightenv(envs, site, state; backend, allocator)
217+
halves = halfstorage(envs, kind)
218+
if halves.rsources[site] !== GR
219+
halves.right[site] = prepare_righthalf(kind, envs.operator, envs, GR, site, backend, allocator)
220+
halves.rsources[site] = GR
221+
end
222+
return something(halves.right[site])
223+
end
224+
225+
has_halves(envs) = false
226+
has_halves(envs::FiniteEnvironments) = !isnothing(envs.halves)

0 commit comments

Comments
 (0)