Toffoli gate as a reversible AND

A gate with two controls computes the AND of two bits while keeping the inputs, which is what makes it reversible.

Intermediate · 3 qubits · 1 gates · 1 two-qubit · depth 1 · reviewed November 2026

Open in the Circuit Builder

What is happening

The Toffoli, or CCX, flips the target only when both controls are 1. With the target starting at 0, the target ends up as the AND of the controls. The inputs remain on their wires, so the operation can be undone.

Reversibility is needed because quantum gates are unitary. Any classical computation can be written with Toffoli gates, which is why quantum computers can run classical logic as a special case.

Step by step

  1. 1. Inputs 1, 1, 0

    Both controls are 1 and the target is 0.

  2. 2. CCX

    Both controls are 1, so the target flips to 1. The output is |111⟩.

Expected result

|111⟩ with 100% probability. Change either control to 0 and the target stays 0.

Qubit 0 is the leftmost bit. In Qiskit, bitstrings are printed in the reverse order.

Qiskit code

from qiskit import QuantumCircuit
from math import pi

qc = QuantumCircuit(3, 3)
qc.x(0)
qc.x(1)
qc.ccx(0, 1, 2)
qc.barrier()
qc.measure(0, 0)
qc.measure(1, 1)
qc.measure(2, 2)

# Qiskit prints bit strings with qubit 0 on the RIGHT; this site writes qubit 0 on the LEFT.

Try this

  • Put H on both controls and see the four-way superposition produce the AND truth table in the amplitudes.
  • Check how many CNOTs and T gates a Toffoli needs on real hardware: six CNOTs and seven T-type gates in the standard decomposition.

Gates used

Related reading

Questions

Why are T gates expensive?

In most error-correction schemes, T gates need extra resources called magic states, so the T count is a key cost measure.