Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Vectors and Linear Algebra

Linear algebra is the backbone of quantum mechanics. States are vectors, observables are matrices, and measurement outcomes are eigenvalues. Everything that follows in the course is, at bottom, an eigenvalue problem in a vector space. This page builds that vocabulary from the 2D picture up.

Vectors

What is a vector?

A vector is an ordered collection of numbers describing the state or configuration of a system:

Vectors may live in real or complex vector spaces depending on their components. In quantum mechanics the components are generally complex.

The plot shows a vector built as v=2e1+3e2\mathbf{v} = 2e_1 + 3e_2, decomposed along the two basis directions.

Source
import numpy as np
import matplotlib.pyplot as plt

e1 = np.array([1, 0])
e2 = np.array([0, 1])
v = 2 * e1 + 3 * e2

fig, ax = plt.subplots(figsize=(6, 5))
ax.set_xlim(-0.5, 3.5)
ax.set_ylim(-0.5, 3.5)
ax.set_aspect('equal')

ax.arrow(0, 0, *e1, head_width=0.1, color='gray', length_includes_head=True)
ax.arrow(0, 0, *e2, head_width=0.1, color='gray', length_includes_head=True)
ax.text(1.05, -0.25, r'$e_1$', fontsize=13)
ax.text(-0.28, 1.05, r'$e_2$', fontsize=13)

ax.arrow(0, 0, *v, head_width=0.15, color='#2e4057', length_includes_head=True)
ax.plot([0, v[0]], [v[1], v[1]], '--', color='gray')
ax.plot([v[0], v[0]], [0, v[1]], '--', color='gray')
ax.text(v[0] + 0.1, v[1] + 0.1, r'$\mathbf{v} = 2e_1 + 3e_2$', color='#2e4057', fontsize=13)

ax.axhline(0, color='black', lw=0.8)
ax.axvline(0, color='black', lw=0.8)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Fig.1 A vector as a linear combination of basis vectors')
plt.tight_layout()
plt.show()
<Figure size 600x500 with 1 Axes>

Inner products, norms, and orthogonality

The next natural operation multiplies two vectors into a number that measures how much they share a direction. Throughout this section, a{\color{orange} \vec a} is orange and b{\color{#3d81f6} \vec b} is blue, in every equation and every figure.

A worked example, with every number wearing its vector’s color. For a=(62){\color{orange} \vec a = \begin{pmatrix} 6 \\ 2 \end{pmatrix}} and b=(53){\color{#3d81f6} \vec b = \begin{pmatrix} 5 \\ -3 \end{pmatrix}}:

ab=(6)(5)+(2)(3)=306=24.{\color{orange} \vec a} \cdot {\color{#3d81f6} \vec b} = ({\color{orange} 6})({\color{#3d81f6} 5}) + ({\color{orange} 2})({\color{#3d81f6} -3}) = 30 - 6 = 24.

The two forms together are a machine for extracting angles from components. And the special case cosθ=0\cos\theta = 0 earns its own name:

Vectors that are both orthogonal and normalized are orthonormal, compactly written with the Kronecker delta:

eiej=δij={1i=j0ij.\langle e_i \mid e_j \rangle = \delta_{ij} = \begin{cases} 1 & i = j \\ 0 & i \neq j \end{cases}.

The norm is the square root of a vector’s inner product with itself, giving its length:

aa=a12+a22,a=a12+a22.\langle a \mid a \rangle = a_1^2 + a_2^2, \qquad \|a\| = \sqrt{a_1^2 + a_2^2}.

A vector with a=1\|a\| = 1 is normalized.

Projection: the most quantum operation in this appendix

The answer defines the orthogonal projection. Choose kk so that the leftover error vector e=akb{\color{#d81b60} \vec e} = {\color{orange} \vec a} - k{\color{#3d81f6} \vec b} is orthogonal to b{\color{#3d81f6} \vec b}; any other choice leaves a longer error (Pythagoras). That choice gives:

Source
a_v = np.array([2.0, 3.0])
b_v = np.array([4.0, 1.0])
p_v = (a_v @ b_v) / (b_v @ b_v) * b_v

fig_pr, ax_pr = plt.subplots(figsize=(6.4, 4.6))
s = np.linspace(-0.5, 1.35, 2)
ax_pr.plot(s * b_v[0], s * b_v[1], color="0.85", lw=1.2, zorder=0)
for vec, color, label, dx, dy in [
    (a_v, "orange", r"$\vec a$", -0.25, 0.15),
    (b_v, "#3d81f6", r"$\vec b$", 0.1, -0.3),
    (p_v, "#004d40", r"$\vec p$", 0.05, 0.22),
]:
    ax_pr.annotate("", xy=vec, xytext=(0, 0),
                   arrowprops=dict(arrowstyle="->", color=color, lw=2.6))
    ax_pr.text(vec[0] + dx, vec[1] + dy, label, color=color, fontsize=14)
ax_pr.plot([p_v[0], a_v[0]], [p_v[1], a_v[1]], "--", color="#d81b60", lw=2.2)
ax_pr.text((p_v[0] + a_v[0]) / 2 + 0.12, (p_v[1] + a_v[1]) / 2, r"$\vec e$",
           color="#d81b60", fontsize=14)
u_hat = b_v / np.linalg.norm(b_v)
n_hat = np.array([-u_hat[1], u_hat[0]])
sq = 0.16
corner = p_v
ax_pr.plot([corner[0] - sq * u_hat[0], corner[0] - sq * u_hat[0] + sq * n_hat[0]],
           [corner[1] - sq * u_hat[1], corner[1] - sq * u_hat[1] + sq * n_hat[1]],
           color="0.4", lw=1)
ax_pr.plot([corner[0] - sq * u_hat[0] + sq * n_hat[0], corner[0] + sq * n_hat[0]],
           [corner[1] - sq * u_hat[1] + sq * n_hat[1], corner[1] + sq * n_hat[1]],
           color="0.4", lw=1)
ax_pr.set_xlim(-0.8, 5.2)
ax_pr.set_ylim(-0.8, 3.6)
ax_pr.set_aspect("equal")
ax_pr.grid(True, ls=":", alpha=0.5)
ax_pr.set_title("the projection p is the shadow of a along b; the error e is orthogonal", fontsize=10)
plt.show()
<Figure size 640x460 with 1 Axes>

The picture contains a decomposition. Walking along p{\color{#004d40} \vec p} and then along e{\color{#d81b60} \vec e} lands exactly on a{\color{orange} \vec a}:

a=pparallel to b+eorthogonal to b.{\color{orange} \vec a} = \underbrace{{\color{#004d40} \vec p}}_{\text{parallel to } {\color{#3d81f6} \vec b}} + \underbrace{{\color{#d81b60} \vec e}}_{\text{orthogonal to } {\color{#3d81f6} \vec b}}.

Now the quantum payoff. For an orthonormal basis {e1,e2,}\{e_1, e_2, \ldots\} the projection formula collapses to ck=ekac_k = \langle e_k \mid a \rangle, and expanding a vector in a basis is nothing but projecting it onto each basis direction in turn:

a=kckek,ck=eka.\vec a = \sum_k c_k\, e_k, \qquad c_k = \langle e_k \mid a \rangle.

In quantum mechanics the state ψ\psi is the vector, the eigenstates of a measurement are the orthonormal basis, and the numbers ck2=ekψ2|c_k|^2 = |\langle e_k \mid \psi \rangle|^2 become the probabilities of each outcome (the Born rule). Every quantum measurement is, at its mathematical heart, the orthogonal projection drawn above.

Matrices

Linear systems as matrix equations

A system of linear equations is a matrix acting on a vector. The system

a11x1+a12x2=b1a21x1+a22x2=b2\begin{aligned} a_{11}x_1 + a_{12}x_2 &= b_1 \\ a_{21}x_1 + a_{22}x_2 &= b_2 \end{aligned}

packs into a single matrix-vector product:

Matrix times vector is the dot product of each row of AA with x\mathbf{x}:

Ax=(a11x1+a12x2a21x1+a22x2).A\mathbf{x} = \begin{pmatrix} a_{11}x_1 + a_{12}x_2 \\ a_{21}x_1 + a_{22}x_2 \end{pmatrix}.

If AA is invertible, the solution is x=A1b\mathbf{x} = A^{-1}\mathbf{b}.

import numpy as np

A = np.array([[2, 3],
              [1, -2]])
b = np.array([7, 1])

# Solve by inversion, and with the recommended solver
x_inv = np.linalg.inv(A) @ b
x_solve = np.linalg.solve(A, b)

print("solution via inverse:", x_inv)
print("solution via solve  :", x_solve)
solution via inverse: [2.42857143 0.71428571]
solution via solve  : [2.42857143 0.71428571]

Eigenvalues and eigenvectors

When a matrix acts on a vector it usually changes both its length and direction. Certain special vectors keep their direction and are only rescaled. These are eigenvectors, and the scaling factors are eigenvalues.

Rearranging to (AλI)v=0(A - \lambda I)\mathbf{v} = 0, a nonzero solution exists only when the characteristic equation holds:

det(AλI)=0.\det(A - \lambda I) = 0.

The sign and size of λ\lambda say what happens to the eigenvector: λ>1\lambda > 1 stretches it, 0<λ<10 < \lambda < 1 compresses it, λ<0\lambda < 0 flips it, and λ=0\lambda = 0 collapses it. In quantum mechanics this exact structure reappears for operators acting on wavefunctions: eigenvalues are the measurable observables (energies, momenta) and eigenvectors are the states with definite outcomes. The next page makes that leap explicit, showing that a function is just a vector with infinitely many components and the Schrodinger equation H^ψ=Eψ\hat H\psi = E\psi is this same eigenvalue problem Av=λvA\mathbf{v}=\lambda\mathbf{v}.

import numpy as np

A = np.array([[2, 1],
              [1, 2]])
vals, vecs = np.linalg.eig(A)

print("eigenvalues :", vals)
print("eigenvectors (columns):\n", vecs)

# Verify A v = lambda v for the first eigenpair
print("\nA @ v0        :", A @ vecs[:, 0])
print("lambda0 * v0  :", vals[0] * vecs[:, 0])
eigenvalues : [3.+0.j 1.+0.j]
eigenvectors (columns):
 [[ 0.70710678+0.j -0.70710678+0.j]
 [ 0.70710678+0.j  0.70710678+0.j]]

A @ v0        : [2.12132034+0.j 2.12132034+0.j]
lambda0 * v0  : [2.12132034+0.j 2.12132034+0.j]

The whole story in one picture: a matrix turns almost every vector, but an eigenvector comes back parallel to itself.

Source
A_eig = np.array([[2, 1], [1, 2]])
w = np.array([1.0, -0.35])
u = np.array([1.0, 1.0]) / np.sqrt(2)

fig_e, (axg, axe) = plt.subplots(1, 2, figsize=(9, 4.4))
for ax_, v1, v2, c1, c2, ttl in [
    (axg, w, A_eig @ w, "0.45", "steelblue", "a generic vector turns"),
    (axe, u, A_eig @ u, "0.45", "crimson", "an eigenvector only stretches"),
]:
    ax_.annotate("", xy=v1, xytext=(0, 0), arrowprops=dict(arrowstyle="->", color=c1, lw=2.5))
    ax_.annotate("", xy=v2, xytext=(0, 0), arrowprops=dict(arrowstyle="->", color=c2, lw=2.5))
    ax_.text(*(v1 * 1.15), r"$\vec{v}$", color=c1, fontsize=13)
    ax_.text(*(v2 * 1.06), r"$A\vec{v}$", color=c2, fontsize=13)
    ax_.set_xlim(-0.6, 3.0); ax_.set_ylim(-1.2, 2.6)
    ax_.set_aspect("equal"); ax_.grid(True, ls=":", alpha=0.6)
    ax_.set_title(ttl, fontsize=11)
fig_e.tight_layout()
plt.show()
<Figure size 900x440 with 2 Axes>

On the left, v\vec v (gray) and Av\color{steelblue} A\vec v (blue) point in different directions: the matrix rotated the vector. On the right, the eigenvector u=12(1,1)\vec u = \tfrac{1}{\sqrt 2}(1,1) obeys Au=3u\color{crimson} A\vec u = 3\vec u: same direction, three times longer. Eigenvectors are the directions along which a matrix acts like simple multiplication.

The geometric action of a matrix

Different kinds of matrices deform a shape in characteristic ways:

Matrix typeExampleEffect
Diagonal, unequal entries[[1.8, 0], [0, 0.8]]Stretch, different scaling per axis
Diagonal, equal entries[[0.7, 0], [0, 0.7]]Uniform scaling
Rotation (orthogonal)[[cosθ, -sinθ], [sinθ, cosθ]]Rotation, preserves lengths and angles
Off-diagonal term[[1, k], [0, 1]]Shear, slants without changing area

We apply each matrix to the same small polygon to see how it transforms.

Source
import numpy as np
import matplotlib.pyplot as plt

coords = np.array([[0, 0], [0.5, 0.5], [0.5, 1.5], [0, 1], [0, 0]]).T

stretch = np.array([[1.8, 0], [0, 0.8]])
rotation = np.array([[np.cos(np.pi / 6), -np.sin(np.pi / 6)],
                     [np.sin(np.pi / 6),  np.cos(np.pi / 6)]])
shear = np.array([[1, 0.8], [0, 1]])
scaling = np.array([[0.7, 0], [0, 0.7]])

transforms = {
    "Stretch (non-uniform)": stretch,
    "Rotation (30 deg)": rotation,
    "Shear": shear,
    "Scaling (uniform)": scaling,
}

fig, axes = plt.subplots(2, 2, figsize=(8, 8))
for ax, (name, M) in zip(axes.flat, transforms.items()):
    ax.plot(coords[0], coords[1], 'r--', label='original')
    new_coords = M @ coords
    ax.plot(new_coords[0], new_coords[1], 'b-', label='transformed')
    ax.set_title(name, fontsize=11)
    ax.set_aspect('equal', 'box')
    ax.set_xlim(-2, 2)
    ax.set_ylim(-1, 2)
    ax.grid(True, ls=':')
    ax.legend(fontsize=8, loc='upper left')

plt.suptitle('Fig.2 How different matrices deform the same shape', fontsize=13)
plt.tight_layout()
plt.show()
<Figure size 800x800 with 4 Axes>

Problems

Problem 1: Dot product and angle

Find ab\langle a \mid b \rangle and the angle between a=(1,2)a = (1, 2) and b=(3,1)b = (3, -1).

Problem 2: Normalize a vector

Normalize v=(3,4)v = (3, 4).

Problem 3: Check orthogonality

Are (1,1)(1, 1) and (1,1)(1, -1) orthogonal? Are they orthonormal?

Problem 4: Eigenvalues by hand

Find the eigenvalues of A=(2112)A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}.

Problem 5: Solve a linear system

Solve (2312)x=(71)\begin{pmatrix} 2 & 3 \\ 1 & -2 \end{pmatrix}\mathbf{x} = \begin{pmatrix} 7 \\ 1 \end{pmatrix} by hand and check against the code.

Problem 6: Rotation preserves length

Show that the rotation matrix R=(cosθsinθsinθcosθ)R = \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} leaves the norm of any vector unchanged.

Problem 7: Eigenvector direction

Verify that (1,1)(1, 1) is an eigenvector of A=(2112)A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} and identify its eigenvalue.

Problem 8: Complex inner product

For complex vectors the inner product conjugates the first entry: ab=iaibi\langle a \mid b \rangle = \sum_i a_i^* b_i. Compute aa\langle a \mid a \rangle for a=(1, i)a = (1,\ i) and confirm it is real and positive.