Skip to content

Commit e8a1e7d

Browse files
AFeuerpfeillkdvos
andauthored
Add OperatorStyle and GeometryStyle (#352)
* add styles * start rewrite * start defining the styles * implement styles for states and operators and add tests * add include styles * fix InfiniteMPO test * dispatch styles on type and rename IsfiniteStyle to GeometryStyle * readd Base.isfinite for AbstractMPS, MPO and MPOHamiltonian * add tests for styles of types * fix include order * fix OperatorStyle for MPOHamiltonian * Add WindowStyle and increase code coverage * fix test and hopefully also runic * more tests * fix other tests * make GeometryStyle names more descriptive * implement changes * remove unnecessary Base.isfinite definitions, because it is already defined for AbstractMPO * fix braille tests * addd * revert unnecessary changes * update style docs * fix formatting * more formatting --------- Co-authored-by: Lukas Devos <[email protected]>
1 parent 8d378c6 commit e8a1e7d

File tree

13 files changed

+149
-3
lines changed

13 files changed

+149
-3
lines changed

src/MPSKit.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ include("utility/logging.jl")
9797
using .IterativeLoggers
9898
include("utility/iterativesolvers.jl")
9999

100+
include("utility/styles.jl")
100101
include("utility/periodicarray.jl")
101102
include("utility/windowarray.jl")
102103
include("utility/multiline.jl")

src/operators/mpo.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Matrix Product Operator (MPO) acting on a finite tensor product space with a lin
1717
"""
1818
const FiniteMPO{O} = MPO{O, Vector{O}}
1919
Base.isfinite(::Type{<:FiniteMPO}) = true
20+
GeometryStyle(::Type{<:FiniteMPO}) = FiniteChainStyle()
21+
OperatorStyle(::Type{<:MPO}) = MPOStyle()
2022

2123
function FiniteMPO(Os::AbstractVector{O}) where {O}
2224
for i in eachindex(Os)[1:(end - 1)]
@@ -37,6 +39,7 @@ Matrix Product Operator (MPO) acting on an infinite tensor product space with a
3739
"""
3840
const InfiniteMPO{O} = MPO{O, PeriodicVector{O}}
3941
Base.isfinite(::Type{<:InfiniteMPO}) = false
42+
GeometryStyle(::Type{<:InfiniteMPO}) = InfiniteChainStyle()
4043

4144
function InfiniteMPO(Os::AbstractVector{O}) where {O}
4245
for i in eachindex(Os)

src/operators/mpohamiltonian.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ operators in a form that is compatible with this constructor.
3131
struct MPOHamiltonian{TO <: JordanMPOTensor, V <: AbstractVector{TO}} <: AbstractMPO{TO}
3232
W::V
3333
end
34+
OperatorStyle(::Type{<:MPOHamiltonian}) = HamiltonianStyle()
3435

3536
const FiniteMPOHamiltonian{O <: MPOTensor} = MPOHamiltonian{O, Vector{O}}
3637
Base.isfinite(::Type{<:FiniteMPOHamiltonian}) = true
38+
GeometryStyle(::Type{<:FiniteMPOHamiltonian}) = FiniteChainStyle()
3739

3840
function FiniteMPOHamiltonian(Ws::AbstractVector{O}) where {O <: MPOTensor}
3941
for i in eachindex(Ws)[1:(end - 1)]
@@ -45,6 +47,7 @@ end
4547

4648
const InfiniteMPOHamiltonian{O <: MPOTensor} = MPOHamiltonian{O, PeriodicVector{O}}
4749
Base.isfinite(::Type{<:InfiniteMPOHamiltonian}) = false
50+
GeometryStyle(::Type{<:InfiniteMPOHamiltonian}) = InfiniteChainStyle()
4851

4952
function InfiniteMPOHamiltonian(Ws::AbstractVector{O}) where {O <: MPOTensor}
5053
for i in eachindex(Ws)

src/states/abstractmps.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ abstract type AbstractFiniteMPS <: AbstractMPS end
173173

174174
Base.eltype::AbstractMPS) = eltype(typeof(ψ))
175175
VectorInterface.scalartype(T::Type{<:AbstractMPS}) = scalartype(site_type(T))
176+
Base.isfinite::AbstractMPS) = isfinite(typeof(ψ))
176177

177178
function Base.checkbounds::AbstractMPS, i)
178179
return Base.checkbounds(Bool, ψ, i) || throw(BoundsError(ψ, i))

src/states/finitemps.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ function Base.similar(ψ::FiniteMPS{A, B}) where {A, B}
311311
return FiniteMPS{A, B}(similar.ALs), similar.ARs), similar.ACs), similar.Cs))
312312
end
313313

314-
Base.isfinite::FiniteMPS) = true
314+
Base.isfinite(::Type{<:FiniteMPS}) = true
315+
GeometryStyle(::Type{<:FiniteMPS}) = FiniteChainStyle()
315316

316317
Base.eachindex::FiniteMPS) = eachindex.AL)
317318
Base.eachindex(l::IndexStyle, ψ::FiniteMPS) = eachindex(l, ψ.AL)

src/states/infinitemps.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ Base.size(ψ::InfiniteMPS, args...) = size(ψ.AL, args...)
241241
Base.length::InfiniteMPS) = length.AL)
242242
Base.eltype::InfiniteMPS) = eltype(typeof(ψ))
243243
Base.eltype(::Type{<:InfiniteMPS{A}}) where {A} = A
244-
Base.isfinite::InfiniteMPS) = false
244+
Base.isfinite(::Type{<:InfiniteMPS}) = false
245+
GeometryStyle(::Type{<:InfiniteMPS}) = InfiniteChainStyle()
245246

246247
Base.copy::InfiniteMPS) = InfiniteMPS(copy.AL), copy.AR), copy.C), copy.AC))
247248
function Base.copy!::InfiniteMPS, ϕ::InfiniteMPS)

src/states/multilinemps.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Base.convert(::Type{InfiniteMPS}, st::MultilineMPS) = only(st)
101101
Base.eltype(t::MultilineMPS) = eltype(t[1])
102102
Base.copy!::MultilineMPS, ϕ::MultilineMPS) = (copy!.(parent(ψ), parent(ϕ)); ψ)
103103

104-
Base.isfinite(ψ::MultilineMPS) = false
104+
Base.isfinite(::Type{<:MultilineMPS}) = false
105105

106106
for f_space in (:physicalspace, :left_virtualspace, :right_virtualspace)
107107
@eval $f_space(t::MultilineMPS, i::Int, j::Int) = $f_space(t[i], j)

src/states/quasiparticle_state.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ const FiniteQP{S <: FiniteMPS, T1, T2} = QP{S, T1, T2}
212212
const InfiniteQP{S <: InfiniteMPS, T1, T2} = QP{S, T1, T2}
213213
const MultilineQP{Q <: QP} = Multiline{Q}
214214

215+
GeometryStyle(::Type{<:QP{S, T1, T2}}) where {S, T1, T2} = GeometryStyle(S)
216+
215217
TensorKit.spacetype(::Union{QP{S}, Type{<:QP{S}}}) where {S} = spacetype(S)
216218
TensorKit.sectortype(::Union{QP{S}, Type{<:QP{S}}}) where {S} = sectortype(S)
217219

src/utility/multiline.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function Base.axes(m::Multiline, i::Int)
2929
i == 2 ? axes(parent(m)[1], 1) : throw(ArgumentError("Invalid index $i"))
3030
end
3131
Base.eachindex(m::Multiline) = CartesianIndices(size(m))
32+
Base.isfinite(m::Multiline) = isfinite(typeof(m))
3233

3334
eachsite(m::Multiline) = eachsite(first(parent(m)))
3435

@@ -54,6 +55,12 @@ function Base.repeat(A::Multiline, rows::Int, cols::Int)
5455
return Multiline(outer)
5556
end
5657

58+
# Style
59+
# ----------------
60+
61+
OperatorStyle(::Type{Multiline{T}}) where {T} = OperatorStyle(T)
62+
GeometryStyle(::Type{Multiline{T}}) where {T} = GeometryStyle(T)
63+
5764
# VectorInterface
5865
# ---------------
5966
VectorInterface.scalartype(::Type{Multiline{T}}) where {T} = scalartype(T)

src/utility/styles.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
abstract type OperatorStyle
3+
OperatorStyle(x)
4+
OperatorStyle(::Type{T})
5+
6+
Trait to describe the operator behavior of the input `x` or type `T`, which can be either
7+
* `MPOStyle()`: product of local factors;
8+
* `HamiltonianStyle()`: sum of local terms.
9+
"""
10+
abstract type OperatorStyle end
11+
OperatorStyle(x) = OperatorStyle(typeof(x))
12+
OperatorStyle(T::Type) = throw(MethodError(OperatorStyle, T)) # avoid stackoverflow if not defined
13+
14+
struct MPOStyle <: OperatorStyle end
15+
struct HamiltonianStyle <: OperatorStyle end
16+
17+
@doc (@doc OperatorStyle) MPOStyle
18+
@doc (@doc OperatorStyle) HamiltonianStyle
19+
20+
"""
21+
abstract type GeometryStyle
22+
GeometryStyle(x)
23+
GeometryStyle(::Type{T})
24+
25+
Trait to describe the geometry of the input `x` or type `T`, which can be either
26+
* `FiniteChainStyle()`: object is defined on a finite chain;
27+
* `InfiniteChainStyle()`: object is defined on an infinite chain.
28+
"""
29+
abstract type GeometryStyle end
30+
GeometryStyle(x) = GeometryStyle(typeof(x))
31+
GeometryStyle(T::Type) = throw(MethodError(GeometryStyle, T)) # avoid stackoverflow if not defined
32+
33+
struct FiniteChainStyle <: GeometryStyle end
34+
struct InfiniteChainStyle <: GeometryStyle end
35+
36+
@doc (@doc GeometryStyle) FiniteChainStyle
37+
@doc (@doc GeometryStyle) InfiniteChainStyle

0 commit comments

Comments
 (0)