Swap
From QETLAB
Swap | |
Swaps two subsystems within a state or operator | |
Other toolboxes required | none |
---|---|
Related functions | PermutationOperator PermuteSystems SwapOperator |
Function category | 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 m_{i}'s) and its second row should contain the column dimensions (i.e., the n_{i}'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.
%% SWAP Swaps two subsystems within a state or operator
% This function has one required argument:
% X: a vector or matrix to have its subsystems swapped
%
% SX = Swap(X) swaps the two subsystems of the vector or square matrix X,
% where it is assumed that both the number of rows of X and the number of
% columns of X are perfect squares and the subsystems have equal
% dimension.
%
% This function has three optional arguments:
% SYS (default [1,2])
% DIM (default [sqrt(length(X)),sqrt(length(X))])
% ROW_ONLY (default 0)
%
% SX = Swap(X,SYS,DIM,ROW_ONLY) swaps the two subsystems of the vector or
% matrix X, where the dimensions of the (possibly more than 2) subsystems
% are given by DIM and the indices of the two subsystems to be swapped
% are specified in the 1-by-2 vector SYS. If X is non-square and not a
% vector, different row and column dimensions can be specified by putting
% the row dimensions in the first row of DIM and the column dimensions in
% the second row of DIM. If ROW_ONLY is set to 1, then only the rows of X
% are swapped, but not the columns -- this is equivalent to multiplying X
% on the left by the corresponding swap operator, but not on the right.
%
% URL: http://www.qetlab.com/Swap
% requires: opt_args.m, PermuteSystems.m
% author: Nathaniel Johnston (nathaniel@njohnston.ca)
% package: QETLAB
% last updated: November 12, 2014
function SX = Swap(X,varargin)
dX = size(X);
round_dim = round(sqrt(dX));
% set optional argument defaults: sys=[1,2], dim=round(sqrt(length(X))), row_only=0
[sys,dim,row_only] = opt_args({ [1,2], [round_dim(1),round_dim(1);round_dim(2),round_dim(2)], 0 },varargin{:});
% allow the user to enter a single number for dim
num_sys = length(dim);
if(num_sys == 1)
dim = [dim,dX(1)/dim;dim,dX(2)/dim];
if abs(dim(1,2) - round(dim(1,2))) + abs(dim(2,2) - round(dim(2,2))) >= 2*prod(dX)*eps
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.');
end
dim(1,2) = round(dim(1,2));
dim(2,2) = round(dim(2,2));
num_sys = 2;
end
% verify that the input sys makes sense
if any(sys < 1) || any(sys > num_sys)
error('Swap:InvalidSys','The subsystems in SYS must be between 1 and length(DIM) inclusive.');
elseif(length(sys) ~= 2)
error('Swap:InvalidSys','SYS must be a vector with exactly two elements.');
end
% swap the indicated subsystems
perm = 1:num_sys;
perm(sys) = perm(sys(end:-1:1));
SX = PermuteSystems(X,perm,dim,row_only);