Math Utilities

Matrices

QBase.partial_traceFunction
partial_trace(
    ρ :: AbstractMatrix, dims :: Vector{Int64}, id :: Int64
) :: Matrix

Performs the partial trace on matrix ρ with respect to the subsystem at the specified id and returns resulting reduced matrix. The returned matrix is square with dimension equal to the product of dims with the element corresponding to id removed.

Inputs:

  • ρ : The matrix on which the partial trace is performed. This matrix should be square and have dimension equal to the product of the dims.
  • dims : A vector containing positive integer elements which specify the dimension of each subsystem. E.g. A 3-qubit system has dims = [2,2,2], a system containing a qubit and a qutrit has dims = [2,3].
  • id : A positive integer indexing the dims vector to signify the subsystem to be traced out.

The partial trace can be understood as a quantum operation $\mathcal{E}$ mapping the input Hilbert space to the output Hilbert space, $\mathcal{E} \; : \; \mathcal{H}_X \rightarrow \mathcal{H}_Y$. A quantum operation admits the operator sum representation, $\mathcal{E}(\rho) = \sum_i E_i \rho E_i^{\dagger}$. For example, given a 3-qubit system with density matrix $\rho_{ABC}$, the partial trace with respect to system $B$, is explicitly,

\[\rho_{AC} = \text{Tr}_B[\rho_{ABC}] = \mathcal{E}_B(\rho_{ABC}) = \sum_i E_i \rho_{ABC} E_i^{\dagger}\]

where $E_i = \mathbb{I}_A \otimes \langle i |_B \otimes\mathbb{I}_C$, represents the Kraus operators for the quantum operation and $\rho_{AC}$ is the reduced density operator remaining after system $B$ is traced out.

A DomainError is thrown if ρ is not square or of proper dimension.

source
partial_trace(ρ::State, system::Vector{Int64}, id::Int64) :: State

Takes the partialtrace of a State ρ to construct a new State: ``\text{Tr}B[\rho{AB}] = \rhoA``.

source
QBase.n_product_idFunction
n_product_id( tensor_coord :: Vector{Int64}, dims :: Vector{Int64} ) :: Int64

For a given tensor product of subspace dimension dims and tensor coordingate tensor_coord returns the corresponding matrix index of the kronecker product. The tenosr product dims is a vector of positive definite integers specifying the dimension of each of the matrices in the tensor product. The tensor_coord is a vector of positive definite integers specifying the target index in each matrix in the tensor product. The dims and tensor_coord should describe either row or column indices of the tensor product.

A DomainError is thrown if any index in tensor_coord is not valid or the number of elements in tensor_coord does not equal that of dims.

source
QBase.computational_basis_vectorsFunction
computational_basis_vectors( dim::Int64 ) ::Vector{ Vector{Int64} }

Returns the set of orthonormal column vectors $\{|1\rangle,\dots,|i\rangle,\dots,|d\rangle \}$ spanning a vector space of dimension dim. Note that $|1\rangle = ( 1, 0, \dots, 0 )^T$.

source

Validation Methods

QBase.is_hermitianFunction
is_hermitian( M :: AbstractMatrix{<:Number}; atol=ATOL :: Float64 ) :: Bool

Returns true if the supplied matrix is hermitian (self-adjoint), M == M'.

source
QBase.is_positive_semidefiniteFunction
is_positive_semidefinite(M :: AbstractMatrix) :: Bool

Returns true if the supplied matrix is square and positive semidefinite (all eigen values are real and greater than or equal to 0).

source
QBase.is_orthonormal_basisFunction
is_orthonormal_basis(basis_vecs ::  Vector;  atol=ATOL ::  Float64) :: Bool

Returns true if basis_vecs ``{|\psij\rangle}{j=1}^n forms an orthonormal basis:

\[\langle \psi_j |\psi_j \rangle = 1 \quad \text{and} \quad \langle \psi_j | \psi_{k} \rangle = 0\]

for all $j$ and $k$ where $j\neq k$.

source
QBase.is_completeFunction
is_complete(Π :: Vector{<:AbstractMatrix}; atol=ATOL :: Float64) :: Bool

Returns true if the provided set of matrices sums to the identity.

source
QBase.commutesFunction
commutes(A :: Matrix, B :: Matrix; atol=ATOL :: Float64) :: Bool

Returns true if matrix A commutes with matrix B. Two matrices commute if A*B == B*A. The matrices must have compatible dimensions:

  • A: Matrix, mxn dimensions
  • B: Matrix, nxm dimensions

A DomainError is thrown if the matrices are not compatible.

source

Combinatorics

Set Partitions

QBase.stirling2Function
stirling2( n :: Int64, k :: Int64  ) :: Int64

Counts the number of ways to partition n items into k unlabelled groups. This quantity is known as Stirling's number of the 2nd kind:

\[\left\{n \atop k \right\} = \frac{1}{k!}\sum_{i=0}^k (-1)^i\binom{k}{i}(k-i)^n\]

Throws a DomainError if inputs do not satisfy n ≥ k ≥ 1.

source
QBase.stirling2_partitionsFunction
stirling2_partitions( n :: Int64, k :: Int64 ) :: Vector{Vector{Vector{Int64}}}

Enumerates the unique partitions of n items into k unlabelled sets. Each partition is a vector containing a set of k vectors designating each group.

E.g.

julia> stirling2_partitions( 4, 2 )
7-element Array{Array{Array{Int64,1},1},1}:
 [[1, 2, 3], [4]]
 [[3], [1, 2, 4]]
 [[1, 2], [3, 4]]
 [[1, 3], [2, 4]]
 [[2], [1, 3, 4]]
 [[2, 3], [1, 4]]
 [[1], [2, 3, 4]]

This recursive algorithm was inspired by this blog.

source
QBase.stirling2_matricesFunction
stirling2_matrices( n :: Int64, k :: Int64 ) :: Vector{Matrix{Bool}}

Generates the set of matrices with k rows and n columns where rows correspond to the groups and columns are the grouped elements. A non-zero element designates that the column id is grouped into the corresponding row.

E.g.

julia> stirling2_matrices( 4, 2 )
7-element Array{Array{Bool,2},1}:
 [1 1 1 0; 0 0 0 1]
 [0 0 1 0; 1 1 0 1]
 [1 1 0 0; 0 0 1 1]
 [1 0 1 0; 0 1 0 1]
 [0 1 0 0; 1 0 1 1]
 [0 1 1 0; 1 0 0 1]
 [1 0 0 0; 0 1 1 1]

A DomainError is thrown if n ≥ k ≥ 1 is not satisfied.

source

Permutations

QBase.permutation_matricesFunction
permutation_matrices( dim :: Int64 ) :: Vector{Matrix{Bool}}

Generates the set of square permutation matrices of dimension dim.

E.g.

julia> permutation_matrices( 3 )
6-element Array{Array{Bool,2},1}:
 [1 0 0; 0 1 0; 0 0 1]
 [1 0 0; 0 0 1; 0 1 0]
 [0 1 0; 1 0 0; 0 0 1]
 [0 0 1; 1 0 0; 0 1 0]
 [0 1 0; 0 0 1; 1 0 0]
 [0 0 1; 0 1 0; 1 0 0]
source

Combinations

QBase.n_choose_k_matricesFunction
n_choose_k_matrices( n :: Int64, k :: Int64 ) :: Vector{Matrix{Bool}}

Generates a set of n by k matrices which represent all combinations of selecting k columns from n rows. Each column, contains a single non-zero element and k rows contain a non-zero element.

E.g.

julia> n_choose_k_matrices( 4, 2 )
6-element Array{Array{Bool,2},1}:
 [1 0; 0 1; 0 0; 0 0]
 [1 0; 0 0; 0 1; 0 0]
 [1 0; 0 0; 0 0; 0 1]
 [0 0; 1 0; 0 1; 0 0]
 [0 0; 1 0; 0 0; 0 1]
 [0 0; 0 0; 1 0; 0 1]

A DomainError is thrown if n ≥ k ≥ 1 is not satisfied.

source
QBase.base_n_valFunction
base_n_val(
    num_array :: Vector{Int64},
    base :: Int64;
    big_endian=true :: Bool
) :: Int64

Given an array representing a number in base-n returns the value of that number in base-10.

Inputs:

  • num_array - Vector containing semi-positive integers less than base.
  • base - The base-n number represented by num_array.
  • big_endian - true if most significant place is at index 1, else false.
source