SwapOperator
From QETLAB
SwapOperator | |
Produces a unitary operator that swaps two subsystems | |
Other toolboxes required | none |
---|---|
Related functions | PermutationOperator PermuteSystems Swap SymmetricProjection |
Function category | Permutations and symmetry of subsystems |
SwapOperator is a function that returns a unitary operator that swaps two subsystems. The unitary can be chosen to be full or sparse.
Syntax
- S = SwapOperator(DIM)
- S = SwapOperator(DIM,SP)
Argument descriptions
- DIM: A specification of the dimensions of the subsystems that S will act on. DIM can be provided in one of two ways:
- If DIM is a scalar, then both subsystems are assumed to have dimension DIM.
- If the subsystems have unequal dimensions, DIM should be a 1-by-2 vector containing their dimensions.
- SP (optional, default 0): If set equal to 1, the swap operator is sparse. If equal to 0, the swap operator is full.
Examples
The standard swap operator
The following code produces the operator that swaps two qubits:
>> SwapOperator(2) ans = 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1
Source code
Click on "expand" to the right to view the MATLAB source code for this function.
%% SWAPOPERATOR Produces a unitary operator that swaps two subsystems
% This function has one required argument:
% DIM: the dimensions of the subsystems
%
% S = SwapOperator(DIM) is the unitary operator that swaps two copies of
% DIM-dimensional space. If the two subsystems are not of the same
% dimension, DIM should be a 1-by-2 vector containing the dimension of
% the subsystems.
%
% This function has one optional argument:
% SP (default 0)
%
% S = SwapOperator(DIM,SP) is as above, but the swap operator produced is
% sparse if SP = 1 and is full if SP = 0.
%
% URL: http://www.qetlab.com/SwapOperator
% requires: iden.m, opt_args.m, PermuteSystems.m, Swap.m
% author: Nathaniel Johnston (nathaniel@njohnston.ca)
% package: QETLAB
% last updated: November 12, 2014
function S = SwapOperator(dim,varargin)
% set optional argument defaults: sp=0
[sp] = opt_args({ 0 },varargin{:});
% allow the user to enter a single number for dim
if(length(dim)==1)
dim = [dim,dim];
end
% swap the rows of Id appropriately
S = Swap(iden(prod(dim),sp),[1,2],dim,1);