From b1a077b17b7e43363e556ef3432494ba02653121 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Tue, 18 Nov 2025 11:26:31 -0500 Subject: [PATCH 1/9] define GeometryStyle and OperatorStyle for multiple arguments by recursion --- src/utility/styles.jl | 12 ++++++++++++ test/operators.jl | 5 +++++ test/other.jl | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/src/utility/styles.jl b/src/utility/styles.jl index 104b4e562..5708dcdff 100644 --- a/src/utility/styles.jl +++ b/src/utility/styles.jl @@ -10,6 +10,12 @@ Trait to describe the operator behavior of the input `x` or type `T`, which can abstract type OperatorStyle end OperatorStyle(x) = OperatorStyle(typeof(x)) OperatorStyle(T::Type) = throw(MethodError(OperatorStyle, T)) # avoid stackoverflow if not defined +OperatorStyle(x::OperatorStyle) = x + +OperatorStyle(x, y) = OperatorStyle(OperatorStyle(x)::OperatorStyle, OperatorStyle(y)::OperatorStyle) +OperatorStyle(::T, ::T) where {T<:OperatorStyle} = T() +OperatorStyle(x::OperatorStyle, y::OperatorStyle) = throw(MethodError(OperatorStyle, (x, y))) +@inline OperatorStyle(x, y, zs...) = OperatorStyle(OperatorStyle(x, y), zs...) struct MPOStyle <: OperatorStyle end struct HamiltonianStyle <: OperatorStyle end @@ -29,6 +35,12 @@ Trait to describe the geometry of the input `x` or type `T`, which can be either abstract type GeometryStyle end GeometryStyle(x) = GeometryStyle(typeof(x)) GeometryStyle(T::Type) = throw(MethodError(GeometryStyle, T)) # avoid stackoverflow if not defined +GeometryStyle(x::GeometryStyle) = x + +GeometryStyle(x, y) = GeometryStyle(GeometryStyle(x)::GeometryStyle, GeometryStyle(y)::GeometryStyle) +GeometryStyle(::T, ::T) where {T<:GeometryStyle} = T() +GeometryStyle(x::GeometryStyle, y::GeometryStyle) = throw(MethodError(GeometryStyle, (x, y))) +@inline GeometryStyle(x, y, zs...) = GeometryStyle(GeometryStyle(x, y), zs...) struct FiniteChainStyle <: GeometryStyle end struct InfiniteChainStyle <: GeometryStyle end diff --git a/test/operators.jl b/test/operators.jl index b2390437d..245621049 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -71,6 +71,8 @@ module TestOperators mps₁ = FiniteMPS(ψ₁) mps₂ = FiniteMPS(ψ₂) + @test @constinferred GeometryStyle(mps₁, mpo₁, mps₁) == GeometryStyle(mps₁) + @test convert(TensorMap, mpo₁ * mps₁) ≈ O₁ * ψ₁ @test mpo₁ * ψ₁ ≈ O₁ * ψ₁ @test convert(TensorMap, mpo₃ * mps₁) ≈ O₃ * ψ₁ @@ -140,6 +142,7 @@ module TestOperators @test GeometryStyle(H) == FiniteChainStyle() @test OperatorStyle(typeof(H)) == HamiltonianStyle() @test OperatorStyle(H) == HamiltonianStyle() + @test OperatorStyle(H, H´) == OperatorStyle(H) # Infinite Ws = [Wmid] @@ -410,6 +413,8 @@ module TestOperators ψ = InfiniteMPS([pspace], [ou ⊕ pspace]) W = MPSKit.DenseMPO(make_time_mpo(ham, 1im * 0.5, WII())) + + @test GeometryStyle(ψ, W) == GeometryStyle(ψ) @test W * (W * ψ) ≈ (W * W) * ψ atol = 1.0e-2 # TODO: there is a normalization issue here end diff --git a/test/other.jl b/test/other.jl index 17b3138fe..223a0d705 100644 --- a/test/other.jl +++ b/test/other.jl @@ -120,5 +120,11 @@ module TestMiscellaneous @test GeometryStyle(FiniteMPOHamiltonian) == FiniteChainStyle() @test GeometryStyle(InfiniteMPO) == InfiniteChainStyle() @test GeometryStyle(InfiniteMPOHamiltonian) == InfiniteChainStyle() + + @test GeometryStyle(GeometryStyle(FiniteMPS)) == GeometryStyle(FiniteMPS) + @test GeometryStyle(FiniteMPS, FiniteMPO) == FiniteChainStyle() + @test_throws MethodError GeometryStyle(FiniteMPS, InfiniteMPO) + @test @constinferred GeometryStyle(InfiniteMPS, InfiniteMPO, InfiniteMPS) == InfiniteChainStyle() + @test_throws MethodError GeometryStyle(FiniteMPS, FiniteMPO, InfiniteMPS) end end From 839d06a567262bb0d962805d9f947a411a649c37 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Tue, 18 Nov 2025 11:45:47 -0500 Subject: [PATCH 2/9] fix formatting --- src/utility/styles.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utility/styles.jl b/src/utility/styles.jl index 5708dcdff..927250484 100644 --- a/src/utility/styles.jl +++ b/src/utility/styles.jl @@ -10,10 +10,10 @@ Trait to describe the operator behavior of the input `x` or type `T`, which can abstract type OperatorStyle end OperatorStyle(x) = OperatorStyle(typeof(x)) OperatorStyle(T::Type) = throw(MethodError(OperatorStyle, T)) # avoid stackoverflow if not defined -OperatorStyle(x::OperatorStyle) = x +OperatorStyle(x::OperatorStyle) = x OperatorStyle(x, y) = OperatorStyle(OperatorStyle(x)::OperatorStyle, OperatorStyle(y)::OperatorStyle) -OperatorStyle(::T, ::T) where {T<:OperatorStyle} = T() +OperatorStyle(::T, ::T) where {T <: OperatorStyle} = T() OperatorStyle(x::OperatorStyle, y::OperatorStyle) = throw(MethodError(OperatorStyle, (x, y))) @inline OperatorStyle(x, y, zs...) = OperatorStyle(OperatorStyle(x, y), zs...) @@ -38,7 +38,7 @@ GeometryStyle(T::Type) = throw(MethodError(GeometryStyle, T)) # avoid stackoverf GeometryStyle(x::GeometryStyle) = x GeometryStyle(x, y) = GeometryStyle(GeometryStyle(x)::GeometryStyle, GeometryStyle(y)::GeometryStyle) -GeometryStyle(::T, ::T) where {T<:GeometryStyle} = T() +GeometryStyle(::T, ::T) where {T <: GeometryStyle} = T() GeometryStyle(x::GeometryStyle, y::GeometryStyle) = throw(MethodError(GeometryStyle, (x, y))) @inline GeometryStyle(x, y, zs...) = GeometryStyle(GeometryStyle(x, y), zs...) From 0bebc3021df8990dc8598cdb384e3254c451f2f9 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Tue, 18 Nov 2025 11:46:16 -0500 Subject: [PATCH 3/9] fix test --- test/operators.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/operators.jl b/test/operators.jl index 245621049..f5dce96b5 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -142,7 +142,7 @@ module TestOperators @test GeometryStyle(H) == FiniteChainStyle() @test OperatorStyle(typeof(H)) == HamiltonianStyle() @test OperatorStyle(H) == HamiltonianStyle() - @test OperatorStyle(H, H´) == OperatorStyle(H) + @test OperatorStyle(H, H′) == OperatorStyle(H) # Infinite Ws = [Wmid] From f4caf1712a5194e7c1fdc48577cfdb754eac8942 Mon Sep 17 00:00:00 2001 From: Andreas Feuerpfeil <36232041+AFeuerpfeil@users.noreply.github.com> Date: Tue, 18 Nov 2025 15:48:29 -0500 Subject: [PATCH 4/9] Update src/utility/styles.jl Rephrase error messages based on Lukas' suggestion Co-authored-by: Lukas Devos --- src/utility/styles.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/styles.jl b/src/utility/styles.jl index 927250484..ec9421511 100644 --- a/src/utility/styles.jl +++ b/src/utility/styles.jl @@ -14,7 +14,7 @@ OperatorStyle(x::OperatorStyle) = x OperatorStyle(x, y) = OperatorStyle(OperatorStyle(x)::OperatorStyle, OperatorStyle(y)::OperatorStyle) OperatorStyle(::T, ::T) where {T <: OperatorStyle} = T() -OperatorStyle(x::OperatorStyle, y::OperatorStyle) = throw(MethodError(OperatorStyle, (x, y))) +OperatorStyle(x::OperatorStyle, y::OperatorStyle) = error("Unknown combination of operator styles $x and $y") @inline OperatorStyle(x, y, zs...) = OperatorStyle(OperatorStyle(x, y), zs...) struct MPOStyle <: OperatorStyle end From f9172a86d4e8d3073e3d1e760a2be34ed00962d4 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Tue, 18 Nov 2025 15:50:54 -0500 Subject: [PATCH 5/9] also change error message of GeometryStyle --- src/utility/styles.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/styles.jl b/src/utility/styles.jl index ec9421511..962859019 100644 --- a/src/utility/styles.jl +++ b/src/utility/styles.jl @@ -39,7 +39,7 @@ GeometryStyle(x::GeometryStyle) = x GeometryStyle(x, y) = GeometryStyle(GeometryStyle(x)::GeometryStyle, GeometryStyle(y)::GeometryStyle) GeometryStyle(::T, ::T) where {T <: GeometryStyle} = T() -GeometryStyle(x::GeometryStyle, y::GeometryStyle) = throw(MethodError(GeometryStyle, (x, y))) +GeometryStyle(x::GeometryStyle, y::GeometryStyle) = error("Unknown combination of geometry styles $x and $y") @inline GeometryStyle(x, y, zs...) = GeometryStyle(GeometryStyle(x, y), zs...) struct FiniteChainStyle <: GeometryStyle end From 86993c8e6dff7b8139451b912a15c373ab433c9f Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Tue, 18 Nov 2025 16:06:38 -0500 Subject: [PATCH 6/9] fix tests --- test/other.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/other.jl b/test/other.jl index 223a0d705..823c17ebb 100644 --- a/test/other.jl +++ b/test/other.jl @@ -123,8 +123,8 @@ module TestMiscellaneous @test GeometryStyle(GeometryStyle(FiniteMPS)) == GeometryStyle(FiniteMPS) @test GeometryStyle(FiniteMPS, FiniteMPO) == FiniteChainStyle() - @test_throws MethodError GeometryStyle(FiniteMPS, InfiniteMPO) + @test_throws Error GeometryStyle(FiniteMPS, InfiniteMPO) @test @constinferred GeometryStyle(InfiniteMPS, InfiniteMPO, InfiniteMPS) == InfiniteChainStyle() - @test_throws MethodError GeometryStyle(FiniteMPS, FiniteMPO, InfiniteMPS) + @test_throws Error GeometryStyle(FiniteMPS, FiniteMPO, InfiniteMPS) end end From 29877dfcf47078eee8e6633cc00637b1e0f88325 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Tue, 18 Nov 2025 16:52:00 -0500 Subject: [PATCH 7/9] fix tests --- test/other.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/other.jl b/test/other.jl index 823c17ebb..eb24429c8 100644 --- a/test/other.jl +++ b/test/other.jl @@ -123,8 +123,8 @@ module TestMiscellaneous @test GeometryStyle(GeometryStyle(FiniteMPS)) == GeometryStyle(FiniteMPS) @test GeometryStyle(FiniteMPS, FiniteMPO) == FiniteChainStyle() - @test_throws Error GeometryStyle(FiniteMPS, InfiniteMPO) + @test_throws ErrorException GeometryStyle(FiniteMPS, InfiniteMPO) @test @constinferred GeometryStyle(InfiniteMPS, InfiniteMPO, InfiniteMPS) == InfiniteChainStyle() - @test_throws Error GeometryStyle(FiniteMPS, FiniteMPO, InfiniteMPS) + @test_throws ErrorException GeometryStyle(FiniteMPS, FiniteMPO, InfiniteMPS) end end From a747023c700723b4eae13fae31cc1dd58f52222e Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Tue, 18 Nov 2025 19:40:36 -0500 Subject: [PATCH 8/9] improve code coverage --- test/other.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/other.jl b/test/other.jl index eb24429c8..2eb3443ca 100644 --- a/test/other.jl +++ b/test/other.jl @@ -112,6 +112,9 @@ module TestMiscellaneous @test OperatorStyle(MPO) == MPOStyle() @test OperatorStyle(InfiniteMPO) == MPOStyle() + @test OperatorStyle(HamiltonianStyle()) == HamiltonianStyle() + @test @constinferred OperatorStyle(MPO, InfiniteMPO, MPO) == MPOStyle() + @test_throws ErrorException OperatorStyle(MPO, HamiltonianStyle()) @test GeometryStyle(FiniteMPOHamiltonian) == FiniteChainStyle() @test GeometryStyle(InfiniteMPS) == InfiniteChainStyle() From 00642acfd7b83ee4c764e6e4a431c7a1442a3b0d Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Wed, 19 Nov 2025 07:52:56 -0500 Subject: [PATCH 9/9] add styles to abstractmps --- src/states/abstractmps.jl | 83 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/src/states/abstractmps.jl b/src/states/abstractmps.jl index 5c1e82c1d..b27763a7d 100644 --- a/src/states/abstractmps.jl +++ b/src/states/abstractmps.jl @@ -15,7 +15,7 @@ const MPSTensor{S} = GenericMPSTensor{S, 2} # the usual mps tensors on which we """ MPSTensor([f, eltype], d::Int, left_D::Int, [right_D]::Int]) - MPSTensor([f, eltype], physicalspace::Union{S,CompositeSpace{S}}, + MPSTensor([f, eltype], physicalspace::Union{S,CompositeSpace{S}}, left_virtualspace::S, [right_virtualspace]::S) where {S<:ElementarySpace} Construct an `MPSTensor` with given physical and virtual spaces. @@ -164,6 +164,8 @@ If this hasn't been computed before, this can be computed as: - `kind=:ALAC` : AL[i] * AC[i+1] """ AC2 +AC2(psi::AbstractMPS, site::Int; kwargs...) = AC2(GeometryStyle(psi), psi, site; kwargs...) + #=========================================================================================== MPS types ===========================================================================================# @@ -202,7 +204,7 @@ TensorKit.sectortype(ψtype::Type{<:AbstractMPS}) = sectortype(site_type(ψtype) """ left_virtualspace(ψ::AbstractMPS, [pos=1:length(ψ)]) - + Return the virtual space of the bond to the left of sites `pos`. !!! warning @@ -245,4 +247,79 @@ physicalspace(ψ::AbstractMPS) = map(Base.Fix1(physicalspace, ψ), eachsite(ψ)) Return an iterator over the sites of the MPS `state`. """ -eachsite(ψ::AbstractMPS) = eachindex(ψ) +eachsite(ψ::AbstractMPS) = eachsite(GeometryStyle(ψ), ψ) + +eachsite(::GeometryStyle, ψ::AbstractMPS) = eachindex(ψ) + +# TensorKit utility +# ----------------- + +function TensorKit.dot(ψ₁::AbstractMPS, ψ₂::AbstractMPS; kwargs...) + geometry_style = GeometryStyle(ψ₁) & GeometryStyle(ψ₂) + return TensorKit.dot(geometry_style, ψ₁, ψ₂; kwargs...) +end +function Base.isapprox(ψ₁::AbstractMPS, ψ₂::AbstractMPS; kwargs...) + return isapprox(dot(ψ₁, ψ₂), 1; kwargs...) +end +TensorKit.norm(ψ::AbstractMPS) = TensorKit.norm(GeometryStyle(ψ), ψ) +TensorKit.normalize!(ψ::AbstractMPS) = TensorKit.normalize!(GeometryStyle(ψ), ψ) +TensorKit.normalize(ψ::AbstractMPS) = normalize!(copy(ψ)) +#=========================================================================================== +Fixedpoints +===========================================================================================# + +""" + l_RR(ψ, location) + +Left dominant eigenvector of the `AR`-`AR` transfermatrix. +""" +l_RR(ψ::AbstractMPS, loc::Int = 1) = l_RR(GeometryStyle(ψ), ψ, loc) + +""" + l_RL(ψ, location) + +Left dominant eigenvector of the `AR`-`AL` transfermatrix. +""" +l_RL(ψ::AbstractMPS, loc::Int = 1) = l_RL(GeometryStyle(ψ), ψ, loc) + +""" + l_LR(ψ, location) + +Left dominant eigenvector of the `AL`-`AR` transfermatrix. +""" +l_LR(ψ::AbstractMPS, loc::Int = 1) = l_LR(GeometryStyle(ψ), ψ, loc) + +""" + l_LL(ψ, location) + +Left dominant eigenvector of the `AL`-`AL` transfermatrix. +""" +l_LL(ψ::AbstractMPS, loc::Int = 1) = l_LL(GeometryStyle(ψ), ψ, loc) + +""" + r_RR(ψ, location) + +Right dominant eigenvector of the `AR`-`AR` transfermatrix. +""" +r_RR(ψ::AbstractMPS, loc::Int = length(ψ)) = r_RR(GeometryStyle(ψ), ψ, loc) + +""" + r_RL(ψ, location) + +Right dominant eigenvector of the `AR`-`AL` transfermatrix. +""" +r_RL(ψ::AbstractMPS, loc::Int = length(ψ)) = r_RL(GeometryStyle(ψ), ψ, loc) + +""" + r_LR(ψ, location) + +Right dominant eigenvector of the `AL`-`AR` transfermatrix. +""" +r_LR(ψ::AbstractMPS, loc::Int = length(ψ)) = r_LR(GeometryStyle(ψ), ψ, loc) + +""" + r_LL(ψ, location) + +Right dominant eigenvector of the `AL`-`AL` transfermatrix. +""" +r_LL(ψ::AbstractMPS, loc::Int = length(ψ)) = r_LL(GeometryStyle(ψ), ψ, loc)