Skip to content

Commit 04d9fde

Browse files
Add support for local expectation values of tensor product and MPO terms (#425)
* Add support for local expectation values of tensor product and MPO terms * Fix MPO term validation and arithmetic Validate physical and bond spaces, preserve input containers, and support scalar promotion without losing tensor-product dispatch. Reject unsupported term accumulation and real/imaginary operations, and consolidate regression tests. * Support factored observables in single-layer PEPO expectation values Add the missing single-layer MPO overload and document both PEPO evaluation forms. Cover tensor-product and MPO terms in single-layer and purified PEPOs with compact tests for trivial and fermionic symmetry. * Add BP fallback for factored expectation values Convert BP environments to CTMRG environments for MPO and tensor-product terms, with a TODO for native BP boundary contractions. Add regression comparisons against dense BP expectation values. * Fix storagetype for factored terms and scale them through VectorInterface * Unify scalar type in a MPO/TensorProduct when adding to a LocalOperator * Docstring clarifications * Split local operator into focused smaller files * Preparation to fusing charges into MPO and tensor product terms * Number-of-site consistency check --------- Co-authored-by: Yue Zhengyuan <yuezy1997@icloud.com>
1 parent 881b617 commit 04d9fde

14 files changed

Lines changed: 633 additions & 42 deletions

File tree

‎src/PEPSKit.jl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ include("utility/symmetrization.jl")
8181
include("operators/infinitepepo.jl")
8282
include("operators/transfermatrix.jl")
8383
include("operators/localoperator.jl")
84+
include("operators/localoperator/dense.jl")
85+
include("operators/localoperator/tensorproduct.jl")
86+
include("operators/localoperator/mpo.jl")
8487
include("operators/localcircuit.jl")
8588
include("operators/lattices/squarelattice.jl")
8689
include("operators/models.jl")

‎src/algorithms/contractions/local_patch/network_expr.jl‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,40 @@ function operator_contraction_expr(::Type{<:AbstractTensorMap}, nsites)
260260
),
261261
]
262262
end
263+
264+
# The MPO bond is small - the operator's Schmidt rank, e.g. 3 for Heisenberg XYZ - so it is
265+
# labelled as a physical rather than a virtual dimension, which gives `@autoopt` the right
266+
# order of magnitude when it searches for a contraction order.
267+
mpolabel(args...) = physicallabel(:mpo, args...)
268+
269+
function operator_contraction_expr(::Type{<:MPOTerm}, nsites)
270+
# one factor per site, linked by a chain of bonds running left to right, rank-3 at the
271+
# ends of the chain and rank-4 in the bulk:
272+
# W₁ : bra₁ ← ket₁ ⊗ b₁
273+
# Wᵢ : bᵢ₋₁ ⊗ braᵢ ← ketᵢ ⊗ bᵢ
274+
# W_N : b_{N-1} ⊗ bra_N ← ket_N
275+
return map(1:nsites) do i
276+
bra, ket = physicallabel(:O, 2, i), physicallabel(:O, 1, i)
277+
out, ins = if nsites == 1
278+
(bra,), (ket,)
279+
elseif i == 1
280+
(bra,), (ket, mpolabel(1))
281+
elseif i == nsites
282+
(mpolabel(nsites - 1), bra), (ket,)
283+
else
284+
(mpolabel(i - 1), bra), (ket, mpolabel(i))
285+
end
286+
return tensorexpr(:(operator[$i]), out, ins)
287+
end
288+
end
289+
290+
# A TensorProductTerm is represented with all trivial MPO bond legs removed.
291+
# Each factor connects only the ket and bra physical indices of its site.
292+
# This method is more specific than the `MPOTerm` one above, so it takes precedence.
293+
function operator_contraction_expr(::Type{<:TensorProductTerm}, nsites)
294+
return map(1:nsites) do i
295+
return tensorexpr(
296+
:(operator[$i]), (physicallabel(:O, 2, i),), (physicallabel(:O, 1, i),)
297+
)
298+
end
299+
end

‎src/algorithms/expectation_value/expectation_value.jl‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ function local_expectation_value(inds, state, operator::AbstractTensorMap, env)
5454
return trmul(operator, ρ)
5555
end
5656

57+
"""
58+
$(SIGNATURES)
59+
60+
Compute the contribution of an [`MPOTerm`](@ref), including a [`TensorProductTerm`](@ref), given as one tensor per site in `inds`.
61+
For a PEPS or purified PEPO, compute ``⟨bra|O|ket⟩ / ⟨bra|ket⟩``; for a single-layer density matrix PEPO, compute ``tr(O * state) / tr(state)``.
62+
Insert the factors into the patch contraction directly and divide by the local norm of the same patch.
63+
"""
64+
function local_expectation_value(inds, bra, operator::MPOTerm, ket, env)
65+
return contract_local_operator(inds, operator, ket, bra, env) /
66+
contract_local_norm(inds, ket, bra, env)
67+
end
68+
function local_expectation_value(inds, state, operator::MPOTerm, env)
69+
return contract_local_operator(inds, operator, state, env) /
70+
contract_local_norm(inds, state, env)
71+
end
72+
73+
# TODO: Implement boundary_contraction_expr for BPEnv to contract its messages directly.
74+
local_expectation_value(inds, bra, operator::MPOTerm, ket, env::BPEnv) =
75+
local_expectation_value(inds, bra, operator, ket, CTMRGEnv(env))
76+
5777
# Expectation value of a local partition function tensor
5878
# ------------------------------------------------------
5979

‎src/algorithms/expectation_value/patch_contractions.jl‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Direct local patch contractions
22
# -------------------------------
33

4+
"""
5+
Check that a factored operator contains one factor per site.
6+
"""
7+
_check_factor_count(inds, O) = nothing
8+
function _check_factor_count(inds, O::MPOTerm)
9+
length(inds) == length(O) ||
10+
throw(ArgumentError("Got $(length(inds)) sites but $(length(O)) operator factors."))
11+
return nothing
12+
end
13+
414
"""
515
contract_local_operator(inds, O, ket::InfinitePEPS, bra::InfinitePEPS, env)
616
contract_local_operator(inds, O, ket::InfinitePEPO, bra::InfinitePEPO, env)
@@ -16,13 +26,15 @@ function contract_local_operator(
1626
inds::Vector{CartesianIndex{2}}, O,
1727
ket::InfinitePEPS, bra::InfinitePEPS, env,
1828
)
29+
_check_factor_count(inds, O)
1930
static_inds = Tuple(Val.(inds))
2031
return _contract_local_operator(static_inds, O, (ket, bra), env)
2132
end
2233
function contract_local_operator(
2334
inds::Vector{CartesianIndex{2}}, O, state::InfinitePEPO, env
2435
)
2536
size(state, 3) == 1 || throw(DimensionMismatch("only single-layer densitymatrices are supported"))
37+
_check_factor_count(inds, O)
2638
static_inds = Tuple(Val.(inds))
2739
return _contract_local_operator(static_inds, O, state, env)
2840
end
@@ -31,6 +43,7 @@ function contract_local_operator(
3143
)
3244
size(ket) == size(bra) || throw(DimensionMismatch("incompatible bra and ket dimensions"))
3345
size(ket, 3) == 1 || throw(DimensionMismatch("only single-layer densitymatrices are supported"))
46+
_check_factor_count(inds, O)
3447
static_inds = Tuple(Val.(inds))
3548
return _contract_local_operator(static_inds, O, (ket, bra), env)
3649
end

‎src/operators/localoperator.jl‎

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,46 +50,26 @@ end
5050
# Default to Any for eltype: needs to be abstract anyways so not that much to gain
5151
LocalOperator(lattice, terms) = LocalOperator{Any}(lattice, terms)
5252
LocalOperator(lattice, terms::Pair...) = LocalOperator(lattice, terms)
53-
# TODO: add terms beyond AbstractTensorMap
54-
# e.g. tensor product of 1-site operators, MPOs
55-
add_term!(operator::LocalOperator, inds::Tuple, term::AbstractTensorMap) = add_term!(operator, collect(inds), term)
56-
add_term!(operator::LocalOperator, inds::Vector, term::AbstractTensorMap) = add_term!(operator, map(CartesianIndex{2}, inds), term)
57-
function add_term!(
58-
operator::LocalOperator, inds::Vector{CartesianIndex{2}}, term::AbstractTensorMap;
59-
atol = zero(real(scalartype(term))),
60-
)
61-
# input checks
62-
length(inds) == numin(term) == numout(term) || throw(ArgumentError("Incompatible number of indices and tensor legs"))
63-
allunique(inds) || throw(ArgumentError("`inds` should not contain repeated coordinates."))
64-
for (i, ind) in enumerate(inds)
65-
ind_translated = CartesianIndex(mod1.(Tuple(ind), size(operator)))
66-
physicalspace(operator, ind_translated) == domain(term)[i] == codomain(term)[i] ||
67-
throw(SpaceMismatch("Incompatible physical spaces"))
68-
end
69-
norm(term) <= atol && return operator # skip adding negligible terms
70-
71-
# permute input
72-
if !issorted(inds)
73-
I = sortperm(inds)
74-
inds = inds[I]
75-
term = permute(term, (Tuple(I), Tuple(I) .+ numout(term)))
76-
end
77-
78-
# translate coordinates
79-
_shift_into_unitcell!(inds, size(operator))
80-
81-
if haskey(operator.terms, inds)
82-
operator.terms[inds] = VI.add!!(operator.terms[inds], term)
83-
else
84-
operator.terms[inds] = term
85-
end
8653

87-
return operator
54+
"""
55+
Promote tensor factors to a common scalar type, reusing vectors whose element type already specifies one.
56+
"""
57+
_promote_term_scalars(term::AbstractVector{<:AbstractTensorMap{T}}) where {T <: Number} = term
58+
function _promote_term_scalars(term::AbstractVector)
59+
T = promote_type(map(scalartype, term)...)
60+
return map(t -> scalartype(t) === T ? t : VI.scale(t, one(T)), term)
8861
end
8962

63+
"""
64+
Return the tensor factors of a dense or factored local term.
65+
"""
66+
_term_tensors(term::AbstractTensorMap) = (term,)
67+
_term_tensors(term::AbstractVector{<:AbstractTensorMap}) = term
68+
9069
function TensorKit.storagetype(lo::LocalOperator)
9170
T = scalartype(lo)
92-
return mapreduce(t -> similarstoragetype(t, T), TensorKit.promote_storagetype, values(lo.terms))
71+
tensors = Iterators.flatten(Iterators.map(_term_tensors, values(lo.terms)))
72+
return mapreduce(t -> similarstoragetype(t, T), TensorKit.promote_storagetype, tensors)
9373
end
9474

9575
"""
@@ -158,24 +138,45 @@ Base.eltype(::Type{LocalOperator{O, S}}) where {O, S} = O
158138
# Real and imaginary part
159139
# -----------------------
160140
function Base.real(O::LocalOperator)
141+
any(term -> term isa MPOTerm, values(O.terms)) &&
142+
throw(ArgumentError("Taking the real part is not implemented for MPO or tensor product terms."))
161143
return LocalOperator(O.lattice, (sites => real(op) for (sites, op) in O.terms)...)
162144
end
163145
function Base.imag(O::LocalOperator)
146+
any(term -> term isa MPOTerm, values(O.terms)) &&
147+
throw(ArgumentError("Taking the imaginary part is not implemented for MPO or tensor product terms."))
164148
return LocalOperator(O.lattice, (sites => imag(op) for (sites, op) in O.terms)...)
165149
end
166150

167151
# Linear Algebra
168152
# --------------
169-
Base.:*(α::Number, O::LocalOperator) =
170-
LocalOperator(physicalspace(O), inds => α * operator for (inds, operator) in O.terms)
153+
"""
154+
Scale a single term of a [`LocalOperator`](@ref) by `α`.
155+
156+
An [`MPOTerm`](@ref), including a [`TensorProductTerm`](@ref), is scaled by scaling its first
157+
factor only - scaling the whole container would scale every factor and give `α^N`.
158+
"""
159+
function _scale_local_term end
160+
Base.:*(α::Number, O::LocalOperator) = LocalOperator(
161+
physicalspace(O), inds => _scale_local_term(operator, α) for (inds, operator) in O.terms
162+
)
171163
Base.:*(O::LocalOperator, α::Number) = α * O
172164

173165
Base.:/(O::LocalOperator, α::Number) = O * inv(α)
174166
Base.:\(α::Number, O::LocalOperator) = inv(α) * O
175167

168+
"""
169+
Accumulate dense terms while rejecting addition involving an MPO or tensor product at the same sites.
170+
"""
171+
function _add_local_terms(term1, term2)
172+
(term1 isa MPOTerm || term2 isa MPOTerm) &&
173+
throw(ArgumentError("Accumulating terms with the same sites is not implemented for MPO or tensor product terms."))
174+
return VI.add(term1, term2)
175+
end
176+
176177
function Base.:+(O1::LocalOperator, O2::LocalOperator)
177178
checklattice(O1, O2)
178-
return LocalOperator(physicalspace(O1), mergewith(VI.add, O1.terms, O2.terms))
179+
return LocalOperator(physicalspace(O1), mergewith(_add_local_terms, O1.terms, O2.terms))
179180
end
180181

181182
Base.:-(O::LocalOperator) = -1 * O
@@ -184,12 +185,12 @@ Base.:-(O1::LocalOperator, O2::LocalOperator) = O1 + (-O2)
184185
# VectorInterface
185186
# ---------------
186187

187-
# Since we allow abstract types in T, value and type domain might not match
188+
# Terms in a LocalOperator may have different scalar types. Return their promoted scalar type.
189+
# A term is either a tensor or a container of factors sharing one scalar type, and `scalartype` resolves both.
188190
function VI.scalartype(operator::LocalOperator)
189-
return promote_type((scalartype(term[2]) for term in operator.terms)...)
191+
return promote_type((scalartype(term) for term in values(operator.terms))...)
190192
end
191193

192-
193194
# Equivalence
194195
# -----------
195196

@@ -234,11 +235,17 @@ end
234235
# ---------------
235236
TensorKit.spacetype(::Type{<:LocalOperator{<:Any, S}}) where {S} = S
236237

238+
"""
239+
Fuse auxiliary charges into the physical spaces of a term in `LocalOperator`.
240+
"""
241+
function _fuse_charge end
242+
237243
"""
238244
add_physical_charge(H::LocalOperator, charges::AbstractMatrix{<:Sector})
239245
240246
Change the spaces of a `LocalOperator` by fusing in an auxiliary charge into the domain of
241247
the operator on every site, according to a given matrix of 'auxiliary' physical charges.
248+
MPO and tensor-product terms are not yet supported.
242249
"""
243250
function MPSKit.add_physical_charge(H::LocalOperator, charges::AbstractMatrix{<:Sector})
244251
size(H) == size(charges) ||
@@ -258,6 +265,6 @@ function MPSKit.add_physical_charge(H::LocalOperator, charges::AbstractMatrix{<:
258265

259266
return LocalOperator(
260267
Pspaces,
261-
inds => fuse_charge(op, Tuple(map(Base.Fix1(getindex, periodic_charges), inds))) for (inds, op) in H.terms
268+
inds => _fuse_charge(op, Tuple(map(Base.Fix1(getindex, periodic_charges), inds))) for (inds, op) in H.terms
262269
)
263270
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Dense AbstractTensorMap terms
2+
3+
add_term!(operator::LocalOperator, inds::Tuple, term::AbstractTensorMap; kwargs...) = add_term!(operator, collect(inds), term; kwargs...)
4+
add_term!(operator::LocalOperator, inds::AbstractVector, term::AbstractTensorMap; kwargs...) = add_term!(operator, CartesianIndex{2}[CartesianIndex{2}(ind) for ind in inds], term; kwargs...)
5+
function add_term!(
6+
operator::LocalOperator, inds::Vector{CartesianIndex{2}}, term::AbstractTensorMap;
7+
atol = zero(real(scalartype(term))),
8+
)
9+
# input checks
10+
length(inds) == numin(term) == numout(term) || throw(ArgumentError("Incompatible number of indices and tensor legs"))
11+
allunique(inds) || throw(ArgumentError("`inds` should not contain repeated coordinates."))
12+
for (i, ind) in enumerate(inds)
13+
ind_translated = CartesianIndex(mod1.(Tuple(ind), size(operator)))
14+
physicalspace(operator, ind_translated) == domain(term)[i] == codomain(term)[i] ||
15+
throw(SpaceMismatch("Incompatible physical spaces"))
16+
end
17+
norm(term) <= atol && return operator # skip adding negligible terms
18+
19+
# permute input
20+
if !issorted(inds)
21+
I = sortperm(inds)
22+
inds = inds[I]
23+
term = permute(term, (Tuple(I), Tuple(I) .+ numout(term)))
24+
end
25+
26+
# translate coordinates
27+
inds = _shift_into_unitcell!(copy(inds), size(operator))
28+
29+
if haskey(operator.terms, inds)
30+
operator.terms[inds] isa MPOTerm &&
31+
throw(ArgumentError("Accumulating terms with the same sites is not implemented for MPO or tensor product terms."))
32+
operator.terms[inds] = VI.add!!(operator.terms[inds], term)
33+
else
34+
operator.terms[inds] = term
35+
end
36+
37+
return operator
38+
end
39+
40+
"""
41+
Detect an identically zero dense tensor.
42+
"""
43+
_local_term_iszero(term::AbstractTensorMap) = iszero(norm(term))
44+
45+
_scale_local_term(term::AbstractTensorMap, α::Number) = VI.scale(term, α)
46+
47+
_fuse_charge(term::AbstractTensorMap, charges::Tuple) = fuse_charge(term, charges)

0 commit comments

Comments
 (0)