toqito.perms.swap
- toqito.perms.swap(rho, sys=None, dim=None, row_only=False)[source]
Swap two subsystems within a state or operator.
Swaps the two subsystems of the vector or matrix
rho, where the dimensions of the (possibly more than 2) subsystems are given bydimand the indices of the two subsystems to be swapped are specified in the 1-by-2 vectorsys.If
rhois non-square and not a vector, different row and column dimensions can be specified by putting the row dimensions in the first row ofdimand the column dimensions in the second row ofdim.If
row_onlyis set toTrue, then only the rows ofrhoare swapped, but not the columns – this is equivalent to multiplyingrhoon the left by the corresponding swap operator, but not on the right.Examples
Consider the following matrix
\[\begin{split}X = \begin{pmatrix} 1 & 5 & 9 & 13 \\ 2 & 6 & 10 & 14 \\ 3 & 7 & 11 & 15 \\ 4 & 8 & 12 & 16 \end{pmatrix}.\end{split}\]If we apply the
swapfunction provided bytoqitoon \(X\), we should obtain the following matrix\[\begin{split}\text{Swap}(X) = \begin{pmatrix} 1 & 9 & 5 & 13 \\ 3 & 11 & 7 & 15 \\ 2 & 10 & 6 & 14 \\ 4 & 12 & 8 & 16 \end{pmatrix}.\end{split}\]This can be observed by the following example in
toqito.>>> from toqito.perms import swap >>> import numpy as np >>> test_mat = np.array( >>> [[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]] >>> ) >>> swap(test_mat) [[ 1 9 5 13] [ 3 11 7 15] [ 2 10 6 14] [ 4 12 8 16]]
It is also possible to use the
sysanddimarguments, it is possible to specify the system and dimension on which to apply the swap operator. For instance forsys = [1 ,2]anddim = 2we have that\[\begin{split}\text{Swap}(X)_{2, [1, 2]} = \begin{pmatrix} 1 & 9 & 5 & 13 \\ 3 & 11 & 7 & 15 \\ 2 & 10 & 6 & 14 \\ 4 & 12 & 8 & 16 \end{pmatrix}.\end{split}\]Using
toqitowe can see this gives the proper result.>>> from toqito.perms import swap >>> import numpy as np >>> test_mat = np.array( >>> [[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]] >>> ) >>> swap(test_mat, [1, 2], 2) [[ 1 9 5 13] [ 3 11 7 15] [ 2 10 6 14] [ 4 12 8 16]]
It is also possible to perform the
swapfunction on vectors in addition to matrices.>>> from toqito.perms import swap >>> import numpy as np >>> test_vec = np.array([1, 2, 3, 4]) >>> swap(test_vec) [1 3 2 4]
- Raises:
ValueError – If dimension does not match the number of subsystems.
- Parameters:
rho – A vector or matrix to have its subsystems swapped.
sys – Default: [1, 2]
dim – Default:
[sqrt(len(X), sqrt(len(X)))]row_only – Default:
False
- Returns:
The swapped matrix.