# Swap

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 Other toolboxes required Swap Swaps two subsystems within a state or operator none PermutationOperatorPermuteSystemsSwapOperator Permutations and symmetry of subsystems

Swap is a function that swaps the position of two subsystems in the space on which a quantum state or operator live. Note that the state or operator can have more than two subsystems altogether – the remaining subsystems are not affected. To permute the positions of more than two subsystems, use the PermuteSystems function.

## Syntax

• SX = Swap(X)
• SX = Swap(X,SYS)
• SX = Swap(X,SYS,DIM)
• SX = Swap(X,SYS,DIM,ROW_ONLY)

## Argument descriptions

• X: a vector (e.g., a pure quantum state) or a matrix to have its subsystems permuted
• SYS (optional, default [1,2]): a 1-by-2 vector containing the indices of the subsystems to be swapped
• 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 three 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]).
• If the subsystems aren't square (i.e., $X \in M_{m_1, n_1} \otimes \cdots \otimes M_{m_p, n_p}$) then DIM should be a matrix with two rows. The first row of DIM should contain the row dimensions of the subsystems (i.e., the mi's) and its second row should contain the column dimensions (i.e., the ni's). In other words, you should set DIM = [m_1, ..., m_p; n_1, ..., n_p].
• ROW_ONLY (optional, default 0): If set equal to 1, only the rows of X are permuted (this is equivalent to multiplying X on the left by SwapOperator(DIM,SYS)). If equal to 0, both the rows and columns of X are permuted (this is equivalent to multiplying X on both the left and right by the swap operator).

## Examples

The following code constructs a 4-by-4 matrix (thought of as an operator in $M_2 \otimes M_2$) and then swaps the two subsystems, which is equivalent to multiplying on the left and right by the swap operator:

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

X =

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

>> Swap(X)

ans =

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

>> SwapOperator(2)*X*SwapOperator(2)

ans =

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

## Source code

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

1. %%  SWAP    Swaps two subsystems within a state or operator
2. %   This function has one required argument:
3. %     X: a vector or matrix to have its subsystems swapped
4. %
5. %   SX = Swap(X) swaps the two subsystems of the vector or square matrix X,
6. %   where it is assumed that both the number of rows of X and the number of
7. %   columns of X are perfect squares and the subsystems have equal
8. %   dimension.
9. %
10. %   This function has three optional arguments:
11. %     SYS (default [1,2])
12. %     DIM (default [sqrt(length(X)),sqrt(length(X))])
13. %     ROW_ONLY (default 0)
14. %
15. %   SX = Swap(X,SYS,DIM,ROW_ONLY) swaps the two subsystems of the vector or
16. %   matrix X, where the dimensions of the (possibly more than 2) subsystems
17. %   are given by DIM and the indices of the two subsystems to be swapped
18. %   are specified in the 1-by-2 vector SYS. If X is non-square and not a
19. %   vector, different row and column dimensions can be specified by putting
20. %   the row dimensions in the first row of DIM and the column dimensions in
21. %   the second row of DIM. If ROW_ONLY is set to 1, then only the rows of X
22. %   are swapped, but not the columns -- this is equivalent to multiplying X
23. %   on the left by the corresponding swap operator, but not on the right.
24. %
25. %   URL: http://www.qetlab.com/Swap
26. 
27. %   requires: opt_args.m, PermuteSystems.m
28. %   author: Nathaniel Johnston (nathaniel@njohnston.ca)
29. %   package: QETLAB
30. %   last updated: November 12, 2014
31. 
32. function SX = Swap(X,varargin)
33. 
34. dX = size(X);
35. round_dim = round(sqrt(dX));
36. 
37. % set optional argument defaults: sys=[1,2], dim=round(sqrt(length(X))), row_only=0
38. [sys,dim,row_only] = opt_args({ [1,2], [round_dim(1),round_dim(1);round_dim(2),round_dim(2)], 0 },varargin{:});
39. 
40. % allow the user to enter a single number for dim
41. num_sys = length(dim);
42. if(num_sys == 1)
43.     dim = [dim,dX(1)/dim;dim,dX(2)/dim];
44.     if abs(dim(1,2) - round(dim(1,2))) + abs(dim(2,2) - round(dim(2,2))) >= 2*prod(dX)*eps
45.         error('Swap:InvalidDim','The value of DIM must evenly divide the number of rows and columns of X; please provide the DIM array containing the dimensions of the subsystems.');
46.     end
47.     dim(1,2) = round(dim(1,2));
48.     dim(2,2) = round(dim(2,2));
49.     num_sys = 2;
50. end
51. 
52. % verify that the input sys makes sense
53. if any(sys < 1) || any(sys > num_sys)
54.     error('Swap:InvalidSys','The subsystems in SYS must be between 1 and length(DIM) inclusive.');
55. elseif(length(sys) ~= 2)
56.     error('Swap:InvalidSys','SYS must be a vector with exactly two elements.');
57. end
58. 
59. % swap the indicated subsystems
60. perm = 1:num_sys;
61. perm(sys) = perm(sys(end:-1:1));
62. SX = PermuteSystems(X,perm,dim,row_only);