Developer Interface

This page covers the named-array model that indices and tensors are built on, the interface for implementing a new tensor type, and experimental features that are not yet part of the stable user-facing API. For the stable user-facing API, see the User Interface.

Named array types

A concrete tensor type subtypes AbstractNamedTensor. NamedTensor is the built-in implementation, and ITensor is the NamedTensor with dimension names that are IndexNames. Its NamedTensor(array, dimnames) constructor pairs an array of any kind with its dimension names directly. User code usually builds one by calling an array constructor on indices or by indexing an array (see Constructors) rather than calling it. The underlying named-range model has NamedUnitRange as the named-range type that a tensor's dimensions are (Index is the flavor keyed by an index name).

ITensorBase.AbstractNamedTensorType
AbstractNamedTensor{DimName}

Supertype for tensors whose dimensions are labeled by names of type DimName rather than ordered by position. Subtypes such as NamedTensor line their dimensions up by name under contraction, addition, and indexing. Unlike an AbstractArray, the rank and element type live in the data rather than the type, so ndims and eltype are not fixed at the type level.

See also NamedTensor, dimnames, inds.

source
ITensorBase.NamedTensorType
NamedTensor(array::AbstractArray, dims)

A tensor whose dimensions are labeled by names instead of ordered by position. It pairs an underlying array with one name per dimension (dims), so contraction, addition, and indexing line dimensions up by name. A NamedTensor is usually built by calling randn, zeros, and the like on indices, or through nameddims, rather than constructed directly. ITensor is the NamedTensor with dimension names that are IndexNames.

Examples

julia> NamedTensor(zeros(2, 3), (:i, :j))
named(Base.OneTo(2), :i)×named(Base.OneTo(3), :j) NamedTensor{Symbol}:
2×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0
source
ITensorBase.NamedUnitRangeType
NamedUnitRange{Name}

A unit range with a name attached, used as a named dimension (axis) of a tensor. It pairs an underlying integer unit range with a name of type Name. Index is the NamedUnitRange flavor whose name is an IndexName. Build one by calling named on a range, or use Index to mint a fresh unique name.

Examples

julia> named(1:3, :i)
named(1:3, :i)

See also Index, named.

source

Named array operations

Construct named objects with named and nameddims, recover their parts with name, unnamed, and dimnames, and query their types with dimnametype, nametype, and unnamedtype. setname and replacedimnames rename, and aligndims and aligneddims reorder a tensor's dimensions by name (a copy and a view, respectively).

ITensorBase.namedFunction
named(value, name)

Attach name to value, pairing them into a single named object. On a scalar this produces a Named. Arrays and unit ranges have their own more specific methods.

Examples

julia> named(2, :i)
named(2, :i)
source
ITensorBase.nameddimsFunction
nameddims(a, dimnames)

Construct a named dimensions array from an unnamed parent a and named dimensions dimnames. The parent is usually an AbstractArray, but any object that a NamedTensor can wrap works (e.g. a TensorKit TensorMap).

Examples

julia> nameddims(zeros(2, 3), (:i, :j))
named(Base.OneTo(2), :i)×named(Base.OneTo(3), :j) NamedTensor{Symbol}:
2×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0

See also NamedTensor, named.

source
ITensorBase.nameFunction
name(a)

The name attached to a named object a, such as a Named scalar, a named array, or a named unit range. This is the inverse of the name component of named: name recovers the name, unnamed recovers the value.

Examples

julia> using ITensorBase: name

julia> name(named(2, :i))
:i

See also named, unnamed, setname.

source
ITensorBase.unnamedFunction
unnamed(a)

The underlying value of a named object a, with its name stripped off. This is the inverse of the value component of named: name recovers the name, unnamed recovers the value. On an AbstractNamedTensor it returns the underlying unnamed array.

Examples

julia> using ITensorBase: unnamed

julia> unnamed(named(2, :i))
2

See also named, name.

source
ITensorBase.dimnamesFunction
dimnames(a::AbstractNamedTensor)
dimnames(a::AbstractNamedTensor, dim::Int)

The dimension names of a, as a collection in dimension order. The second form returns the name of dimension dim.

Examples

julia> a = nameddims(zeros(2, 3), (:i, :j));

julia> dimnames(a)
2-element Vector{Symbol}:
 :i
 :j

julia> dimnames(a, 2)
:j

See also inds, nameddims.

source
ITensorBase.dimnametypeFunction
dimnametype(a::AbstractNamedTensor)
dimnametype(type::Type{<:AbstractNamedTensor})

The type of an individual dimension name of a. The primary method dispatches on the array type, and dimnametype(a) forwards to dimnametype(typeof(a)). A type that does not fix its dimname flavor (such as the unparameterized NamedTensor) returns Any, the same way eltype(Array) is Any.

Examples

julia> a = nameddims(zeros(2, 3), (:i, :j));

julia> dimnametype(a)
Symbol

julia> dimnametype(typeof(a))
Symbol
source
ITensorBase.nametypeFunction
nametype(type::Type)

The type of the name carried by a named type, such as a Named scalar type, a named array type, or a named unit range type.

Examples

julia> using ITensorBase: nametype

julia> nametype(typeof(named(2, :i)))
Symbol

See also name, unnamedtype.

source
ITensorBase.unnamedtypeFunction
unnamedtype(type::Type)

The type of the underlying (unnamed) value carried by a named type.

Examples

julia> using ITensorBase: unnamedtype

julia> unnamedtype(typeof(named(2, :i)))
Int64

See also unnamed, nametype.

source
ITensorBase.setnameFunction
setname(a, name)

Return a copy of the named object a with its name replaced by name, keeping the underlying value unchanged.

Examples

julia> using ITensorBase: setname

julia> setname(named(2, :i), :j)
named(2, :j)

See also named, name.

source
ITensorBase.replacedimnamesFunction
replacedimnames(a::AbstractNamedTensor, replacements::Pair...)
replacedimnames(f, a::AbstractNamedTensor)

Return a tensor with the same data as a but with its dimension names replaced. The first form takes old => new pairs, replacing matching names and leaving the rest unchanged. The second form replaces each name with f(name).

Examples

julia> using ITensorBase: replacedimnames

julia> a = nameddims(zeros(2, 3), (:i, :j));

julia> dimnames(replacedimnames(a, :i => :k))
2-element Vector{Symbol}:
 :k
 :j

See also dimnames.

source
ITensorBase.aligndimsFunction
aligndims(a::AbstractNamedTensor, dims)

Reorder the dimensions of a into the order given by dims, matched by name. Returns a tensor with the same data and dimension names as a but with the dimensions permuted, and throws a NameMismatch if dims is not a permutation of a's dimension names.

Examples

julia> a = nameddims(zeros(2, 3), (:i, :j));

julia> aligndims(a, (:j, :i))
named(Base.OneTo(3), :j)×named(Base.OneTo(2), :i) NamedTensor{Symbol}:
3×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0
 0.0  0.0
source
aligndims(a::AbstractNamedTensor, codomain, domain)

Reorder the dimensions of a into (codomain..., domain...), matched by name, and forward the codomain/domain split to the underlying storage. Like the two-argument form, the result has the same data and dimension names as a, and a NameMismatch is thrown if (codomain..., domain...) is not a permutation of a's dimension names. A storage backend that supports a bipartition (such as a TensorKit TensorMap) uses it, while a dense backend stores the result flat.

source
ITensorBase.aligneddimsFunction
aligneddims(a::AbstractNamedTensor, dims)

Like aligndims, but returns a lazily-permuted view that shares data with a instead of copying. Reorders the dimensions of a into the order given by dims, matched by name, and throws a NameMismatch if dims is not a permutation of a's dimension names.

Examples

julia> a = nameddims(reshape(1:6, 2, 3), (:i, :j));

julia> dimnames(aligneddims(a, (:j, :i)))
2-element Vector{Symbol}:
 :j
 :i

See also aligndims.

source

Experimental

These features support building and applying operators, where an operator is a tensor whose dimension names are split into a codomain (output) set and a domain (input) set. The API is still being refined and is subject to change. Build an operator with operator or allocate one with similar_operator, apply it to a tensor with apply, and recover its underlying tensor and name sets with state, codomainnames, and domainnames.

ITensorBase.operatorFunction
operator(a, codomain, domain)

Build a named operator from a tensor (or plain array) a by partitioning its dimension names into a codomain (output) set and a domain (input) set. The operator pairs each codomain name with a domain name, so it can be applied to a tensor with apply, contracting over the domain. codomain and domain may be given as dimension names or as named ranges such as Indexes. Recover the underlying tensor with state and the name sets with codomainnames and domainnames.

Examples

julia> op = operator(zeros(2, 2), ("i",), ("j",));

julia> collect(codomainnames(op))
1-element Vector{String}:
 "i"

julia> collect(domainnames(op))
1-element Vector{String}:
 "j"

See also state, codomainnames, domainnames, apply, similar_operator.

source
ITensorBase.similar_operatorFunction
similar_operator(prototype, [T,] unnamed_domain_axes, [codomain_names,] domain_names) -> op
similar_operator(prototype, [T,] named_domain_axes) -> op

Allocate an operator-shaped named array with undefined data, with the user-supplied side as the domain (input) and a matching codomain (output). Element type defaults to eltype(prototype). Codomain names default to fresh uniquename-generated names. The first form takes unnamed (raw) axes and explicit names, the second takes already-named axes and reuses their names as the domain. Storage layout (including the bra/ket flip on the domain side for graded axes) is delegated to TensorAlgebra.similar_map.

Examples

julia> op = similar_operator(zeros(2, 2), (Base.OneTo(2),), (:i,), (:j,));

julia> collect(domainnames(op))
1-element Vector{Symbol}:
 :j

See also operator, uniquename.

source
ITensorBase.applyFunction
apply(x::AbstractNamedTensor, y::AbstractNamedTensor)

Apply the operator x to y. This contracts the state tensors of x and y over their shared names, then renames each surviving codomain name of x back to its paired domain name, so the result carries the same names y would map to. Applying the identity operator leaves y unchanged.

Examples

julia> op = operator(reshape(Float64[1, 0, 0, 1], 2, 2), ("i",), ("j",));

julia> v = nameddims([3.0, 4.0], ("j",));

julia> apply(op, v) == v
true

See also operator, state, codomainnames, domainnames.

source
ITensorBase.stateFunction
state(a)

The underlying tensor of a named operator, with its codomain/domain structure forgotten. An operator carries a tensor together with a pairing of its codomain and domain dimension names (its Choi, or state, representation). state returns that tensor on its own. For a plain tensor that is not an operator, state returns it unchanged.

Examples

julia> a = nameddims(zeros(2), (:i,));

julia> state(a) == a
true

See also operator, codomainnames, domainnames.

source
ITensorBase.codomainnamesFunction
codomainnames(a)

The codomain (output) dimension names of an operator a. An operator pairs each of its codomain names with a domain name. Applying the operator contracts over the domain and leaves the codomain.

Examples

julia> op = operator(zeros(2, 2), ("i",), ("j",));

julia> collect(codomainnames(op))
1-element Vector{String}:
 "i"

See also domainnames, operator, apply.

source
ITensorBase.domainnamesFunction
domainnames(a)

The domain (input) dimension names of an operator a. These are the names contracted over when the operator is applied to a tensor.

Examples

julia> op = operator(zeros(2, 2), ("i",), ("j",));

julia> collect(domainnames(op))
1-element Vector{String}:
 "j"

See also codomainnames, operator, apply.

source