Lesson 4 of 15
The Toffoli Gate (CCNOT)
The Toffoli Gate
The Toffoli gate (also called CCNOT or controlled-controlled-NOT) is a universal reversible 3-qubit gate. It flips the target qubit if and only if both control qubits are in state .
Truth Table
| (target) | Output | ||
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 ← flipped |
| 1 | 1 | 1 | 0 ← flipped |
State Vector Representation
A 3-qubit system has basis states. We index them as:
So the basis states map to indices:
| Index | State |
|---|---|
| 0 | $ |
| 1 | $ |
| 2 | $ |
| 3 | $ |
| 4 | $ |
| 5 | $ |
| 6 | $ |
| 7 | $ |
The Toffoli gate swaps amplitudes at indices 6 and 7 — it exchanges .
Universality
The Toffoli gate is classically universal: any Boolean function can be computed reversibly using Toffoli gates. Combined with the Hadamard gate, it is also quantum universal.
Self-Inverse
Like CNOT, the Toffoli gate is its own inverse: applying it twice returns to the original state.
Implementation
def toffoli(state):
result = list(state)
result[6], result[7] = state[7], state[6]
return result
Your Task
Implement toffoli(state) for an 8-element state vector that swaps the amplitudes of and .
Python runtime loading...
Loading...
Click "Run" to execute your code.