Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/PEPSKit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ include("utility/symmetrization.jl")
include("operators/infinitepepo.jl")
include("operators/transfermatrix.jl")
include("operators/localoperator.jl")
include("operators/localoperator/dense.jl")
include("operators/localoperator/tensorproduct.jl")
include("operators/localoperator/mpo.jl")
include("operators/localcircuit.jl")
include("operators/lattices/squarelattice.jl")
include("operators/models.jl")
Expand Down
37 changes: 37 additions & 0 deletions src/algorithms/contractions/local_patch/network_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,40 @@ function operator_contraction_expr(::Type{<:AbstractTensorMap}, nsites)
),
]
end

# The MPO bond is small - the operator's Schmidt rank, e.g. 3 for Heisenberg XYZ - so it is
# labelled as a physical rather than a virtual dimension, which gives `@autoopt` the right
# order of magnitude when it searches for a contraction order.
mpolabel(args...) = physicallabel(:mpo, args...)

function operator_contraction_expr(::Type{<:MPOTerm}, nsites)
# one factor per site, linked by a chain of bonds running left to right, rank-3 at the
# ends of the chain and rank-4 in the bulk:
# W₁ : bra₁ ← ket₁ ⊗ b₁
# Wᵢ : bᵢ₋₁ ⊗ braᵢ ← ketᵢ ⊗ bᵢ
# W_N : b_{N-1} ⊗ bra_N ← ket_N
return map(1:nsites) do i
bra, ket = physicallabel(:O, 2, i), physicallabel(:O, 1, i)
out, ins = if nsites == 1
(bra,), (ket,)
elseif i == 1
(bra,), (ket, mpolabel(1))
elseif i == nsites
(mpolabel(nsites - 1), bra), (ket,)
else
(mpolabel(i - 1), bra), (ket, mpolabel(i))
end
return tensorexpr(:(operator[$i]), out, ins)
end
end

# A TensorProductTerm is represented with all trivial MPO bond legs removed.
# Each factor connects only the ket and bra physical indices of its site.
# This method is more specific than the `MPOTerm` one above, so it takes precedence.
function operator_contraction_expr(::Type{<:TensorProductTerm}, nsites)
return map(1:nsites) do i
return tensorexpr(
:(operator[$i]), (physicallabel(:O, 2, i),), (physicallabel(:O, 1, i),)
)
end
end
20 changes: 20 additions & 0 deletions src/algorithms/expectation_value/expectation_value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ function local_expectation_value(inds, state, operator::AbstractTensorMap, env)
return trmul(operator, ρ)
end

"""
$(SIGNATURES)

Compute the contribution of an [`MPOTerm`](@ref), including a [`TensorProductTerm`](@ref), given as one tensor per site in `inds`.
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)``.
Insert the factors into the patch contraction directly and divide by the local norm of the same patch.
"""
function local_expectation_value(inds, bra, operator::MPOTerm, ket, env)
return contract_local_operator(inds, operator, ket, bra, env) /
contract_local_norm(inds, ket, bra, env)
end
function local_expectation_value(inds, state, operator::MPOTerm, env)
return contract_local_operator(inds, operator, state, env) /
contract_local_norm(inds, state, env)
end

# TODO: Implement boundary_contraction_expr for BPEnv to contract its messages directly.
local_expectation_value(inds, bra, operator::MPOTerm, ket, env::BPEnv) =
local_expectation_value(inds, bra, operator, ket, CTMRGEnv(env))

# Expectation value of a local partition function tensor
# ------------------------------------------------------

Expand Down
13 changes: 13 additions & 0 deletions src/algorithms/expectation_value/patch_contractions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Direct local patch contractions
# -------------------------------

"""
Check that a factored operator contains one factor per site.
"""
_check_factor_count(inds, O) = nothing
function _check_factor_count(inds, O::MPOTerm)
length(inds) == length(O) ||
throw(ArgumentError("Got $(length(inds)) sites but $(length(O)) operator factors."))
return nothing
end

"""
contract_local_operator(inds, O, ket::InfinitePEPS, bra::InfinitePEPS, env)
contract_local_operator(inds, O, ket::InfinitePEPO, bra::InfinitePEPO, env)
Expand All @@ -16,13 +26,15 @@ function contract_local_operator(
inds::Vector{CartesianIndex{2}}, O,
ket::InfinitePEPS, bra::InfinitePEPS, env,
)
_check_factor_count(inds, O)
static_inds = Tuple(Val.(inds))
return _contract_local_operator(static_inds, O, (ket, bra), env)
end
function contract_local_operator(
inds::Vector{CartesianIndex{2}}, O, state::InfinitePEPO, env
)
size(state, 3) == 1 || throw(DimensionMismatch("only single-layer densitymatrices are supported"))
_check_factor_count(inds, O)
static_inds = Tuple(Val.(inds))
return _contract_local_operator(static_inds, O, state, env)
end
Expand All @@ -31,6 +43,7 @@ function contract_local_operator(
)
size(ket) == size(bra) || throw(DimensionMismatch("incompatible bra and ket dimensions"))
size(ket, 3) == 1 || throw(DimensionMismatch("only single-layer densitymatrices are supported"))
_check_factor_count(inds, O)
static_inds = Tuple(Val.(inds))
return _contract_local_operator(static_inds, O, (ket, bra), env)
end
Expand Down
91 changes: 49 additions & 42 deletions src/operators/localoperator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,46 +50,26 @@ end
# Default to Any for eltype: needs to be abstract anyways so not that much to gain
LocalOperator(lattice, terms) = LocalOperator{Any}(lattice, terms)
LocalOperator(lattice, terms::Pair...) = LocalOperator(lattice, terms)
# TODO: add terms beyond AbstractTensorMap
# e.g. tensor product of 1-site operators, MPOs
add_term!(operator::LocalOperator, inds::Tuple, term::AbstractTensorMap) = add_term!(operator, collect(inds), term)
add_term!(operator::LocalOperator, inds::Vector, term::AbstractTensorMap) = add_term!(operator, map(CartesianIndex{2}, inds), term)
function add_term!(
operator::LocalOperator, inds::Vector{CartesianIndex{2}}, term::AbstractTensorMap;
atol = zero(real(scalartype(term))),
)
# input checks
length(inds) == numin(term) == numout(term) || throw(ArgumentError("Incompatible number of indices and tensor legs"))
allunique(inds) || throw(ArgumentError("`inds` should not contain repeated coordinates."))
for (i, ind) in enumerate(inds)
ind_translated = CartesianIndex(mod1.(Tuple(ind), size(operator)))
physicalspace(operator, ind_translated) == domain(term)[i] == codomain(term)[i] ||
throw(SpaceMismatch("Incompatible physical spaces"))
end
norm(term) <= atol && return operator # skip adding negligible terms

# permute input
if !issorted(inds)
I = sortperm(inds)
inds = inds[I]
term = permute(term, (Tuple(I), Tuple(I) .+ numout(term)))
end

# translate coordinates
_shift_into_unitcell!(inds, size(operator))

if haskey(operator.terms, inds)
operator.terms[inds] = VI.add!!(operator.terms[inds], term)
else
operator.terms[inds] = term
end

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

"""
Return the tensor factors of a dense or factored local term.
"""
_term_tensors(term::AbstractTensorMap) = (term,)
_term_tensors(term::AbstractVector{<:AbstractTensorMap}) = term

function TensorKit.storagetype(lo::LocalOperator)
T = scalartype(lo)
return mapreduce(t -> similarstoragetype(t, T), TensorKit.promote_storagetype, values(lo.terms))
tensors = Iterators.flatten(Iterators.map(_term_tensors, values(lo.terms)))
return mapreduce(t -> similarstoragetype(t, T), TensorKit.promote_storagetype, tensors)
end

"""
Expand Down Expand Up @@ -158,24 +138,45 @@ Base.eltype(::Type{LocalOperator{O, S}}) where {O, S} = O
# Real and imaginary part
# -----------------------
function Base.real(O::LocalOperator)
any(term -> term isa MPOTerm, values(O.terms)) &&
throw(ArgumentError("Taking the real part is not implemented for MPO or tensor product terms."))
return LocalOperator(O.lattice, (sites => real(op) for (sites, op) in O.terms)...)
end
function Base.imag(O::LocalOperator)
any(term -> term isa MPOTerm, values(O.terms)) &&
throw(ArgumentError("Taking the imaginary part is not implemented for MPO or tensor product terms."))
return LocalOperator(O.lattice, (sites => imag(op) for (sites, op) in O.terms)...)
end

# Linear Algebra
# --------------
Base.:*(α::Number, O::LocalOperator) =
LocalOperator(physicalspace(O), inds => α * operator for (inds, operator) in O.terms)
"""
Scale a single term of a [`LocalOperator`](@ref) by `α`.

An [`MPOTerm`](@ref), including a [`TensorProductTerm`](@ref), is scaled by scaling its first
factor only - scaling the whole container would scale every factor and give `α^N`.
"""
function _scale_local_term end
Base.:*(α::Number, O::LocalOperator) = LocalOperator(
physicalspace(O), inds => _scale_local_term(operator, α) for (inds, operator) in O.terms
)
Base.:*(O::LocalOperator, α::Number) = α * O

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

"""
Accumulate dense terms while rejecting addition involving an MPO or tensor product at the same sites.
"""
function _add_local_terms(term1, term2)
(term1 isa MPOTerm || term2 isa MPOTerm) &&
throw(ArgumentError("Accumulating terms with the same sites is not implemented for MPO or tensor product terms."))
return VI.add(term1, term2)
end

function Base.:+(O1::LocalOperator, O2::LocalOperator)
checklattice(O1, O2)
return LocalOperator(physicalspace(O1), mergewith(VI.add, O1.terms, O2.terms))
return LocalOperator(physicalspace(O1), mergewith(_add_local_terms, O1.terms, O2.terms))
end

Base.:-(O::LocalOperator) = -1 * O
Expand All @@ -184,12 +185,12 @@ Base.:-(O1::LocalOperator, O2::LocalOperator) = O1 + (-O2)
# VectorInterface
# ---------------

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


# Equivalence
# -----------

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

"""
Fuse auxiliary charges into the physical spaces of a term in `LocalOperator`.
"""
function _fuse_charge end

"""
add_physical_charge(H::LocalOperator, charges::AbstractMatrix{<:Sector})

Change the spaces of a `LocalOperator` by fusing in an auxiliary charge into the domain of
the operator on every site, according to a given matrix of 'auxiliary' physical charges.
MPO and tensor-product terms are not yet supported.
"""
function MPSKit.add_physical_charge(H::LocalOperator, charges::AbstractMatrix{<:Sector})
size(H) == size(charges) ||
Expand All @@ -258,6 +265,6 @@ function MPSKit.add_physical_charge(H::LocalOperator, charges::AbstractMatrix{<:

return LocalOperator(
Pspaces,
inds => fuse_charge(op, Tuple(map(Base.Fix1(getindex, periodic_charges), inds))) for (inds, op) in H.terms
inds => _fuse_charge(op, Tuple(map(Base.Fix1(getindex, periodic_charges), inds))) for (inds, op) in H.terms
)
end
47 changes: 47 additions & 0 deletions src/operators/localoperator/dense.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Dense AbstractTensorMap terms

add_term!(operator::LocalOperator, inds::Tuple, term::AbstractTensorMap; kwargs...) = add_term!(operator, collect(inds), term; kwargs...)
add_term!(operator::LocalOperator, inds::AbstractVector, term::AbstractTensorMap; kwargs...) = add_term!(operator, CartesianIndex{2}[CartesianIndex{2}(ind) for ind in inds], term; kwargs...)
function add_term!(
operator::LocalOperator, inds::Vector{CartesianIndex{2}}, term::AbstractTensorMap;
atol = zero(real(scalartype(term))),
)
# input checks
length(inds) == numin(term) == numout(term) || throw(ArgumentError("Incompatible number of indices and tensor legs"))
allunique(inds) || throw(ArgumentError("`inds` should not contain repeated coordinates."))
for (i, ind) in enumerate(inds)
ind_translated = CartesianIndex(mod1.(Tuple(ind), size(operator)))
physicalspace(operator, ind_translated) == domain(term)[i] == codomain(term)[i] ||
throw(SpaceMismatch("Incompatible physical spaces"))
end
norm(term) <= atol && return operator # skip adding negligible terms

# permute input
if !issorted(inds)
I = sortperm(inds)
inds = inds[I]
term = permute(term, (Tuple(I), Tuple(I) .+ numout(term)))
end

# translate coordinates
inds = _shift_into_unitcell!(copy(inds), size(operator))

if haskey(operator.terms, inds)
operator.terms[inds] isa MPOTerm &&
throw(ArgumentError("Accumulating terms with the same sites is not implemented for MPO or tensor product terms."))
operator.terms[inds] = VI.add!!(operator.terms[inds], term)
else
operator.terms[inds] = term
end

return operator
end

"""
Detect an identically zero dense tensor.
"""
_local_term_iszero(term::AbstractTensorMap) = iszero(norm(term))

_scale_local_term(term::AbstractTensorMap, α::Number) = VI.scale(term, α)

_fuse_charge(term::AbstractTensorMap, charges::Tuple) = fuse_charge(term, charges)
Loading
Loading