Skip to content

Commit bd6ccdf

Browse files
committed
implement styles for states and operators and add tests
1 parent 140005f commit bd6ccdf

5 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/operators/multilinempo.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Type that represents multiple lines of `MPO` objects.
1212
See also: [`Multiline`](@ref), [`AbstractMPO`](@ref)
1313
"""
1414
const MultilineMPO = Multiline{<:AbstractMPO}
15-
OperatorStyle(mpos::MultilineMPO) = OperatorStyle(O)
1615

1716
function MultilineMPO(Os::AbstractMatrix)
1817
return MultilineMPO(map(FiniteMPO, eachrow(Os)))

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)