Tensor

From QETLAB
Revision as of 15:00, 22 September 2014 by Nathaniel (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Tensor
Kronecker tensor product of two or more matrices

Other toolboxes required none
Related functions TensorSum
Function category Basic operation

Tensor is a function that produces the Kronecker (tensor) product of two or more matrices, and thus extends MATLAB's built-in kron function.

Syntax

  • KRN = Tensor(A)
  • KRN = Tensor(A,M)
  • KRN = Tensor(A,B,C,...)

Argument descriptions

  • A: If A is a cell, then KRN is the Kronecker product of all matrices within A.
  • M (optional): A scalar indicating how many times A should be tensored with itself (if M is provided, A must be a matrix).
  • B,C,... (optional): Matrices. If these are provided, then A must be a matrix, and the output is $A \otimes B \otimes C \otimes \cdots$.

Examples

Several different input formats

The Tensor function accepts input in many different formats, so that you may use whichever is most convenient at a particular time. For example, the following three code snippets all result in the same operator: Tensor(A,3), Tensor(A,A,A), and Tensor({A,A,A}).

Multiple copies of Werner states

When investigating the NPPT bound entanglement conjecture, you may want to tensor Werner states with themselves multiple times. The following code tensors a particular Werner state with itself 6 times:

>> rho = WernerState(3,1/2,1);
>> rho6 = Tensor(rho,6);

Note that rho6 is a 531441-by-531441 matrix, so we don't display it here.

Source code

Click on "expand" to the right to view the MATLAB source code for this function.

  1. %%  TENSOR   Kronecker tensor product of two or more matrices or vectors
  2. %   This function can be called in one of three ways:
  3. %
  4. %   KRN = Tensor(A,B,C,...) returns the Kronecker product A \otimes B
  5. %   \otimes C \otimes ...
  6. %
  7. %   KRN = Tensor(A,m) returns the Kronecker product of A with itself m
  8. %   times.
  9. %
  10. %   KRN = Tensor(A), if A is a cell, returns the Kronecker product of
  11. %   all matrices contained within A.
  12. %
  13. %   URL: http://www.qetlab.com/Tensor
  14.  
  15. %   requires: nothing
  16. %   author: Nathaniel Johnston (nathaniel@njohnston.ca)
  17. %   package: QETLAB
  18. %   last updated: May 10, 2016
  19.  
  20. function krn = Tensor(A,varargin)
  21.  
  22. % If A is a cell, tensor together each element of the cell.
  23. if(iscell(A))
  24.     krn = A{1};
  25.     for j = 2:length(A);
  26.         krn = kron(krn,A{j});
  27.     end
  28.  
  29. % If two arguments were received and the second is a scalar, tensor A with itself that many times.
  30. elseif(nargin == 2 && length(varargin{1}) == 1)
  31.     % Tensor naively if we only want a few copies.
  32.     if(varargin{1} <= 4 || (length(A) > 3 && ~issparse(A)))
  33.         krn = 1;
  34.         for j = 1:varargin{1};
  35.             krn = kron(krn,A);
  36.         end
  37.  
  38.     % Be more clever (a-la exponentiation by squaring) if we want lots of
  39.     % copies. For n copies, this procedure only does O(log(n)) Kronecker
  40.     % products. Doesn't help much for full matrices, but in practice is
  41.     % about a 50% speedup for some sparse matrices.
  42.     else
  43.         l2 = floor(log2(varargin{1}));
  44.         krn_cell = cell(1,l2+1);
  45.         b = de2bi(varargin{1});
  46.  
  47.         krn = 1;
  48.         krn_cell{1} = A;
  49.         for j = 1:l2
  50.             krn_cell{j+1} = kron(krn_cell{j},krn_cell{j});
  51.             if(b(j) == 1)
  52.                 krn = kron(krn,krn_cell{j});
  53.             end
  54.         end
  55.         krn = kron(krn,krn_cell{l2+1});
  56.     end
  57.  
  58. % If two or more arguments were received and the second isn't a scalar, tensor together all arguments.
  59. else
  60.     krn = A;
  61.     for j = 2:nargin;
  62.         krn = kron(krn,varargin{j-1});
  63.     end
  64. end