16 — Multi-Qubit Gates
CNOT, controlled gates, CZ, SWAP — the gates that create entanglement and enable computation
Why This Matters

Single-qubit gates rotate one qubit on its Bloch sphere. That's useful, but it can't create entanglement — and without entanglement, there's no quantum advantage. To build anything beyond trivial, you need gates that act on two or more qubits, making one qubit's transformation depend on another qubit's state.

The workhorse is the CNOT gate (Controlled-NOT). Together with single-qubit gates, it forms a universal gate set — meaning any quantum computation can be built from these pieces. This page covers CNOT in depth, then generalizes to controlled-U gates, CZ, and SWAP.

The punchline

A controlled gate applies a single-qubit gate \(U\) to a target qubit, but only when the control qubit is \(\ket{1}\). This conditional behavior is what creates correlations between qubits — the mechanism behind entanglement.

Controlled-U Gates

A controlled-U gate applies a single-qubit gate \(U\) to a target qubit, but only when the control qubit is \(\ket{1}\). This is the general pattern behind all two-qubit conditional gates.

Controlled-U definition

$$C(U) = \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes U$$

Action: \(C(U)\ket{0}\ket{\psi} = \ket{0}\ket{\psi}\) and \(C(U)\ket{1}\ket{\psi} = \ket{1}U\ket{\psi}\).

The projectors \(\ket{0}\bra{0}\) and \(\ket{1}\bra{1}\) pick out the control qubit's state. If control is \(\ket{0}\), do nothing (\(I\)). If control is \(\ket{1}\), apply \(U\).

C(X) vs C(\(-X\)) — global phase matters here!

Recall that \(X\) and \(-X\) are the same gate up to global phase — they produce identical measurement outcomes on any state. But \(C(X) \neq C(-X)\)!

$$C(-X) = \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes (-X) = \ket{0}\bra{0} \otimes I - \ket{1}\bra{1} \otimes X$$

The minus sign only appears in the \(\ket{1}\)-controlled block, so it's a relative phase between the \(\ket{0}\) and \(\ket{1}\) branches — physically observable. Global phases on individual gates become relative phases when those gates are controlled.

The CNOT Gate — C(X)

The most important controlled gate: CNOT is \(C(X)\), applying X (bit-flip) to the target when the control is \(\ket{1}\).

CNOT derived from C(U)

Plug \(U = X\) into the controlled-U action rules:

$$C(X)\ket{0}\ket{\psi} = \ket{0}\ket{\psi} \qquad \text{(control is 0 → do nothing)}$$

$$C(X)\ket{1}\ket{\psi} = \ket{1}X\ket{\psi} \qquad \text{(control is 1 → flip target)}$$

Apply to all four basis states:

$$C(X)\ket{0}\ket{0} = \ket{0}\ket{0} = \ket{00}$$

$$C(X)\ket{0}\ket{1} = \ket{0}\ket{1} = \ket{01}$$

$$C(X)\ket{1}\ket{0} = \ket{1}X\ket{0} = \ket{1}\ket{1} = \ket{11}$$

$$C(X)\ket{1}\ket{1} = \ket{1}X\ket{1} = \ket{1}\ket{0} = \ket{10}$$

In short: \(\text{CNOT}\ket{a,b} = \ket{a, b \oplus a}\), where \(\oplus\) is XOR (addition mod 2). The same pattern works for any gate: \(C(Z)\) is CZ, \(C(S)\) is CS, etc.

The CNOT matrix

$$\text{CNOT} = \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes X = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix}$$

Read the columns: column \(j\) tells you where basis state \(\ket{j}\) goes. Columns 0 and 1 are unchanged (\(\ket{00} \to \ket{00}\), \(\ket{01} \to \ket{01}\)). Columns 2 and 3 are swapped (\(\ket{10} \to \ket{11}\), \(\ket{11} \to \ket{10}\)).

Worked Example: Verify the outer product form gives the matrix

Recall \(\ket{0}\bra{0} = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}\) and \(\ket{1}\bra{1} = \begin{pmatrix} 0 & 0 \\ 0 & 1 \end{pmatrix}\).

$$\ket{0}\bra{0} \otimes I = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix} \otimes \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{pmatrix}$$

$$\ket{1}\bra{1} \otimes X = \begin{pmatrix} 0 & 0 \\ 0 & 1 \end{pmatrix} \otimes \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} = \begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix}$$

$$\text{Sum} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix} = \text{CNOT} \quad\checkmark$$

CNOT is its own inverse

Apply CNOT twice: \(\text{CNOT}^2\ket{a,b} = \text{CNOT}\ket{a, b \oplus a} = \ket{a, b \oplus a \oplus a} = \ket{a,b}\). Since \(a \oplus a = 0\), the second application undoes the first. In matrix language: \(\text{CNOT}^\dagger = \text{CNOT}\) (the gate is Hermitian). This also means it's unitary: \(\text{CNOT}^\dagger \cdot \text{CNOT} = \text{CNOT}^2 = I\).

The CZ Gate — C(Z)

CZ (Controlled-Z) applies a Z gate to the target when the control is \(\ket{1}\). Since \(Z\ket{0} = \ket{0}\) and \(Z\ket{1} = -\ket{1}\), the only basis state that gets a sign flip is \(\ket{11}\).

CZ action on basis states

$$\text{CZ}\ket{00} = \ket{00} \qquad \text{CZ}\ket{01} = \ket{01} \qquad \text{CZ}\ket{10} = \ket{10} \qquad \text{CZ}\ket{11} = -\ket{11}$$

Control is 0? Do nothing. Control is 1? Apply Z to target — but Z only affects \(\ket{1}\), so only \(\ket{11}\) gets a sign flip.

$$\text{CZ} = C(Z) = \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes Z = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{pmatrix}$$

CZ is symmetric

Unlike CNOT, the CZ gate is symmetric in control and target. It doesn't matter which qubit you call the control — the matrix is the same either way. You can verify: \(\ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes Z = I \otimes \ket{0}\bra{0} + Z \otimes \ket{1}\bra{1}\). Both give the same diagonal matrix.

In circuit diagrams, CZ is drawn with dots on both wires (no target arrow) to emphasize this symmetry.

Worked Example 2: CZ on a superposition

Apply CZ to \(\ket{+}\ket{+} = \frac{1}{2}(\ket{00} + \ket{01} + \ket{10} + \ket{11})\):

$$\text{CZ}\ket{+}\ket{+} = \frac{1}{2}(\ket{00} + \ket{01} + \ket{10} - \ket{11})$$

Only the \(\ket{11}\) term got a minus sign. Check the entanglement test: \(ad - bc = \frac{1}{2} \cdot (-\frac{1}{2}) - \frac{1}{2} \cdot \frac{1}{2} = -\frac{1}{2} \neq 0\). The output is entangled. CZ can create entanglement, just like CNOT.

Phase Kickback

Here's something counterintuitive: in a controlled gate, sometimes the control qubit changes while the target stays the same. This is called phase kickback.

Worked Example: CZ on \(\ket{+} \otimes \ket{1}\)

Start with \(\ket{+} \otimes \ket{1}\). The control is in superposition, the target is \(\ket{1}\).

Step 1: Expand into Dirac terms:

$$\ket{+} \otimes \ket{1} = \frac{1}{\sqrt{2}}(\ket{0} + \ket{1}) \otimes \ket{1} = \frac{1}{\sqrt{2}}(\ket{01} + \ket{11})$$

Step 2: Apply CZ to each term:

$$\frac{1}{\sqrt{2}}(\ket{01} + \ket{11}) \xrightarrow{\text{CZ}} \frac{1}{\sqrt{2}}(\ket{01} - \ket{11})$$

The \(\ket{01}\) term: not both 1, unchanged. The \(\ket{11}\) term: both 1, gets −1.

Step 3: Factor back:

$$\frac{1}{\sqrt{2}}(\ket{01} - \ket{11}) = \frac{1}{\sqrt{2}}(\ket{0} - \ket{1}) \otimes \ket{1} = \ket{-} \otimes \ket{1}$$

What just happened

The target (\(\ket{1}\)) is completely unchanged. The control went from \(\ket{+}\) to \(\ket{-}\).

Why: \(\ket{1}\) is an eigenstate of Z with eigenvalue −1. From the target's perspective, it just picks up a "global" phase of −1. But because the gate is controlled, that −1 only applies to the \(\ket{1}\) branch of the control — making it a relative phase. The eigenvalue "kicked back" onto the control qubit.

No kickback when target is \(\ket{0}\): Z's eigenvalue on \(\ket{0}\) is +1 — nothing to kick back.

Phase kickback is the engine behind quantum algorithms. The target encodes a function's answer in its eigenvalue, and the control extracts it as a detectable phase shift — without ever reading the target directly.

The SWAP Gate

SWAP exchanges the states of two qubits: \(\text{SWAP}\ket{a,b} = \ket{b,a}\).

SWAP action on basis states

$$\text{SWAP}\ket{00} = \ket{00} \qquad \text{SWAP}\ket{01} = \ket{10} \qquad \text{SWAP}\ket{10} = \ket{01} \qquad \text{SWAP}\ket{11} = \ket{11}$$

Same values stay the same (\(\ket{00}, \ket{11}\)). Different values get swapped (\(\ket{01} \leftrightarrow \ket{10}\)).

$$\text{SWAP} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}$$

SWAP from three CNOTs

SWAP can be decomposed into three CNOT gates. This is important because hardware typically implements CNOT natively, not SWAP.

The key: these are not three identical CNOTs (that would just give one CNOT, since CNOT is its own inverse). The middle CNOT has control and target reversed:

$$\text{SWAP} = \underbrace{\text{CNOT}_{1 \to 2}}_{\text{qubit 1 controls 2}} \cdot \underbrace{\text{CNOT}_{2 \to 1}}_{\text{qubit 2 controls 1}} \cdot \underbrace{\text{CNOT}_{1 \to 2}}_{\text{qubit 1 controls 2}}$$

The pattern is: forward → backward → forward. The middle step sends information back from qubit 2 to qubit 1 — without it, information can only flow one direction.

Worked Example: Trace SWAP decomposition on \(\ket{10}\)

We want \(\text{SWAP}\ket{10} = \ket{01}\). Trace through all three steps:

$$\ket{10} \xrightarrow{\text{CNOT}_{1 \to 2}} \ket{11} \xrightarrow{\text{CNOT}_{2 \to 1}} \ket{01} \xrightarrow{\text{CNOT}_{1 \to 2}} \ket{01}$$

  1. CNOT\(_{1 \to 2}\): Qubit 1 is control = 1, so flip qubit 2: \(\ket{10} \to \ket{11}\)
  2. CNOT\(_{2 \to 1}\): Qubit 2 is control = 1, so flip qubit 1: \(\ket{11} \to \ket{01}\)
  3. CNOT\(_{1 \to 2}\): Qubit 1 is control = 0, so do nothing: \(\ket{01} \to \ket{01}\)

Result: \(\ket{10} \to \ket{01}\). Swapped. ✓

Gate Ordering — When Do Gates Commute?

In a quantum circuit, gates on different qubits always commute. Gates on the same qubit generally do not.

Commutativity rule

If gate \(A\) acts on qubit 1 and gate \(B\) acts on qubit 2, then:

$$(A \otimes I)(I \otimes B) = A \otimes B = (I \otimes B)(A \otimes I)$$

The order doesn't matter — you can apply them in either order or simultaneously. This follows directly from the mixed-product rule.

But if both gates act on the same qubit: \(AB \neq BA\) in general. For example, \(XZ = iY\) but \(ZX = -iY\).

Worked Example 4: Parallel gates

A circuit applies H to qubit 1 and X to qubit 2. The combined operation is:

$$(H \otimes I)(I \otimes X) = H \otimes X$$

$$(I \otimes X)(H \otimes I) = H \otimes X$$

Same result either way. In circuit notation, these gates can be drawn in the same time step (same column) because they act on different qubits.

Circuit Identity: Controlled \(V^\dagger U V\)

A useful circuit identity lets you implement a controlled version of \(V^\dagger U V\) without directly constructing that unitary:

The conjugation identity

$$C(V^\dagger U V) = (I \otimes V^\dagger) \cdot C(U) \cdot (I \otimes V)$$

To apply a controlled \(V^\dagger U V\): first apply \(V\) to the target, then the controlled-\(U\), then \(V^\dagger\) to the target. The \(V\) and \(V^\dagger\) are unconditional (applied regardless of the control qubit).

Why this works

When the control is \(\ket{0}\): the middle gate does nothing, so the target gets \(V^\dagger \cdot I \cdot V = V^\dagger V = I\). No net effect.

When the control is \(\ket{1}\): the middle gate applies \(U\), so the target gets \(V^\dagger \cdot U \cdot V = V^\dagger U V\). Exactly what we wanted.

Worked Example 5: Building C(HXH) = C(Z)

We know that \(HXH = Z\) (conjugating X by H gives Z). So \(C(Z) = C(HXH)\):

$$C(Z) = (I \otimes H) \cdot \text{CNOT} \cdot (I \otimes H)$$

Apply H to the target, then CNOT, then H to the target again. This gives CZ using only CNOT and single-qubit H gates — useful when your hardware only has CNOT as its native two-qubit gate.

Summary
What you need to know