From 9e329e718e9c7452e2c3287169df17fae559701d Mon Sep 17 00:00:00 2001 From: lkdvos Date: Wed, 4 Oct 2023 11:19:11 +0200 Subject: [PATCH] Refactor `Multiline` --- src/MPSKit.jl | 25 +++++----- src/operators/mpomultiline.jl | 10 ++-- src/states/{multiline.jl => mpsmultiline.jl} | 50 ++++---------------- src/utility/multiline.jl | 35 ++++++++++++++ 4 files changed, 61 insertions(+), 59 deletions(-) rename src/states/{multiline.jl => mpsmultiline.jl} (62%) create mode 100644 src/utility/multiline.jl diff --git a/src/MPSKit.jl b/src/MPSKit.jl index 2fd642a5..c0e071c9 100644 --- a/src/MPSKit.jl +++ b/src/MPSKit.jl @@ -10,27 +10,27 @@ using LinearAlgebra: diag, Diagonal using LinearAlgebra: LinearAlgebra using Base: @kwdef -#bells and whistles for mpses +# bells and whistles for mpses export InfiniteMPS, FiniteMPS, WindowMPS, MPSMultiline export PeriodicArray, Window export MPSTensor export QP, LeftGaugedQP, RightGaugedQP export leftorth, rightorth, leftorth!, rightorth!, poison!, uniform_leftorth, uniform_rightorth -export r_LL, l_LL, r_RR, l_RR, r_RL, r_LR, l_RL, l_LR #should be properties +export r_LL, l_LL, r_RR, l_RR, r_RL, r_LR, l_RL, l_LR # should be properties -#useful utility functions? +# useful utility functions? export add_util_leg, max_Ds, recalculate! export left_virtualspace, right_virtualspace, physicalspace export entanglementplot, transferplot -#hamiltonian things +# hamiltonian things export Cache export SparseMPO, MPOHamiltonian, DenseMPO, MPOMultiline export ∂C, ∂AC, ∂AC2, environments, expectation_value, effective_excitation_hamiltonian export leftenv, rightenv -#algos +# algos export find_groundstate!, find_groundstate, leading_boundary export VUMPS, DMRG, DMRG2, IDMRG1, IDMRG2, GradientGrassmann export excitations, FiniteExcited, QuasiparticleAnsatz @@ -45,7 +45,7 @@ export approximate!, approximate export periodic_boundary_conditions export exact_diagonalization -#transfer matrix +# transfer matrix export TransferMatrix export transfer_left, transfer_right @@ -53,7 +53,7 @@ export transfer_left, transfer_right @deprecate params(args...) environments(args...) @deprecate InfiniteMPO(args...) DenseMPO(args...) -#default settings +# default settings module Defaults const eltype = ComplexF64 const maxiter = 100 @@ -68,15 +68,16 @@ module Defaults end include("utility/periodicarray.jl") -include("utility/utility.jl") #random utility functions +include("utility/multiline.jl") +include("utility/utility.jl") # random utility functions include("utility/plotting.jl") include("utility/linearcombination.jl") -#maybe we should introduce an abstract state type +# maybe we should introduce an abstract state type include("states/window.jl") include("states/abstractmps.jl") include("states/infinitemps.jl") -include("states/multiline.jl") +include("states/mpsmultiline.jl") include("states/finitemps.jl") include("states/windowmps.jl") include("states/orthoview.jl") @@ -86,14 +87,14 @@ include("states/ortho.jl") include("operators/densempo.jl") include("operators/sparsempo/sparseslice.jl") include("operators/sparsempo/sparsempo.jl") -include("operators/mpohamiltonian.jl") #the mpohamiltonian objects +include("operators/mpohamiltonian.jl") # the mpohamiltonian objects include("operators/mpomultiline.jl") include("operators/projection.jl") include("transfermatrix/transfermatrix.jl") include("transfermatrix/transfer.jl") -abstract type Cache end #cache "manages" environments +abstract type Cache end # cache "manages" environments include("environments/FinEnv.jl") include("environments/abstractinfenv.jl") diff --git a/src/operators/mpomultiline.jl b/src/operators/mpomultiline.jl index d040b4c9..4112c366 100644 --- a/src/operators/mpomultiline.jl +++ b/src/operators/mpomultiline.jl @@ -1,7 +1,7 @@ # MPOMultiline # ------------ """ - const MPOMultiline{O<:Union{SparseMPO,DenseMPO}} = Multiline{O} + const MPOMultiline = Multiline{<:Union{SparseMPO,DenseMPO}} Type that represents multiple lines of `MPO` objects. @@ -11,7 +11,7 @@ Type that represents multiple lines of `MPO` objects. See also: [`Multiline`](@ref) """ -const MPOMultiline{O<:Union{SparseMPO,DenseMPO}} = Multiline{O} +const MPOMultiline = Multiline{<:Union{SparseMPO,DenseMPO}} function MPOMultiline(Os::AbstractMatrix{<:MPOTensor}) data = map(eachrow(Os)) do Orow @@ -28,9 +28,9 @@ Base.getindex(t::MPOMultiline, I::CartesianIndex{2}) = t[I.I...] # converters -Base.convert(::Type{MPOMultiline}, t::Union{SparseMPO,DenseMPO}) = Multiline([t]); -Base.convert(::Type{DenseMPO}, t::MPOMultiline{<:DenseMPO}) = only(t) -Base.convert(::Type{SparseMPO}, t::MPOMultiline{<:SparseMPO}) = only(t) +Base.convert(::Type{MPOMultiline}, t::Union{SparseMPO,DenseMPO}) = Multiline([t]) +Base.convert(::Type{DenseMPO}, t::MPOMultiline) = only(t) +Base.convert(::Type{SparseMPO}, t::MPOMultiline) = only(t) function Base.:*(mpo::MPOMultiline, st::MPSMultiline) size(mpo) == size(st) || throw(ArgumentError("dimension mismatch")) diff --git a/src/states/multiline.jl b/src/states/mpsmultiline.jl similarity index 62% rename from src/states/multiline.jl rename to src/states/mpsmultiline.jl index 118a7c4b..dd1fbf8a 100644 --- a/src/states/multiline.jl +++ b/src/states/mpsmultiline.jl @@ -1,41 +1,7 @@ -" -It is possible to have matrix product (operators / states) that are also periodic in the vertical direction -For examples, as fix points of statmech problems -These should be represented as respectively MultiLine{<:DenseMPO} / Multiline{<:InfiniteMPS} -" - -""" - struct Multiline{T} <: AbstractVector{T} - -Object that represents multiple lines of objects of type `T`. Typically used to represent -multiple lines of `InfiniteMPS` (`MPSMultiline`) or MPO (`Multiline{<:AbstractMPO}`). - -# Fields -- `data::PeriodicArray{T,1}`: the data of the multiline object - -See also: [`MPSMultiline`](@ref) and [`MPOMultiline`](@ref) -""" -struct Multiline{T} <: AbstractVector{T} - data::PeriodicArray{T,1} -end - -Multiline(data::AbstractVector{T}) where {T} = Multiline{T}(data) - -# AbstractArray interface -# ----------------------- -Base.parent(m::Multiline) = m.data -Base.size(m) = size(parent(m)) -Base.getindex(m, i::Int) = parent(m)[i] -Base.setindex!(m, v, i::Int) = (setindex!(parent(m), v, i); m) - -Base.copy(t::Multiline) = Multiline(map(copy, t.data)) -# Multiline(t::AbstractArray) = Multiline(PeriodicArray(t)); -# Base.iterate(t::Multiline, args...) = iterate(t.data, args...); - # MPSMultiline # ------------ """ - const MPSMultiline{A<:InfiniteMPS} = Multiline{A} + const MPSMultiline = Multiline{<:InfiniteMPS} Type that represents multiple lines of `InfiniteMPS` objects. @@ -50,9 +16,9 @@ Type that represents multiple lines of `InfiniteMPS` objects. See also: [`Multiline`](@ref) """ -const MPSMultiline{A<:InfiniteMPS} = Multiline{A} - -function MPSMultiline(pspaces::AbstractMatrix{S}, Dspaces::AbstractMatrix{S}; kwargs...) where {S} +const MPSMultiline = Multiline{<:InfiniteMPS} +MPSMultiline(mpss::AbstractVector{<:InfiniteMPS}) = Multiline(mpss) +function MPSMultiline(pspaces::AbstractMatrix{S}, Dspaces::AbstractMatrix{S}; kwargs...) where {S<:VectorSpace} data = map(eachrow(pspaces), eachrow(Dspaces)) do (p, D) return InfiniteMPS(p, D; kwargs...) end @@ -66,7 +32,7 @@ function MPSMultiline(As::AbstractMatrix{T}; kwargs...) where {T<:GenericMPSTens end function MPSMultiline(ALs::AbstractMatrix{<:GenericMPSTensor}, C₀::AbstractVector{<:MPSBondTensor}; kwargs...) - data = map(eachrow(ALs), C₀) do (ALrow, C₀row) + data = map(eachrow(ALs), C₀) do ALrow, C₀row return InfiniteMPS(ALrow, C₀row; kwargs...) end return MPSMultiline(data) @@ -106,6 +72,6 @@ end Base.convert(::Type{MPSMultiline}, st::InfiniteMPS) = Multiline([st]) Base.convert(::Type{InfiniteMPS}, st::MPSMultiline) = only(st) -# Base.eltype(t::MPSMultiline) = eltype(t[1]); -left_virtualspace(t::MPSMultiline, i::Int, j::Int) = left_virtualspace(t[i], j); -right_virtualspace(t::MPSMultiline, i::Int, j::Int) = right_virtualspace(t[i], j); +Base.eltype(t::MPSMultiline) = eltype(t[1]) +left_virtualspace(t::MPSMultiline, i::Int, j::Int) = left_virtualspace(t[i], j) +right_virtualspace(t::MPSMultiline, i::Int, j::Int) = right_virtualspace(t[i], j) diff --git a/src/utility/multiline.jl b/src/utility/multiline.jl new file mode 100644 index 00000000..b7ff84bd --- /dev/null +++ b/src/utility/multiline.jl @@ -0,0 +1,35 @@ +""" + struct Multiline{T} + +Object that represents multiple lines of objects of type `T`. Typically used to represent +multiple lines of `InfiniteMPS` (`MPSMultiline`) or MPO (`Multiline{<:AbstractMPO}`). + +# Fields +- `data::PeriodicArray{T,1}`: the data of the multiline object + +See also: [`MPSMultiline`](@ref) and [`MPOMultiline`](@ref) +""" +struct Multiline{T} + data::PeriodicArray{T,1} + function Multiline{T}(data::AbstractVector{T}) where {T} + L = length(data[1]) + for d in data[2:end] + @assert length(d) == L "All lines must have the same length" + end + return new{T}(data) + end +end +Multiline(data::AbstractVector{T}) where {T} = Multiline{T}(data) + +# AbstractArray interface +# ----------------------- +Base.parent(m::Multiline) = m.data +Base.size(m::Multiline) = (length(parent(m)), length(m.data[1])) +Base.size(m::Multiline, i::Int) = getindex(size(m), i) +Base.length(m::Multiline) = length(parent(m)) + +Base.getindex(m::Multiline, i::Int) = getindex(parent(m), i) +Base.setindex!(m::Multiline, v, i::Int) = (setindex!(parent(m), v, i); m) + +Base.copy(t::Multiline) = Multiline(map(copy, t.data)) +Base.iterate(t::Multiline, args...) = iterate(parent(t), args...) \ No newline at end of file