Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 39 ยท Getting Started

Matrices and Unitaries

Understand matrices as quantum operations: matrix-vector products, the adjoint, unitary and Hermitian matrices, projectors and eigenvalues, with runnable NumPy examples.

If quantum states are vectors, quantum operations are matrices. Every gate you will meet in this course, from the humble NOT to multi-qubit entangling gates, is a square matrix applied to a state vector. This lesson explains the handful of matrix ideas you need and singles out the one property that makes an operation physically legal: being unitary.

Matrices as machines

A matrix M with d rows and d columns takes a vector of length d and returns another vector of length d. Each output entry is a weighted sum of input entries:

(M v)[r] = sum over c of M[r][c] * v[c]

In NumPy you write M @ v. A matrix that swaps the two entries of a one-qubit vector is the quantum NOT, called X:

X = [[0, 1], [1, 0]]

Apply it to |0> = [1, 0] and you get [0, 1] = |1>. Apply it to a superposition [a, b] and you get [b, a]: the amplitudes trade places.

Python Applying a matrix to a state

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the last line. Matrix multiplication is not commutative: ZX and XZ differ (by a sign here). In a circuit diagram, gates are drawn left to right in time order, but in the formula the first gate is written rightmost, because it touches the state first: U3 U2 U1 |psi>.

The adjoint (conjugate transpose)

The adjoint or dagger of a matrix, written Mโ€ , is formed by transposing it and conjugating every entry. In NumPy: M.conj().T. It plays the role that the conjugate plays for single numbers, and it is the matrix analogue of turning a ket into a bra.

Unitary matrices

A matrix U is unitary if

Uโ€  U = I

where I is the identity matrix. This single equation carries a lot of meaning:

  • It preserves length. The norm of U v equals the norm of v. A normalized state stays normalized, so probabilities still add to 1.
  • It is reversible. The inverse of U is simply Uโ€ . Every quantum gate can be undone by running its dagger, which is why quantum computation is reversible until you measure.
  • It preserves overlaps. <U a | U b> = <a | b>, so distinguishable states stay distinguishable.

Closed quantum systems evolve by unitary matrices only. Nothing else is allowed (apart from measurement, covered next). This is the single biggest difference from classical code, where you may freely overwrite a variable. A classical AND gate destroys information (two inputs, one output), so it cannot be a unitary matrix as written. Quantum versions need extra wires, a subject for Controlled Gates.

Python Testing unitarity and reversibility

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The "matrix" [[1,1],[0,1]] stretches vectors, so it fails the test. It cannot be a quantum gate.

Hermitian matrices and observables

A matrix is Hermitian if it equals its own dagger: Mโ€  = M. Hermitian matrices have real eigenvalues, and in quantum mechanics they represent measurable quantities (observables). The matrices X, Y and Z you will meet in the next chapters are both Hermitian and unitary, which is why each one is its own inverse.

An eigenvector of M is a vector that M only stretches: M v = lambda v. The stretch factor lambda is the eigenvalue. For a Hermitian observable, the eigenvalues are the possible measurement outcomes and the eigenvectors are the states in which that outcome is certain. np.linalg.eigh computes them for Hermitian matrices.

Outer products and projectors

The inner product <a|b> of two vectors is a number. The outer product |a><b| is a matrix: entry [r][c] equals a[r] * conj(b[c]). In NumPy: np.outer(a, b.conj()).

The outer product of a normalized state with itself, P = |a><a|, is a projector: it extracts the part of any vector that points along a. It satisfies P P = P (projecting twice changes nothing) and it is Hermitian, but it is not unitary. Projectors describe measurement outcomes, as the next chapter shows.

Python Outer products, projectors and eigenvalues

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The squared length of the projected vector (0.36) is the probability of finding the state in |0>. This is the Born rule written with matrices.

Matrices you will see again

Matrix Role
I (identity) Does nothing
X, Y, Z (Pauli matrices) Basic one-qubit gates and observables
H (Hadamard) Converts between definite and equal-mix states
Uโ€  Undoes U
Outer product of a state with itself Projector onto that state

When a simulation misbehaves, check the three invariants first: matrices are unitary, state norms stay at 1, and the order of multiplication is right-to-left.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does it mean for a matrix U to be unitary?

    Unitary means Uโ€ U = I. It preserves length and is inverted by its adjoint.
  2. 2. In the product U3 U2 U1 applied to a state, which gate acts first?

    The rightmost matrix touches the vector first, so U1 acts first.
  3. 3. Why can a quantum gate always be undone?

    Unitary matrices have the adjoint as their inverse. Not every gate is Hermitian, but every gate is unitary.
  4. 4. Which matrix can NOT be a quantum gate?

    A projector discards information (it maps |1> to the zero vector), so it is not unitary. It describes a measurement outcome, not a gate.

Summary

  • Quantum operations are matrices multiplied onto state vectors; order is right to left.
  • The adjoint Mโ€  is the conjugate transpose.
  • Gates are unitary: Uโ€ U = I, which preserves norms and makes every gate reversible.
  • Hermitian matrices represent observables and have real eigenvalues.
  • Projectors |a><a| are not unitary and model measurement outcomes.

Next: Quantum Hardware Landscape