Skip to content

Commit 0520f9b

Browse files
committed
add styles to finitemps and infinitemps
1 parent 66a6bff commit 0520f9b

File tree

4 files changed

+37
-73
lines changed

4 files changed

+37
-73
lines changed

src/states/abstractmps.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ TensorKit utility
256256
===========================================================================================#
257257

258258
function TensorKit.dot(ψ₁::AbstractMPS, ψ₂::AbstractMPS; kwargs...)
259-
geometry_style = GeometryStyle(ψ₁, ψ₂)
260-
return TensorKit.dot(geometry_style, ψ₁, ψ₂; kwargs...)
259+
return TensorKit.dot(GeometryStyle(ψ₁, ψ₂), ψ₁, ψ₂; kwargs...)
261260
end
262261
function Base.isapprox(ψ₁::AbstractMPS, ψ₂::AbstractMPS; kwargs...)
263262
return isapprox(dot(ψ₁, ψ₂), 1; kwargs...)

src/states/finitemps.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ Base.@propagate_inbounds function Base.getindex(ψ::FiniteMPS, i::Int)
337337
end
338338
339339
# TODO: check where gauge center is to determine efficient kind
340-
AC2(psi::FiniteMPS, site::Int) = psi.AC[site] * _transpose_tail(psi.AR[site + 1])
340+
AC2(::FiniteChainStyle, psi::AbstractMPS, site::Integer) = psi.AC[site] * _transpose_tail(psi.AR[site + 1])
341341
342342
_complex_if_not_missing(x) = ismissing(x) ? x : complex(x)
343343
function Base.complex(mps::FiniteMPS)
@@ -541,33 +541,32 @@ function TensorKit.rmul!(ψ::FiniteMPS, a::Number)
541541
return ψ
542542
end
543543

544-
function TensorKit.dot(ψ₁::FiniteMPS, ψ₂::FiniteMPS)
544+
function TensorKit.dot(::FiniteChainStyle, ψ₁::AbstractMPS, ψ₂::AbstractMPS)
545545
#todo : rewrite this without having to gauge
546546
length(ψ₁) == length(ψ₂) || throw(ArgumentError("MPS with different length"))
547547
ρr = TransferMatrix(ψ₂.AR[2:end], ψ₁.AR[2:end]) * r_RR(ψ₂)
548548
return tr(_transpose_front(ψ₁.AC[1])' * _transpose_front(ψ₂.AC[1]) * ρr)
549549
end
550550

551-
function TensorKit.norm(ψ::FiniteMPS)
551+
function TensorKit.norm(::FiniteChainStyle, ψ::AbstractMPS)
552552
c = ψ.center
553553
if isinteger(c) # center is an AC
554554
return norm(ψ.AC[Int(c)])
555555
else # center is a bond-tensor
556556
return norm(ψ.C[Int(c - 1 / 2)])
557557
end
558558
end
559-
TensorKit.normalize!(ψ::FiniteMPS) = rmul!(ψ, 1 / norm(ψ))
560-
TensorKit.normalize(ψ::FiniteMPS) = normalize!(copy(ψ))
559+
TensorKit.normalize!(::FiniteChainStyle, ψ::AbstractMPS) = rmul!(ψ, 1 / norm(ψ))
561560

562561
#===========================================================================================
563562
Fixedpoints
564563
===========================================================================================#
565564

566-
function r_RR(ψ::FiniteMPS, site::Int = length(ψ))
565+
function r_RR(::FiniteChainStyle, ψ::AbstractMPS, site::Int = length(ψ))
567566
Vr = right_virtualspace(ψ.AR[site])
568567
return isomorphism(storagetype(site_type(ψ)), Vr Vr)
569568
end
570-
function l_LL(ψ::FiniteMPS, site::Int = 1)
569+
function l_LL(::FiniteChainStyle, ψ::AbstractMPS, site::Int = 1)
571570
Vl = left_virtualspace(ψ.AL[site])
572571
return isomorphism(storagetype(site_type(ψ)), Vl Vl)
573572
end

src/states/infinitemps.jl

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ end
227227
Utility
228228
===========================================================================================#
229229

230-
function AC2(ψ::InfiniteMPS, i::Integer; kind = :ACAR)
230+
function AC2(::InfiniteChainStyle, ψ::AbstractMPS, i::Integer; kind = :ACAR)
231231
if kind == :ACAR
232232
return ψ.AC[i] * _transpose_tail(ψ.AR[i + 1])
233233
elseif kind == :ALAC
@@ -276,7 +276,7 @@ end
276276

277277
Base.eachindex(ψ::InfiniteMPS) = eachindex(ψ.AL)
278278
Base.eachindex(l::IndexStyle, ψ::InfiniteMPS) = eachindex(l, ψ.AL)
279-
eachsite(ψ::InfiniteMPS) = PeriodicArray(eachindex(ψ))
279+
eachsite(::InfiniteChainStyle, ψ::AbstractMPS) = PeriodicArray(eachindex(ψ))
280280

281281
Base.checkbounds(::Type{Bool}, ψ::InfiniteMPS, i::Integer) = true
282282

@@ -294,87 +294,38 @@ physicalspace(ψ::InfiniteMPS, n::Integer) = physicalspace(ψ.AL[n])
294294
# return ProductSpace{S}(space.(Ref(t), Base.front(Base.tail(TensorKit.allind(t)))))
295295
# end
296296

297-
TensorKit.norm(ψ::InfiniteMPS) = norm(ψ.AC[1])
298-
function TensorKit.normalize!(ψ::InfiniteMPS)
297+
TensorKit.norm(::InfiniteChainStyle, ψ::AbstractMPS) = norm(ψ.AC[1])
298+
function TensorKit.normalize!(::InfiniteChainStyle, ψ::AbstractMPS)
299299
normalize!.(ψ.C)
300300
normalize!.(ψ.AC)
301301
return ψ
302302
end
303303

304-
function TensorKit.dot(ψ₁::InfiniteMPS, ψ₂::InfiniteMPS; krylovdim = 30)
304+
function TensorKit.dot(::InfiniteChainStyle, ψ₁::AbstractMPS, ψ₂::AbstractMPS; krylovdim = 30)
305305
init = similar(ψ₁.AL[1], _firstspace(ψ₂.AL[1]) _firstspace(ψ₁.AL[1]))
306306
randomize!(init)
307307
val, = fixedpoint(
308308
TransferMatrix(ψ₂.AL, ψ₁.AL), init, :LM, Arnoldi(; krylovdim = krylovdim)
309309
)
310310
return val
311311
end
312-
function Base.isapprox(ψ₁::InfiniteMPS, ψ₂::InfiniteMPS; kwargs...)
313-
return isapprox(dot(ψ₁, ψ₂), 1; kwargs...)
314-
end
315312

316313
#===========================================================================================
317314
Fixedpoints
318315
===========================================================================================#
319316

320-
"""
321-
l_RR(ψ, location)
322-
323-
Left dominant eigenvector of the `AR`-`AR` transfermatrix.
324-
"""
325-
l_RR(ψ::InfiniteMPS, loc::Int = 1) = adjoint(ψ.C[loc - 1]) * ψ.C[loc - 1]
326-
327-
"""
328-
l_RL(ψ, location)
329-
330-
Left dominant eigenvector of the `AR`-`AL` transfermatrix.
331-
"""
332-
l_RL(ψ::InfiniteMPS, loc::Int = 1) = ψ.C[loc - 1]
333-
334-
"""
335-
l_LR(ψ, location)
336-
337-
Left dominant eigenvector of the `AL`-`AR` transfermatrix.
338-
"""
339-
l_LR(ψ::InfiniteMPS, loc::Int = 1) = ψ.C[loc - 1]'
340-
341-
"""
342-
l_LL(ψ, location)
343-
344-
Left dominant eigenvector of the `AL`-`AL` transfermatrix.
345-
"""
346-
function l_LL(ψ::InfiniteMPS{A}, loc::Int = 1) where {A}
347-
return isomorphism(storagetype(A), left_virtualspace(ψ, loc), left_virtualspace(ψ, loc))
317+
l_RR(::InfiniteChainStyle, ψ::AbstractMPS, loc::Int = 1) = adjoint(ψ.C[loc - 1]) * ψ.C[loc - 1]
318+
l_RL(::InfiniteChainStyle, ψ::AbstractMPS, loc::Int = 1) = ψ.C[loc - 1]
319+
l_LR(::InfiniteChainStyle, ψ::AbstractMPS, loc::Int = 1) = ψ.C[loc - 1]'
320+
function l_LL(::InfiniteChainStyle, ψ::AbstractMPS, loc::Int = 1)
321+
return isomorphism(storagetype(eltype(ψ)), left_virtualspace(ψ, loc), left_virtualspace(ψ, loc))
348322
end
349323

350-
"""
351-
r_RR(ψ, location)
352-
353-
Right dominant eigenvector of the `AR`-`AR` transfermatrix.
354-
"""
355-
function r_RR(ψ::InfiniteMPS{A}, loc::Int = length(ψ)) where {A}
324+
function r_RR(::InfiniteChainStyle, ψ::AbstractMPS, loc::Int = length(ψ))
356325
return isomorphism(
357-
storagetype(A), right_virtualspace(ψ, loc), right_virtualspace(ψ, loc)
326+
storagetype(eltype(ψ)), right_virtualspace(ψ, loc), right_virtualspace(ψ, loc)
358327
)
359328
end
360-
361-
"""
362-
r_RL(ψ, location)
363-
364-
Right dominant eigenvector of the `AR`-`AL` transfermatrix.
365-
"""
366-
r_RL(ψ::InfiniteMPS, loc::Int = length(ψ)) = ψ.C[loc]'
367-
368-
"""
369-
r_LR(ψ, location)
370-
371-
Right dominant eigenvector of the `AL`-`AR` transfermatrix.
372-
"""
373-
r_LR(ψ::InfiniteMPS, loc::Int = length(ψ)) = ψ.C[loc]
374-
375-
"""
376-
r_LL(ψ, location)
377-
378-
Right dominant eigenvector of the `AL`-`AL` transfermatrix.
379-
"""
380-
r_LL(ψ::InfiniteMPS, loc::Int = length(ψ)) = ψ.C[loc] * adjoint(ψ.C[loc])
329+
r_RL(::InfiniteChainStyle, ψ::AbstractMPS, loc::Int = length(ψ)) = ψ.C[loc]'
330+
r_LR(::InfiniteChainStyle, ψ::AbstractMPS, loc::Int = length(ψ)) = ψ.C[loc]
331+
r_LL(::InfiniteChainStyle, ψ::AbstractMPS, loc::Int = length(ψ)) = ψ.C[loc] * adjoint(ψ.C[loc])

test/states.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module TestStates
1111
using MPSKit: _transpose_front, _transpose_tail
1212
using MPSKit: GeometryStyle, FiniteChainStyle, InfiniteChainStyle
1313
using MPSKit: TransferMatrix
14+
using MPSKit: AC2
1415
using TensorKit
1516
using TensorKit:
1617

@@ -120,6 +121,9 @@ module TestStates
120121
@test all(x -> x D, left_virtualspace(ψ))
121122
@test all(x -> x D, right_virtualspace(ψ))
122123

124+
normalize!(ψ)
125+
@test norm(ψ) norm(InfiniteChainStyle(), ψ) 1.0
126+
123127
for i in 1:length(ψ)
124128
@plansor difference[-1 -2; -3] := ψ.AL[i][-1 -2; 1] * ψ.C[i][1; -3] -
125129
ψ.C[i - 1][-1; 1] * ψ.AR[i][1 -2; -3]
@@ -134,6 +138,17 @@ module TestStates
134138
@test TransferMatrix(ψ.AL[i], ψ.AR[i]) * r_LR(ψ, i) r_LR(ψ, i + 1)
135139
@test TransferMatrix(ψ.AR[i], ψ.AL[i]) * r_RL(ψ, i) r_RL(ψ, i + 1)
136140
@test TransferMatrix(ψ.AR[i], ψ.AR[i]) * r_RR(ψ, i) r_RR(ψ, i + 1)
141+
142+
@test l_LL(ψ, i) l_LL(InfiniteChainStyle(), ψ, i)
143+
@test l_LR(ψ, i) l_LR(InfiniteChainStyle(), ψ, i)
144+
@test l_RL(ψ, i) l_RL(InfiniteChainStyle(), ψ, i)
145+
@test l_RR(ψ, i) l_RR(InfiniteChainStyle(), ψ, i)
146+
@test r_LL(ψ, i) r_LL(InfiniteChainStyle(), ψ, i)
147+
@test r_LR(ψ, i) r_LR(InfiniteChainStyle(), ψ, i)
148+
@test r_RL(ψ, i) r_RL(InfiniteChainStyle(), ψ, i)
149+
@test r_RR(ψ, i) r_RR(InfiniteChainStyle(), ψ, i)
150+
151+
@test @constinferred AC2(ψ, i) AC2(InfiniteChainStyle(), ψ, i)
137152
end
138153
end
139154

0 commit comments

Comments
 (0)