
# The Qubit

> The qubit is the hydrogen atom of quantum computing — the simplest nontrivial quantum system, and
> the one we'll never stop using. Here we make it concrete: what it is physically, how superposition
> and the Born rule look in $\mathbb{C}^2$, and — for the first time in this program — how to **build
> and run a real quantum circuit on AWS Braket**. Everything stays on the free local simulator.

## Learning Objectives

After this lesson you will be able to:

1. Define a **qubit** as a two-level quantum system and give physical realizations.
2. Write a general qubit state and compute computational-basis **measurement probabilities**.
3. Explain **superposition** and how a single Hadamard creates it.
4. Build, run, and read out a circuit on the Braket **`LocalSimulator`** (free).
5. Connect the simulator's amplitudes and shot counts to the Born rule.

---

## Intuition

A bit is a switch: $0$ or $1$. A **qubit** is a quantum two-level system whose state can be any
*superposition* of $\lvert0\rangle$ and $\lvert1\rangle$ — a complex blend that, when measured, yields
$0$ or $1$ with probabilities set by the amplitudes. Physically a qubit can be the spin of an electron
($\uparrow/\downarrow$), the polarization of a photon (H/V), two energy levels of an atom or a
superconducting circuit (Term 4.4), and more. What makes all of them "qubits" is shared mathematics:
the state space is $\mathbb{C}^2$, and the rules are the four postulates of Course 1.1.

---

## Theory

### The qubit state

By Postulate 1 ([1.1.1](../c01-postulates/01-state-postulate.md)), a qubit's state is a unit vector in
$\mathbb{C}^2$:

$$
\lvert\psi\rangle = \alpha\lvert0\rangle + \beta\lvert1\rangle, \qquad \alpha,\beta\in\mathbb{C}, \qquad |\alpha|^2 + |\beta|^2 = 1,
$$

with the computational basis $\lvert0\rangle = \begin{psmallmatrix}1\\0\end{psmallmatrix}$,
$\lvert1\rangle = \begin{psmallmatrix}0\\1\end{psmallmatrix}$. The amplitudes $\alpha,\beta$ are complex;
their magnitudes give probabilities and their *relative* phase encodes interference
([1.1.1](../c01-postulates/01-state-postulate.md)).

### Born rule for a qubit

Measuring in the computational basis ($Z$ observable, [1.1.2](../c01-postulates/02-observables-measurement-postulate.md)):

$$
p(0) = |\langle0|\psi\rangle|^2 = |\alpha|^2, \qquad p(1) = |\langle1|\psi\rangle|^2 = |\beta|^2, \qquad p(0)+p(1)=1.
$$

A result of $0$ collapses the state to $\lvert0\rangle$; a result of $1$ to $\lvert1\rangle$.

### Superposition and the Hadamard

The most important single-qubit operation for *creating* superposition is the **Hadamard** gate
$H = \tfrac1{\sqrt2}\begin{psmallmatrix}1&1\\1&-1\end{psmallmatrix}$ ([Appendix E](../../appendix/E-math-identities.md)):

$$
H\lvert0\rangle = \tfrac1{\sqrt2}(\lvert0\rangle + \lvert1\rangle) = \lvert+\rangle, \qquad
H\lvert1\rangle = \tfrac1{\sqrt2}(\lvert0\rangle - \lvert1\rangle) = \lvert-\rangle.
$$

Starting from the definite state $\lvert0\rangle$, one $H$ produces an equal superposition: measuring it
in the computational basis gives $0$ or $1$ with probability $\tfrac12$ each. This is the "fair quantum
coin," and it's the natural first circuit to run. (Gates get the full treatment in Term 2; here we use
$H$ as our entry point.)

---

## Worked Examples

### Example 1 — Probabilities of a general qubit

For $\lvert\psi\rangle = \tfrac1{\sqrt3}\lvert0\rangle + \sqrt{\tfrac23}\,e^{i\pi/4}\lvert1\rangle$:
$p(0) = |1/\sqrt3|^2 = 1/3$ and $p(1) = |\sqrt{2/3}\,e^{i\pi/4}|^2 = 2/3$ (the phase $e^{i\pi/4}$ does not
affect the magnitude). Sum $= 1$. ✓ The phase *would* matter if we measured in the $X$ or $Y$ basis
([1.2.3](03-bases-and-expectation.md)).

### Example 2 — The Hadamard "coin"

Apply $H$ to $\lvert0\rangle$: $\lvert+\rangle = \tfrac1{\sqrt2}(\lvert0\rangle+\lvert1\rangle)$, so a
$Z$-measurement gives $0$ and $1$ each with probability $\tfrac12$. Apply a *second* $H$: since $H^2 = I$,
$HH\lvert0\rangle = \lvert0\rangle$ — measuring now gives $0$ with certainty. Two "random" coin flips
compose to a deterministic result: **interference**, not classical randomness. We'll see this directly on
the simulator below.

---

## Hands-on (Python)

### First, in NumPy

```python
import numpy as np

ket0 = np.array([1, 0], dtype=complex)
ket1 = np.array([0, 1], dtype=complex)
H = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2)

plus = H @ ket0                                  # |+>
print(np.round(plus, 4))                         # [0.7071 0.7071]
print(np.abs(plus)**2)                           # [0.5 0.5]  Born probabilities

print(np.allclose(H @ (H @ ket0), ket0))         # True: HH = I (interference -> back to |0>)
```

### Now on AWS Braket (local simulator — free) ⚙️

This is your first Braket circuit. It runs entirely on your machine via `LocalSimulator` — **no AWS
account, no cost** (see [Appendix A](../../appendix/A-braket-setup.md)). Three concepts: a
`Circuit` holds gates; `LocalSimulator` executes it; `shots` is how many times we sample the
measurement.

```python
# the_qubit.py — free, local, no AWS account needed.
from braket.circuits import Circuit
from braket.devices import LocalSimulator

# Build a one-qubit circuit: H on qubit 0 prepares |+>.
circ = Circuit().h(0)
print(circ)                                      # ASCII circuit diagram

# Run it. Braket measures all qubits in the computational (Z) basis by default.
device = LocalSimulator()                        # default backend: state-vector ("braket_sv")
result = device.run(circ, shots=1000).result()
print(result.measurement_counts)                 # ~ Counter({'0': ~500, '1': ~500})
```

```text
T  : |0|
q0 : -H-

Counter({'0': 508, '1': 492})
```

The counts are *samples* from the Born distribution $p(0)=p(1)=\tfrac12$ — finite-shot estimates with the
$1/\sqrt{M}$ error from [0.2.2](../../term-0-foundations/c02-probability-information/02-random-variables-expectation.md).
Now confirm the interference of Example 2 — two Hadamards return a *definite* $0$:

```python
circ2 = Circuit().h(0).h(0)                      # H then H = identity on |0>
counts = LocalSimulator().run(circ2, shots=1000).result().measurement_counts
print(counts)                                    # Counter({'0': 1000}) — always 0
```

If you also want the exact amplitudes (simulator-only superpower; real hardware never gives these), add a
state-vector result type:

```python
circ3 = Circuit().h(0)
circ3.state_vector()                             # request the full amplitude vector
sv = LocalSimulator().run(circ3, shots=0).result().values[0]
print(np.round(sv, 4))                           # [0.7071+0.j 0.7071+0.j] = |+>
```

> ⚠️ **Cost reminder.** `LocalSimulator` is free. The on-demand simulators (SV1/DM1/TN1) and QPUs are
> billed — we don't touch them until Term 5, and never automatically. Everything in Terms 1–4 runs
> locally.

---

## Exercises

**E1 (easy).** For $\lvert\psi\rangle = \sqrt{0.2}\,\lvert0\rangle + \sqrt{0.8}\,\lvert1\rangle$, give the
$Z$-measurement probabilities and predict the approximate counts for 1000 shots.

<details><summary>Solution</summary>

$p(0)=0.2$, $p(1)=0.8$. Over 1000 shots expect ≈ 200 `'0'` and ≈ 800 `'1'`, with statistical spread
$\sim\sqrt{1000\cdot0.2\cdot0.8}\approx 13$ counts.
</details>

**E2 (easy).** Write the Braket circuit that prepares $\lvert1\rangle$ from the default $\lvert0\rangle$,
and predict its measurement counts.

<details><summary>Solution</summary>

`Circuit().x(0)` (Pauli-$X$ = NOT flips $\lvert0\rangle\to\lvert1\rangle$). Counts: all `'1'`
(`Counter({'1': 1000})`), since the state is the definite $\lvert1\rangle$.
</details>

**E3 (medium).** Using only NumPy, verify that applying $H$ to $\lvert1\rangle$ gives $\lvert-\rangle$ and
that its $Z$-measurement probabilities are also $\tfrac12,\tfrac12$. Why do $\lvert+\rangle$ and
$\lvert-\rangle$ give identical computational-basis statistics despite being different states?

<details><summary>Solution</summary>

$H\lvert1\rangle = \tfrac1{\sqrt2}(\lvert0\rangle-\lvert1\rangle)=\lvert-\rangle$; $|{\pm}1/\sqrt2|^2=\tfrac12$
each. They share $Z$-statistics because those depend only on amplitude *magnitudes*; the distinguishing
relative phase ($+$ vs $-$) only shows up in a different basis (e.g. $X$), where $\lvert+\rangle\to0$ and
$\lvert-\rangle\to1$ with certainty — see [1.2.3](03-bases-and-expectation.md).
</details>

**E4 (medium).** Run (or simulate by hand) `Circuit().h(0).h(0).h(0)` on $\lvert0\rangle$. What are the
output statistics and why?

<details><summary>Solution</summary>

Three Hadamards: $H^3 = H^2\cdot H = I\cdot H = H$, so the state is $\lvert+\rangle$ and the counts are
≈ 50/50. An odd number of $H$'s acts like a single $H$; an even number like the identity.
</details>

**E5 (hard).** A state $\lvert\psi\rangle = \cos\tfrac\theta2\lvert0\rangle + \sin\tfrac\theta2\lvert1\rangle$
(real amplitudes). You run many shots and observe $p(1) \approx 0.15$. Estimate $\theta$, and state how
many shots you'd need to pin $p(1)$ to within $\pm0.01$ at 95% confidence.

<details><summary>Solution</summary>

$p(1) = \sin^2(\theta/2) = 0.15 \Rightarrow \sin(\theta/2) = \sqrt{0.15}\approx0.387 \Rightarrow \theta/2\approx0.398 \Rightarrow \theta\approx0.80$ rad ($\approx46°$).
For a $\pm\varepsilon$ estimate of a probability at 95% confidence, Hoeffding gives
$M \ge \frac{1}{2\varepsilon^2}\ln\frac{2}{\delta}$ with $\varepsilon=0.01$, $\delta=0.05$:
$M \ge \frac{1}{2(0.01)^2}\ln(40) \approx \frac{\ln 40}{2\times10^{-4}}\approx 1.84\times10^4$ shots
(from [0.2.2](../../term-0-foundations/c02-probability-information/02-random-variables-expectation.md)).
</details>

---

## Checkpoint

1. What is a qubit, and what is its state space? Name two physical realizations.
2. Write a general qubit state and its computational-basis measurement probabilities.
3. What does a single Hadamard do to $\lvert0\rangle$, and what do *two* do? Why?
4. In the Braket code, what are `Circuit`, `LocalSimulator`, `shots`, and `measurement_counts`?
5. Why are local-simulator runs free, and which Braket resources are not?

<details><summary>Answers</summary>

1. A two-level quantum system; state space $\mathbb{C}^2$. Realizations: electron spin, photon
   polarization, atomic/superconducting energy levels (any two).
2. $\lvert\psi\rangle=\alpha\lvert0\rangle+\beta\lvert1\rangle$ with $|\alpha|^2+|\beta|^2=1$; $p(0)=|\alpha|^2$, $p(1)=|\beta|^2$.
3. One $H$: $\lvert0\rangle\to\lvert+\rangle$ (equal superposition). Two: $H^2=I$ returns $\lvert0\rangle$
   — interference, not random.
4. `Circuit` is the gate sequence; `LocalSimulator` runs it on your machine; `shots` = number of
   measurement samples; `measurement_counts` is the histogram of outcome bit-strings.
5. `LocalSimulator` runs on your own CPU (no AWS service invoked); on-demand simulators (SV1/DM1/TN1) and
   QPUs are billed cloud resources (Term 5).

</details>

---

## Further Reading

- **[NC]** Nielsen & Chuang, §1.2, §2.2 — the qubit and superposition.
- **[Mer]** Mermin, *Quantum Computer Science*, Ch. 1 — qubits for computer scientists.
- **[SDK]** `amazon-braket-sdk-python` README & examples — the `Circuit` / `LocalSimulator` API.

---

← Prev: [1.1.4 Composite Systems Postulate](../c01-postulates/04-composite-systems-postulate.md) · Up: [Term 1](../README.md) · Next: [The Bloch Sphere](02-bloch-sphere.md) →
