# TensorSum

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 Other toolboxes required TensorSum Computes a vector or operator from its tensor decomposition none FilterNormalFormOperatorSchmidtDecompositionSchmidtDecomposition Basic operation

TensorSum is a function that computes a vector or operator from its tensor decomposition. It acts as an inverse of the OperatorSchmidtDecomposition and SchmidtDecomposition functions, but also works for multipartite decompositions.

## Syntax

• TS = TensorSum(A1,A2,...)
• TS = TensorSum(S,A1,A2,...)

## Argument descriptions

• A1,A2,...: Either matrices or cells containing matrices (and they should all be the same: either all matrices or all cells). If they are matrices, then the k-th column of each Ai will be tensored together for all k, and then the sum over k will be taken at the end. If they are cells, then the k-th element of each Ai will be tensored together for all k, and then the sum over k will be taken at the end.
• S (optional, default [1,1,...,1]): A vector of weights (such as Schmidt coefficients) that will be applied when summing the terms at the end of the computation.

## Examples

### Inverse of SchmidtDecomposition

This function acts as an inverse of the SchmidtDecomposition function:

```>> vec = [1,2,3,4];
>> [s,u,v] = SchmidtDecomposition(vec);
>> TensorSum(s,u,v)

ans =

1.0000
2.0000
3.0000
4.0000```

### Inverse of OperatorSchmidtDecomposition

This function also acts as an inverse of the OperatorSchmidtDecomposition function:

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

X =

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

>> [s,U,V] = OperatorSchmidtDecomposition(X);
>> TensorSum(s,U,V)

ans =

1.0000    2.0000    3.0000    4.0000
5.0000    6.0000    7.0000    8.0000
9.0000   10.0000   11.0000   12.0000
13.0000   14.0000   15.0000   16.0000```

## Source code

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

1. `%%  TENSORSUM  Computes a vector or operator from its tensor decomposition`
2. `%   This function can be called in two ways:`
3. `%   TS = TensorSum(A1,A2,...)`
4. `%   TS = TensorSum(S,A1,A2,...)`
5. `%`
6. `%   Each term Ai is either a matrix or a cell containing matrices (and they`
7. `%   should all be the same: either all matrices or all cells). If they are`
8. `%   matrices, then the k-th column of each Ai will be tensored together for`
9. `%   all k, and then the sum over k will be taken at the end. If they are`
10. `%   cells, then the k-th element of each Ai will be tensored together for`
11. `%   all k, and then the sum over k will be taken at the end.`
12. `%`
13. `%   If S is provided, it is a vector of weights (such as Schmidt`
14. `%   coefficients) that will be applied when summing the terms at the end of`
15. `%   the computation.`
16. `%`
17. `%   This function acts as an inverse of the SchmidtDecomposition and`
18. `%   OperatorSchmidtDecomposition functions. For example, if [S,U,V] =`
19. `%   SchmidtDecomposition(VEC) then VEC = TensorSum(S,U,V).`
20. `%`
21. `%   URL: http://www.qetlab.com/TensorSum`
22. ` `
23. `%   requires: nothing`
24. `%   author: Nathaniel Johnston (nathaniel@njohnston.ca)`
25. `%   package: QETLAB`
26. `%   last updated: December 19, 2014`
27. ` `
28. `function ts = TensorSum(a1,a2,varargin)`
29. ` `
30. `% Determine the number of terms in the tensor decomposition.`
31. `if(iscell(a2))`
32. `    len = length(a2);`
33. `else`
34. `    len = size(a2,2);`
35. `end`
36. ` `
37. `% Determine whether or not a vector of coefficients was provided, then read`
38. `% in the input arguments.`
39. `if(iscell(a1) || min(size(a1)) > 1 || len == 1)`
40. `    s = ones(len,1);`
41. `    A = [{a1} {a2} varargin];`
42. `else`
43. `    s = a1;`
44. `    A = [{a2} varargin];`
45. `end`
46. ` `
47. `% Get some more preliminary values (dimensions, etc.) and then convert`
48. `% everything into cell matrices (instead of matrices of column vectors) if`
49. `% necessary.`
50. `num_parties = length(A);`
51. `dim = zeros(2,num_parties);`
52. `for j = 1:num_parties`
53. `    if(~iscell(A{j}))`
54. `        A{j} = mat2cell(A{j},size(A{j},1),ones(1,size(A{j},2)));`
55. `    end`
56. `    dim(:,j) = size(A{j}{1}).';`
57. `end`
58. ` `
59. `% Finally, actually compute the tensor sum.`
60. `ts = sparse(prod(dim(1,:)),prod(dim(2,:)));`
61. `for j = 1:len`
62. `    ts_tmp = A{1}{j};`
63. `    for k = 2:num_parties`
64. `        ts_tmp = kron(ts_tmp,A{k}{j});`
65. `    end`
66. `    ts = ts + s(j)*ts_tmp;`
67. `end`