|q⟩ Bad Qubits

beginner · Programming · The Qubit State Vector & Dirac Notation

Column Vectors in Code

The mathematical object at the heart of quantum computing is the state vector — a column of complex numbers that encodes everything the theory predicts about a qubit. When the simulator runs your circuit it hands back exactly this column, so knowing how to read it turns abstract algebra into something you can inspect and reason about directly.

The column vector for one qubit

A single qubit lives in a two-dimensional complex vector space. Its state is written

ψ=α0+β1=(αβ),|\psi\rangle = \alpha|0\rangle + \beta|1\rangle = \begin{pmatrix} \alpha \\ \beta \end{pmatrix},

where α,βC\alpha, \beta \in \mathbb{C} are the amplitudes and normalization requires α2+β2=1|\alpha|^2 + |\beta|^2 = 1. The two basis states are the simplest cases:

0=(10),1=(01).|0\rangle = \begin{pmatrix} 1 \\ 0 \end{pmatrix}, \qquad |1\rangle = \begin{pmatrix} 0 \\ 1 \end{pmatrix}.

The simulator stores this column as a JavaScript array indexed by basis state. For a one-qubit circuit the array has two entries: index 0 holds the amplitude α\alpha for 0|0\rangle and index 1 holds β\beta for 1|1\rangle.

Mapping array positions to basis strings

The index into the amplitude array is the decimal value of the binary basis string. For two qubits the array has four entries:

| index | basis state | binary string | |------:|:-----------:|:-------------:| | 0 | 00|00\rangle | "00" | | 1 | 01|01\rangle | "01" | | 2 | 10|10\rangle | "10" | | 3 | 11|11\rangle | "11" |

This convention means that qubit 0 is the most significant bit: the leftmost character in the binary string corresponds to qubit 0, and the rightmost to the last qubit. After running your circuit you can read off any amplitude by looking at the array element whose index matches the decimal value of the basis label you care about.

From amplitudes to probabilities

Each amplitude is a complex number, but the Born rule maps it to a real, non-negative probability:

P(outcome b)=αb2,P(\text{outcome } b) = |\alpha_b|^2,

where αb\alpha_b is the amplitude at array position bb. For example, if the simulator returns the state [12,i2][\tfrac{1}{\sqrt{2}},\, \tfrac{i}{\sqrt{2}}] you immediately know P(0)=12P(0) = \tfrac{1}{2} and P(1)=12P(1) = \tfrac{1}{2}, regardless of the complex phase.

Try it

Prepare the qubit in the 1|1\rangle state. Look at the State Vector panel after running: the amplitude array should read [0, 1], meaning all probability sits at index 1 (the 1|1\rangle basis state).

Run your code to see the quantum state.

After the grader passes, try changing your circuit to c.h(0) and re-running: you will see [12,12][\tfrac{1}{\sqrt{2}},\, \tfrac{1}{\sqrt{2}}] — two equal amplitudes, each with magnitude 12\tfrac{1}{\sqrt{2}} and probability 12\tfrac{1}{2}.

Sign in on the full site to ask questions and join the discussion.