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 reinventing the wheel. Some of them are so simple it would probably be quicker to recode them than find this page, but since you're here anyway...
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.
If you make use of this code in your research, consider including a citation to this web page in any resulting publication. Not only is it fair to give credit when you've made use of other people's work, it is important for scientific reproducability to document any code you used to help produce your results. Also, if you found this code useful, then others probably will too! Citing this page helps others find it.
I leave it to your judgement whether you feel your results made sufficient use of this code to warrant a citation; I do not insist on it. But consider whether using this code has been as helpful to your research as the least useful paper you are citing. (Typically, this sets the bar very low!). If so, you should probably include a citation to this web page.
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...
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 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

The Mathematica versions can only do two subsystems 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

Normalise:
normalise(ρ or ψ)

Normalise the state vector
ψ
or density matrixρ
.  Matlab/Octave

Exchange subsystems:
sysexchange(ρ or ψ, sys, dim)

Exchange the order of two subsystems (specified in vector
sys
) of a multipartite density matrixρ
or state vectorψ
, wheredim
specifies the dimensions of all the subsystems.  Matlab/Octave

Permute subsystems:
syspermute(ρ or ψ, perm, dim)

Permuate the order of the subsystems in a multipartite density matrix
ρ
or state vectorψ
according to permutationperm
.dim
specifies the dimensions of all the subsystems.  Matlab/Octave

Tensor product:
tensor(A,B,C,...)

Tensor product of arguments (generalises builtin 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

Schmidt decomposition:
schmidt(ψ,dim)

Schmidt decomposition of a pure state
ψ
, whose subsystem dimensions are given by the vectordim
.  Matlab/Octave

HilbertSchmidt representation:
pauli(M)
,unpauli(R)

Transform a 4x4 matrix
M
to the HilbertSchmidt representation in the Pauli basis, or viceversa,
i.e.M = ¼ R_{ij}σ_{i}σ_{j}
.  Matlab/Octave
 Spinflip:
flip(ρ or ψ)

Implements Wooters' spinflip operation:
σ_{y}⊗σ_{y} ρ^{*}σ_{y}⊗σ_{y}
for 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 lightyears.
 Matlab/Octave
 Matrix absolute value:
absm(M)

Absolute value of matrix
M
, defined asM = 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
E
to density matrixρ
or state vectorψ
. The representation used forE
is specified byrep
(ChoiJamiolkowski 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
.rep
anddim
can 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 forE
is 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
. Thefrom
anddim
arguments can often be guessed automatically, so they don't necessarily need to be supplied.  Matlab/Octave

R representation:
chan2R(ρ)

Convert the ChoiJamiolkowski state representation
ρ
of a qubit channel to the corresponding R representation (Paulibasis representation).  Matlab/Octave

Minimum output Rényi entropy:
chanMinRenyi(p,E,rep,dim)

Numerically estimate the minimum output
Rényi
p
entropy of channelE
specified in representationrep
and with input and output dimensions given by the vectordim
. Though who cares now? The minimum output entropy conjecture forp=1
is 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 × d
unitary matrix, uniformly distributed according to the Haar measure.  Matlab/Octave
 Random isometry:
randV(n,m)

Generate a random
n × m
isometry.  Matlab/Octave
 Random Hermitian matrix:
randH(dim)

Generate a random
d × d
Hermitian matrix.  Matlab/Octave
 Random matrix:
randM(dim)

Generate a random
d × d
complex 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 pentropy:
renyi(p,ρ)

Rényi pentropy 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 vonNeumann 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 2qubit state vector
ψ
or density matrixρ
, defined asa*db*c
whereψ=(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 2qubit 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
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/Octave

Prettyprint matrices:
pprintm(M,[rowlabels,[collabels]]
 Prettyprint 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 matricesM
into a single vector, or unpack them again. Arguments tovpack
can be in any order. ArgumentV
tovunpack
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 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 semidefinite program, given in dual (linear matrix
inequality) form:
minimize c^{T} x subject to Σ_{i} F^{(k)}_{i} >= G^{(k)} Ax = b  Matlab/Octave

Solve primal form SDP:
runSDP(C,A,b)

Solve the following semidefinite program, given in primal form:
minimize trace(CX) subject to X >= 0 trace(A^{(i)}X) = b^{(i)}  Matlab/Octave
 `Smart' plotting:
smartplot(...)

Read the internal documentation for the lowdown on this one (try
"
help smartplot
" at a Matlab/Octave prompt). Essentially,smartplot
returns points sampled from a usersupplied function, which can then be plotted with the builtin 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 userdefined 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