Skip to content

Commit

Permalink
Refactor IDMRG structs
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos committed Jan 16, 2025
1 parent a729ad2 commit c6dedd2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
18 changes: 11 additions & 7 deletions src/algorithms/approximate/idmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
alg::IDMRG1, envs=environments(ψ, toapprox))
log = IterLog("IDMRG")
ϵ::Float64 = 2 * alg.tol
local iter

LoggingExtras.withlevel(; alg.verbosity) do
@infov 2 loginit!(log, ϵ)
for iter in 1:(alg.maxiter)
for outer iter in 1:(alg.maxiter)
C_current = ψ.C[:, 0]

# left to right sweep
Expand Down Expand Up @@ -46,8 +47,9 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
end

# TODO: immediately compute in-place
nst = MultilineMPS(map(x -> x, ψ.AR); tol=alg.tol_gauge)
copy!(ψ, nst) # ensure output destination is unchanged
alg_gauge = updatetol(alg.alg_gauge, iter, ϵ)
ψ′ = MultilineMPS(map(x -> x, ψ.AR); alg_gauge.tol, alg_gauge.maxiter)
copy!(ψ, ψ′) # ensure output destination is unchanged

recalculate!(envs, ψ, toapprox)
return ψ, envs, ϵ
Expand All @@ -59,10 +61,11 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
ϵ::Float64 = 2 * alg.tol
log = IterLog("IDMRG2")
O, ϕ = toapprox
local iter

LoggingExtras.withlevel(; alg.verbosity) do
@infov 2 loginit!(log, ϵ)
for iter in 1:(alg.maxiter)
for outer iter in 1:(alg.maxiter)
C_current = ψ.C[:, 0]

# sweep from left to right
Expand Down Expand Up @@ -123,9 +126,10 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
end

# TODO: immediately compute in-place
nst = MultilineMPS(map(x -> x, ψ.AR); tol=alg.tol_gauge)
copy!(ψ, nst) # ensure output destination is unchanged
recalculate!(envs, ψ, toapprox)
alg_gauge = updatetol(alg.alg_gauge, iter, ϵ)
ψ′ = MultilineMPS(map(x -> x, ψ.AR); alg_gauge.tol, alg_gauge.maxiter)
copy!(ψ, ψ′) # ensure output destination is unchanged

recalculate!(envs, ψ, toapprox)
return ψ, envs, ϵ
end
75 changes: 38 additions & 37 deletions src/algorithms/groundstate/idmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ Single site infinite DMRG algorithm for finding the dominant eigenvector.
$(TYPEDFIELDS)
"""
struct IDMRG1{A} <: Algorithm
@kwdef struct IDMRG1{A} <: Algorithm
"tolerance for convergence criterium"
tol::Float64
"tolerance for gauging algorithm"
tol_gauge::Float64
"algorithm used for the eigenvalue solvers"
eigalg::A
tol::Float64 = Defaults.tol

"maximal amount of iterations"
maxiter::Int
maxiter::Int = Defaults.maxiter

"setting for how much information is displayed"
verbosity::Int
end
function IDMRG1(; tol=Defaults.tol, tol_gauge=Defaults.tolgauge, eigalg=(;),
maxiter=Defaults.maxiter, verbosity=Defaults.verbosity)
eigalg′ = eigalg isa NamedTuple ? Defaults.alg_eigsolve(; eigalg...) : eigalg
return IDMRG1{typeof(eigalg′)}(tol, tol_gauge, eigalg′, maxiter, verbosity)
verbosity::Int = Defualts.verbosity

"algorithm used for gauging the MPS"
alg_gauge = Defaults.alg_gauge()

"algorithm used for the eigenvalue solvers"
alg_eigsolve::A = Defaults.alg_eigsolve()
end

function find_groundstate(ost::InfiniteMPS, H, alg::IDMRG1, envs=environments(ost, H))
ϵ::Float64 = calc_galerkin(ost, H, ost, envs)
ψ = copy(ost)
log = IterLog("IDMRG")
local iter

LoggingExtras.withlevel(; alg.verbosity) do
@infov 2 loginit!(log, ϵ, expectation_value(ψ, H, envs))
for iter in 1:(alg.maxiter)
alg_eigsolve = updatetol(alg.eigalg, iter, ϵ)
for outer iter in 1:(alg.maxiter)
alg_eigsolve = updatetol(alg.alg_eigsolve, iter, ϵ)
C_current = ψ.C[0]

# left to right sweep
Expand Down Expand Up @@ -69,10 +69,11 @@ function find_groundstate(ost::InfiniteMPS, H, alg::IDMRG1, envs=environments(os
end
end

nst = InfiniteMPS.AR[1:end]; tol=alg.tol_gauge)
recalculate!(envs, nst, H, nst)
alg_gauge = updatetol(alg.alg_gauge, iter, ϵ)
ψ′ = InfiniteMPS.AR[1:end]; alg_gauge.tol, alg_gauge.maxiter)
recalculate!(envs, ψ′, H, ψ′)

return nst, envs, ϵ
return ψ′, envs, ϵ
end

"""
Expand All @@ -83,27 +84,25 @@ Two-site infinite DMRG algorithm for finding the dominant eigenvector.
## Fields
$(TYPEDFIELDS)
- `trscheme::TruncationScheme`: truncation algorithm for [`tsvd`](@extref TensorKit.tsvd)
"""
struct IDMRG2{A} <: Algorithm
@kwdef struct IDMRG2{A} <: Algorithm
"tolerance for convergence criterium"
tol::Float64
"tolerance for gauging algorithm"
tol_gauge::Float64
"algorithm used for the eigenvalue solvers"
eigalg::A
tol::Float64 = Defaults.tol

"maximal amount of iterations"
maxiter::Int
maxiter::Int = Defaults.maxiter

"setting for how much information is displayed"
verbosity::Int
verbosity::Int = Defualts.verbosity

"algorithm used for gauging the MPS"
alg_gauge = Defaults.alg_gauge()

"algorithm used for the eigenvalue solvers"
alg_eigsolve::A = Defaults.alg_eigsolve()

"algorithm used for [truncation](@extref TensorKit.tsvd) of the two-site update"
trscheme::TruncationScheme
end
function IDMRG2(; tol=Defaults.tol, tol_gauge=Defaults.tolgauge, eigalg=(;),
maxiter=Defaults.maxiter, verbosity=Defaults.verbosity,
trscheme=truncerr(1e-6))
eigalg′ = eigalg isa NamedTuple ? Defaults.alg_eigsolve(; eigalg...) : eigalg
return IDMRG2{typeof(eigalg′)}(tol, tol_gauge, eigalg′, maxiter, verbosity, trscheme)
trscheme::TruncationScheme = truncerr(1e-6)
end

function find_groundstate(ost::InfiniteMPS, H, alg::IDMRG2, envs=environments(ost, H))
Expand All @@ -112,11 +111,12 @@ function find_groundstate(ost::InfiniteMPS, H, alg::IDMRG2, envs=environments(os

ψ = copy(ost)
log = IterLog("IDMRG2")
local iter

LoggingExtras.withlevel(; alg.verbosity) do
@infov 2 loginit!(log, ϵ)
for iter in 1:(alg.maxiter)
alg_eigsolve = updatetol(alg.eigalg, iter, ϵ)
for outer iter in 1:(alg.maxiter)
alg_eigsolve = updatetol(alg.alg_eigsolve, iter, ϵ)
C_current = ψ.C[0]

# sweep from left to right
Expand Down Expand Up @@ -213,7 +213,8 @@ function find_groundstate(ost::InfiniteMPS, H, alg::IDMRG2, envs=environments(os
end
end

ψ′ = InfiniteMPS.AR[1:end]; tol=alg.tol_gauge)
alg_gauge = updatetol(alg.alg_gauge, iter, ϵ)
ψ′ = InfiniteMPS.AR[1:end]; alg_gauge.tol, alg_gauge.maxiter)
recalculate!(envs, ψ′, H, ψ′)

return ψ′, envs, ϵ
Expand Down

0 comments on commit c6dedd2

Please sign in to comment.