Main Content

cnotGate

CNOT gate (controlled X gate)

Since R2023a

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

Description

example

g = cnotGate(controlQubit,targetQubit) applies a controlled NOT 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 or NOT gate (xGate) to the target qubit. This gate is also known as the controlled X gate, and cnotGate is equivalent to the cxGate.

  • If controlQubit and targetQubit are vectors of the same length, cnotGate returns a column vector of gates, where g(i) represents a controlled NOT 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.

Examples

collapse all

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

g = cnotGate(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 NOT 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 = cnotGate(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

More About

collapse all

Matrix Representation of Controlled NOT Gate

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

[1000010000010010].

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