From 989c1d635a6b9b893c5ba867ef6fe31834e1bc5b Mon Sep 17 00:00:00 2001 From: herviou Date: Fri, 17 Apr 2026 10:53:53 +0200 Subject: [PATCH 1/5] Fixing changebonds, some inconsistency remaining --- src/algorithms/changebonds/changebonds.jl | 5 ++++- src/algorithms/changebonds/optimalexpand.jl | 11 +++++++++++ src/algorithms/changebonds/randexpand.jl | 6 ++++++ src/algorithms/changebonds/svdcut.jl | 5 +++-- src/states/finitemps.jl | 9 +++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/algorithms/changebonds/changebonds.jl b/src/algorithms/changebonds/changebonds.jl index 700b5ab0b..3cff7c4b1 100644 --- a/src/algorithms/changebonds/changebonds.jl +++ b/src/algorithms/changebonds/changebonds.jl @@ -9,15 +9,18 @@ See also: [`SvdCut`](@ref), [`RandExpand`](@ref), [`VUMPSSvdCut`](@ref), [`Optim function changebonds end function changebonds! end + +##= This is never useful # write in terms of MultilineMPS function changebonds( - ψ::InfiniteMPS, operator::InfiniteMPO, alg, envs = environments(ψ, operator) + ψ::InfiniteMPS, operator::InfiniteMPO, alg::OptimalExpand, envs = environments(ψ, operator) ) ψ′, envs′ = changebonds( convert(MultilineMPS, ψ), convert(MultilineMPO, operator), alg, Multiline([envs]) ) return convert(InfiniteMPS, ψ′), envs end +# =# _expand(ψ, AL′, AR′) = _expand!(copy(ψ), AL′, AR′) function _expand!(ψ::InfiniteMPS, AL′::PeriodicVector, AR′::PeriodicVector) diff --git a/src/algorithms/changebonds/optimalexpand.jl b/src/algorithms/changebonds/optimalexpand.jl index c33c358c4..59fd002ca 100644 --- a/src/algorithms/changebonds/optimalexpand.jl +++ b/src/algorithms/changebonds/optimalexpand.jl @@ -17,6 +17,17 @@ $(TYPEDFIELDS) trscheme::TruncationStrategy end +##= This is only useful for OptimalExpand +function changebonds( + ψ::InfiniteMPS, operator::InfiniteMPO, alg::OptimalExpand, envs = environments(ψ, operator) + ) + ψ′, envs′ = changebonds( + convert(MultilineMPS, ψ), convert(MultilineMPO, operator), alg, Multiline([envs]) + ) + return convert(InfiniteMPS, ψ′), envs #This does not sound safe, it relies on the onsite modification of the environments, and the compatibility of the conversion +end +# =# + function changebonds( ψ::InfiniteMPS, H::InfiniteMPOHamiltonian, alg::OptimalExpand, envs = environments(ψ, H) diff --git a/src/algorithms/changebonds/randexpand.jl b/src/algorithms/changebonds/randexpand.jl index 80003ead2..e67a05509 100644 --- a/src/algorithms/changebonds/randexpand.jl +++ b/src/algorithms/changebonds/randexpand.jl @@ -50,6 +50,12 @@ end changebonds(ψ::AbstractMPS, alg::RandExpand) = changebonds!(copy(ψ), alg) changebonds(ψ::MultilineMPS, alg::RandExpand) = changebonds!(copy(ψ), alg) +function changebonds(ψ, H, alg::RandExpand, envs) + newψ = changebonds(ψ, alg) + return newψ, environments(newψ, H) +end + + function changebonds!(ψ::AbstractFiniteMPS, alg::RandExpand) for i in 1:(length(ψ) - 1) AC2 = randomize!(_transpose_front(ψ.AC[i]) * _transpose_tail(ψ.AR[i + 1])) diff --git a/src/algorithms/changebonds/svdcut.jl b/src/algorithms/changebonds/svdcut.jl index d5d4ea48a..2b674d5be 100644 --- a/src/algorithms/changebonds/svdcut.jl +++ b/src/algorithms/changebonds/svdcut.jl @@ -108,8 +108,9 @@ function changebonds(ψ::InfiniteMPS, alg::SvdCut) return normalize!(ψ) end -function changebonds(ψ, H, alg::SvdCut, envs = environments(ψ, H)) - return changebonds(ψ, alg), envs +function changebonds(ψ, H, alg::SvdCut, envs) + newψ = changebonds(ψ, alg) + return newψ, environments(newψ, H) end changebonds(mpo::FiniteMPOHamiltonian, alg::SvdCut) = changebonds!(copy(mpo), alg) diff --git a/src/states/finitemps.jl b/src/states/finitemps.jl index 5bfdcba92..6f65dbbe5 100644 --- a/src/states/finitemps.jl +++ b/src/states/finitemps.jl @@ -275,6 +275,15 @@ function FiniteMPS( return FiniteMPS(rand, Defaults.eltype, Pspaces, maxVspaces; kwargs...) end + +function FiniteMPS( + elt::Type, Pspaces::Vector{<:Union{S, CompositeSpace{S}}}, maxVspaces::Union{S, Vector{S}}; + kwargs... + ) where {S <: ElementarySpace} + return FiniteMPS(rand, elt, Pspaces, maxVspaces; kwargs...) +end + + # Also accept single physical space and length function FiniteMPS(N::Int, V::VectorSpace, args...; kwargs...) return FiniteMPS(fill(V, N), args...; kwargs...) From e1f8e390d9ce3348f52723076a3f2b250772c3f4 Mon Sep 17 00:00:00 2001 From: herviou Date: Fri, 17 Apr 2026 11:27:13 +0200 Subject: [PATCH 2/5] Fixing formatting --- src/algorithms/changebonds/randexpand.jl | 4 ++-- src/algorithms/changebonds/svdcut.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/algorithms/changebonds/randexpand.jl b/src/algorithms/changebonds/randexpand.jl index e67a05509..ccf839139 100644 --- a/src/algorithms/changebonds/randexpand.jl +++ b/src/algorithms/changebonds/randexpand.jl @@ -51,8 +51,8 @@ changebonds(ψ::AbstractMPS, alg::RandExpand) = changebonds!(copy(ψ), alg) changebonds(ψ::MultilineMPS, alg::RandExpand) = changebonds!(copy(ψ), alg) function changebonds(ψ, H, alg::RandExpand, envs) - newψ = changebonds(ψ, alg) - return newψ, environments(newψ, H) + newψ = changebonds(ψ, alg) + return newψ, environments(newψ, H) end diff --git a/src/algorithms/changebonds/svdcut.jl b/src/algorithms/changebonds/svdcut.jl index 2b674d5be..bcdb5b039 100644 --- a/src/algorithms/changebonds/svdcut.jl +++ b/src/algorithms/changebonds/svdcut.jl @@ -109,8 +109,8 @@ function changebonds(ψ::InfiniteMPS, alg::SvdCut) end function changebonds(ψ, H, alg::SvdCut, envs) - newψ = changebonds(ψ, alg) - return newψ, environments(newψ, H) + newψ = changebonds(ψ, alg) + return newψ, environments(newψ, H) end changebonds(mpo::FiniteMPOHamiltonian, alg::SvdCut) = changebonds!(copy(mpo), alg) From afdc245f24b30a6ed0823e0fbfd0a08a8d2d0d37 Mon Sep 17 00:00:00 2001 From: herviou Date: Fri, 17 Apr 2026 11:54:55 +0200 Subject: [PATCH 3/5] Fixing an order of calls (no local bug) --- src/algorithms/changebonds/changebonds.jl | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/algorithms/changebonds/changebonds.jl b/src/algorithms/changebonds/changebonds.jl index 3cff7c4b1..009351328 100644 --- a/src/algorithms/changebonds/changebonds.jl +++ b/src/algorithms/changebonds/changebonds.jl @@ -9,19 +9,6 @@ See also: [`SvdCut`](@ref), [`RandExpand`](@ref), [`VUMPSSvdCut`](@ref), [`Optim function changebonds end function changebonds! end - -##= This is never useful -# write in terms of MultilineMPS -function changebonds( - ψ::InfiniteMPS, operator::InfiniteMPO, alg::OptimalExpand, envs = environments(ψ, operator) - ) - ψ′, envs′ = changebonds( - convert(MultilineMPS, ψ), convert(MultilineMPO, operator), alg, Multiline([envs]) - ) - return convert(InfiniteMPS, ψ′), envs -end -# =# - _expand(ψ, AL′, AR′) = _expand!(copy(ψ), AL′, AR′) function _expand!(ψ::InfiniteMPS, AL′::PeriodicVector, AR′::PeriodicVector) for i in 1:length(ψ) From 9a29b8c9d2211f1d771a90497e7a04ce1212c82f Mon Sep 17 00:00:00 2001 From: herviou Date: Mon, 20 Apr 2026 10:30:21 +0200 Subject: [PATCH 4/5] Lukas proposed change, more explicit calls/type convert --- src/algorithms/changebonds/optimalexpand.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/changebonds/optimalexpand.jl b/src/algorithms/changebonds/optimalexpand.jl index 59fd002ca..cefa2dc09 100644 --- a/src/algorithms/changebonds/optimalexpand.jl +++ b/src/algorithms/changebonds/optimalexpand.jl @@ -24,7 +24,7 @@ function changebonds( ψ′, envs′ = changebonds( convert(MultilineMPS, ψ), convert(MultilineMPO, operator), alg, Multiline([envs]) ) - return convert(InfiniteMPS, ψ′), envs #This does not sound safe, it relies on the onsite modification of the environments, and the compatibility of the conversion + return convert(InfiniteMPS, ψ′), only(parent(envs′)) end # =# From 44d514c31db2367f1e7388c5d71bf064b3ae1918 Mon Sep 17 00:00:00 2001 From: herviou Date: Thu, 30 Apr 2026 10:31:47 +0200 Subject: [PATCH 5/5] Fixing some inconsistencies, and at least adding details in docstrings --- src/algorithms/changebonds/changebonds.jl | 3 +++ src/algorithms/changebonds/optimalexpand.jl | 8 ++++++-- src/algorithms/changebonds/randexpand.jl | 10 ++++++++++ src/algorithms/changebonds/svdcut.jl | 8 ++++++++ src/algorithms/changebonds/vumpssvd.jl | 7 ++++--- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/algorithms/changebonds/changebonds.jl b/src/algorithms/changebonds/changebonds.jl index 009351328..5e7a5bdca 100644 --- a/src/algorithms/changebonds/changebonds.jl +++ b/src/algorithms/changebonds/changebonds.jl @@ -3,6 +3,9 @@ changebonds(ψ::AbstractMPS, alg) -> ψ′ Change the bond dimension of `ψ` using the algorithm `alg`, and return the new `ψ` and the new `envs`. +For AbstractInfiniteMPS, changebonds returns new environments without modifying the one provided. +changedbonds! can modifiy both the provided state and environments, depending on the algorithm. +For FiniteMPS, changebonds also modifies the environments. See also: [`SvdCut`](@ref), [`RandExpand`](@ref), [`VUMPSSvdCut`](@ref), [`OptimalExpand`](@ref) """ diff --git a/src/algorithms/changebonds/optimalexpand.jl b/src/algorithms/changebonds/optimalexpand.jl index cefa2dc09..5e93a29bd 100644 --- a/src/algorithms/changebonds/optimalexpand.jl +++ b/src/algorithms/changebonds/optimalexpand.jl @@ -5,6 +5,8 @@ An algorithm that expands the given mps as described in [Zauner-Stauber et al. Phys. Rev. B 97 (2018)](@cite zauner-stauber2018), by selecting the dominant contributions of a two-site updated MPS tensor, orthogonal to the original ψ. +changedbonds! is only defined for FiniteMPS, and modify its environment. + ## Fields $(TYPEDFIELDS) @@ -18,6 +20,7 @@ $(TYPEDFIELDS) end ##= This is only useful for OptimalExpand +### Simple wrapper to convert between diffrent type of InifniteMPS. function changebonds( ψ::InfiniteMPS, operator::InfiniteMPO, alg::OptimalExpand, envs = environments(ψ, operator) ) @@ -50,7 +53,7 @@ function changebonds( end newψ = _expand(ψ, AL′, AR′) - recalculate!(envs, newψ, H) + envs = environments(newψ, H) return newψ, envs end @@ -75,13 +78,14 @@ function changebonds(ψ::MultilineMPS, H, alg::OptimalExpand, envs = environment end newψ = _expand(ψ, AL′, AR′) - recalculate!(envs, newψ, H) + envs = environments(newψ, H) # recalculate!(envs, newψ, H) return newψ, envs end function changebonds(ψ::AbstractFiniteMPS, H, alg::OptimalExpand, envs = environments(ψ, H)) return changebonds!(copy(ψ), H, alg, envs) end + function changebonds!(ψ::AbstractFiniteMPS, H, alg::OptimalExpand, envs = environments(ψ, H)) #inspired by the infinite mps algorithm, alternative is to use https://arxiv.org/pdf/1501.05504.pdf diff --git a/src/algorithms/changebonds/randexpand.jl b/src/algorithms/changebonds/randexpand.jl index ccf839139..bb1075dd5 100644 --- a/src/algorithms/changebonds/randexpand.jl +++ b/src/algorithms/changebonds/randexpand.jl @@ -9,6 +9,9 @@ parallel, and therefore the expansion will never go beyond the local two-site su The truncation strategy dictates the number of expanded states, by generating uniformly distributed weights for each state in the two-site space and truncating that. +The environments are not used here. +changebonds! modify both the provided state and environments. + ## Fields $(TYPEDFIELDS) @@ -56,6 +59,13 @@ function changebonds(ψ, H, alg::RandExpand, envs) end +function changebonds!(ψ, H, alg::RandExpand, envs) + ψ = changebonds!(ψ, alg) + recalculate!(envs, ψ, H) + return ψ, envs +end + + function changebonds!(ψ::AbstractFiniteMPS, alg::RandExpand) for i in 1:(length(ψ) - 1) AC2 = randomize!(_transpose_front(ψ.AC[i]) * _transpose_tail(ψ.AR[i + 1])) diff --git a/src/algorithms/changebonds/svdcut.jl b/src/algorithms/changebonds/svdcut.jl index bcdb5b039..21b32c52e 100644 --- a/src/algorithms/changebonds/svdcut.jl +++ b/src/algorithms/changebonds/svdcut.jl @@ -4,6 +4,8 @@ $(TYPEDEF) An algorithm that uses truncated SVD to change the bond dimension of a state or operator. This is achieved by a sweeping algorithm that locally performs (optimal) truncations in a gauged basis. +changedbonds! is only defined for FiniteMPS and FiniteMPO. + See also [`changebonds(!)`](@ref changebonds) ## Fields @@ -36,6 +38,12 @@ function changebonds!(ψ::AbstractFiniteMPS, alg::SvdCut; normalize::Bool = true return normalize ? normalize!(ψ) : ψ end +function changebonds!(ψ::AbstractFiniteMPS, H, alg::SvdCut, envs) + ψ = changebonds!(ψ, alg) + recalculate!(envs, ψ, H) + return ψ, envs +end + # Note: it might be better to go to an MPS representation first # such that the SVD cut happens in a canonical form; # this would still not be the correct norm, so we will ignore this for now. diff --git a/src/algorithms/changebonds/vumpssvd.jl b/src/algorithms/changebonds/vumpssvd.jl index c924976ce..0b59b3af0 100644 --- a/src/algorithms/changebonds/vumpssvd.jl +++ b/src/algorithms/changebonds/vumpssvd.jl @@ -3,6 +3,8 @@ $(TYPEDEF) An algorithm that uses a two-site update step to change the bond dimension of a state. +changedbonds! is not defined. + ## Fields $(TYPEDFIELDS) @@ -44,9 +46,8 @@ function changebonds_1( collapsed = InfiniteMPS( [nstate.AL[1]], nstate.C[1]; alg.alg_gauge.tol, alg.alg_gauge.maxiter ) - recalculate!(envs, collapsed, H, collapsed) - return collapsed, envs + return collapsed, environments(collapsed, H) end function changebonds_n(state::InfiniteMPS, H, alg::VUMPSSvdCut, envs = environments(state, H)) @@ -75,7 +76,7 @@ function changebonds_n(state::InfiniteMPS, H, alg::VUMPSSvdCut, envs = environme copied[loc] = AL1 copied[loc + 1] = AL2 state = InfiniteMPS(copied; alg.alg_gauge.tol, alg.alg_gauge.maxiter) - recalculate!(envs, state, H, state) + envs = environments(state, H) end return state, envs end