Difference between revisions of "PartialTrace"

From QETLAB
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{Function
 
{{Function
 
|name=PartialTrace
 
|name=PartialTrace
|desc=Computes the [[partial trace]] of a matrix
+
|desc=Computes the [[partial trace]] of a vector or matrix
 
|rel=[[PartialMap]]<br />[[PartialTranspose]]
 
|rel=[[PartialMap]]<br />[[PartialTranspose]]
 
|cat=[[List of functions#Superoperators|Superoperators]]
 
|cat=[[List of functions#Superoperators|Superoperators]]
 
|upd=November 22, 2012
 
|upd=November 22, 2012
 
|v=0.50}}
 
|v=0.50}}
<tt>'''PartialTrace'''</tt> is a [[List of functions|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.
+
<tt>'''PartialTrace'''</tt> is a [[List of functions|function]] that computes the [[partial trace]] (i.e., reduced density matrix) of a vector or a matrix. The trace may be taken on any subset of the subsystems on which the matrix acts.
  
 
==Syntax==
 
==Syntax==
Line 15: Line 15:
  
 
==Argument descriptions==
 
==Argument descriptions==
* <tt>X</tt>: A matrix to have its partial trace returned.
+
* <tt>X</tt>: A vector or a square matrix to have its partial trace (i.e., reduced density matrix) returned.
 
* <tt>SYS</tt> (optional, default 2): A scalar or vector containing the indices of the subsystems on which the trace is to be applied.
 
* <tt>SYS</tt> (optional, default 2): A scalar or vector containing the indices of the subsystems on which the trace is to be applied.
 
* <tt>DIM</tt> (optional, by default has all subsystems of equal dimension): A specification of the dimensions of the subsystems that <tt>X</tt> lives on. <tt>DIM</tt> can be provided in one of two ways:
 
* <tt>DIM</tt> (optional, by default has all subsystems of equal dimension): A specification of the dimensions of the subsystems that <tt>X</tt> lives on. <tt>DIM</tt> can be provided in one of two ways:
 
** If <tt>DIM</tt> is a scalar, it is assumed that <tt>X</tt> lives on the tensor product of two spaces, the first of which has dimension <tt>DIM</tt> and the second of which has dimension <tt>length(X)/DIM</tt>.
 
** If <tt>DIM</tt> is a scalar, it is assumed that <tt>X</tt> lives on the tensor product of two spaces, the first of which has dimension <tt>DIM</tt> and the second of which has dimension <tt>length(X)/DIM</tt>.
 
** If $X \in M_{n_1} \otimes \cdots \otimes M_{n_p}$ then <tt>DIM</tt> should be a row vector containing the dimensions (i.e., <tt>DIM = [n_1, ..., n_p]</tt>).
 
** If $X \in M_{n_1} \otimes \cdots \otimes M_{n_p}$ then <tt>DIM</tt> should be a row vector containing the dimensions (i.e., <tt>DIM = [n_1, ..., n_p]</tt>).
* <tt>MODE</tt> (optional, default -1): A flag that determines which of two algorithms is used to compute the partial trace. If <tt>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 <tt>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:
+
* <tt>MODE</tt> (optional, default -1): A flag that determines which of two algorithms is used to compute the partial trace. Is only used if <tt>X</tt> is a matrix (not a vector). If <tt>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 <tt>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:
 
** <tt>MODE = 0</tt>: Best used when <tt>X</tt> is full or non-numeric. Sometimes also appropriate when <tt>X</tt> is sparse, particularly when <tt>XPT</tt> will be quite small compared to <tt>X</tt> (i.e., when most of the subsystems are traced out).
 
** <tt>MODE = 0</tt>: Best used when <tt>X</tt> is full or non-numeric. Sometimes also appropriate when <tt>X</tt> is sparse, particularly when <tt>XPT</tt> will be quite small compared to <tt>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).
 
** <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).
Line 78: Line 78:
  
 
Notice that, because input to <tt>PartialTrace</tt> was sparse, so it its output.
 
Notice that, because input to <tt>PartialTrace</tt> was sparse, so it its output.
 +
 +
===Partial trace of a vector===
 +
If a pure state vector is provided as the input <tt>X</tt>, its reduced density matrix (i.e., the partial trace of <tt>X*X'</tt>) will be returned:
 +
<syntaxhighlight>
 +
>> X = MaxEntangled(2)
 +
 +
X =
 +
 +
    0.7071
 +
        0
 +
        0
 +
    0.7071
 +
 +
>> PartialTrace(X)
 +
 +
ans =
 +
 +
    0.5000        0
 +
        0    0.5000
 +
</syntaxhighlight>
 +
 +
Applying the <tt>PartialTrace</tt> function to a vector <tt>X</tt> is significantly faster and less memory-intensive than applying it to the pure state matrix <tt>X*X'</tt>, especially in high-dimensional systems:
 +
<syntaxhighlight>
 +
>> X = RandomStateVector(10*20*30);
 +
>> tic;PartialTrace(X*X',3,[10,20,30]);toc
 +
Elapsed time is 2.029789 seconds.
 +
 +
>> tic;PartialTrace(X,3,[10,20,30]);toc
 +
Elapsed time is 0.001600 seconds.
 +
</syntaxhighlight>
  
 
{{SourceCode|name=PartialTrace}}
 
{{SourceCode|name=PartialTrace}}

Latest revision as of 11:04, 26 May 2022

PartialTrace
Computes the partial trace of a vector or matrix

Other toolboxes required none
Related functions PartialMap
PartialTranspose
Function category Superoperators

PartialTrace is a function that computes the partial trace (i.e., reduced density matrix) of a vector or 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 vector or a square matrix to have its partial trace (i.e., reduced density matrix) 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 lives on. DIM can be provided in one of two ways:
    • If DIM is a scalar, it is assumed that X lives on the tensor product of two spaces, the first of which has dimension DIM and the second of which has dimension length(X)/DIM.
    • If $X \in M_{n_1} \otimes \cdots \otimes M_{n_p}$ then DIM should be a row vector containing the dimensions (i.e., DIM = [n_1, ..., n_p]).
  • MODE (optional, default -1): A flag that determines which of two algorithms is used to compute the partial trace. Is only used if X is a matrix (not a vector). If MODE = -1 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. 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: Best used when X is full or non-numeric. Sometimes also appropriate when X is sparse, particularly when XPT will be quite small compared to X (i.e., when most of the subsystems are traced out).
    • MODE = 1: Best used when X is sparse, particularly when XPT will not be significantly smaller than X itself (i.e., when only a few of the subsystems are traced out).

Examples

A bipartite square matrix

By default, the PartialTrace function takes the trace over the second subsystem:

>> X = reshape(1:16,4,4)'

X =

     1     2     3     4
     5     6     7     8
     9    10    11    12
    13    14    15    16

>> PartialTrace(X)

ans =

     7    11
    23    27

By specifying the SYS argument, you can take the trace over the first subsystem instead:

>> PartialTrace(X,1)

ans =

    12    14
    20    22

Taking the trace over both the first and second subsystems results in the standard trace of X:

>> PartialTrace(X,[1,2])

ans =

    34

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:

>> PartialTrace(sprand(1000000,1000000,0.000001),[1,3,4,5,6,8],[2,2,5,5,10,100,2,5])

ans =

   (2,1)       0.9638
   (1,2)       0.5716
   (4,3)       0.7358
   (4,4)       0.7300

Notice that, because input to PartialTrace was sparse, so it its output.

Partial trace of a vector

If a pure state vector is provided as the input X, its reduced density matrix (i.e., the partial trace of X*X') will be returned:

>> X = MaxEntangled(2)

X =

    0.7071
         0
         0
    0.7071

>> PartialTrace(X)

ans =

    0.5000         0
         0    0.5000

Applying the PartialTrace function to a vector X is significantly faster and less memory-intensive than applying it to the pure state matrix X*X', especially in high-dimensional systems:

>> X = RandomStateVector(10*20*30);
>> tic;PartialTrace(X*X',3,[10,20,30]);toc
Elapsed time is 2.029789 seconds.

>> tic;PartialTrace(X,3,[10,20,30]);toc
Elapsed time is 0.001600 seconds.

Source code

Click here to view this function's source code on github.