Main Content

ccxGate

Controlled controlled X gate (CCNOT or Toffoli gate)

Since R2023a

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

Description

g = ccxGate(controlQubit1,controlQubit2,targetQubit) applies a controlled controlled X gate to a target qubit based on the state of two control qubits and returns a quantum.gate.SimpleGate object. If one or both control qubits are in the |0 state, then this gate does nothing. If both control qubits are in the |1 state, then this gate applies the Pauli X gate (xGate) to the target qubit. This gate is also known as the CCNOT gate or Toffoli gate.

  • If controlQubit1, controlQubit2, and targetQubit are vectors of the same length, ccxGate returns a column vector of gates, where g(i) represents a controlled controlled X gate applied to the ith element triplet of controlQubit1, controlQubit2, and targetQubit.

  • If any of controlQubit1, controlQubit2, or targetQubit is a scalar, and at least one of the inputs is a vector, then MATLAB® expands the scalar to match the size of the vector input.

  • The qubit indices in each element triplet of controlQubit1, controlQubit2, and targetQubit must not be the same.

example

Examples

collapse all

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

g = ccxGate(1,2,3)
g = 

  SimpleGate with properties:

             Type: "ccx"
    ControlQubits: [1 2]
     TargetQubits: 3
           Angles: [1×0 double]

Get the matrix representation of the gate.

M = getMatrix(g)
M =

     1     0     0     0     0     0     0     0
     0     1     0     0     0     0     0     0
     0     0     1     0     0     0     0     0
     0     0     0     1     0     0     0     0
     0     0     0     0     1     0     0     0
     0     0     0     0     0     1     0     0
     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     1     0

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

g = ccxGate(1:3,2:4,3:5)
g = 

  3×1 SimpleGate array with gates:

    Id   Gate   Control   Target
     1   ccx    [1,2]     3     
     2   ccx    [2,3]     4     
     3   ccx    [3,4]     5     

Input Arguments

collapse all

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

Example: 3

Example: 4:6

Second 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 Controlled X Gate

The matrix representation of a controlled controlled X gate applied to two control qubits (with index 1 and 2) and a target qubit (with index 3) is

[1000000001000000001000000001000000001000000001000000000100000010].

Version History

Introduced in R2023a