PartialTrace

PartialTrace is a function that computes the partial trace of a matrix. The trace may be taken on any subset of the subsystems on which the matrix acts.

Syntax

 * XPT = PartialTrace(X)
 * XPT = PartialTrace(X,SYS)
 * XPT = PartialTrace(X,SYS,DIM)
 * XPT = PartialTrace(X,SYS,DIM,MODE)

Argument descriptions

 * X: A matrix to have its partial trace returned.
 * SYS (optional, default 2): A scalar or vector containing the indices of the subsystems on which the trace is to be applied.
 * DIM (optional, by default has all subsystems of equal dimension): A specification of the dimensions of the subsystems that X</tt> lives on. DIM</tt> can be provided in one of two ways:
 * If DIM</tt> is a scalar, it is assumed that X</tt> lives on the tensor product of two spaces, the first of which has dimension DIM</tt> and the second of which has dimension length(X)/DIM</tt>.
 * If $X \in M_{n_1} \otimes \cdots \otimes M_{n_p}$ then DIM</tt> should be a row vector containing the dimensions (i.e., DIM = [n_1, ..., n_p]</tt>).
 * MODE</tt> (optional, default -1): A flag that determines which of two algorithms is used to compute the partial trace. If MODE = -1</tt> then this script chooses whichever algorithm it thinks will be faster based on the dimensions of the subsystems being traced out and the sparsity of X</tt>. If you wish to force the script to use a specific one of the algorithms (not recommended!), they are generally best used in the following situations:
 * MODE = 0</tt>: Best used when X</tt> is full or non-numeric. Sometimes also appropriate when X</tt> is sparse, particularly when XPT</tt> will be quite small compared to X</tt> (i.e., when most of the subsystems are traced out).
 * <tt>MODE = 1</tt>: Best used when <tt>X</tt> is sparse, particularly when <tt>XPT</tt> will not be significantly smaller than <tt>X</tt> itself (i.e., when only a few of the subsystems are traced out).

A bipartite square matrix
By default, the <tt>PartialTrace</tt> function takes the trace over the second subsystem:

By specifying the <tt>SYS</tt> argument, you can take the trace over the first subsystem instead:

Taking the trace over both the first and second subsystems results in the standard trace of <tt>X</tt>:

A large sparse matrix
This function has no trouble dealing with large sparse matrices. The following line of code generates a random sparse matrix in $M_2 \otimes M_2 \otimes M_5 \otimes M_5 \otimes M_{10} \otimes M_{100} \otimes M_2 \otimes M_5$ and traces out the first, third, fourth, fifth, sixth, and eighth subsystems (resulting in an operator living in $M_2 \otimes M_2$) in under 1/2 of a second on a standard desktop computer:

Notice that, because input to <tt>PartialTrace</tt> was sparse, so it its output.