# swapGate

Swap gate

Since R2023a

Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.

## Syntax

``g = swapGate(targetQubit1,targetQubit2)``

## Description

example

````g = swapGate(targetQubit1,targetQubit2)` applies a swap gate that swaps the states of two target qubits and returns a `quantum.gate.SimpleGate` object. If `targetQubit1` and `targetQubit2` are vectors of the same length, then `swapGate` returns a column vector of gates, where `g(i)` represents a swap gate applied to two target qubits with indices `targetQubit1(i)` and `targetQubit2(i)`. If either `targetQubit1` or `targetQubit2` is a scalar, and the other input is a vector, then MATLAB® expands the scalar to match the size of the vector input.The qubit indices in each element pair of `targetQubit1` and `targetQubit2` must not be the same. ```

## Examples

collapse all

Create a swap gate that acts on two target qubits with indices 1 and 2.

`g = swapGate(1,2)`
```g = SimpleGate with properties: Type: "swap" ControlQubits: [1×0 double] TargetQubits: [1 2] Angles: [1×0 double] ```

Get the matrix representation of the gate.

`M = getMatrix(g)`
```M = 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1```

Create an array of three swap gates. The first gate acts on target qubits 1 and 2, the next gate acts on target qubits 2 and 3, and the final gate acts on target qubits 3 and 4.

`g = swapGate(1:3,2:4)`
```g = 3×1 SimpleGate array with gates: Id Gate Control Target 1 swap [1,2] 2 swap [2,3] 3 swap [3,4] ```

## Input Arguments

collapse all

First target qubit of the gate, specified as a positive integer scalar index or vector of qubit indices.

Example: `1`

Example: `3:5`

Second target qubit of the gate, specified as a positive integer scalar index or vector of qubit indices.

Example: `2`

Example: `6:8`

collapse all

### Matrix Representation of Swap Gate

The matrix representation of a swap gate applied to two target qubits is

`$\left[\begin{array}{cccc}1& 0& 0& 0\\ 0& 0& 1& 0\\ 0& 1& 0& 0\\ 0& 0& 0& 1\end{array}\right].$`

## Version History

Introduced in R2023a