Skip to content

Commit 5be03c3

Browse files
authored
Merge pull request #1 from AFeuerpfeil/pr-mpo-styles
Pr mpo styles
2 parents fa91804 + bd6ccdf commit 5be03c3

File tree

10 files changed

+73
-2
lines changed

10 files changed

+73
-2
lines changed

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+
IsfiniteStyle(::FiniteMPO) = FiniteStyle()
21+
OperatorStyle(::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+
IsfiniteStyle(::InfiniteMPO) = InfiniteStyle()
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(::MPOHamiltonian) = HamiltonianStyle()
3435

3536
const FiniteMPOHamiltonian{O <: MPOTensor} = MPOHamiltonian{O, Vector{O}}
3637
Base.isfinite(::Type{<:FiniteMPOHamiltonian}) = true
38+
IsfiniteStyle(::FiniteMPOHamiltonian) = FiniteStyle()
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+
IsfiniteStyle(::InfiniteMPOHamiltonian) = InfiniteStyle()
4851

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

src/states/abstractmps.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const MPSTensor{S} = GenericMPSTensor{S, 2} # the usual mps tensors on which we
1515

1616
"""
1717
MPSTensor([f, eltype], d::Int, left_D::Int, [right_D]::Int])
18-
MPSTensor([f, eltype], physicalspace::Union{S,CompositeSpace{S}},
18+
MPSTensor([f, eltype], physicalspace::Union{S,CompositeSpace{S}},
1919
left_virtualspace::S, [right_virtualspace]::S) where {S<:ElementarySpace}
2020
2121
Construct an `MPSTensor` with given physical and virtual spaces.
@@ -201,7 +201,7 @@ TensorKit.sectortype(ψtype::Type{<:AbstractMPS}) = sectortype(site_type(ψtype)
201201

202202
"""
203203
left_virtualspace(ψ::AbstractMPS, [pos=1:length(ψ)])
204-
204+
205205
Return the virtual space of the bond to the left of sites `pos`.
206206
207207
!!! warning

src/states/finitemps.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ function Base.similar(ψ::FiniteMPS{A, B}) where {A, B}
312312
end
313313

314314
Base.isfinite::FiniteMPS) = true
315+
IsfiniteStyle(::FiniteMPS) = FiniteStyle()
315316

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

src/states/infinitemps.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ Base.length(ψ::InfiniteMPS) = length(ψ.AL)
242242
Base.eltype::InfiniteMPS) = eltype(typeof(ψ))
243243
Base.eltype(::Type{<:InfiniteMPS{A}}) where {A} = A
244244
Base.isfinite::InfiniteMPS) = false
245+
IsfiniteStyle(::InfiniteMPS) = InfiniteStyle()
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Base.eltype(t::MultilineMPS) = eltype(t[1])
102102
Base.copy!::MultilineMPS, ϕ::MultilineMPS) = (copy!.(parent(ψ), parent(ϕ)); ψ)
103103

104104
Base.isfinite::MultilineMPS) = false
105+
IsfiniteStyle(::MultilineMPS) = InfiniteStyle()
105106

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

src/states/quasiparticle_state.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ end
208208

209209
# gauge independent code
210210
const QP{S, T1, T2} = Union{LeftGaugedQP{S, T1, T2}, RightGaugedQP{S, T1, T2}}
211+
# TODO: Remove FiniteQP and InfiniteQP in favor of styles.
211212
const FiniteQP{S <: FiniteMPS, T1, T2} = QP{S, T1, T2}
212213
const InfiniteQP{S <: InfiniteMPS, T1, T2} = QP{S, T1, T2}
213214
const MultilineQP{Q <: QP} = Multiline{Q}
@@ -228,6 +229,8 @@ eachsite(state::QP) = eachsite(state.left_gs)
228229
istopological(qp::QP) = qp.left_gs !== qp.right_gs
229230
istrivial(qp::QP) = !istopological(qp) && isone(auxiliarysector(qp))
230231

232+
IsfiniteStyle(qp::QP) = IsfiniteStyle(qp.left_gs)
233+
231234
Base.copy(a::QP) = copy!(similar(a), a)
232235
Base.copyto!(a::QP, b::QP) = copy!(a, b)
233236
function Base.copy!(a::T, b::T) where {T <: QP}

src/utility/styles.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1+
"""
2+
`OperatorStyle`
3+
4+
Holy trait used as a dispatch tag for operator representations.
5+
Concrete subtypes (`MPOStyle` and `HamiltonianStyle`) indicate
6+
whether an operator is stored as an MPO or as a Hamiltonian.
7+
Use `OperatorStyle` in method signatures to select implementation-
8+
specific code paths for different operator types.
9+
10+
To opt a custom operator type into this dispatch scheme implement:
11+
```julia
12+
OperatorStyle(::T) where {T<:YourOperatorType}
13+
```
14+
"""
115
abstract type OperatorStyle end
216

317
struct MPOStyle <: OperatorStyle end
418
struct HamiltonianStyle <: OperatorStyle end
519

620

21+
"""
22+
`IsfiniteStyle`
23+
Holy trait used as a dispatch tag to distinguish between finite
24+
and infinite types. Concrete subtypes (`FiniteStyle` and
25+
`InfiniteStyle`) indicate whether a system is finite or infinite.
26+
Use `IsfiniteStyle` in method signatures to select implementation-
27+
specific code paths for different types.
28+
29+
To opt a custom type into this dispatch scheme implement:
30+
```julia
31+
IsfiniteStyle(::T) where {T<:YourType}
32+
```
33+
"""
734
abstract type IsfiniteStyle end
835

936
struct FiniteStyle <: IsfiniteStyle end

test/operators.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module TestOperators
1010
using MPSKit
1111
using MPSKit: _transpose_front, _transpose_tail, C_hamiltonian, AC_hamiltonian,
1212
AC2_hamiltonian
13+
using MPSKit: IsfiniteStyle, FiniteStyle, InfiniteStyle, OperatorStyle, MPOStyle,
14+
HamiltonianStyle
1315
using TensorKit
1416
using TensorKit:
1517
using VectorInterface: One
@@ -32,6 +34,10 @@ module TestOperators
3234
mpo₂ = FiniteMPO(O₂)
3335
mpo₃ = FiniteMPO(O₃)
3436

37+
@test IsfiniteStyle(mpo₁) == FiniteStyle()
38+
@test OperatorStyle(mpo₁) == MPOStyle()
39+
40+
3541
@test @constinferred physicalspace(mpo₁) == fill(V, L)
3642
Vleft = @constinferred left_virtualspace(mpo₁)
3743
Vright = @constinferred right_virtualspace(mpo₂)
@@ -81,6 +87,18 @@ module TestOperators
8187
end
8288
end
8389

90+
@testset "InfiniteMPO" begin
91+
P =^2
92+
T = Float64
93+
94+
H1 = randn(T, P P)
95+
H1 += H1'
96+
H = InfiniteMPO(H1)
97+
98+
@test IsfiniteStyle(H) == InfiniteStyle()
99+
@test OperatorStyle(H) == MPOStyle()
100+
end
101+
84102
@testset "MPOHamiltonian constructors" begin
85103
P =^2
86104
T = Float64
@@ -109,6 +127,9 @@ module TestOperators
109127
H′ = FiniteMPOHamiltonian(map(Base.Fix1(collect, Any), Ws)) # without type info
110128
@test H H′
111129

130+
@test IsfiniteStyle(H) == FiniteStyle()
131+
@test OperatorStyle(H) == HamiltonianStyle()
132+
112133
# Infinite
113134
Ws = [Wmid]
114135
H = InfiniteMPOHamiltonian(
@@ -119,6 +140,9 @@ module TestOperators
119140

120141
H′ = InfiniteMPOHamiltonian(map(Base.Fix1(collect, Any), Ws)) # without type info
121142
@test all(parent(H) .≈ parent(H′))
143+
144+
@test IsfiniteStyle(H) == InfiniteStyle()
145+
@test OperatorStyle(H) == HamiltonianStyle()
122146
end
123147

124148
@testset "Finite MPOHamiltonian" begin

test/states.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module TestStates
99
using Test, TestExtras
1010
using MPSKit
1111
using MPSKit: _transpose_front, _transpose_tail
12+
using MPSKit: IsfiniteStyle, FiniteStyle, InfiniteStyle
1213
using MPSKit: TransferMatrix
1314
using TensorKit
1415
using TensorKit:
@@ -25,6 +26,7 @@ module TestStates
2526
ψ = FiniteMPS(rand, elt, L, d, D)
2627

2728
@test isfinite(ψ)
29+
@test IsfiniteStyle(ψ) == FiniteStyle()
2830
@test @constinferred physicalspace(ψ) == fill(d, L)
2931
@test all(x -> x D, @constinferred left_virtualspace(ψ))
3032
@test all(x -> x D, @constinferred right_virtualspace(ψ))
@@ -101,6 +103,8 @@ module TestStates
101103
ψ = InfiniteMPS([rand(elt, D * d, D), rand(elt, D * d, D)]; tol)
102104

103105
@test !isfinite(ψ)
106+
@test IsfiniteStyle(ψ) == InfiniteStyle()
107+
104108
@test physicalspace(ψ) == fill(d, 2)
105109
@test all(x -> x D, left_virtualspace(ψ))
106110
@test all(x -> x D, right_virtualspace(ψ))
@@ -231,6 +235,8 @@ module TestStates
231235
ϕ₁ = LeftGaugedQP(rand, ψ)
232236
ϕ₂ = LeftGaugedQP(rand, ψ)
233237

238+
@test IsfiniteStyle(ϕ₁) == FiniteStyle()
239+
234240
@test @constinferred physicalspace(ϕ₁) == physicalspace(ψ)
235241
@test @constinferred left_virtualspace(ϕ₁) == left_virtualspace(ψ)
236242
@test @constinferred right_virtualspace(ϕ₁) == right_virtualspace(ψ)
@@ -266,6 +272,8 @@ module TestStates
266272
ϕ₁ = LeftGaugedQP(rand, ψ)
267273
ϕ₂ = LeftGaugedQP(rand, ψ)
268274

275+
@test IsfiniteStyle(ϕ₁) == InfiniteStyle()
276+
269277
@test @constinferred physicalspace(ϕ₁) == physicalspace(ψ)
270278
@test @constinferred left_virtualspace(ϕ₁) == left_virtualspace(ψ)
271279
@test @constinferred right_virtualspace(ϕ₁) == right_virtualspace(ψ)

0 commit comments

Comments
 (0)