# cxGate

Controlled X gate (CNOT gate)

Since R2023a

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

## Syntax

``g = cxGate(controlQubit,targetQubit)``

## Description

````g = cxGate(controlQubit,targetQubit)` applies a controlled X gate to a target qubit based on the state of a control qubit and returns a `quantum.gate.SimpleGate` object. If the control qubit is in the $|0〉$ state, then this gate does nothing. If the control qubit is in the $|1〉$ state, then this gate applies a Pauli X gate (`xGate`) to the target qubit. This gate is also known as the controlled NOT gate, and `cxGate` is equivalent to the `cnotGate`. If `controlQubit` and `targetQubit` are vectors of the same length, `cxGate` returns a column vector of gates, where `g(i)` represents a controlled X gate applied to a control qubit with index `controlQubit(i)` and a target qubit with index `targetQubit(i)`. If either `controlQubit` or `targetQubit` 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 `controlQubit` and `targetQubit` must not be the same. ```

example

## Examples

collapse all

Create a controlled X gate that acts on a control qubit with index 1 and a target qubit with index 2.

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

Get the matrix representation of the gate.

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

Create an array of three controlled X gates. The first gate acts on control qubit 1 and target qubit 2, the next gate acts on control qubit 2 and target qubit 3, and the final gate acts on control qubit 3 and target qubit 4.

`g = cxGate(1:3,2:4)`
```g = 3×1 SimpleGate array with gates: Id Gate Control Target 1 cx 1 2 2 cx 2 3 3 cx 3 4 ```

## Input Arguments

collapse all

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

Example: `2`

Example: `6:8`

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

Example: `1`

Example: `3:5`

collapse all

### Matrix Representation of Controlled X Gate

The matrix representation of a controlled X gate applied to control qubit 1 and target qubit 2 is

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

If the control qubit is in the $|0〉$ state, then this gate leaves the target qubit as is. If the control qubit is in the $|1〉$ state, then this gate applies a Pauli X gate (NOT gate) to the target qubit.

## Version History

Introduced in R2023a