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). No guarantee they work as advertised, but I use them myself so I do correct bugs when I come across them. The Matlab code should run under Octave too, though personally I've had somewhat mixed success.

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

If anyone can think of an even shorter way to code these, I'd be interested in hearing about it...

Partial trace: TrX(ρ or ψ, sys, dim)
Partial trace over the subsystem(s) specified by (vector) sys of a density matrix ρ or state vector ψ, where the subsystem dimensions are given by vector dim.
Matlab
Partial transpose: Tx(ρ,sys,dim)
Partial trace of a density matrix ρ with respect to the subsystem specified by sys, where the subsystem dimensions are given by vector dim.
Matlab

The Mathematica versions can only do two sub-systems until I get round to updating them.

Bi/tripartite partial trace: TrX23(ρ,sys,dim)
Bi/tripartite partial transpose: Tx23(ρ,sys,dim)
Partial trace or transpose of a bipartite or tripartite density matrix ρ with respect to subsystem sys, where the subsystems have dimensions specified by vector dim.
Matlab   Mathematica
Purification: purification(ρ)
Return a minimal purification of density matrix ρ.
Matlab
Normalise: normalise(ρ or ψ)
Normalise the state vector ψ or density matrix ρ.
Matlab
Exchange subsystems: exchange(ρ or ψ, x, dim)
Exchange the order of two subsystems (specified in vector x) of a multi-partite density matrix ρ or state vector ψ, where dim specifies the dimensions of all the subsystems.
Matlab
Permute subsystems: syspermute(ρ or ψ, perm, dim)
Permuate the order of the subsystems in a multi-partite density matrix ρ or state vector ψ according to permutation perm. dim specifies the dimensions of all the subsystems.
Matlab
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
Schmidt decomposition: schmidt(ψ,dim)
Schmidt decomposition of a pure state ψ, whose subsystem dimensions are given by the vector dim.
Matlab
Hilbert-Schmidt representation: pauli(M), unpauli(R)
Transform matrix M to Hilbert-Schmidt representation in pauli basis, or vice-versa,
i.e. M = ¼ Rijσiσj.
Matlab
Spin-flip: flip(ρ or ψ)
Implements Wooters' spin-flip operation: σy⊗σy ρ*σy⊗σy for density matrices, or σy⊗σyψ* for state vectors.
Matlab
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
Matrix absolute value: absm(M)
Absolute value of matrix M, defined as |M| = sqrt(MM).
Matlab
Integer division: div(a,b)
The integer part of a/b. Not really linear algebra at all, but there you go.
Matlab

Channels (CP maps)

Apply channel: applychan(E,ρ or ψ, rep, dim)
Apply channel E to density matrix ρ or state vector ψ. The representation used for E is specified by rep (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 vector dim. rep and dim can often be guessed automatically, so these don't necessarily need to be supplied.
Matlab
Convert channel representation: chanconv(E,from,to,dim)
Convert between different representations of a channel E. The representation used for E is specified by from, and the desired output representation by to (see applychan, above, for the possible representations). The input and output dimensions of the channel are given by the vector dim. The from and dim arguments can often be guessed automatically, so they don't necessarily need to be supplied.
Matlab
R representation: chan2R(ρ)
Convert the Choi-Jamiolkowski state representation ρ of a qubit channel to the corresponding R representation (Pauli-basis representation).
Matlab
Minimum output Rényi entropy: chanMinRenyi(p,E,rep,dim)
Numerically estimate the minimum output Rényi p-entropy of channel E specified in representation rep and with input and output dimensions given by the vector dim. But who cares now? The minimum output entropy conjecture for p=1 is already dead!
Matlab

Generating Random Things

Random state vector: randPsi(dim)
Generate a random state vector of dimension dim, uniformly distributed according to the Haar measure.
Matlab
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).
Matlab
Random channel: randChan(dim,rep)
Generate a random quantum channel with input and output dimensions equal to dim, distributed according some other measure or other (still dense - were you still worrying?).
Matlab
Random unitary: randU(d)
Generate a random d × d unitary matrix, uniformly distributed according to the Haar measure.
Matlab
Random Hermitian matrix: randH(dim)
Generate a random d × d Hermitian matrix.
Matlab
Random matrix: randM(dim)
Generate a random d × d complex matrix.
Matlab

Entanglement and Entropy

von Neumann entropy: VNent(ρ)
Von Neumann entropy of density matrix ρ. Note: this is not the entropy of entanglement; for that, use VNent(TrX(ρ,2,[dim1,dim2])) or entE (below).
Matlab
Rényi p-entropy: renyi(p,ρ)
Rényi p-entropy of density matrix ρ.
Matlab
Entropy of entanglement: entE(ρ,dim)
Entropy of entanglement of a bipartite density matrix ρ with subsystem dimensions given by the vector dim. 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
Negativity: negativity(ρ,dim)
Negativity of a bipartite density matrix ρ with subsystem dimensions given by the vector dim. Defined as the absolute value of twice the sum of the negative eigenvalues of ρ, or 0 if all eigenvalues are positive.
Matlab
Concurrence: concurrence(ρ or $psi;)
Concurrence of a 2-qubit state vector ψ or density matrix ρ, defined as a*d-b*c where ψ=(a,b,c,d), or max[0,λ1234] where ρ flip(ρ*) has eigenvalues λ.
Matlab
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

Plotting

`Smart' plotting: smartplot(...)
Read the internal documentation for the low-down on this one (try "help smartplot" at a Matlab prompt). Essentially, smartplot returns points sampled from a user-supplied function, which can then be plotted with Matlab's 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
Interesting regions: maxima, turn_pt
Functions to detect a couple of types of interesting regions, for use with smartplot.
Matlab

Utility

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
Kill tinies: killtiny(X)
Set any elements of M that have very small magnitude to exactly zero. M can 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
Pack variables into vector: vpack(s,...,v,...,M,...)
Unpack variables from vector: vunpack(V,s,...,v,...,A,...)
Pack or unpack scalars s, vectors v, and matrices M into a single vector, or unpack them again. Arguments to vpack can be in any order. Argument V to vunpack is 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 Matlab's optimization routines which (used to?) require all optimization parameters to be passed in a single vector.

Matlab