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
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
-
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
-
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
Mathematica
-
Purification:
purification(ρ) -
Return a minimal purification of density matrix
ρ.- Matlab
- Matlab
-
Normalise:
normalise(ρ or ψ) -
Normalise the state vector
ψor density matrixρ.- Matlab
- 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ψ, wheredimspecifies the dimensions of all the subsystems.- Matlab
- Matlab
-
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
- 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 a pure state
ψ, whose subsystem dimensions are given by the vectordim. -
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(ρ or ψ) -
Implements Wooters' spin-flip operation:
σy⊗σy ρ*σy⊗σyfor 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(M†M). -
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
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
-
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
-
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 channelEspecified in representationrepand with input and output dimensions given by the vectordim. But who cares now? The minimum output entropy conjecture forp=1is 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 × dunitary matrix, uniformly distributed according to the Haar measure. -
Matlab
- Random Hermitian matrix:
randH(dim) -
Generate a random
d × dHermitian matrix. -
Matlab
- Random matrix:
randM(dim) -
Generate a random
d × dcomplex matrix. -
Matlab
- 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
- 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 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
- 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
- Concurrence:
concurrence(ρ or $psi;) -
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
- 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
- `Smart' plotting:
smartplot(...) -
Read the internal documentation for the low-down on this one (try
"
help smartplot" at a Matlab prompt). Essentially,smartplotreturns 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
-
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
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
-
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 (used to?) require all optimization parameters to be passed in a single vector.
-
Matlab