Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 39 ยท Gates and Circuits

Controlled Gates: CNOT, CZ, SWAP and Toffoli

Understand controlled gates: the CNOT matrix and truth table, CZ, SWAP, Toffoli, building any controlled-U, reversible classical logic and phase kickback, with NumPy code.

Single-qubit gates cannot create entanglement, and they cannot make one qubit depend on another. For that you need gates that act on two or more qubits. The most important family is the controlled gates: "apply this operation to the target qubit only if the control qubit is 1". This lesson builds the standard ones, shows how they implement reversible classical logic, and introduces the surprising effect called phase kickback.

CNOT

The controlled-NOT gate (CNOT, also CX) has a control qubit and a target qubit. If the control is 0, nothing happens. If the control is 1, the target is flipped. As a 4 by 4 matrix on the basis 00, 01, 10, 11, with the control as the first (leftmost) qubit:

Text
[[1, 0, 0, 0],
 [0, 1, 0, 0],
 [0, 0, 0, 1],
 [0, 0, 1, 0]]

On classical bits it is the XOR gate that overwrites the target with control XOR target, but because the control is kept, no information is lost: it is a permutation of the four basis states, hence unitary.

The crucial point is what it does to superpositions. It acts on every term of a superposition separately, so a control in superposition makes the target depend on it, and the result can be entangled.

Python CNOT truth table and superposition control

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The final line produces the first Bell state, the model entangled pair, which you will study in detail in Bell States and Entanglement.

Building any controlled gate

For any single-qubit gate U, the controlled version is the block matrix

CU = [[I, 0], [0, U]]

meaning the identity acts when the control is 0 and U acts when it is 1. In code, np.block([[I, zeros], [zeros, U]]) or, more systematically, kron(P0, I) + kron(P1, U) with projectors P0 = |0><0| and P1 = |1><1|. CNOT is CU with U = X. CZ (controlled-Z) is CU with U = Z and equals diag(1, 1, 1, -1): it flips the sign of |11> only. CZ is symmetric: which qubit you call the control does not matter.

Python A generic controlled-U builder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line is a useful identity: a CNOT is a CZ surrounded by Hadamards on the target. Hardware often provides only one native two-qubit gate (CZ or CNOT or a close relative) and builds the rest.

SWAP

The SWAP gate exchanges the states of two qubits. Its matrix is the permutation 00 -> 00, 01 -> 10, 10 -> 01, 11 -> 11. It can be built from three CNOTs with alternating control and target:

SWAP = CNOT(a, b) CNOT(b, a) CNOT(a, b)

SWAPs are how a compiler moves qubits next to each other on hardware with limited connectivity, and every one of them costs three two-qubit gates, which is why connectivity matters.

Toffoli and reversible logic

The Toffoli gate (CCX) has two controls and one target: it flips the target only if both controls are 1. It is a 8 by 8 matrix equal to the identity except for swapping the last two basis states (110 and 111). Toffoli is universal for classical reversible computation:

  • With the target starting at 0, the output is a AND b.
  • With both controls fixed at 1, it is NOT.
  • With one control fixed at 1, it is CNOT, and so XOR.

So any classical program can be run on a quantum computer, at the price of extra ancilla (helper) qubits that hold the intermediate results. This is how quantum algorithms embed classical functions such as "is this input a solution?", which you will see in the algorithms chapter.

Python Toffoli as a reversible AND gate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Phase kickback

Controlled gates have a famous side effect. If the target is an eigenvector of U with eigenvalue e^(i*phi), then applying controlled-U leaves the target unchanged but multiplies the |1> branch of the control by e^(i*phi). The phase "kicks back" onto the control. Take control |+> and target |1> with U = Z: Z|1> = -|1>, so the control becomes |->.

Phase kickback is the engine of nearly every quantum algorithm: it turns an invisible property of a function (an eigenvalue) into a visible phase on a control register, which a final Hadamard converts to a measurable bit.

Python Phase kickback turns |+> into |->

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Controlled-NOT is not "if the control is 0 or 1, set the target". It does not read the control. If you measured the control, you would destroy the superposition and the entanglement it creates.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does CNOT do to the basis state |10> (control first)?

    The control is 1, so the target flips from 0 to 1, giving |11>.
  2. 2. Which classical function does a Toffoli gate with the target initially 0 compute?

    The target flips only when both controls are 1, so it ends as a AND b.
  3. 3. How many CNOT gates build a SWAP?

    SWAP equals three CNOTs with alternating control and target.
  4. 4. In phase kickback, the phase from the target's eigenvalue ends up on:

    Controlled-U multiplies the |1> branch of the control by the eigenvalue of U, leaving the target unchanged.

Summary

  • A controlled gate applies U to the target only on the control's |1> branch: CU = |0><0| (x) I + |1><1| (x) U.
  • CNOT creates entanglement when the control is in superposition; CZ is symmetric; (I x H) CZ (I x H) = CNOT.
  • SWAP equals three CNOTs; Toffoli gives reversible AND.
  • Phase kickback moves a target eigenvalue onto the control.

Next: Building a Circuit Simulator