Quantum Programming and Simulation

Chapter 2
Mathematical Foundations for Quantum Programs

Diploma in Quantum Computing
Quantum Programming and Simulation   •   Diploma

Mathematical Foundations for Quantum Programs

Introduction

Quantum programs are built from mathematical objects that describe states, transformations, and measurements. Unlike ordinary bits, which take the value 0 or 1, a qubit may exist in a superposition of both computational-basis states. To program quantum devices reliably, a learner must be able to represent these states using complex vectors, transform them using matrices, combine systems using tensor products, and interpret measurement results probabilistically.

This chapter develops the mathematical language used by quantum programming frameworks such as Qiskit, Cirq, and PennyLane. The emphasis is on practical calculation: determining the state produced by a circuit, checking whether an operation is physically valid, and calculating measurement probabilities.

Learning Objectives

By the end of this chapter, you should be able to:

  1. Explain complex numbers and their role in quantum amplitudes.
  2. Represent qubit states as normalized vectors and use Dirac notation.
  3. Calculate inner products, norms, and orthogonality.
  4. Use matrices to represent quantum gates and apply them to state vectors.
  5. Construct multi-qubit states with tensor products.
  6. Explain why quantum gates are unitary operators.
  7. Calculate eigenvalues and eigenvectors in simple quantum examples.
  8. Determine measurement probabilities using amplitudes and projectors.
  9. Analyze basic quantum circuits using linear algebra.
  10. Recognize numerical and conceptual limitations in quantum simulation.

1. Complex Numbers and Quantum Amplitudes

A complex number has the form

z = a + ib

where a and b are real numbers and i = √(-1). The real part is Re(z) = a, and the imaginary part is Im(z) = b. The complex conjugate is

z* = a - ib

The modulus, or magnitude, is

|z| = √(a² + b²)

and the squared modulus is

|z|² = zz* = a² + b²

In quantum mechanics, amplitudes are generally complex numbers. Probabilities are not amplitudes themselves; they are obtained from squared magnitudes. If an outcome has amplitude α, its probability is

P = |α|²

The probability is real and non-negative, even when the amplitude is complex. Complex phase affects interference. For example, amplitudes 1/√2 and -1/√2 have equal individual probabilities, but their opposite signs can cause cancellation when added.

Example 1: Modulus of a complex amplitude

Let z = 3 + 4i. Then

|z| = √(3² + 4²) = 5

Therefore, |z|² = 25. If this number were used directly as a probability, it would not be valid because probabilities must lie between 0 and 1. Quantum state amplitudes must therefore satisfy a normalization condition.

2. Vectors, Qubits, and Dirac Notation

A column vector is an ordered list of numbers. A single qubit is represented in the computational basis by

|ψ⟩ = α|0⟩ + β|1⟩

where α and β are complex amplitudes. The basis vectors are

|0⟩ = [1, 0]ᵀ, |1⟩ = [0, 1]ᵀ

Thus, the state vector is

|ψ⟩ = [α, β]ᵀ

A valid pure quantum state must be normalized:

|α|² + |β|² = 1

This expresses the fact that the probabilities of all mutually exclusive computational-basis outcomes sum to one. When measured in the computational basis, the probability of 0 is |α|², and the probability of 1 is |β|².

The notation |ψ⟩ is called ket notation. Its conjugate transpose is the bra

⟨ψ| = [α*, β*]

The bra and ket together form an inner product:

⟨ψ|ψ⟩ = |α|² + |β|²

For a normalized state, ⟨ψ|ψ⟩ = 1.

Global and relative phase

Multiplying a state by a complex number of unit modulus gives

eiθ|ψ⟩

where θ is real. This global phase does not change measurement probabilities and is physically unobservable when applied to the entire state. However, a relative phase between components is observable through interference. The states

(|0⟩ + |1⟩)/√2

and

(|0⟩ - |1⟩)/√2

produce identical computational-basis probabilities but behave differently under later gates.

3. Inner Products, Norms, and Orthogonality

For vectors |u⟩ and |v⟩, the inner product is

⟨u|v⟩ = ∑ᵢ uᵢ* vᵢ

The conjugation is essential when vector entries are complex. The norm is

||v|| = √(⟨v|v⟩)

A vector is normalized when its norm equals 1. Two vectors are orthogonal when

⟨u|v⟩ = 0

The computational basis states are orthonormal because

⟨0|0⟩ = 1, ⟨1|1⟩ = 1, ⟨0|1⟩ = 0

Orthonormal bases are important because every state can be expressed uniquely as a linear combination of their basis vectors.

Worked Example 2: Checking normalization

Consider

|ψ⟩ = (1 + i)/2 |0⟩ + (1 - i)/2 |1⟩

The squared magnitudes are

|(1+i)/2|² = (1² + 1²)/4 = 1/2

and

|(1-i)/2|² = (1² + 1²)/4 = 1/2

Their sum is 1, so the state is normalized. Measurement in the computational basis gives probabilities 1/2 for both 0 and 1.

4. Matrices as Quantum Operations

A matrix maps vectors to vectors. If a gate is represented by matrix U, then its action on a state is

|ψ'⟩ = U|ψ⟩

For a 2 × 2 matrix,

U = [u₀₀ u₀₁; u₁₀ u₁₁]

and a qubit state [α, β]ᵀ, matrix multiplication gives

U|ψ⟩ = [u₀₀α + u₀₁β, u₁₀α + u₁₁β]ᵀ

The order of multiplication matters. In general, UV ≠ VU. If gate V is applied first and U second, the total operation is UV.

Common single-qubit gates

The identity gate is

I = [1 0; 0 1]

The Pauli-X gate is

X = [0 1; 1 0]

It exchanges |0⟩ and |1⟩. The Pauli-Z gate is

Z = [1 0; 0 -1]

It leaves |0⟩ unchanged and adds a phase of −1 to |1⟩. The Hadamard gate is

H = (1/√2)[1 1; 1 -1]

Its important actions are

H|0⟩ = (|0⟩ + |1⟩)/√2

and

H|1⟩ = (|0⟩ - |1⟩)/√2

The phase gate with angle θ is

P(θ) = [1 0; 0 eiθ]

It changes the relative phase of the |1⟩ component.

Worked Example 3: Applying a Hadamard gate

Starting with |0⟩,

H|0⟩ = (1/√2)[1 1; 1 -1][1; 0] = (1/√2)[1; 1]

Thus the final state is (|0⟩ + |1⟩)/√2. A computational-basis measurement produces 0 or 1 with equal probability.

5. Unitary Operators

A physically valid reversible quantum gate is represented by a unitary matrix. A matrix U is unitary if

U†U = UU† = I

where U† is the conjugate transpose of U. This condition means that the columns and rows of U form orthonormal sets.

Unitarity preserves inner products and norms:

⟨Uu|Uv⟩ = ⟨u|U†U|v⟩ = ⟨u|v⟩

Consequently, a normalized state remains normalized after a unitary operation. Quantum gates are therefore reversible: the inverse is

U⁻¹ = U†

Checking a gate

For the Pauli-X matrix, which is real and symmetric, X† = X. Therefore,

X†X = XX = I

so X is unitary. A matrix that scales every vector by 2 is not unitary, because it changes norm 1 to norm 2 and cannot preserve probabilities.

6. Tensor Products and Multi-Qubit States

The tensor product combines state spaces. If

|a⟩ = [a₀, a₁]ᵀ, |b⟩ = [b₀, b₁]ᵀ

then

|a⟩ ⊗ |b⟩ = [a₀b₀, a₀b₁, a₁b₀, a₁b₁]ᵀ

The standard two-qubit basis, in the usual ordering, is

|00⟩ = [1,0,0,0]ᵀ
|01⟩ = [0,1,0,0]ᵀ
|10⟩ = [0,0,1,0]ᵀ
|11⟩ = [0,0,0,1]ᵀ

The ordering convention must be checked in software because some frameworks display qubits in reverse order.

For operators, the tensor product acts as

(A ⊗ B)(|a⟩ ⊗ |b⟩) = (A|a⟩) ⊗ (B|b⟩)

If A and B are unitary, then A ⊗ B is also unitary.

Separable and entangled states

A multi-qubit state is separable if it can be written as a tensor product of individual states. For example,

|+⟩⊗|0⟩ = (|00⟩ + |10⟩)/√2

The Bell state

|Φ⁺⟩ = (|00⟩ + |11⟩)/√2

cannot be expressed as a product of two single-qubit states. It is entangled. Measuring either qubit in the computational basis gives 0 or 1 with equal probability, but the two results always agree.

7. Eigenvalues and Eigenvectors

An eigenvector |v⟩ of a matrix A satisfies

A|v⟩ = λ|v⟩

where λ is the corresponding eigenvalue. Eigenvalues are found from the characteristic equation

det(A − λI) = 0

For the Pauli-Z operator,

Z|0⟩ = +1|0⟩
Z|1⟩ = −1|1⟩

Thus |0⟩ and |1⟩ are eigenvectors with eigenvalues +1 and −1. Quantum measurements are closely related to eigenvalues and eigenspaces of observables. A measurement of Z returns +1 or −1, while a computational-basis measurement reports 0 or 1 according to the corresponding eigenstate.

8. Measurement and Probability

A projective measurement is described by projectors Pm. These satisfy

Pm² = Pm, Pm† = Pm, ∑ₘ Pm = I

For a normalized state |ψ⟩, the probability of outcome m is

p(m) = ⟨ψ|Pm|ψ⟩

After obtaining outcome m, the post-measurement state is

|ψm⟩ = Pm|ψ⟩ / √p(m)

provided p(m) > 0. For computational-basis measurement of a single qubit, the projectors are

P₀ = |0⟩⟨0|, P₁ = |1⟩⟨1|

For |ψ⟩ = α|0⟩ + β|1⟩, these give probabilities |α|² and |β|². Measurement is generally non-unitary and irreversible because it selects an outcome and changes the state.

Expectation values

For an observable A, the expected value is

⟨A⟩ = ⟨ψ|A|ψ⟩

If repeated measurements are performed on identically prepared systems, the sample average approaches this expectation value. For a Hermitian observable, the expectation value is real.

9. Worked Circuit Analysis

Consider a two-qubit circuit that starts in |00⟩, applies H to the first qubit, and then applies a controlled-NOT gate with the first qubit as control and the second as target.

After the Hadamard operation,

(H ⊗ I)|00⟩ = (|00⟩ + |10⟩)/√2

The controlled-NOT maps |00⟩ → |00⟩ and |10⟩ → |11⟩. Therefore,

CNOT (|00⟩ + |10⟩)/√2 = (|00⟩ + |11⟩)/√2

The final state is the Bell state |Φ⁺⟩. Measurement produces 00 with probability 1/2 and 11 with probability 1/2. Outcomes 01 and 10 have probability zero.

This example demonstrates a central programming pattern: gates first create amplitudes, subsequent gates modify their phases and correlations, and measurement converts amplitudes into classical data.

10. Applied Case: Validating a Two-Qubit Simulator

The following is an illustrative case.

Background

A software team is developing a state-vector simulator for two-qubit educational circuits. The simulator stores a state as a four-component complex vector and applies gates by matrix multiplication.

Problem

Students report that applying a Hadamard gate twice does not return the original state. The expected identity is

H² = I

Analysis

The team tests the implementation using

H|0⟩ = (|0⟩ + |1⟩)/√2

Applying H again gives

H[(|0⟩ + |1⟩)/√2] = |0⟩

The mathematical result is correct. The investigation identifies two possible programming errors: the matrix was entered without the factor 1/√2, or the simulator used a transpose rather than a conjugate transpose when validating unitarity.

Approach and solution

The team adds three automated tests:

  1. Verify H†H = I.
  2. Verify that the norm of every test state remains 1 after applying H.
  3. Verify H(H|ψ⟩) = |ψ⟩ for basis states and randomly generated normalized states.

They also document the basis ordering and use complex-valued data types throughout the simulator.

Outcome

The corrected simulator returns the original state after two Hadamard operations. The tests also prevent later errors involving phase gates and controlled operations.

Learning points

Mathematical identities provide effective software tests. A quantum simulator should validate normalization, unitarity, basis ordering, matrix dimensions, and numerical tolerances rather than relying only on visual circuit output.

11. Practical Implementation and Limitations

A state-vector simulator for n qubits requires 2ⁿ complex amplitudes. Memory therefore grows exponentially. This makes classical simulation practical for small circuits but difficult for large general states.

In numerical software, exact equality is often inappropriate because floating-point arithmetic introduces small errors. A test may use a tolerance such as

|x − y| < ε

where ε is a chosen small positive tolerance. The value should be justified by the data type, circuit depth, and numerical method; it should not be selected merely to hide implementation errors.

Important implementation practices include:

  • State whether basis states are ordered |00⟩, |01⟩, |10⟩, |11⟩ or by another convention.
  • Check matrix dimensions before multiplication.
  • Use conjugate transpose when calculating inner products.
  • Confirm normalization after operations during debugging.
  • Distinguish a quantum state vector from a probability vector.
  • Keep global phase distinctions separate from observable relative phase.
  • Use repeated-shot simulation when comparing with real measurement data.

Quantum software also raises governance issues. Simulators and hardware results should be reproducible through documented software versions, random seeds where appropriate, gate conventions, and measurement assumptions. Claims about quantum advantage must distinguish exact simulation, noisy simulation, and execution on physical quantum hardware.

Chapter Summary

Quantum programs use complex linear algebra to represent and manipulate information. A qubit is a normalized complex vector, and its amplitudes determine measurement probabilities through squared magnitudes. Bras, kets, inner products, norms, and orthogonality provide the language for state-space calculations.

Matrices represent quantum operations. Valid reversible gates are unitary, satisfying U†U = I, and therefore preserve norms and probabilities. Tensor products construct multi-qubit states and operators. They also reveal the difference between separable and entangled states. Eigenvalues and eigenvectors connect matrices with quantum observables and measurement outcomes. Projectors provide a general probability rule, while expectation values summarize repeated measurements.

For practical quantum programming, correct matrix order, basis ordering, conjugation, normalization, and numerical tolerance are essential. These foundations allow a programmer to predict circuit behavior, test simulators, and interpret measurement results responsibly.

Key Terms

Complex amplitude: A complex coefficient whose squared magnitude contributes to a measurement probability.

Complex conjugate: The number obtained by changing the sign of a complex number’s imaginary part.

Ket: Dirac notation for a state vector, written such as |ψ⟩.

Bra: The conjugate transpose of a ket, written such as ⟨ψ|.

Normalization: The condition that the squared magnitudes of all state amplitudes sum to one.

Inner product: A scalar formed from a bra and ket, measuring overlap between vectors.

Norm: The length or magnitude of a vector.

Orthogonality: A relationship in which the inner product of two vectors is zero.

Computational basis: The basis consisting of |0⟩ and |1⟩ for one qubit.

Quantum gate: A matrix operation applied to a quantum state.

Unitary operator: An operator satisfying U†U = I.

Conjugate transpose: The transpose of a matrix followed by complex conjugation.

Tensor product: An operation that combines vector or operator spaces.

Entanglement: A non-separable relationship between quantum systems.

Eigenvector: A vector whose direction is unchanged by an operator, apart from scaling.

Eigenvalue: The scaling factor associated with an eigenvector.

Projector: An operator that selects a subspace associated with a measurement outcome.

Expectation value: The theoretical average of repeated measurements of an observable.

Global phase: A common unit-modulus factor that does not affect measurement probabilities.

Relative phase: The phase difference between state components, which can affect interference.

Review Questions

Short Answer Questions

  1. Why must a qubit state be normalized?
  2. What is the difference between an amplitude and a probability?
  3. State the mathematical condition for a matrix to be unitary.
  4. What does the tensor product accomplish in multi-qubit systems?
  5. Why is measurement generally different from a unitary gate?

Long Answer Questions

  1. Explain Dirac notation and show how a qubit ket is represented as a column vector.
  2. Describe how the Hadamard gate transforms both computational-basis states.
  3. Explain the importance of conjugation in complex inner products.
  4. Compare global phase and relative phase, including their physical consequences.
  5. Explain how eigenvalues and eigenvectors are connected to quantum observables.

Analytical / Application Questions

  1. Starting with |0⟩, calculate the state after applying H, then Z, and determine the computational-basis measurement probabilities.
  2. Determine whether (|00⟩ + |01⟩)/√2 is separable. If it is, express it as a tensor product.
  3. A simulator returns a state vector whose squared amplitudes sum to 1.0004 after a long circuit. Explain at least two possible causes and propose appropriate diagnostic tests.

Multiple Choice Questions with Answers

Question 1

For a normalized qubit |ψ⟩ = α|0⟩ + β|1⟩, what is the probability of measuring 1?

A. |α|²

B. |β|²

C. α + β

D. αβ

Correct Answer: B. |β|²

Question 2

Which condition defines a unitary matrix U?

A. U + U† = I

B. U² = I

C. U†U = I

D. det(U) = 0

Correct Answer: C. U†U = I

Question 3

What state results from applying the Hadamard gate to |0⟩?

A. |1⟩

B. (|0⟩ − |1⟩)/√2

C. (|0⟩ + |1⟩)/√2

D. |0⟩ + |1⟩

Correct Answer: C. (|0⟩ + |1⟩)/√2

Question 4

What is the squared modulus of 3 + 4i?

A. 5

B. 7

C. 16

D. 25

Correct Answer: D. 25

Question 5

Which operation combines two single-qubit state spaces into a two-qubit state space?

A. Matrix inverse

B. Tensor product

C. Trace subtraction

D. Eigenvalue division

Correct Answer: B. Tensor product

Question 6

What are the computational-basis measurement probabilities of (|0⟩ − |1⟩)/√2?

A. 0 and 1

B. 1 and 0

C. 1/2 and 1/2

D. 1/4 and 3/4

Correct Answer: C. 1/2 and 1/2

Question 7

Which equation expresses the eigenvector relationship for operator A?

A. A|v⟩ = λ|v⟩

B. A + |v⟩ = λ

C. A|v⟩ = 0 for every vector

D. A = |v⟩λ

Correct Answer: A. A|v⟩ = λ|v⟩

Question 8

What is the final state when CNOT acts on (|00⟩ + |10⟩)/√2 with the first qubit as control?

A. (|00⟩ + |01⟩)/√2

B. (|00⟩ + |11⟩)/√2

C. |10⟩

D. (|10⟩ + |11⟩)/√2

Correct Answer: B. (|00⟩ + |11⟩)/√2

Question 9

Which expression gives the expectation value of observable A in state |ψ⟩?

A. ⟨ψ|A|ψ⟩

B. A|ψ⟩ only

C. ⟨ψ|ψ⟩A²

D. |A|²

Correct Answer: A. ⟨ψ|A|ψ⟩

Question 10

Why does a general state-vector simulator become difficult to use as the number of qubits increases?

A. The number of amplitudes grows as n²

B. The number of amplitudes grows as 2ⁿ

C. Unitary gates cannot be represented by matrices

D. Measurement probabilities become negative

Correct Answer: B. The number of amplitudes grows as 2ⁿ

Quantum Programming and Simulation
Chapter 2 of 15