
# Linear Operators & Matrices

> Quantum gates, observables, Hamiltonians, and measurements are all **linear operators**. This
> lesson nails the operator toolkit: how an operator becomes a matrix once you fix a basis, how the
> **adjoint** is defined (it's where "Hermitian" and "unitary" will come from), and the
> basis-independent quantities — **trace** and **determinant** — we rely on constantly.

## Learning Objectives

After this lesson you will be able to:

1. Represent a linear operator as a matrix in a chosen basis and compute its action on kets.
2. Define the **adjoint** $A^\dagger$ via the inner product and show it equals the conjugate transpose.
3. Perform a **change of basis** and recognize similarity transformations.
4. Compute and use the **trace** (cyclicity, basis-independence) and the **determinant**.
5. Evaluate **commutators** $[A,B]$ and explain why they matter physically.

---

## Intuition

A linear operator is a function on vectors that respects addition and scaling: it sends lines to
lines and the origin to the origin. Fix a basis and the operator is completely captured by what it
does to the basis vectors — a finite table of numbers, i.e. a **matrix**. Change the basis and the
*same* operator gets a *different* matrix. The quantities that don't change under such relabeling —
trace, determinant, eigenvalues — are the operator's physical fingerprints.

---

## Theory

### Linear operators

A map $A:\mathcal{H}\to\mathcal{H}$ is a **linear operator** if for all $|\psi\rangle,|\phi\rangle$ and
$a\in\mathbb{C}$,

$$
A\big(a|\psi\rangle + |\phi\rangle\big) = a\,A|\psi\rangle + A|\phi\rangle .
$$

Linearity means $A$ is determined by its action on a basis: if $A|j\rangle = \sum_i A_{ij}|i\rangle$,
then for $|\psi\rangle = \sum_j c_j|j\rangle$,

$$
A|\psi\rangle = \sum_j c_j A|j\rangle = \sum_{i} \Big(\sum_j A_{ij} c_j\Big)|i\rangle .
$$

The coefficients $A_{ij} = \langle i|A|j\rangle$ (from [0.1.3](03-dirac-notation.md)) are the matrix
of $A$; the formula above is matrix–vector multiplication. **Composition is matrix multiplication:**
$(AB)_{ik} = \sum_j A_{ij}B_{jk}$, derived by inserting completeness $A B = A\,I\,B = \sum_j A|j\rangle\langle j|B$.

### The adjoint

The **adjoint** (Hermitian conjugate) $A^\dagger$ is *defined* by the inner-product relation

$$
\boxed{\ \langle\phi|A\psi\rangle = \langle A^\dagger\phi|\psi\rangle \quad\text{for all }|\phi\rangle,|\psi\rangle.\ }
$$

Equivalently $\langle\phi|A|\psi\rangle = \overline{\langle\psi|A^\dagger|\phi\rangle}$. **Claim:** in
any orthonormal basis, the matrix of $A^\dagger$ is the conjugate transpose of the matrix of $A$:
$(A^\dagger)_{ij} = \overline{A_{ji}}$.

**Proof.** $(A^\dagger)_{ij} = \langle i|A^\dagger|j\rangle$. By the defining relation with
$|\phi\rangle = |j\rangle$, $|\psi\rangle = |i\rangle$: $\langle j|A|i\rangle = \langle A^\dagger j|i\rangle
= \overline{\langle i | A^\dagger | j\rangle} = \overline{(A^\dagger)_{ij}}$. Hence $(A^\dagger)_{ij} =
\overline{\langle j|A|i\rangle} = \overline{A_{ji}}$. ∎

Properties (all from the definition): $(A^\dagger)^\dagger = A$, $(aA+B)^\dagger = \overline{a}A^\dagger + B^\dagger$,
$(AB)^\dagger = B^\dagger A^\dagger$, and $\big(|\psi\rangle\langle\phi|\big)^\dagger = |\phi\rangle\langle\psi|$.

These three names — defined next lesson but anchored here — are *adjoint conditions*:
**Hermitian** $A^\dagger = A$, **unitary** $U^\dagger = U^{-1}$, **normal** $A^\dagger A = A A^\dagger$.

### Change of basis

Let $\{|k\rangle\}$ and $\{|k'\rangle\}$ be two orthonormal bases, related by a unitary $S$ with
$|k'\rangle = S|k\rangle$ (columns of $S$ are the new basis vectors in old coordinates). A ket's
coordinate column transforms as $c' = S^\dagger c$, and an operator's matrix transforms by a
**similarity transformation**:

$$
A' = S^\dagger A\, S .
$$

**Derivation.** $A'_{ij} = \langle i'|A|j'\rangle = \langle S i|A|S j\rangle = \langle i|S^\dagger A S|j\rangle = (S^\dagger A S)_{ij}$.
Same operator, new matrix. Quantities preserved by $A\mapsto S^\dagger A S$ are exactly the physical
invariants below.

### Trace

The **trace** is $\operatorname{Tr}(A) = \sum_k A_{kk} = \sum_k\langle k|A|k\rangle$. Key properties:

- **Linear:** $\operatorname{Tr}(aA+B) = a\operatorname{Tr}(A) + \operatorname{Tr}(B)$.
- **Cyclic:** $\operatorname{Tr}(ABC) = \operatorname{Tr}(CAB) = \operatorname{Tr}(BCA)$. (Proof: expand
  indices; $\operatorname{Tr}(AB) = \sum_{i,j}A_{ij}B_{ji} = \operatorname{Tr}(BA)$, then iterate.)
- **Basis-independent:** $\operatorname{Tr}(S^\dagger A S) = \operatorname{Tr}(A S S^\dagger) =
  \operatorname{Tr}(A)$ by cyclicity and $SS^\dagger = I$. (We proved basis-independence directly in
  [0.1.3 E3](03-dirac-notation.md) too.)
- **Equals the sum of eigenvalues** (next lesson).

The trace is how expectation values and probabilities are computed in the density-matrix formalism:
$\langle A\rangle = \operatorname{Tr}(\rho A)$ (Term 1.5).

### Determinant and commutator

The **determinant** $\det A$ is the product of eigenvalues; it is multiplicative
($\det(AB) = \det A\,\det B$), basis-independent ($\det(S^{-1}AS) = \det A$), and $A$ is invertible iff
$\det A \neq 0$. For unitaries $|\det U| = 1$.

The **commutator** $[A,B] = AB - BA$ measures failure to commute; the **anticommutator** is
$\{A,B\} = AB+BA$. Commutators are central in quantum mechanics: observables commute **iff** they are
simultaneously diagonalizable (Term 0.1.5) and can be measured together without disturbance; the
canonical $[\hat x,\hat p] = i\hbar$ gives the uncertainty principle (Term 1.3). For Pauli matrices,
$[X,Y] = 2iZ$ (see [Appendix E](../../appendix/E-math-identities.md)).

---

## Worked Examples

### Example 1 — Building a matrix from its action

Define $A$ on a qubit by $A|0\rangle = |1\rangle$, $A|1\rangle = |0\rangle$ (it swaps basis states).
The columns of the matrix are the images of the basis kets in coordinates:

$$
A = \big(\,A|0\rangle \ \big|\ A|1\rangle\,\big) = \begin{pmatrix} 0 & 1 \\ 1 & 0\end{pmatrix} = X .
$$

This is the Pauli-$X$ / NOT gate. Its adjoint is $X^\dagger = \overline{X}^T = X$ (Hermitian), and
since $X^2 = I$ also $X^\dagger = X^{-1}$ (unitary): $X$ is both Hermitian and unitary — a fact we'll
see is special to involutions.

### Example 2 — Change of basis: $Z$ seen in the Hadamard basis

The Pauli-$Z = \operatorname{diag}(1,-1)$ is diagonal in $\{|0\rangle,|1\rangle\}$. Change to the
Hadamard basis via $S = H = \tfrac1{\sqrt2}\begin{psmallmatrix}1 & 1\\ 1 & -1\end{psmallmatrix}$ (which
is unitary and Hermitian, so $S^\dagger = S$):

$$
Z' = S^\dagger Z S = H Z H = \tfrac12\begin{pmatrix}1&1\\1&-1\end{pmatrix}\begin{pmatrix}1&0\\0&-1\end{pmatrix}\begin{pmatrix}1&1\\1&-1\end{pmatrix}
= \begin{pmatrix}0 & 1\\ 1 & 0\end{pmatrix} = X .
$$

So $Z$ in the $\{|+\rangle,|-\rangle\}$ basis *is* $X$ — the famous identity $HZH = X$. Same operator,
different matrix; note $\operatorname{Tr}$ and $\det$ are unchanged ($0$ and $-1$ respectively).

---

## Hands-on (Python)

```python
import numpy as np

def dag(A):
    return A.conj().T                       # adjoint = conjugate transpose

X = np.array([[0, 1], [1, 0]], dtype=complex)
Y = np.array([[0, -1j], [1j, 0]], dtype=complex)
Z = np.array([[1, 0], [0, -1]], dtype=complex)
H = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2)

# Build an operator column-by-column from its action on the basis:
ket0 = np.array([1, 0], dtype=complex)
ket1 = np.array([0, 1], dtype=complex)
A = np.column_stack([X @ ket0, X @ ket1])   # columns are A|0>, A|1>
print(np.allclose(A, X))                    # True

# Adjoint, trace, determinant:
print(np.allclose(dag(X), X))               # X is Hermitian
print(np.trace(Z), np.linalg.det(Z))        # 0,  (-1+0j)

# Change of basis HZH = X (similarity transform with S = H, S† = H):
print(np.allclose(dag(H) @ Z @ H, X))       # True
print(np.isclose(np.trace(dag(H) @ Z @ H), np.trace(Z)))  # trace invariant: True
```

```python
# Commutators / anticommutators of Paulis:
def comm(A, B):  return A @ B - B @ A
def acomm(A, B): return A @ B + B @ A

print(np.allclose(comm(X, Y), 2j * Z))      # [X,Y] = 2iZ
print(np.allclose(acomm(X, Y), np.zeros((2, 2))))  # Paulis anticommute: {X,Y}=0
```

> **Braket preview.** When you build `Circuit().x(0)` in Term 2, the SDK is composing exactly these
> operator matrices (as a unitary) and applying them to the state vector. The math here *is* what the
> simulator does under the hood.

---

## Exercises

**E1 (easy).** Find the matrix of the operator $A = |0\rangle\langle 1| + |1\rangle\langle 0|$ and
identify it.

<details><summary>Solution</summary>

$|0\rangle\langle1| = \begin{psmallmatrix}0&1\\0&0\end{psmallmatrix}$, $|1\rangle\langle0| = \begin{psmallmatrix}0&0\\1&0\end{psmallmatrix}$;
their sum is $\begin{psmallmatrix}0&1\\1&0\end{psmallmatrix} = X$, the NOT gate.
</details>

**E2 (easy).** Verify $(AB)^\dagger = B^\dagger A^\dagger$ for $A = X$, $B = S = \operatorname{diag}(1,i)$.

<details><summary>Solution</summary>

$AB = X S = \begin{psmallmatrix}0&i\\1&0\end{psmallmatrix}$, so $(AB)^\dagger = \begin{psmallmatrix}0&1\\-i&0\end{psmallmatrix}$.
$B^\dagger = \operatorname{diag}(1,-i)$, $A^\dagger = X$, so $B^\dagger A^\dagger = \operatorname{diag}(1,-i)X
= \begin{psmallmatrix}0&1\\-i&0\end{psmallmatrix}$. They match. ∎
</details>

**E3 (medium).** Prove $\operatorname{Tr}(AB) = \operatorname{Tr}(BA)$ from the index definition, and
give a counterexample to $\operatorname{Tr}(ABC) = \operatorname{Tr}(ACB)$.

<details><summary>Solution</summary>

$\operatorname{Tr}(AB) = \sum_i (AB)_{ii} = \sum_{i,j}A_{ij}B_{ji} = \sum_{j,i}B_{ji}A_{ij} = \sum_j (BA)_{jj}
= \operatorname{Tr}(BA)$. But cyclicity allows only *rotations*, not arbitrary swaps. Counterexample with
Paulis: $\operatorname{Tr}(XYZ)$ vs $\operatorname{Tr}(XZY)$. Since $XYZ = X(iX) = iX^2 = iI$... let's
use $\operatorname{Tr}(XYZ)$: $YZ = iX$ so $XYZ = iX^2 = iI$, trace $2i$. And $XZY$: $ZY = -iX$, so
$XZY = -iX^2 = -iI$, trace $-2i \neq 2i$. ∎
</details>

**E4 (medium).** Show that the trace of a commutator is always zero, $\operatorname{Tr}([A,B]) = 0$.
What does this imply about whether $[\hat x,\hat p] = i\hbar I$ can hold for finite matrices?

<details><summary>Solution</summary>

$\operatorname{Tr}(AB - BA) = \operatorname{Tr}(AB) - \operatorname{Tr}(BA) = 0$ by cyclicity. But
$\operatorname{Tr}(i\hbar I) = i\hbar\,n \neq 0$ in dimension $n$. So the canonical commutation relation
$[\hat x,\hat p] = i\hbar I$ has **no finite-dimensional matrix realization** — position and momentum
require infinite-dimensional Hilbert spaces. (Qubits, being finite, only ever use bounded observables
like the Paulis.)
</details>

**E5 (hard).** Prove that the trace is the *unique* (up to scale) linear functional $f$ on operators
satisfying $f(AB) = f(BA)$ for all $A,B$ — i.e. cyclicity essentially characterizes the trace.

<details><summary>Solution sketch</summary>

Using $f(AB)=f(BA)$ with $A = |i\rangle\langle j|$, $B = |k\rangle\langle l|$: $AB = \delta_{jk}|i\rangle\langle l|$
and $BA = \delta_{li}|k\rangle\langle j|$, so $f(|i\rangle\langle l|)\delta_{jk} = f(|k\rangle\langle j|)\delta_{li}$.
Take $j=k$, $i\neq l$: forces $f(|i\rangle\langle l|) = 0$ for $i\neq l$. Take $i=l$, $j=k$: forces all
diagonal values $f(|i\rangle\langle i|)$ equal, say to $c$. Then by linearity $f(A) = \sum_i A_{ii}\,c
= c\operatorname{Tr}(A)$. Hence $f$ is a scalar multiple of the trace. ∎
</details>

---

## Checkpoint

1. How is an operator's matrix determined by its action on a basis?
2. Give the defining inner-product relation for the adjoint and the resulting matrix rule.
3. How does an operator's matrix transform under a change of orthonormal basis?
4. List three properties of the trace and explain why basis-independence matters physically.
5. What is the physical significance of two observables commuting?

<details><summary>Answers</summary>

1. The $j$-th column is $A|j\rangle$ in coordinates; equivalently $A_{ij} = \langle i|A|j\rangle$.
2. $\langle\phi|A\psi\rangle = \langle A^\dagger\phi|\psi\rangle$; in an ONB, $(A^\dagger)_{ij} = \overline{A_{ji}}$ (conjugate transpose).
3. $A' = S^\dagger A S$ with $S$ unitary (similarity transform).
4. Linear, cyclic ($\operatorname{Tr}(AB)=\operatorname{Tr}(BA)$), basis-independent, equals sum of
   eigenvalues. Basis-independence means physical predictions (e.g. $\operatorname{Tr}(\rho A)$) don't
   depend on the arbitrary choice of coordinates.
5. Commuting observables share an eigenbasis, so they can be measured simultaneously with definite
   joint values and without mutual disturbance.

</details>

---

## Further Reading

- **[Axl]** Axler, *Linear Algebra Done Right*, Ch. 3, 5, 7, 10 — operators, adjoints, trace, determinant.
- **[NC]** Nielsen & Chuang, §2.1.5–2.1.7 — operators, adjoints, the outer-product representation, trace.
- **[HJ]** Horn & Johnson, *Matrix Analysis* — the definitive matrix reference.

---

← Prev: [Dirac Notation](03-dirac-notation.md) · Up: [Term 0](../README.md) · Next: [Eigenvalues & the Spectral Theorem](05-eigen-spectral-theorem.md) →
