Exam Prep — Matrix ↔ Circuit
Given a circuit, find its matrix. Given a matrix, find its circuit.
Method
Two directions

Circuit → Matrix:

  1. For gates acting in parallel on different qubits at the same time step: take the tensor product (\(\otimes\)). Top qubit is the left factor.
  2. For gates acting sequentially: multiply the matrices. The gate closest to the input (leftmost in the circuit) goes on the right of the product.
  3. Combined: if step 1 is \(U_1\) and step 2 is \(U_2\), the total circuit matrix is \(U_2 \cdot U_1\) (right-to-left).

Matrix → Circuit:

  1. Check if the matrix matches a known gate (CNOT, CZ, SWAP, etc.).
  2. Check if it's a tensor product of two single-qubit gates: does it have the block structure \(A \otimes B\)?
  3. If neither, try to decompose it as a product of known gate matrices.

Key rule: right-to-left. In a circuit drawn left-to-right, the first gate applied is on the right in the matrix product. This is the #1 source of exam errors.

Gate Matrix Reference
Single-qubit gates (2×2)

$$I = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} \qquad X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \qquad Y = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix} \qquad Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}$$

$$H = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \qquad S = \begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix} \qquad T = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{pmatrix}$$

Two-qubit gates (4×4)

$$\text{CNOT} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix} \qquad \text{CZ} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{pmatrix}$$

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

CNOT: control = top qubit (q1), target = bottom qubit (q2). The bottom-right 2×2 block is X. If control and target are swapped, the matrix changes — it's NOT the same.

Tensor Product Computation

For two 2×2 matrices \(A\) and \(B\), the tensor product \(A \otimes B\) is a 4×4 matrix with block structure:

$$A \otimes B = \begin{pmatrix} a_{11}B & a_{12}B \\ a_{21}B & a_{22}B \end{pmatrix} = \begin{pmatrix} a_{11}b_{11} & a_{11}b_{12} & a_{12}b_{11} & a_{12}b_{12} \\ a_{11}b_{21} & a_{11}b_{22} & a_{12}b_{21} & a_{12}b_{22} \\ a_{21}b_{11} & a_{21}b_{12} & a_{22}b_{11} & a_{22}b_{12} \\ a_{21}b_{21} & a_{21}b_{22} & a_{22}b_{21} & a_{22}b_{22} \end{pmatrix}$$

Each entry of \(A\) becomes a 2×2 block by multiplying it with the entire matrix \(B\).

Tensor product example — \(H \otimes I\)

Step 1 — Write out the block structure

$$H \otimes I = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 \cdot I & 1 \cdot I \\ 1 \cdot I & (-1) \cdot I \end{pmatrix}$$

Step 2 — Expand each block

$$= \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1 \end{pmatrix}$$

Useful identity: \((A \otimes B)(C \otimes D) = (AC) \otimes (BD)\). This is the mixed product property — it lets you multiply tensor products without expanding to 4×4 first.

Worked Examples

Example 1 — Parallel gates, then sequential

Circuit: Apply H on qubit 1 and I on qubit 2 (first step), then apply X on qubit 1 and I on qubit 2 (second step). Find the total matrix.

Step 1 — First time step: parallel gates

H on q1, I on q2  →  tensor product: \(H \otimes I\).

$$H \otimes I = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1 \end{pmatrix}$$

Step 2 — Second time step: parallel gates

X on q1, I on q2  →  tensor product: \(X \otimes I\).

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

Step 3 — Combine (right-to-left)

Second step acts after first, so total matrix = \((X \otimes I)(H \otimes I)\).

Using the mixed product property: \((X \otimes I)(H \otimes I) = (XH) \otimes (I \cdot I) = XH \otimes I\).

$$XH = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & -1 \\ 1 & 1 \end{pmatrix}$$

$$\text{Total} = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \end{pmatrix}$$

Example 2 — CNOT followed by H ⊗ I

Circuit: First apply CNOT (control q1, target q2), then apply H on q1 and I on q2. Find the total matrix.

Step 1 — Identify gate matrices

First gate: \(\text{CNOT} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix}\)

Second gate: \(H \otimes I = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1 \end{pmatrix}\)

Step 2 — Multiply (right-to-left: second × first)

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

Column by column:

$$U = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 \\ 1 & 0 & 0 & -1 \\ 0 & 1 & -1 & 0 \end{pmatrix}$$

Verification

Check: \(U\ket{00} = \frac{1}{\sqrt{2}}(\ket{00}+\ket{10})\). This is correct — CNOT does nothing to \(\ket{00}\), then H on q1 creates a superposition.

Example 3 — Matrix → Circuit (reverse direction)

Given this 4×4 matrix, identify the circuit:

$$U = \frac{1}{\sqrt{2}}\begin{pmatrix} 0 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & -1 \\ 1 & 0 & -1 & 0 \end{pmatrix}$$

Step 1 — Check if it's a tensor product

For \(A \otimes B\), the top-left 2×2 block should be \(a_{11}B\) and the top-right should be \(a_{12}B\). Here the top-left block is \(\frac{1}{\sqrt{2}}\bigl(\begin{smallmatrix}0&1\\1&0\end{smallmatrix}\bigr)\) and the top-right is \(\frac{1}{\sqrt{2}}\bigl(\begin{smallmatrix}0&1\\1&0\end{smallmatrix}\bigr)\). Same block, so \(a_{11} = a_{12}\). Bottom-left block is the same as top-left, bottom-right has a sign flip. This gives \(a_{21} = a_{11}\), \(a_{22} = -a_{11}\).

So \(A = \frac{1}{\sqrt{2}}\bigl(\begin{smallmatrix}1&1\\1&-1\end{smallmatrix}\bigr) = H\) and \(B = \bigl(\begin{smallmatrix}0&1\\1&0\end{smallmatrix}\bigr) = X\).

Step 2 — Verify

$$H \otimes X = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 \cdot X & 1 \cdot X \\ 1 \cdot X & (-1) \cdot X \end{pmatrix} = \frac{1}{\sqrt{2}}\begin{pmatrix} 0 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & -1 \\ 1 & 0 & -1 & 0 \end{pmatrix} \checkmark$$

Answer

The circuit is: H on qubit 1, X on qubit 2 (applied in parallel, single time step).

Watch Out
Common pitfalls
  1. Multiplication order is right-to-left. If the circuit applies gate A then gate B, the matrix is \(B \cdot A\), not \(A \cdot B\). The gate closest to the input goes on the right.
  2. Tensor product order matters. Top qubit (q1) is the left factor. \(H \otimes I\) means H on q1 and I on q2. Swapping gives a different 4×4 matrix. \(I \otimes H \neq H \otimes I\).
  3. CNOT control/target changes the matrix. CNOT with control=q1, target=q2 has X in the bottom-right block. Swapping control and target gives a different matrix (X in off-diagonal blocks in a different pattern). Know which convention is used.
  4. \(1/\sqrt{2}\) factors accumulate. Two Hadamards multiply: \(\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}} = \frac{1}{2}\). Track scalar prefactors carefully — losing a \(\frac{1}{\sqrt{2}}\) is a common error.
  5. Tensor product vs matrix multiplication. Parallel gates (same time step, different qubits) → \(\otimes\). Sequential gates (different time steps) → matrix multiply. Don't mix them up.
Practice
Problem 1

Circuit: Z on qubit 1, X on qubit 2 (parallel, single time step). What is the 4×4 matrix?

Solution

Parallel gates on different qubits → tensor product. Top qubit is left factor.

$$Z \otimes X = \begin{pmatrix} 1 \cdot X & 0 \cdot X \\ 0 \cdot X & (-1) \cdot X \end{pmatrix} = \begin{pmatrix} 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & -1 \\ 0 & 0 & -1 & 0 \end{pmatrix}$$

Problem 2

Circuit: First apply \(H \otimes H\) (Hadamard on both qubits), then apply CNOT (control q1, target q2). What is the total matrix?

Solution

First, compute \(H \otimes H\):

$$H \otimes H = \frac{1}{2}\begin{pmatrix} 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 \\ 1 & 1 & -1 & -1 \\ 1 & -1 & -1 & 1 \end{pmatrix}$$

Right-to-left: CNOT acts second, so total = \(\text{CNOT} \cdot (H \otimes H)\).

$$U = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix} \cdot \frac{1}{2}\begin{pmatrix} 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 \\ 1 & 1 & -1 & -1 \\ 1 & -1 & -1 & 1 \end{pmatrix}$$

CNOT leaves the first two rows unchanged and swaps the last two rows:

$$U = \frac{1}{2}\begin{pmatrix} 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 \\ 1 & -1 & -1 & 1 \\ 1 & 1 & -1 & -1 \end{pmatrix}$$

Problem 3

Identify the circuit for this matrix:

$$U = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \end{pmatrix}$$

Solution

Check known gates. Compare with CNOT:

$$\text{CNOT}_{12} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix}$$

Not the same — in our matrix, the swap happens in rows 1–2 (middle), not rows 2–3 (bottom).

This is CNOT with control = q2, target = q1 (reversed control). When q2 = 1 (i.e., basis states \(\ket{01}\) and \(\ket{11}\)), the target q1 gets flipped:

\(\ket{01} \to \ket{11}\), \(\ket{11} \to \ket{01}\). Check the matrix: column 2 (index 01) maps to row 4 (index 11), and column 4 (index 11) maps to row 2 (index 01). Correct.

Answer: CNOT with control = q2 (bottom), target = q1 (top).

Problem 4

Circuit: Apply H on qubit 1, I on qubit 2 (first step), then apply CZ (second step). What is the total matrix?

Solution

First step: \(H \otimes I = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1 \end{pmatrix}\)

Second step: \(\text{CZ} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{pmatrix}\)

Total = \(\text{CZ} \cdot (H \otimes I)\). CZ only negates row 4 (the \(\ket{11}\) row):

$$U = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 0 & -1 & 0 \\ 0 & -1 & 0 & 1 \end{pmatrix}$$

Problem 5

Identify the circuit for this matrix:

$$U = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 0 & 1 & 0 & -1 \\ 1 & 0 & -1 & 0 \end{pmatrix}$$

Solution

Try to decompose. Check if it's a tensor product: top-left block is \(\frac{1}{\sqrt{2}}\bigl(\begin{smallmatrix}1&0\\0&1\end{smallmatrix}\bigr)\), top-right is \(\frac{1}{\sqrt{2}}\bigl(\begin{smallmatrix}1&0\\0&1\end{smallmatrix}\bigr)\). Both are \(\frac{1}{\sqrt{2}}I\).

Bottom-left: \(\frac{1}{\sqrt{2}}\bigl(\begin{smallmatrix}0&1\\1&0\end{smallmatrix}\bigr)\), bottom-right: \(\frac{1}{\sqrt{2}}\bigl(\begin{smallmatrix}0&-1\\-1&0\end{smallmatrix}\bigr)\). Bottom-right = \(-1 \times\) bottom-left.

So \(A = \frac{1}{\sqrt{2}}\bigl(\begin{smallmatrix}1&1\\0&0\end{smallmatrix}\bigr)\)? No — that's not unitary. The blocks don't factor as a clean tensor product.

Instead, try known decompositions. Notice this maps \(\ket{00} \to \frac{1}{\sqrt{2}}(\ket{00}+\ket{10})\) and \(\ket{10} \to \frac{1}{\sqrt{2}}(\ket{00}-\ket{10})\) — that's H on qubit 1. But \(\ket{01} \to \frac{1}{\sqrt{2}}(\ket{01}+\ket{11})\) while \(\ket{11} \to \frac{1}{\sqrt{2}}(\ket{01}-\ket{11})\) — also H on qubit 1. Rows 3–4 are swapped compared to \(H \otimes I\).

Compare with Example 2: \((H \otimes I) \cdot \text{CNOT}\):

$$\frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 \\ 1 & 0 & 0 & -1 \\ 0 & 1 & -1 & 0 \end{pmatrix}$$

Not the same. Try \(\text{CNOT} \cdot (H \otimes I)\) (H first, then CNOT):

CNOT swaps the last two rows of \(H \otimes I\):

$$\frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 0 & 1 & 0 & -1 \\ 1 & 0 & -1 & 0 \end{pmatrix} \checkmark$$

Answer: H on qubit 1 (first step), then CNOT with control = q1, target = q2 (second step).