Tensor
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.
Contents
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); >> 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.
%% TENSOR Kronecker tensor product of two or more matrices or vectors
% This function can be called in one of three ways:
%
% KRN = Tensor(A,B,C,...) returns the Kronecker product A \otimes B
% \otimes C \otimes ...
%
% KRN = Tensor(A,m) returns the Kronecker product of A with itself m
% times.
%
% KRN = Tensor(A), if A is a cell, returns the Kronecker product of
% all matrices contained within A.
%
% URL: http://www.qetlab.com/Tensor
% requires: nothing
% author: Nathaniel Johnston (nathaniel@njohnston.ca)
% package: QETLAB
% last updated: May 24, 2018
function krn = Tensor(A,varargin)
% If A is a cell, tensor together each element of the cell.
if(iscell(A))
krn = A{1};
for j = 2:length(A);
krn = kron(krn,A{j});
end
% If two arguments were received and the second is a scalar, tensor A with itself that many times.
elseif(nargin == 2 && length(varargin{1}) == 1)
% Tensor naively if we only want a few copies.
if(varargin{1} <= 4 || (length(A) > 3 && ~issparse(A)))
krn = 1;
for j = 1:varargin{1};
krn = kron(krn,A);
end
% Be more clever (a-la exponentiation by squaring) if we want lots of
% copies. For n copies, this procedure only does O(log(n)) Kronecker
% products. Doesn't help much for full matrices, but in practice is
% about a 50% speedup for some sparse matrices.
else
l2 = floor(log2(varargin{1}));
krn_cell = cell(1,l2+1);
b = dec_to_bin(varargin{1});
krn = 1;
krn_cell{1} = A;
for j = 1:l2
krn_cell{j+1} = kron(krn_cell{j},krn_cell{j});
if(b(j) == 1)
krn = kron(krn,krn_cell{j});
end
end
krn = kron(krn,krn_cell{l2+1});
end
% If two or more arguments were received and the second isn't a scalar, tensor together all arguments.
else
krn = A;
for j = 2:nargin;
krn = kron(krn,varargin{j-1});
end
end