Matlab Code

I've collected here various functions, routines, and other bits of Matlab code organized by topic, that might save someone, somewhere, re-inventing the wheel. Comments within the code should be enough to figure out what they do and how to use them. No guarantee they work as advertised, but I use them myself so I do correct bugs when I come across them.

If you worry about such things, then consider them all to be released under the GPL license. Many of them are so simple it would probably be quicker to re-code them than find this page, but since you're here...

Linear algebra

If anyone can think of an even shorter way to code these I'd be interested...

Note: the Mathematica versions can only do two sub-systems till I get round to updating them.

Partial trace: TrX(ρ,sys,dim)
Partial transpose: Tx(Ρ,sys,dim)
Partial trace or transpose of density matrix ρ with respect to subsystem sys, where the subsystems have dimensions specified by vector dim. (2 or 3 subsystems can be specified; for more complex systems use one TrX or Tx inside another, perhaps along with exhange (below).)
Matlab   Mathematica
Exchange subsystems: exchange(ρ,x,dim)
Exchange order of two subsystems (specified in vector x) of multi-partite state ρ. dim specifies the dimensions of all the subsystems of ρ.
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 state ψ with subsystem dimensions given by 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(ρ/ψ)
Implements Wooters' spin-flip operation: σy⊗σyρ*σy⊗σy for density matrices, σ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

Entanglement

von Neumann entropy: entVN(ρ)
Von Neumann entropy of density matrix ρ.
Note: it is not the entropy of the reduced density matrix; for that, use entVN(TrX(ρ,2,[dim1,dim2])) or entE (below).
Matlab
Entropy of entanglement: entE(ρ,dim)
Entropy of entanglement of bipartite density matrix ρ with subsystems of dimensions defined by vector dim. The entropy of entanglement is defined as the von-Neumann entropy of the reduced density matrix of either subsystem, and for bipartite pure states, all entanglement measures are equivalent to it.
Matlab
Negativity: neg(ρ,dim)
Negativity of density matrix ρ with subsystems of dimensions defined by 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(ψ)
Concurrence of 2-qubit state vector ψ or density matrix ρ, defined as a*d-b*c where ψ=(a,b,c,d), or max[0,λ1234] where ρ 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 (type "help smartplot" at a Matlab prompt). Essentially, smartplot returns points sampled from a user-supplied function, which can then be plotted with Matlab's inbuilt 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

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 require all optimization parameters to be passed in a single vector.
Matlab