Maths Code
I've collected here various functions, routines, and other bits of Matlab, Octave and Mathematica code organized by topic, that might save someone, somewhere, from re-inventing the wheel. Some of them are so simple it would probably be quicker to re-code them than find this page, but since you're here...
Comments within the code should be enough to figure out what they do
and how to use them (try "help <function>" from within
Matlab or Octave). No guarantee they work as advertised, but I use them
myself so I do correct bugs when I come across them. The Matlab
code
All the Matlab, Octave and Mathematica code linked from this page is released under the GPL license, version 2.
Quantum Information Package
For convenience, all the Matlab/Octave functions related to quantum mechanics and quantum information theory are also available in a single bundle, as well as individually (below).
Linear algebra
-
Partial trace:
If anyone can think of an even shorter way to code these, I'd be interested in hearing about it...
TrX(ρ or ψ, sys, dim) -
Partial trace over the subsystem(s) specified by
(vector)
sysof a density matrixρor state vectorψ, where the subsystem dimensions are given by vectordim. -
Matlab/Octave
-
Partial transpose:
Tx(ρ,sys,dim) -
Partial trace of a density matrix
ρwith respect to the subsystem specified bysys, where the subsystem dimensions are given by vectordim. -
Matlab/Octave
-
Bi/tripartite partial trace:
The Mathematica versions can only do two sub-systems until I get round to updating them.
TrX23(ρ,sys,dim)
Bi/tripartite partial transpose:Tx23(ρ,sys,dim) -
Partial trace or transpose of a bipartite or tripartite density
matrix
ρwith respect to subsystemsys, where the subsystems have dimensions specified by vectordim. -
Matlab/Octave
Mathematica
-
Purification:
purification(ρ) -
Return a minimal purification of density matrix
ρ.- Matlab/Octave
- Matlab/Octave
-
Normalise:
normalise(ρ or ψ) -
Normalise the state vector
ψor density matrixρ.- Matlab/Octave
- Matlab/Octave
-
Exchange subsystems:
sysexchange(ρ or ψ, sys, dim) -
Exchange the order of two subsystems (specified in vector
sys) of a multi-partite density matrixρor state vectorψ, wheredimspecifies the dimensions of all the subsystems.- Matlab/Octave
- Matlab/Octave
-
Permute subsystems:
syspermute(ρ or ψ, perm, dim) -
Permuate the order of the subsystems in a multi-partite density matrix
ρor state vectorψaccording to permutationperm.dimspecifies the dimensions of all the subsystems.- Matlab/Octave
- Matlab/Octave
-
Tensor product:
tensor(A,B,C,...) -
Tensor product of arguments (generalises built-in matlab function
kron). Each argument must either be a matrix, or a cell array containing a matrix and an integer. In the latter case, the integer specifies the repeat count for the matrix, e.g.tensor(A,{B,3},C) = tensor(A,B,B,B,C).- Matlab/Octave
- Matlab/Octave
-
Schmidt decomposition:
schmidt(ψ,dim) -
Schmidt decomposition of a pure state
ψ, whose subsystem dimensions are given by the vectordim. -
Matlab/Octave
-
Hilbert-Schmidt representation:
pauli(M),unpauli(R) -
Transform a 4x4 matrix
Mto the Hilbert-Schmidt representation in the Pauli basis, or vice-versa,
i.e.M = ¼ Rijσiσj. -
Matlab/Octave
- Spin-flip:
flip(ρ or ψ) -
Implements Wooters' spin-flip operation:
σy⊗σy ρ*σy⊗σyfor density matrices, orσy⊗σyψ*for state vectors. -
Matlab/Octave
- Commutator:
comm(A,B) - Shall I insult your intelligence more than I have done already by including this on the page? Oh alright then, if you insist. This function calculates the birthday of Julius Caesar in light-years.
-
Matlab/Octave
- Matrix absolute value:
absm(M) -
Absolute value of matrix
M, defined as|M| = sqrt(M†M). -
Matlab/Octave
- Integer division:
div(a,b) -
The integer part of
a/b. Not really linear algebra at all, but there you go. -
Matlab/Octave
Channels (CP maps)
-
Apply channel:
applychan(E, ρ or ψ, rep, dim) -
Apply channel
Eto density matrixρor state vectorψ. The representation used forEis specified byrep(choi-Jamiolkowski state, purification thereof, linear operator, Kraus decomposition, Steinspring isometry, or Pauli representation of a qubit channel). The input and output dimensions of the channel are given by the vectordim.repanddimcan often be guessed automatically, so these don't necessarily need to be supplied. -
Matlab/Octave
-
Convert channel representation:
chanconv(E,from,to,dim) -
Convert between different representations of a
channel
E. The representation used forEis specified byfrom, and the desired output representation byto(seeapplychan, above, for the possible representations). The input and output dimensions of the channel are given by the vectordim. Thefromanddimarguments can often be guessed automatically, so they don't necessarily need to be supplied. -
Matlab/Octave
-
R representation:
chan2R(ρ) -
Convert the Choi-Jamiolkowski state representation
ρof a qubit channel to the corresponding R representation (Pauli-basis representation). -
Matlab/Octave
-
Minimum output Rényi entropy:
chanMinRenyi(p,E,rep,dim) -
Numerically estimate the minimum output
Rényi
p-entropy of channelEspecified in representationrepand with input and output dimensions given by the vectordim. Though who cares now? The minimum output entropy conjecture forp=1is already dead! -
Matlab/Octave
Generating Random Things
- Random state vector:
randPsi(dim) -
Generate a random state vector of dimension
dim, uniformly distributed according to the Haar measure. -
Matlab/Octave
- Random density matrix:
randRho(dim) -
Generate a random density matrix of dimension
dim, distributed according to some measure or other (don't worry, it's dense in the set of all density matrices, which is all you really care about, right?). -
Matlab/Octave
- Random channel:
randChan(dimA,[dimB,[dimE]],rep) - Generate a random quantum channel with specified input, and (optionally) output and environment dimensions, distributed according some other measure or other (still dense -- were you still worrying?).
-
Matlab/Octave
- Random unitary:
randU(d) -
Generate a random
d × dunitary matrix, uniformly distributed according to the Haar measure. -
Matlab/Octave
- Random isometry:
randV(n,m) -
Generate a random
n × misometry. -
Matlab/Octave
- Random Hermitian matrix:
randH(dim) -
Generate a random
d × dHermitian matrix. -
Matlab/Octave
- Random matrix:
randM(dim) -
Generate a random
d × dcomplex matrix. -
Matlab/Octave
- von Neumann entropy:
VNent(ρ) -
Von Neumann entropy of density matrix
ρ. Note: this is not the entropy of entanglement; for that, useVNent(TrX(ρ,2,[dim1,dim2]))orentE(below). -
Matlab/Octave
- Rényi p-entropy:
renyi(p,ρ) -
Rényi p-entropy of density matrix
ρ. -
Matlab/Octave
- Entropy of entanglement:
entE(ρ,dim) -
Entropy of entanglement of a bipartite density matrix
ρwith subsystem dimensions given by the vectordim. The entropy of entanglement is defined as the von-Neumann entropy of the reduced density matrix of either subsystem. For bipartite pure states, all entanglement measures are asymptotically equivalent to it. -
Matlab/Octave
- Negativity:
negativity(ρ,dim) -
Negativity of a bipartite density matrix
ρwith subsystem dimensions given by the vectordim. Defined as the absolute value of twice the sum of the negative eigenvalues ofρ, or 0 if all eigenvalues are positive. -
Matlab/Octave
- Concurrence:
concurrence(ρ or ψ) -
Concurrence of a 2-qubit state vector
ψor density matrixρ, defined asa*d-b*cwhereψ=(a,b,c,d), ormax[0,λ1-λ2-λ3-λ4]whereρ flip(ρ*)has eigenvaluesλ. -
Matlab/Octave
- Maximum entangled fraction:
maxEfrac(ρ) -
Maximum entangled (or singlet) fraction of a 2-qubit density
matrix
ρ, defined as the maximum over all maximally entangled states|φ>of<φ|ρ|φ>. -
Matlab/Octave
-
Populate some useful variables:
usefulvars
- Populate some useful variables for playing around with quantum information. Read the contents of the file to see what goodies you get.
-
Matlab/Octave
- Kill tinies:
killtiny(X,[tolerance]) -
Set any elements of
Mthat have very small magnitude to exactly zero.Mcan be a matrix or scalar. Real and imaginary parts are set to zero independently if necessary. Useful for tidying up matrices before displaying them. -
Matlab/Octave
-
Pretty-print matrices:
pprintm(M,[rowlabels,[collabels]]
- Pretty-print a matrix in a format that makes it easier to visually parse the structure of the matrix. Rows and columns can be labelled, and labellings suitable for state vectors or density matrices of arbitrary combinations of qudits can be generated automatically.
-
Matlab/Octave
-
Pack variables into vector:
vpack(s,...,v,...,M,...)
Unpack variables from vector:vunpack(V,s,...,v,...,A,...) -
Pack or unpack scalars
s, vectorsv, and matricesMinto a single vector, or unpack them again. Arguments tovpackcan be in any order. ArgumentVtovunpackis the vector to be unpacked, the other arguments are used to determine the dimensions of the objects to unpack into (contents of these arguments are ignored).Intended for use with optimization routines which (used to?) require all optimization parameters to be passed in a single vector.
-
Matlab/Octave
-
Solve dual (LMI) form SDP:
runLMI(c,F,G,A,b)
-
Solve the following semi-definite program, given in dual (linear matrix
inequality) form:
Requires a working SeDuMi installation. (SeDuMi can be made to work under Octave, too.)minimize cT x subject to Σi F(k)i >= G(k) Ax = b -
Matlab/Octave
-
Solve primal form SDP:
runSDP(C,A,b)
-
Solve the following semi-definite program, given in primal form:
Requires a working SeDuMi installation. (SeDuMi can be made to work under Octave, too.)minimize trace(CX) subject to X >= 0 trace(A(i)X) = b(i) -
Matlab/Octave
- `Smart' plotting:
smartplot(...) -
Read the internal documentation for the low-down on this one (try
"
help smartplot" at a Matlab/Octave prompt). Essentially,smartplotreturns points sampled from a user-supplied function, which can then be plotted with the built-in plotting functions.The `smart' part is that a higher density of points is sampled around interesting regions (e.g. turning points). Interesting regions are identified by a user-defined function, so can be anything.
-
Matlab/Octave
- Interesting regions:
maxima, turn_pt -
Functions to detect a couple of types of interesting regions, for
use with
smartplot. -
Matlab/Octave