|q⟩ Bad Qubits

beginner · Programming · Controlled Gates: CNOT, CZ, SWAP

CNOT from CZ and Hadamards

The CNOT and CZ gates both entangle two qubits, but they act in different bases. Understanding the relationship between them deepens your intuition for how gate sets can substitute for one another — a key skill in compiling quantum programs.

CZ versus CNOT

The CZ (controlled-Z) gate applies a ZZ gate to the target qubit only when the control qubit is 1|1\rangle. In the two-qubit computational basis {00,01,10,11}\{|00\rangle, |01\rangle, |10\rangle, |11\rangle\} its matrix is diagonal:

CZ=(1000010000100001).CZ = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{pmatrix}.

The only affected state is 11|11\rangle, which acquires a phase of 1-1.

The CNOT (controlled-X) gate flips the target qubit when the control is 1|1\rangle:

CNOT=(1000010000010010).CNOT = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix}.

Connecting them with Hadamards

The Hadamard gate exchanges the ZZ-basis eigenstates and the XX-basis eigenstates: H0=+H|0\rangle = |+\rangle and H1=H|1\rangle = |-\rangle. Because X=HZHX = HZH, applying HH to the target of a CZ converts the controlled-ZZ action into a controlled-XX action. In full:

CNOT=(IH)  CZ  (IH).CNOT = (I \otimes H)\; CZ\; (I \otimes H).

To verify this, trace what happens to each basis state. Take 10|10\rangle as an example:

  1. HH on qubit 1: 101H0=1+=12(10+11)|10\rangle \to |1\rangle \otimes H|0\rangle = |1\rangle|{+}\rangle = \tfrac{1}{\sqrt{2}}(|10\rangle + |11\rangle).
  2. CZ: the 11|11\rangle component picks up 1-1, giving 12(1011)=1\tfrac{1}{\sqrt{2}}(|10\rangle - |11\rangle) = |1\rangle|{-}\rangle.
  3. HH on qubit 1: 1H=11=11|1\rangle \otimes H|{-}\rangle = |1\rangle|1\rangle = |11\rangle.

So 1011|10\rangle \to |11\rangle, which is exactly what CNOT does to this state. You can check 0000|00\rangle \to |00\rangle, 0101|01\rangle \to |01\rangle, and 1110|11\rangle \to |10\rangle in the same way to confirm the full identity.

Try it

Build the CNOT gate for qubit 0 (control) and qubit 1 (target) using only h and cz. The grader checks the complete 4×4 unitary matrix, so an empty circuit will not pass.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.