Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 39 ยท Getting Started

Complex Numbers and Vectors for Quantum Computing

The math you need for qubits: complex numbers, magnitude, phase, conjugates, vectors, norms, inner products and Dirac bra-ket notation, all explained with Python and NumPy.

Quantum states are vectors of complex numbers. That sentence contains two ideas, complex numbers and vectors, and both are easier than their reputation. This lesson gives you everything you need for the rest of the course, in the order you will use it, with every concept tied to a few lines of Python.

If you have seen high-school algebra you are ready. We will not prove theorems; we will build intuition and tools.

Complex numbers in five minutes

A complex number has the form a + bi, where a and b are ordinary real numbers and i is the imaginary unit with i * i = -1. Python writes it with a j: 3 + 4j. You can picture a complex number as an arrow (or point) on a plane: a is the horizontal position, b the vertical one.

Four operations cover nearly everything:

  • Magnitude (also called modulus or absolute value): the arrow's length, |z| = sqrt(a^2 + b^2).
  • Phase (also called argument or angle): the arrow's direction, measured from the positive horizontal axis.
  • Conjugate: flip the sign of the imaginary part, a - bi. We write it z*.
  • Product with its conjugate: z * z* equals |z|^2, always a real, non-negative number. This is the formula that turns amplitudes into probabilities.
Python Magnitude, phase and conjugate

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.

Euler's formula: phases as rotations

The key identity for quantum computing is Euler's formula:

e^(i*theta) = cos(theta) + i*sin(theta)

The right side is a point on the unit circle at angle theta. So e^(i*theta) is a pure rotation: magnitude exactly 1, phase theta. Multiplying any complex number by it rotates the number by theta without changing its size. Quantum gates that change a state's phase are nothing more than multiplication by such rotations, which is why phases cost nothing in magnitude but matter enormously in interference.

Python Multiplying by e^(i*theta) rotates without stretching

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 two most famous values are worth memorizing: e^(i*pi) = -1 (a half turn) and e^(i*pi/2) = i (a quarter turn). A "minus sign" in an amplitude is just a phase of 180 degrees.

Vectors

A vector is an ordered list of numbers. In this course the numbers are complex and the list length is a power of two. A qubit is a vector with two entries, two qubits a vector with four, and so on. In NumPy a vector is a one-dimensional array with dtype=complex.

Vectors can be added, and multiplied by a number (a scalar). The most important vectors are the basis states. For one qubit there are two:

  • |0> = [1, 0]
  • |1> = [0, 1]

Every one-qubit state is a combination alpha*|0> + beta*|1>, called a superposition, where alpha and beta are the amplitudes. It is just a vector [alpha, beta].

Dirac notation (bra-ket)

Physicists write vectors in a notation due to Paul Dirac:

  • A ket |psi> is a column vector (the state itself).
  • A bra <psi| is the conjugate transpose: a row vector with every entry conjugated.
  • A bra followed by a ket, <phi|psi>, is the inner product (a single complex number).

You will see kets named after what they contain: |0>, |1>, |+>, |01>. The notation is a convenient label, not extra math. When you read |psi>, think "an array called psi".

The inner product

For two vectors phi and psi, the inner product is the sum of conj(phi[k]) * psi[k] over all positions. NumPy computes it with np.vdot(phi, psi) (note that vdot conjugates the first argument, exactly as we need; np.dot does not).

The inner product measures overlap:

  • <psi|psi> is the squared length of psi. For a valid quantum state it is exactly 1.
  • If <phi|psi> = 0, the vectors are orthogonal: perfectly distinguishable. |0> and |1> are orthogonal.
  • |<phi|psi>|^2 is the probability that a state psi is found to be phi by a suitable measurement. This is the general form of the Born rule you will use in Measurement and the Born Rule.
Python Norms, orthogonality and overlap

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.

Normalization

A quantum state must satisfy <psi|psi> = 1: the squared magnitudes of its amplitudes sum to 1. This is called normalization, and it is what lets squared magnitudes serve as probabilities. If you build a state by hand and it does not satisfy the rule, divide it by its norm (np.linalg.norm), as the last lines of the example above do.

A common pitfall: global phase

If you multiply a whole state by e^(i*theta), every amplitude rotates by the same angle. Nothing you can measure changes. States that differ only by such a factor are physically identical, and quantum programmers routinely ignore global phase. What does matter is the relative phase between amplitudes, such as the sign between + and - above. Both |+> and |-> have equal-size amplitudes, yet they behave very differently under later gates.

When two NumPy states look different, check whether one is just a multiple of the other before concluding they are physically different. A helper that compares abs(np.vdot(a, b)) with 1 does exactly that.

Python Global phase is invisible, relative phase is not

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 two states give identical measurement statistics in the basis 0/1, but they are different states, as the first two lines show. Gates in the next chapters will tell them apart.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is |3 + 4i|?

    Magnitude is sqrt(3^2 + 4^2) = sqrt(25) = 5.
  2. 2. Which NumPy function conjugates its first argument when computing an inner product?

    np.vdot conjugates its first argument; np.dot does not, which gives wrong answers for complex vectors.
  3. 3. What does e^(i*pi) equal?

    A rotation by half a turn (180 degrees) maps 1 to -1.
  4. 4. Two states differ only by a global phase e^(i*0.7). What can you say?

    A common factor of magnitude 1 changes no probability and no overlap magnitude, so the states are indistinguishable.

Summary

  • Complex numbers a + bi have a magnitude and a phase; e^(i*theta) is a pure rotation.
  • A qubit state is a normalized complex vector; <psi|psi> = 1.
  • Kets are column vectors, bras are conjugate transposes, and <phi|psi> is an inner product.
  • np.vdot computes inner products correctly for complex vectors.
  • Global phase does not matter; relative phase does.

Next: Matrices and Unitaries