CCX gate

The Toffoli gate: matrix, truth table, the ccx() call in Python and JS, the multi-controlled mcx(), and what it costs on IonQ hardware.

The CCX gate, the Toffoli, flips the target qubit when both control qubits are 1\vert 1 \rangle and leaves it unchanged otherwise. It acts on three qubits and takes no parameters. The Circuit panel draws a dot on each control wire joined to an X box on the target wire.

Kets such as 0\vert 0 \rangle name the basis states and rotations are described on the Bloch sphere; the qubits lesson introduces both.

Call

WhereCall
Pythonqc.ccx(control1, control2, target)
JScircuit.ccx(control1, control2, target)
Qiskit classCCXGate
More controlsmcx([controls], target)

mcx takes a list of any number of controls. IonQ backends accept up to seven; more than that fails at submission with TooManyControls, listed in the Error reference.

Matrix

The matrix uses the basis order control1  control2  target\vert \text{control}_1\;\text{control}_2\;\text{target} \rangle from 000\vert 000 \rangle to 111\vert 111 \rangle; CCX is the identity with its last two rows swapped.

CCX=(1000000001000000001000000001000000001000000001000000000100000010)CCX = \begin{pmatrix} 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 \end{pmatrix}

Effect on basis states

Control 1Control 2TargetTarget after
00anyunchanged
01anyunchanged
10anyunchanged
1101
1110

The controls are never changed in the computational basis. With the target held at 0\vert 0 \rangle, CCX writes the AND of the two controls into it, which is what makes it a universal gate for classical logic inside a quantum circuit.

Inverse

CCX is its own inverse. Two CCX gates in a row on the same qubits cancel.

Usage

from qiskit import QuantumCircuit

qc = QuantumCircuit(3, 3)
qc.x(0)
qc.x(1)
qc.ccx(0, 1, 2)
qc.measure([0, 1, 2], [0, 1, 2])
import { QuantumCircuit } from 'qiskit';

const circuit = QuantumCircuit(3, 3);
circuit.x(0);
circuit.x(1);
circuit.ccx(0, 1, 2);
circuit.measure([0, 1, 2], [0, 1, 2]);

Every shot reads 111: both controls are 1, so the target flips.

On IonQ hardware

CCX is not a native gate and costs more than a CX. The textbook decomposition uses six CX gates, against one for a CX. Qollab does not submit those six: a ccx goes to IonQ as a single X gate with two controls, the form IonQ recommends because a circuit expressed in fewer gates leaves its optimiser more room, and IonQ's compiler decides the two-qubit gates from there without publishing how many. See How your circuit is compiled.

Stay in the loop.

Get the latest tutorials, demos, and project showcases straight to your inbox. No noise, just the good stuff.