|q⟩ Bad Qubits

beginner · Physics · Math Foundations: Linear Algebra

Matrix Multiplication

In quantum mechanics every operation on a state vector — every quantum gate, every time-evolution operator, every measurement projector — is a matrix. To apply two operations in sequence you multiply their matrices. Matrix multiplication is therefore not optional bookkeeping; it is the language in which quantum dynamics is written.

The rule

Given two 2×22 \times 2 matrices

A=(a00a01a10a11),B=(b00b01b10b11),A = \begin{pmatrix} a_{00} & a_{01} \\ a_{10} & a_{11} \end{pmatrix}, \qquad B = \begin{pmatrix} b_{00} & b_{01} \\ b_{10} & b_{11} \end{pmatrix},

their product C=ABC = AB is the 2×22 \times 2 matrix whose (i,j)(i,j) entry is

cij=kaikbkj.c_{ij} = \sum_{k} a_{ik}\, b_{kj}.

In words: take row ii of AA and column jj of BB, multiply them term by term, and sum the results. This is the dot product of a row with a column.

Writing it out for a 2×22 \times 2 product:

AB=(a00b00+a01b10a00b01+a01b11a10b00+a11b10a10b01+a11b11).AB = \begin{pmatrix} a_{00}b_{00} + a_{01}b_{10} & a_{00}b_{01} + a_{01}b_{11} \\ a_{10}b_{00} + a_{11}b_{10} & a_{10}b_{01} + a_{11}b_{11} \end{pmatrix}.

Worked example: the Hadamard gate squared

The Hadamard matrix is

H=12(1111).H = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}.

Computing H2=HHH^2 = H \cdot H:

H2=12(11+1111+1(1)11+(1)111+(1)(1))=12(2002)=(1001)=I.H^2 = \frac{1}{2}\begin{pmatrix} 1\cdot 1 + 1\cdot 1 & 1\cdot 1 + 1\cdot(-1) \\ 1\cdot 1 + (-1)\cdot 1 & 1\cdot 1 + (-1)\cdot(-1) \end{pmatrix} = \frac{1}{2}\begin{pmatrix} 2 & 0 \\ 0 & 2 \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} = I.

So H2=IH^2 = I: applying the Hadamard gate twice brings a qubit back to its original state. This identity, which follows purely from matrix multiplication, tells you that HH is its own inverse.

Order matters: AB does not equal BA in general

Consider the Pauli matrices

X=(0110),Z=(1001).X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}, \qquad Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}.

Multiplying XZXZ:

XZ=(01+1000+1(1)11+0010+0(1))=(0110).XZ = \begin{pmatrix} 0\cdot 1 + 1\cdot 0 & 0\cdot 0 + 1\cdot(-1) \\ 1\cdot 1 + 0\cdot 0 & 1\cdot 0 + 0\cdot(-1) \end{pmatrix} = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}.

Multiplying ZXZX:

ZX=(10+0111+0000+(1)101+(1)0)=(0110).ZX = \begin{pmatrix} 1\cdot 0 + 0\cdot 1 & 1\cdot 1 + 0\cdot 0 \\ 0\cdot 0 + (-1)\cdot 1 & 0\cdot 1 + (-1)\cdot 0 \end{pmatrix} = \begin{pmatrix} 0 & 1 \\ -1 & 0 \end{pmatrix}.

XZZXXZ \neq ZX: the product depends on the order. In quantum computing this non-commutativity is not an annoyance but a fundamental physical fact — it is the algebraic origin of the uncertainty principle.

How matrix products act on state vectors

A matrix acts on a column vector by the same row-times-column rule. For a 2×22 \times 2 matrix MM and a two-component ket

Mψ=(m00m01m10m11)(αβ)=(m00α+m01βm10α+m11β).M|\psi\rangle = \begin{pmatrix} m_{00} & m_{01} \\ m_{10} & m_{11} \end{pmatrix} \begin{pmatrix} \alpha \\ \beta \end{pmatrix} = \begin{pmatrix} m_{00}\alpha + m_{01}\beta \\ m_{10}\alpha + m_{11}\beta \end{pmatrix}.

This is just matrix multiplication with the vector treated as a single-column matrix. Everything you practice here carries over directly to applying gates in quantum circuits.

Try it

This is a numerical exercise — your code should return a number, not a circuit. Compute the (0,1)(0,1) entry — that is, row 0, column 1 — of the product XZXZ, where

X=(0110),Z=(1001).X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}, \qquad Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}.
Run your code to see the quantum state.

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