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
-
Partial trace:
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.
TrX(ρ,sys,dim)
Partial transpose:Tx(Ρ,sys,dim) -
Partial trace or transpose of density matrix
ρwith respect to subsystemsys, where the subsystems have dimensions specified by vectordim. (2 or 3 subsystems can be specified; for more complex systems use oneTrXorTxinside another, perhaps along withexhange(below).) -
Matlab
Mathematica
-
Exchange subsystems:
exchange(ρ,x,dim) -
Exchange order of two subsystems (specified in vector
x) of multi-partite stateρ.dimspecifies the dimensions of all the subsystems ofρ.- Matlab
- 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
- Matlab
-
Schmidt decomposition:
schmidt(ψ,dim) -
Schmidt decomposition of state
ψwith subsystem dimensions given bydim. -
Matlab
-
Hilbert-Schmidt representation:
pauli(M),unpauli(R) -
Transform matrix
Mto 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⊗σyfor 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, useentVN(TrX(ρ,2,[dim1,dim2]))orentE(below). -
Matlab
- Entropy of entanglement:
entE(ρ,dim) -
Entropy of entanglement of bipartite density matrix
ρwith subsystems of dimensions defined by vectordim. 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 vectordim, 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 asa*d-b*cwhereψ=(a,b,c,d), ormax[0,λ1-λ2-λ3-λ4]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,smartplotreturns 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, 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 Matlab's optimization routines which require all optimization parameters to be passed in a single vector. -
Matlab
©2005-2007 Toby Cubitt