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 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 name the basis states and rotations are described on the Bloch sphere; the qubits lesson introduces both.
Call
| Where | Call |
|---|---|
| Python | qc.ccx(control1, control2, target) |
| JS | circuit.ccx(control1, control2, target) |
| Qiskit class | CCXGate |
| More controls | mcx([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 from to ; CCX is the identity with its last two rows swapped.
Effect on basis states
| Control 1 | Control 2 | Target | Target after |
|---|---|---|---|
| 0 | 0 | any | unchanged |
| 0 | 1 | any | unchanged |
| 1 | 0 | any | unchanged |
| 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 0 |
The controls are never changed in the computational basis. With the target held at , 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.
Related
Stay in the loop.
Get the latest tutorials, demos, and project showcases straight to your inbox. No noise, just the good stuff.
Gates