diff --git a/src/algorithms/changebonds/changebonds.jl b/src/algorithms/changebonds/changebonds.jl index 700b5ab0b..5e7a5bdca 100644 --- a/src/algorithms/changebonds/changebonds.jl +++ b/src/algorithms/changebonds/changebonds.jl @@ -3,22 +3,15 @@ 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) """ function changebonds end function changebonds! end -# write in terms of MultilineMPS -function changebonds( - ψ::InfiniteMPS, operator::InfiniteMPO, alg, 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(ψ) diff --git a/src/algorithms/changebonds/optimalexpand.jl b/src/algorithms/changebonds/optimalexpand.jl index c33c358c4..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) @@ -17,6 +19,18 @@ $(TYPEDFIELDS) trscheme::TruncationStrategy 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) + ) + ψ′, envs′ = changebonds( + convert(MultilineMPS, ψ), convert(MultilineMPO, operator), alg, Multiline([envs]) + ) + return convert(InfiniteMPS, ψ′), only(parent(envs′)) +end +# =# + function changebonds( ψ::InfiniteMPS, H::InfiniteMPOHamiltonian, alg::OptimalExpand, envs = environments(ψ, H) @@ -39,7 +53,7 @@ function changebonds( end newψ = _expand(ψ, AL′, AR′) - recalculate!(envs, newψ, H) + envs = environments(newψ, H) return newψ, envs end @@ -64,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 80003ead2..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) @@ -50,6 +53,19 @@ 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!(ψ, 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 d5d4ea48a..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. @@ -108,8 +116,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/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 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...)