# Superoperator dims

 Other toolboxes required superoperator_dims Computes the input, output, and environment dimensions of a superoperator none Helper functions
 This is a helper function that only exists to aid other functions in QETLAB. If you are an end-user of QETLAB, you likely will never have a reason to use this function.

superoperator_dims is a function that computes all of the dimensions of a given superoperator (i.e., the dimensions of its input space, output space, and environment space). It also serves as an error-checking function that will get cranky if the superoperator passed into it does not really represent a superopertor.

## Syntax

• [DA,DB,DE] = superoperator_dims(PHI)
• [DA,DB,DE] = superoperator_dims(PHI,ALLOW_RECT)
• [DA,DB,DE] = superoperator_dims(PHI,ALLOW_RECT,DIM)

## Argument descriptions

### Input arguments

• PHI: A superoperator, represented either as a Choi matrix or as a cell of Kraus operators.
• ALLOW_RECT (optional, default 1): A flag (either 1 or 0) indicating that the input and output spaces of PHI can or can't be rectangular (non-square). If ALLOW_RECT == 0 and PHI has non-square input or output space, an error is produced.
• DIM (optional): A 1-by-2 vector containing the dimensions of the input and output space of PHI. If this argument is provided, the function serves only as an error-checking function that makes sure that the computed dimensions agree with these given dimensions (and gives an error otherwise).

### Output arguments

• DA: A 1-by-2 vector containing the row and column dimensions of the input space of PHI (if ALLOW_RECT == 0 then DA is a scalar, not a vector).
• DB (optional): A 1-by-2 vector containing the row and column dimensions of the output space of PHI (if ALLOW_RECT == 0 then DB is a scalar, not a vector).
• DE (optional): A scalar indicating the environment dimension of PHI (this is sometimes call the "Choi rank" of PHI: it is the minimal number of Kraus operators in any representation of PHI).

## Examples

The following code generates a random quantum channel from $M_3$ to $M_4$ and then computes its dimensions, and verifies that it cannot be written with fewer than 12 Kraus operators (which happens with probability 1):

>> Phi = KrausOperators(RandomSuperoperator([3,4]),[3,4]);
>> [da,db,de] = superoperator_dims(Phi)

da =

3     3

db =

4     4

de =

12

## Source code

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

1. %%  SUPEROPERATOR_DIMS    Computes the input, output, and environment dimensions of a superoperator
2. %   This function has one required input argument:
3. %     PHI: a superoperator, represented as either a Choi matrix or a cell
4. %          of Kraus operators
5. %
6. %   This function has three output argument:
7. %     DA: the dimensions of the input (Alice's) space
8. %     DB: the dimensions of the output (Bob's) space
9. %     DE: the dimension of the environment (Eve's) space
10. %
11. %   [DA,DB,DE] = superoperator_dims(PHI) returns the dimensions of the
12. %   input, output, and environment spaces of PHI, in that order. DA and DB
13. %   are both 1-by-2 vectors containing the row and column dimensions of
14. %   their spaces. DE is always a scalar, and it is equal to the number of
15. %   Kraus operators of PHI (if PHI is provided as a Choi matrix then DE is
16. %   the *minimal* number of Kraus operators of any representation of PHI).
17. %
18. %   This function has two optional input arguments:
19. %     ALLOW_RECT: a flag (either 1 or 0) indicating that the input and
20. %                 output spaces of PHI can be non-square (default 1)
21. %     DIM: a vector or matrix containing the input and output dimensions of
22. %          PHI
23. %
24. %   [DA,DB,DE] = superoperator_dims(PHI,ALLOW_RECT,DIM) is as above. DIM
25. %   should provided if and only if PHI is a Choi matrix with unequal input
26. %   and output dimensions (since it is impossible to determine the input
27. %   and output dimensions from the Choi matrix alone). If ALLOW_RECT == 0
28. %   and PHI acts on non-square matrix spaces, an error will be produced. If
29. %   PHI maps M_{r,c} to M_{x,y} then DIM should be the 2-by-2 matrix
30. %   [r,x;c,y]. If PHI maps M_m to M_n, then DIM can simply be the vector
31. %   [m,n]. If ALLOW_RECT == 0 then DA and DB will be scalars instead of
32. %   vectors.
33. %
34. %   URL: http://www.qetlab.com/superoperator_dims
35. 
36. %   requires: opt_args.m, sporth.m
37. %   authors: Nathaniel Johnston (nathaniel@njohnston.ca)
38. %   package: QETLAB
39. %   last updated: November 27, 2014
40. 
41. function [da,db,de] = superoperator_dims(Phi,varargin)
42. 
43.     % If PHI is a cell, the dimensions are easy to compute.
44.     if(iscell(Phi))
45.         % Start by computing row and environment dimensions.
46.         [db,da] = size(Phi{1,1});
47.         [de,phi_cols] = size(Phi);
48. 
49.         % We now have the row dimensions, so compute the column dimensions.
50.         if(phi_cols > 2)
51.             error('superoperator_dims:InvalidDims','The cell PHI should have at most 2 columns (corresponding to left and right Kraus operators).');
52.         elseif(phi_cols == 2)
53.             [db(2),da(2)] = size(Phi{1,2});
54.         else
55.             da = [da,da];
56.             db = [db,db];
57.         end
58. 
59.         % Set optional argument defaults: allow_rect=1, dim=[da',db']
60.         [allow_rect,dim] = opt_args({ 1, [da',db'] },varargin{:}); % Did the user provide dimensions? Get them.
61.         dim = expand_dim(dim);
62. 
63.         % Now do some error checking.
64.         if((da(1) ~= da(2) || db(1) ~= db(2)) && allow_rect ~= 1)
65.             error('superoperator_dims:InvalidDims','The input and output spaces of PHI must be square.');
66.         elseif(dim(1,1) ~= da(1) || dim(2,1) ~= da(2) || dim(1,2) ~= db(1) || dim(2,2) ~= db(2))
67.             error('superoperator_dims:InvalidDims','The dimensions of PHI do not match those provided in the DIM argument.');
68.         end
69.         for j = 1:de
70.             if(~all(size(Phi{j,1}) == [dim(1,2),dim(1,1)]) || (phi_cols == 2 && ~all(size(Phi{j,2}) == [dim(2,2),dim(2,1)])))
71.                 error('superoperator_dims:InvalidDims','The Kraus operators of PHI do not all have the same size.');
72.             end
73.         end
74. 
75.     % If Phi is a Choi matrix, the dimensions are a bit more of a pain: we have
76.     % to guess a bit if the input and output dimensions are different.
77.     else
78.         % Try to guess DA and DB.
79.         [r,c] = size(Phi);
80.         da = [round(sqrt(r)),round(sqrt(c))];
81.         db = da;
82. 
83.         % Set optional argument defaults: allow_rect=1, dim=[da',db']
84.         [allow_rect,dim] = opt_args({ 1, [da',db'] },varargin{:});
85.         dim = expand_dim(dim);
86. 
87.         if(r ~= c && allow_rect ~= 1)
88.             error('superoperator_dims:InvalidDims','The Choi matrix of PHI must be square.');
89.         end
90.         if(dim(1,1)*dim(1,2) ~= r || dim(2,1)*dim(2,2) ~= c)
91.             error('superoperator_dims:InvalidDims','If the input and output dimensions are unequal and PHI is provided as a Choi matrix, the optional argument DIM must be specified (and its dimensions must agree with PHI).');
92.         end
93. 
94.         [~,de] = sporth(Phi); % environment dimension is the rank of the Choi matrix
95.     end
96. 
97.     % Finally, put DIM back into DA and DB.
98.     if(allow_rect == 1)
99.         da = [dim(1,1),dim(2,1)];
100.         db = [dim(1,2),dim(2,2)];
101.     else
102.         da = dim(1,1);
103.         db = dim(1,2);
104.     end
105. end
106. 
107. % This function lets the user enter just a single number or a 1-by-2 vector
108. % for the dimensions, and then expands that number or vector to a full
109. % 2-by-2 matrix of dimensions appropriately.
110. function dim_mat = expand_dim(dim)
111.     sz = size(dim);
112.     if(max(sz) == 1) % user just entered a single number for DIM
113.         dim_mat = [dim,dim;dim,dim];
114.     elseif(min(sz) == 1) % user entered a 2-dimensional vector for DIM
115.         dim_mat = [dim(:).';dim(:).'];
116.     elseif(max(sz) == 2) % user entered a full 2-by-2 matrix for DIM
117.         dim_mat = dim;
118.     else
119.         error('expand_dim:InvalidDims','The dimensions must be provided in a matrix no larger than 2-by-2.');
120.     end
121. end