Skip to content

Commit 0aa451f

Browse files
authored
[Utility] Be more consistent about what space functions work for what arguments (#310)
* Consistently implement `physicalspace(x)` * Consistently implement `left`- and `right_virtualspace(x)` * Consistently implement `eachsite` * Add tests * Add spaces for multilinemps * some small fixes * format * use functions correctly * improve test coverage * add missing constructor * ensure infinite spaces are periodic
1 parent b154b0d commit 0aa451f

File tree

13 files changed

+72
-20
lines changed

13 files changed

+72
-20
lines changed

src/algorithms/expval.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function expectation_value(ψ::AbstractMPS, (inds, O)::Pair)
4242
dual(_lastspace(last(local_mpo)))
4343
for (site, o) in zip(sites, local_mpo)
4444
if o isa MPOTensor
45-
physicalspace)[site] == physicalspace(o) ||
45+
physicalspace, site) == physicalspace(o) ||
4646
throw(SpaceMismatch("physical space does not match at site $site"))
4747
end
4848
end

src/algorithms/toolbox.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function variance(
207207
state.AC[i], envs.GLs[i], H[i][:, :, :, end], envs.GRs[i][end]
208208
)
209209
end
210-
lattice = physicalspace.(Ref(state), 1:length(state))
210+
lattice = physicalspace(state)
211211
H_renormalized = InfiniteMPOHamiltonian(
212212
lattice, i => e * id(storagetype(eltype(H)), lattice[i]) for (i, e) in enumerate(e_local)
213213
)
@@ -234,7 +234,7 @@ function variance(state::InfiniteQP, H::InfiniteMPOHamiltonian, envs = environme
234234
GR = rightenv(envs, i, gs)
235235
return contract_mpo_expval(gs.AC[i], GL, H[i][:, :, :, end], GR[end])
236236
end
237-
lattice = physicalspace.(Ref(gs), 1:length(state))
237+
lattice = physicalspace(gs)
238238
H_regularized = H - InfiniteMPOHamiltonian(
239239
lattice, i => e * id(storagetype(eltype(H)), lattice[i]) for (i, e) in enumerate(e_local)
240240
)

src/operators/abstractmpo.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Base.isfinite(O::AbstractMPO) = isfinite(typeof(O))
1414
# By default, define things in terms of parent
1515
Base.size(mpo::AbstractMPO, args...) = size(parent(mpo), args...)
1616
Base.length(mpo::AbstractMPO) = length(parent(mpo))
17+
eachsite(mpo::AbstractMPO) = eachindex(mpo)
1718

1819
@inline Base.getindex(mpo::AbstractMPO, i::Int) = getindex(parent(mpo), i)
1920
@inline function Base.setindex!(mpo::AbstractMPO, value, i::Int)
@@ -24,11 +25,11 @@ end
2425
# Properties
2526
# ----------
2627
left_virtualspace(mpo::AbstractMPO, site::Int) = left_virtualspace(mpo[site])
27-
left_virtualspace(mpo::AbstractMPO) = map(left_virtualspace, parent(mpo))
28+
left_virtualspace(mpo::AbstractMPO) = map(Base.Fix1(left_virtualspace, mpo), eachsite(mpo))
2829
right_virtualspace(mpo::AbstractMPO, site::Int) = right_virtualspace(mpo[site])
29-
right_virtualspace(mpo::AbstractMPO) = map(right_virtualspace, parent(mpo))
30+
right_virtualspace(mpo::AbstractMPO) = map(Base.Fix1(right_virtualspace, mpo), eachsite(mpo))
3031
physicalspace(mpo::AbstractMPO, site::Int) = physicalspace(mpo[site])
31-
physicalspace(mpo::AbstractMPO) = map(physicalspace, mpo)
32+
physicalspace(mpo::AbstractMPO) = map(Base.Fix1(physicalspace, mpo), eachsite(mpo))
3233

3334
for ftype in (:spacetype, :sectortype, :storagetype)
3435
@eval TensorKit.$ftype(mpo::AbstractMPO) = $ftype(typeof(mpo))

src/operators/mpo.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ DenseMPO(mpo::MPO) = mpo isa DenseMPO ? copy(mpo) : MPO(map(TensorMap, parent(mp
5454
# -------
5555
Base.parent(mpo::MPO) = mpo.O
5656
Base.copy(mpo::MPO) = MPO(copy.(parent(mpo)))
57+
eachsite(mpo::InfiniteMPO) = PeriodicArray(eachindex(mpo))
5758

5859
function Base.similar(mpo::MPO{<:MPOTensor}, ::Type{O}, L::Int) where {O <: MPOTensor}
5960
return MPO(similar(parent(mpo), O, L))

src/states/abstractmps.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Return the virtual space of the bond to the left of sites `pos`.
187187
function left_virtualspace end
188188
left_virtualspace(A::GenericMPSTensor) = space(A, 1)
189189
left_virtualspace(O::MPOTensor) = space(O, 1)
190+
left_virtualspace::AbstractMPS) = map(Base.Fix1(left_virtualspace, ψ), eachsite(ψ))
190191

191192
"""
192193
right_virtualspace(ψ::AbstractMPS, [pos=1:length(ψ)])
@@ -200,6 +201,7 @@ Return the virtual space of the bond to the right of site(s) `pos`.
200201
function right_virtualspace end
201202
right_virtualspace(A::GenericMPSTensor) = space(A, numind(A))'
202203
right_virtualspace(O::MPOTensor) = space(O, 4)'
204+
right_virtualspace::AbstractMPS) = map(Base.Fix1(right_virtualspace, ψ), eachsite(ψ))
203205

204206
"""
205207
physicalspace(ψ::AbstractMPS, [pos=1:length(ψ)])
@@ -211,6 +213,7 @@ physicalspace(A::MPSTensor) = space(A, 2)
211213
physicalspace(A::GenericMPSTensor) = prod(x -> space(A, x), 2:(numind(A) - 1))
212214
physicalspace(O::MPOTensor) = space(O, 2)
213215
physicalspace(O::AbstractBlockTensorMap{<:Any, <:Any, 2, 2}) = only(space(O, 2))
216+
physicalspace::AbstractMPS) = map(Base.Fix1(physicalspace, ψ), eachsite(ψ))
214217

215218
"""
216219
eachsite(state::AbstractMPS)

src/states/finitemps.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ function right_virtualspace(ψ::FiniteMPS, n::Integer)
394394
_firstspace.C[n])
395395
end
396396

397-
physicalspace::FiniteMPS) = physicalspace.(Ref(ψ), 1:length(ψ))
398397
function physicalspace::FiniteMPS{<:GenericMPSTensor{<:Any, N}}, n::Integer) where {N}
399398
N == 1 && return ProductSpace{spacetype(ψ)}()
400399
return physicalspace(coalesce.ALs[n], ψ.ARs[n], ψ.ACs[n]))

src/states/infinitemps.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,15 @@ end
265265

266266
Base.eachindex::InfiniteMPS) = eachindex.AL)
267267
Base.eachindex(l::IndexStyle, ψ::InfiniteMPS) = eachindex(l, ψ.AL)
268+
eachsite::InfiniteMPS) = PeriodicArray(eachindex(ψ))
268269

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

271272
site_type(::Type{<:InfiniteMPS{A}}) where {A} = A
272273
bond_type(::Type{<:InfiniteMPS{<:Any, B}}) where {B} = B
273274

274-
left_virtualspace::InfiniteMPS) = left_virtualspace.(ψ.AL)
275275
left_virtualspace::InfiniteMPS, n::Integer) = left_virtualspace.AL[n])
276-
right_virtualspace::InfiniteMPS) = right_virtualspace.(ψ.AL)
277276
right_virtualspace::InfiniteMPS, n::Integer) = right_virtualspace.AL[n])
278-
physicalspace::InfiniteMPS) = physicalspace.(ψ.AL)
279277
physicalspace::InfiniteMPS, n::Integer) = physicalspace.AL[n])
280278

281279
# TensorKit.space(ψ::InfiniteMPS{<:MPSTensor}, n::Integer) = space(ψ.AC[n], 2)

src/states/multilinemps.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,8 @@ 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-
left_virtualspace(t::MultilineMPS, i::Int, j::Int) = left_virtualspace(t[i], j)
105-
right_virtualspace(t::MultilineMPS, i::Int, j::Int) = right_virtualspace(t[i], j)
104+
for f_space in (:physicalspace, :left_virtualspace, :right_virtualspace)
105+
@eval $f_space(t::MultilineMPS, i::Int, j::Int) = $f_space(t[i], j)
106+
@eval $f_space(t::MultilineMPS, I::CartesianIndex{2}) = $f_space(t, Tuple(I)...)
107+
@eval $f_space(t::MultilineMPS) = map(Base.Fix1($f_space, t), eachindex(t))
108+
end

src/states/quasiparticle_state.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,16 @@ const MultilineQP{Q <: QP} = Multiline{Q}
215215
TensorKit.spacetype(::Union{QP{S}, Type{<:QP{S}}}) where {S} = spacetype(S)
216216
TensorKit.sectortype(::Union{QP{S}, Type{<:QP{S}}}) where {S} = sectortype(S)
217217

218+
physicalspace(state::QP, i::Int) = physicalspace(state.left_gs, i)
219+
physicalspace(state::QP) = physicalspace(state.left_gs)
218220
left_virtualspace(state::QP, i::Int) = left_virtualspace(state.left_gs, i)
221+
left_virtualspace(state::QP) = map(Base.Fix1(left_virtualspace, state), eachsite(state))
219222
right_virtualspace(state::QP, i::Int) = right_virtualspace(state.right_gs, i)
223+
right_virtualspace(state::QP) = map(Base.Fix1(right_virtualspace, state), eachsite(state))
220224
auxiliaryspace(state::QP) = space(state.Xs[1], 2)
221225

226+
eachsite(state::QP) = eachsite(state.left_gs)
227+
222228
Base.copy(a::QP) = copy!(similar(a), a)
223229
Base.copyto!(a::QP, b::QP) = copy!(a, b)
224230
function Base.copy!(a::T, b::T) where {T <: QP}

src/states/windowmps.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,7 @@ for f in (:physicalspace, :left_virtualspace, :right_virtualspace)
150150
@eval $f::WindowMPS, n::Integer) = n < 1 ? $f.left_gs, n) :
151151
n > length(ψ) ? $f.right_gs, n - length(ψ)) :
152152
$f.window, n)
153-
end
154-
function physicalspace::WindowMPS)
155-
return WindowArray(
156-
physicalspace.left_gs), physicalspace.window), physicalspace.right_gs)
157-
)
153+
@eval $f::WindowMPS) = WindowArray($f.left_gs), $f.window), $f.right_gs))
158154
end
159155
r_RR::WindowMPS) = r_RR.right_gs, length(ψ))
160156
l_LL::WindowMPS) = l_LL.left_gs, 1)

0 commit comments

Comments
 (0)