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 ⊗ CSee also: kron, kronpow
OperatorAlgebra.kronpow — Functionkronpow(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 ofn: 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
OperatorAlgebra.atsite — Functionatsite(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 toop.mat(e.g.,sparse)op::Op: Single-site operator to extendbasis: Vector of site identifiers defining the systemdims: (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 sumsatsite(oc::OpChain, basis): Extends each operator and takes product
Applying Operators
OperatorAlgebra.apply — Functionapply(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 applystate: Product state as vector of vectors, wherestate[i]is the state at site ibasis: (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
sparseorLinearMap. applyonOpSumthrows an error since the result is not a product state.
OperatorAlgebra.apply! — Functionapply!(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 applystate: Product state to modifybasis: (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 modifiedMatrix Representations
Sparse Matrices
The sparse function from SparseArrays is extended to work with operators:
sparse(op::Op): Convert operator's matrix to sparse formatsparse(op::OpSum): Convert all matrices in the sum to sparse formatsparse(op::OpChain): Convert all matrices in the chain to sparse formatsparse(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 representationLinearMap(os::OpSum, basis): Sum of LinearMapsLinearMap(oc::OpChain, basis): Product of LinearMaps
See the Matrix Representations guide for examples.
Linear Algebra Operations
LinearAlgebra.tr — Methodtr(o::Op, basis, [dims])Compute the trace of an operator o over a tensor product space.
Arguments
o::Op: The operator to tracebasis: Vector of site indices defining the Hilbert space structuredims: (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) = 0LinearAlgebra.tr — Methodtr(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 tracebasis: Vector of site indices defining the Hilbert space structuredims: (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 ⊗ σzLinearAlgebra.tr — Methodtr(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 tracebasis: Vector of site indices defining the Hilbert space structuredims: (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 HamiltonianITensorMPS 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