QMath
QBase.QMath
— ModuleA QBase.jl submodule providing general purpose mathematics useful to quantum mechanics.
Exports:
Matrices:
partial_trace
- The partial trace matrix operation.computational_basis_vectors
- A general orthonormal set of vectors.commutes
- Checks if two matrices commute.is_hermitian
- Checks if a matrix is hermitian.is_positive_semidefinite
- Checks if a matrix is positive semi-definite.is_square
- Checks if a matrix is square.
Combinatorics:
stirling2
- Counts the ways to partitionn
items intok
sets.stirling2_partitions
- Enumerates the stirling2 partitions.stirling2_matrices
- Generates matrices representing the stirling2 partitions.permutations
- Passthrough forCombinatorics.permutations
.permutation_matrices
- Generates the set of permutation matrices.combinations
- Passthrough forCombinatorics.combinations
.n_choose_k_matrices
- Generates matrices representing n-choose-k combinations.base_n_val
- Converts a base-n number into the base-10 value.
Probability:
is_probability_distribution
- Checks if a vector is probability distribution.is_conditional_distribution
- Checks if a matrix is a conditional probability distribution.Marginals
- Base type for marginal probability distributions.Conditionals
- Base tyep for conditional probability distributions.
Matrices
QBase.QMath.computational_basis_vectors
— Functioncomputational_basis_vectors( dim::Int64 ) ::Vector{ Vector{Int64} }
Returns an orthonormal set of column vectors spanning a vector space of dimension dim
.
QBase.QMath.partial_trace
— Functionpartial_trace(
ρ::AbstractMatrix, subsystem_dims::Vector{Int64}, subsystem_id::Int64
) :: AbstractMatrix
Performs the partial trace on matrix ρ
with respect to the subsystem at the specified subsystem_id
. The partial trace is 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,
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.
Inputs:
ρ
: The matrix on which the partial trace is performed. This matrix should be square and have dimension equal to the product of thesubsystem_dims
.subsystem_dims
: A vector containing positive integer elements which specify the dimension of each subsystem. E.g. A 3-qubit system hassubsystem_dims = [2,2,2]
, a system containing a qubit and a qutrit hassubsystem_dims = [2,3]
.subsystem_id
: A positive integer indexing thesubsystem_dims
vector to signify the subsytem which the partial trace will trace out.
Output:
- The reduced matrix which results after the partial trace is performed on
ρ
. This matrix is square and has dimension of product ofsubsystem_dims
with the element corresponding tosubsystem_id
removed.
A DomainError
is thrown if ρ
is not square or of proper dimension.
QBase.QMath.commutes
— Functioncommutes(A :: Matrix, B :: Matrix) :: 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.
QBase.QMath.is_hermitian
— Functionis_hermitian( matrix :: Matrix ) :: Bool
Returns true
if the supplied matrix is hermitian (self-adjoint).
QBase.QMath.is_positive_semidefinite
— Functionis_positive_semidefinite(matrix :: Matrix) :: Bool
Returns true
if the supplied matrix is positive semidefinite (all eigen values are real and greater than or equal to 0).
A DomainError
is thrown if the provided matrix is not square.
QBase.QMath.is_square
— Functionis_square( matrix :: Matrix ) :: Bool
Returns true
if the provided matrix is square.
Combinatorics
QBase.QMath.stirling2
— Functionstirling2( 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.
Throws a DomainError
if inputs do not satisfy n ≥ k ≥ 1
.
QBase.QMath.stirling2_partitions
— Functionstirling2_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.
This recursive algorithm was inspired by this blog.
QBase.QMath.stirling2_matrices
— Functionstirling2_matrices( n :: Int64, k :: Int64 ) :: Vector{Matrix{Int64}}
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.
A DomainError
is thrown if n ≥ k ≥ 1
is not satisfied.
QBase.QMath.permutation_matrices
— Functionpermutation_matrices( dim :: Int64 ) :: Vector{Matrix{Int64}}
Generates the set of square permutation matrices of dimension dim
.
QBase.QMath.n_choose_k_matrices
— Functionn_choose_k_matrices( n :: Int64, k :: Int64 ) :: Vector{Matrix{Int64}}
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.
A DomainError
is thrown if n ≥ k ≥ 1
is not satisfied.
QBase.QMath.base_n_val
— Functionbase_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, elsefalse
.
Probability
QBase.QMath.is_probability_distribution
— Functionis_probability_distribution( probabilities :: Vector ) :: Bool
Returns true
if the provided vector is a valid probability distribution:
sum(probabilities) ≈ 1
p[i] ≥ 0 ∀ i
QBase.QMath.is_conditional_distribution
— Functionis_conditional_distribution( probabilities :: Matrix ) :: Bool
Returns true
if the provided matrix is a valid conditional probability distribution. Each column corresponds to probabilisitic outcomes conditioned for a distinct input.
QBase.QMath.Marginals
— TypeMarginals( probabilities :: Vector ) <: AbstractVector{Float64}
A struct representing a marginal probability distribution function. All elements in the marginal distribution must be positive and their sum must be one.
QBase.QMath.Conditionals
— TypeConditionals( probabilities :: Matrix ) <: AbstractMatrix{Float64}
A struct representing a conditional probability distribution function. Conditionals
are organized in a matrix with rows correpsonding to outputs and columns corresponding to inputs. For example if there are $M$ inputs and $N$ outputs, the corresponding conditionals matrix takes the form: