Operations Reference

Tensor Products

OperatorAlgebra.:⊗Function
⊗(a, b)
⊗(as...)

Unicode alias for the Kronecker product (kron). Type \otimes and press Tab.

Examples

# Tensor product of two matrices
A = [1 0; 0 1]
B = [0 1; 1 0]
C = A ⊗ B  # Equivalent to kron(A, B)

# Multiple tensor products
D = A ⊗ B ⊗ C

See also: kron, kronpow

source
OperatorAlgebra.kronpowFunction
kronpow(A, n::Integer)

Compute the n-th Kronecker power of matrix A: A ⊗ A ⊗ ... ⊗ A (n times).

Uses a divide-and-conquer algorithm for efficient computation with large n.

Arguments

  • A: Matrix to take Kronecker power of
  • n: Non-negative integer power

Returns

The n-th Kronecker power of A

Examples

# Identity on 2ⁿ-dimensional space
I2_n = kronpow([1 0; 0 1], n)

# Spin chain with all spins in |↑⟩ state
up = [1, 0]
chain_state = kronpow(up, n_sites)

Throws

  • ArgumentError: If n is negative
source
OperatorAlgebra.atsiteFunction
atsite(T, op::AbstractOp, basis; id=I(local_dim))
atsite(T, op::AbstractOp, basis, dims; ids=map(I, dims))

Extend a single-site operator to the full Hilbert space of a tensor product system.

Constructs the full operator by inserting identity operators at all other sites: I ⊗ ... ⊗ I ⊗ op.mat ⊗ I ⊗ ... ⊗ I

Arguments

  • T: Optional transformation function applied to op.mat (e.g., sparse)
  • op::Op: Single-site operator to extend
  • basis: Vector of site identifiers defining the system
  • dims: (Optional) Vector of local dimensions for each site

Note: I is the identity matrix constructor LinearAlgebra.I

Returns

The full Hilbert space matrix representation

Examples

# Pauli X on site 2 of a 3-site system
σx = Op(PAULI_X, 2)
basis = [1, 2, 3]
σx_full = atsite(σx, basis)  # Returns I ⊗ PAULI_X ⊗ I

# Convert to sparse matrix in the process
σx_sparse = atsite(sparse, σx, basis)

# For sites with different dimensions
dims = [2, 3, 2]  # Site 2 has dimension 3
op = Op(custom_3x3_matrix, 2)
op_full = atsite(op, basis, dims)

Extended Methods

  • atsite(os::OpSum, basis): Extends each term and sums
  • atsite(oc::OpChain, basis): Extends each operator and takes product

See also: Op, kronpow, sparse

source

Applying Operators

OperatorAlgebra.applyFunction
apply(op::AbstractOp, state)
apply(op::AbstractOp, state, basis)

Apply an operator to a product state (non-mutating version).

This function applies operators to product states represented as vectors of local state vectors. For Op, it applies the matrix to the corresponding site. For OpChain, it applies operators in reverse order (right-to-left as in matrix multiplication).

Arguments

  • op: Operator to apply
  • state: Product state as vector of vectors, where state[i] is the state at site i
  • basis: (Optional) Vector of site identifiers if sites are not 1:N

Returns

A new product state after applying the operator

Examples

# Define a product state |↑↓⟩
state = [[1.0, 0.0], [0.0, 1.0]]

# Apply Pauli X to first site
σx = Op(PAULI_X, 1)
new_state = apply(σx, state)  # Results in |↓↑⟩

# Apply operator chain
chain = Op(PAULI_X, 1) * Op(PAULI_Z, 2)
result = apply(chain, state)

Notes

  • Only works for product states. For general states, convert operator to matrix using sparse or LinearMap.
  • apply on OpSum throws an error since the result is not a product state.

See also: apply!, Op, OpChain, sparse

source
OperatorAlgebra.apply!Function
apply!(op::AbstractOp, state)
apply!(op::AbstractOp, state, basis)

Apply an operator to a product state (mutating version).

In-place version of apply. Modifies the state vector directly.

Arguments

  • op: Operator to apply
  • state: Product state to modify
  • basis: (Optional) Vector of site identifiers if sites are not 1:N

Returns

The modified state

Examples

state = [[1.0, 0.0], [0.0, 1.0]]
σx = Op(PAULI_X, 1)
apply!(σx, state)  # state is now modified

See also: apply, Op, OpChain

source

Matrix Representations

Sparse Matrices

The sparse function from SparseArrays is extended to work with operators:

  • sparse(op::Op): Convert operator's matrix to sparse format
  • sparse(op::OpSum): Convert all matrices in the sum to sparse format
  • sparse(op::OpChain): Convert all matrices in the chain to sparse format
  • sparse(op::AbstractOp, basis): Convert to full Hilbert space sparse matrix

See the Matrix Representations guide for examples.

LinearMaps

The LinearMap function from LinearMaps.jl is extended to work with operators:

  • LinearMap(op::Op, basis): Create matrix-free representation
  • LinearMap(os::OpSum, basis): Sum of LinearMaps
  • LinearMap(oc::OpChain, basis): Product of LinearMaps

See the Matrix Representations guide for examples.

Linear Algebra Operations

LinearAlgebra.trMethod
tr(o::Op, basis, [dims])

Compute the trace of an operator o over a tensor product space.

Arguments

  • o::Op: The operator to trace
  • basis: Vector of site indices defining the Hilbert space structure
  • dims: (Optional) Vector of local dimensions for each site. Defaults to uniform dimension from operator matrix.

Returns

The trace value, accounting for identity operators on other sites in the tensor product.

Example

σz = Op([1 0; 0 -1], 1)
tr(σz, [1, 2])  # Trace over 2-site system: tr(σz ⊗ I) = 0
source
LinearAlgebra.trMethod
tr(oc::OpChain, basis, [dims])

Compute the trace of an operator chain (product of operators) over a tensor product space.

Arguments

  • oc::OpChain: The operator chain to trace
  • basis: Vector of site indices defining the Hilbert space structure
  • dims: (Optional) Vector of local dimensions for each site

Returns

The trace value of the operator product, with identity contributions from unoccupied sites.

Example

σx = Op(PAULI_X, 1)
σz = Op(PAULI_Z, 2)
product = σx * σz
tr(product, [1, 2])  # Trace of σx ⊗ σz
source
LinearAlgebra.trMethod
tr(os::OpSum, basis, [dims])

Compute the trace of an operator sum (linear combination) over a tensor product space.

Arguments

  • os::OpSum: The operator sum to trace
  • basis: Vector of site indices defining the Hilbert space structure
  • dims: (Optional) Vector of local dimensions for each site

Returns

The sum of traces of all constituent operators.

Example

hamiltonian = σx + σz + 0.5 * (σx * σz)
tr(hamiltonian, [1, 2])  # Trace of full Hamiltonian
source

ITensorMPS Integration

When ITensorMPS.jl is loaded, operators can be converted to Matrix Product Operators (MPOs):

using OperatorAlgebra
using ITensorMPS  # Extension loads automatically

sites = siteinds("S=1/2", 4)
H = Op(PAULI_X, 1) + Op(PAULI_Z, 2)
mpo = MPO(H, sites)

The extension provides:

  • MPO(op::AbstractOp, sites): Convert any OperatorAlgebra operator to an ITensorMPS MPO

Index