
# Bases & Expectation Values

> A measurement is always made *in a basis*. The computational ($Z$) basis is just one choice; the $X$
> and $Y$ bases ask different questions of the same qubit, and the relative phase that $Z$ can't see
> becomes a definite answer in $X$ or $Y$. This lesson makes "measure in a basis" precise, ties the
> three Pauli bases to the three Bloch axes, and shows the one trick real hardware needs — **rotate the
> basis you care about onto $Z$, then measure** — with runnable Braket code on the free local simulator.

## Learning Objectives

After this lesson you will be able to:

1. Write the **eigenbases** of $X$, $Y$, $Z$ and the Born rule for a measurement in *any* orthonormal basis.
2. Reduce a measurement in an arbitrary basis to a **basis-change unitary** followed by a computational-basis readout.
3. Compute an **expectation value** $\langle A\rangle$ from outcome probabilities, and recognize $\langle X\rangle,\langle Y\rangle,\langle Z\rangle$ as the **Bloch coordinates**.
4. Build Braket circuits that measure a qubit in the $X$, $Y$, and $Z$ bases and **estimate** $\langle\sigma_k\rangle$ from shot counts.
5. Relate the finite-shot error of an estimate to the **variance** of a $\pm1$ observable.

---

## Intuition

In [1.2.1](01-the-qubit.md) we only ever measured in the computational basis — the question "are you
$\lvert0\rangle$ or $\lvert1\rangle$?" But $\lvert+\rangle$ and $\lvert-\rangle$ give *identical*
$Z$-statistics ($50/50$) despite being different states; their distinguishing relative phase is invisible
to $Z$. Ask a different question — "are you $\lvert+\rangle$ or $\lvert-\rangle$?", the $X$-basis
measurement — and the two states separate perfectly. Each Pauli observable corresponds to one of the
three Bloch axes ([1.2.2](02-bloch-sphere.md)); measuring it projects the Bloch vector onto that axis.
Real devices only ever measure along $Z$, so to ask an $X$ or $Y$ question we first *rotate* that axis
onto $Z$ with a gate, then read out. The averaged answer over many shots is the **expectation value**,
and the three expectation values are exactly the coordinates of the Bloch vector.

---

## Theory

### The three Pauli bases

Each Pauli operator is Hermitian with eigenvalues $\pm1$; its two eigenvectors form an orthonormal basis
of $\mathbb{C}^2$ ([1.1.2](../c01-postulates/02-observables-measurement-postulate.md), [Appendix E](../../appendix/E-math-identities.md)):

| Observable | Eigenvalue $+1$ | Eigenvalue $-1$ | Bloch axis |
|---|---|---|---|
| $Z$ | $\lvert0\rangle$ | $\lvert1\rangle$ | $\pm z$ |
| $X$ | $\lvert+\rangle = \tfrac1{\sqrt2}(\lvert0\rangle+\lvert1\rangle)$ | $\lvert-\rangle = \tfrac1{\sqrt2}(\lvert0\rangle-\lvert1\rangle)$ | $\pm x$ |
| $Y$ | $\lvert{+}i\rangle = \tfrac1{\sqrt2}(\lvert0\rangle+i\lvert1\rangle)$ | $\lvert{-}i\rangle = \tfrac1{\sqrt2}(\lvert0\rangle-i\lvert1\rangle)$ | $\pm y$ |

These are the three "natural" bases of a single qubit, mutually unbiased: a state definite in one basis
is maximally uncertain in the other two (e.g. $\lvert0\rangle$ gives $50/50$ in both $X$ and $Y$).

### The Born rule in an arbitrary basis

Let $\{\lvert b_0\rangle, \lvert b_1\rangle\}$ be any orthonormal basis. Measuring $\lvert\psi\rangle$ in
that basis yields outcome $b_k$ with probability ([1.1.2](../c01-postulates/02-observables-measurement-postulate.md))

$$
p(b_k) = |\langle b_k\lvert\psi\rangle|^2, \qquad \sum_k p(b_k) = 1,
$$

and collapses the state to $\lvert b_k\rangle$. For the $X$ basis, $p(+) = |\langle+\lvert\psi\rangle|^2$
and $p(-) = |\langle-\lvert\psi\rangle|^2$; for $Y$, replace $\lvert\pm\rangle$ by $\lvert\pm i\rangle$.

### Measurement as a basis change onto $Z$

Hardware measures one fixed basis (the computational $Z$ basis). To measure an observable
$A = \sum_k \lambda_k \lvert b_k\rangle\langle b_k\lvert$, collect its eigenvectors as the columns of a
unitary $U = \big[\,\lvert b_0\rangle\ \ \lvert b_1\rangle\,\big]$, so that $U\lvert0\rangle = \lvert b_0\rangle$.
Then $U^\dagger$ rotates the eigenbasis onto the computational basis, and

$$
p(b_k) = |\langle b_k\lvert\psi\rangle|^2 = |\langle k\lvert U^\dagger\lvert\psi\rangle|^2 :
$$

**measuring $A$ on $\lvert\psi\rangle$ = applying $U^\dagger$, then a computational-basis measurement.** For
the Pauli bases this needs just one or two gates:

$$
\underbrace{\text{measure }Z}_{\text{nothing}},\qquad
\underbrace{\text{measure }X}_{\text{apply }H},\qquad
\underbrace{\text{measure }Y}_{\text{apply }S^\dagger\text{ then }H}.
$$

Indeed $H\lvert+\rangle=\lvert0\rangle$, $H\lvert-\rangle=\lvert1\rangle$ (so $H = H^\dagger$ rotates the
$X$-basis onto $Z$); and $HS^\dagger\lvert{+}i\rangle = \lvert0\rangle$, $HS^\dagger\lvert{-}i\rangle=\lvert1\rangle$
with $S^\dagger = \operatorname{diag}(1,-i)$ (derived in Exercise E3). After the rotation, computational
outcome $0$ corresponds to eigenvalue $+1$ and outcome $1$ to $-1$.

### Expectation values, and the link to the Bloch vector

The **expectation value** of $A$ is the mean eigenvalue over many measurements
([1.1.2](../c01-postulates/02-observables-measurement-postulate.md)):

$$
\langle A\rangle = \langle\psi\lvert A\lvert\psi\rangle = \sum_k \lambda_k\,p(b_k).
$$

For a $\pm1$-valued observable (any Pauli) this collapses to a difference of two probabilities,

$$
\boxed{\ \langle\sigma\rangle = p_{+} - p_{-} = 1 - 2\,p_{-}\ }
$$

so an estimate is immediate from shot counts: $\langle\sigma\rangle \approx (N_{+} - N_{-})/N$. Computing
all three Pauli expectation values reconstructs the Bloch vector of [1.2.2](02-bloch-sphere.md):

$$
\vec r = \big(\langle X\rangle,\ \langle Y\rangle,\ \langle Z\rangle\big),
\qquad
\langle Z\rangle = \cos\theta,\ \ \langle X\rangle = \sin\theta\cos\varphi,\ \ \langle Y\rangle = \sin\theta\sin\varphi .
$$

So the abstract "measure three observables" *is* the concrete "find the point on the sphere."

---

## Worked Examples

### Example 1 — $\lvert+\rangle$ in all three bases

For $\lvert+\rangle$: in $Z$, $p(0)=p(1)=\tfrac12$ so $\langle Z\rangle = 0$. In $X$, $\lvert+\rangle$ is the
$+1$ eigenstate, so $p(+)=1$ deterministically and $\langle X\rangle = 1$. In $Y$,
$|\langle{+}i\lvert+\rangle|^2 = \tfrac12$, so $\langle Y\rangle = 0$. The Bloch vector is
$(\langle X\rangle,\langle Y\rangle,\langle Z\rangle) = (1,0,0)$ — the $+x$ axis, exactly as
[1.2.2](02-bloch-sphere.md) predicted. Only the $X$ measurement "sees" the relative phase that
distinguishes $\lvert+\rangle$ from $\lvert-\rangle$.

### Example 2 — Expectation values of a tilted real state

Take $\lvert\psi\rangle = \cos\tfrac\pi6\lvert0\rangle + \sin\tfrac\pi6\lvert1\rangle$ (real amplitudes,
$\varphi=0$, $\theta=\tfrac\pi3$). Then $\langle Z\rangle = \cos^2\tfrac\pi6 - \sin^2\tfrac\pi6 = \cos\tfrac\pi3 = \tfrac12$,
$\langle X\rangle = 2\cos\tfrac\pi6\sin\tfrac\pi6 = \sin\tfrac\pi3 = \tfrac{\sqrt3}2$, and $\langle Y\rangle = 0$
(real amplitudes never tilt toward $\pm y$). Bloch vector $\big(\tfrac{\sqrt3}2, 0, \tfrac12\big)$, with
$|\vec r| = 1$ as required for a pure state.

---

## Hands-on (Python)

### Exact, in NumPy

```python
import numpy as np

ket0 = np.array([1, 0], dtype=complex)
ket1 = np.array([0, 1], dtype=complex)
X = np.array([[0, 1], [1, 0]], dtype=complex)
Y = np.array([[0, -1j], [1j, 0]], dtype=complex)
Z = np.array([[1, 0], [0, -1]], dtype=complex)

def expect(A, psi):
    """Exact expectation <psi|A|psi> (real for Hermitian A)."""
    return np.real(psi.conj() @ A @ psi)

plus = (ket0 + ket1) / np.sqrt(2)                       # |+>
print(np.round([expect(A, plus) for A in (X, Y, Z)], 6))  # [1. 0. 0.] -> Bloch +x

# Born probabilities in the X basis come from <+|psi>, <-|psi>:
minus = (ket0 - ket1) / np.sqrt(2)
psi = np.cos(np.pi/6)*ket0 + np.sin(np.pi/6)*ket1       # Worked Example 2
print(round(abs(np.vdot(plus, psi))**2, 6),             # 0.933013  = p(+) in X basis
      round(abs(np.vdot(minus, psi))**2, 6))            # 0.066987  = p(-) in X basis
print(np.round([expect(A, psi) for A in (X, Y, Z)], 6))   # [0.866025 0. 0.5]
```

### Estimating $\langle\sigma\rangle$ on Braket (local simulator — free) ⚙️

Braket always measures in the computational ($Z$) basis, so we **rotate the axis we want onto $Z$** before
measuring — `H` for $X$, `S†` then `H` (`.si(0).h(0)`) for $Y$, nothing for $Z$ — then read
$\langle\sigma\rangle \approx (N_0 - N_1)/N$ from the counts. (The deeper Braket result-type API arrives in
[Term 2.1](../../term-2-qc-core/c01-gates-circuits/04-braket-first-circuits.md); here we sample, as in
[1.3.1](../c03-measurement/01-projective-measurement.md).)

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

device = LocalSimulator()                                # free local state-vector simulator

def estimate_pauli(prep, basis, shots=20000):
    """Estimate <sigma> on the state prepared by `prep` (a function adding gates)."""
    c = Circuit()
    prep(c)                                              # state preparation
    if basis == "X":
        c.h(0)                                           # rotate X-basis onto Z
    elif basis == "Y":
        c.si(0).h(0)                                     # rotate Y-basis onto Z (S† then H)
    # basis == "Z": measure directly
    counts = device.run(c, shots=shots).result().measurement_counts
    n0, n1 = counts.get("0", 0), counts.get("1", 0)
    return (n0 - n1) / (n0 + n1)                          # <sigma> = p(+) - p(-)

prep_plus = lambda c: c.h(0)                              # |+> = H|0>
print("Estimated <X>,<Y>,<Z> on |+>:",
      [round(estimate_pauli(prep_plus, b), 3) for b in ("X", "Y", "Z")])
# ≈ [1.0, 0.0, 0.0]  -> the +x axis, matching the exact NumPy result
```

> **Why ±1 estimates are cheap.** A Pauli outcome is $\pm1$ with variance $\operatorname{Var}(\sigma) = 1 - \langle\sigma\rangle^2 \le 1$
> ([0.2.2](../../term-0-foundations/c02-probability-information/02-random-variables-expectation.md)), so the
> standard error of $(N_0-N_1)/N$ is $\le 1/\sqrt N$. With $20{,}000$ shots, $\langle X\rangle$ on
> $\lvert+\rangle$ lands within $\sim0.007$ of $1$ essentially every run.

---

## Exercises

**E1 (easy).** For $\lvert1\rangle$, give the $X$-basis probabilities and $\langle X\rangle$. Which Bloch axis is this?

<details><summary>Solution</summary>

$\langle+\lvert1\rangle = \tfrac1{\sqrt2}$, $\langle-\lvert1\rangle = -\tfrac1{\sqrt2}$, so $p(+)=p(-)=\tfrac12$ and
$\langle X\rangle = 0$. Likewise $\langle Y\rangle=0$, while $\langle Z\rangle=-1$: the $-z$ axis, the south pole. ∎
</details>

**E2 (easy).** Which single Pauli basis perfectly distinguishes $\lvert+\rangle$ from $\lvert-\rangle$, and which one cannot tell them apart at all?

<details><summary>Solution</summary>

The **$X$ basis** distinguishes them with certainty ($\lvert+\rangle\to+1$, $\lvert-\rangle\to-1$). The **$Z$
basis** cannot: both give $50/50$. ($Y$ also gives $50/50$ for each.) The distinguishing information is the
relative phase, visible only along $\pm x$. ∎
</details>

**E3 (medium).** Show that $U^\dagger = H S^\dagger$ rotates the $Y$ eigenbasis onto the computational basis,
i.e. $HS^\dagger\lvert{+}i\rangle = \lvert0\rangle$ and $HS^\dagger\lvert{-}i\rangle = \lvert1\rangle$.

<details><summary>Solution</summary>

$S^\dagger = \operatorname{diag}(1,-i)$. Then $S^\dagger\lvert{+}i\rangle = \tfrac1{\sqrt2}(\lvert0\rangle + (-i)(i)\lvert1\rangle)
= \tfrac1{\sqrt2}(\lvert0\rangle+\lvert1\rangle) = \lvert+\rangle$, and $H\lvert+\rangle=\lvert0\rangle$. Similarly
$S^\dagger\lvert{-}i\rangle = \tfrac1{\sqrt2}(\lvert0\rangle-\lvert1\rangle)=\lvert-\rangle$ and $H\lvert-\rangle=\lvert1\rangle$.
So in a circuit, `.si(0).h(0)` (apply $S^\dagger$, then $H$) implements $U^\dagger = HS^\dagger$. ∎
</details>

**E4 (medium).** A qubit is measured and reports $\langle X\rangle = 0$, $\langle Y\rangle = 0$, $\langle Z\rangle = 1$.
What is the state? What if instead $(\langle X\rangle,\langle Y\rangle,\langle Z\rangle) = (0, -1, 0)$?

<details><summary>Solution</summary>

The Bloch vector is $\vec r = (\langle X\rangle,\langle Y\rangle,\langle Z\rangle)$. First case: $(0,0,1)$ = north
pole = $\lvert0\rangle$. Second case: $(0,-1,0)$ = $-y$ axis = $\lvert{-}i\rangle = \tfrac1{\sqrt2}(\lvert0\rangle-i\lvert1\rangle)$.
For a pure state $|\vec r|=1$, so the three expectation values fix the state up to global phase. ∎
</details>

**E5 (hard).** You want to estimate $\langle Z\rangle$ of a state with true value $\langle Z\rangle = 0.6$ to within
$\pm0.01$ at $95\%$ confidence. About how many shots are needed?

<details><summary>Solution</summary>

A $Z$ outcome is $\pm1$ with $\operatorname{Var}(Z) = 1 - \langle Z\rangle^2 = 1-0.36 = 0.64$. The estimator
$(N_0-N_1)/N$ has standard error $\sqrt{\operatorname{Var}/N} = \sqrt{0.64/N}$. For a $\pm0.01$ half-width at
$95\%$ ($z\approx1.96$): $1.96\sqrt{0.64/N} \le 0.01 \Rightarrow N \ge 1.96^2\cdot0.64/0.01^2 \approx 2.46\times10^4$
shots (from [0.2.2](../../term-0-foundations/c02-probability-information/02-random-variables-expectation.md)).
Note the worst case is $\langle Z\rangle=0$ (variance $1$), needing $\sim3.84\times10^4$. ∎
</details>

---

## Checkpoint

1. Write the eigenstates of $X$, $Y$, $Z$ and the Bloch axis each basis measures.
2. State the Born rule for a measurement in an arbitrary orthonormal basis.
3. How do you measure $X$ (and $Y$) on hardware that only measures $Z$?
4. Give the formula for $\langle\sigma\rangle$ in terms of the two outcome probabilities, and connect $(\langle X\rangle,\langle Y\rangle,\langle Z\rangle)$ to the Bloch sphere.
5. Why is the standard error of a Pauli expectation estimate at most $1/\sqrt N$?

<details><summary>Answers</summary>

1. $Z$: $\lvert0\rangle,\lvert1\rangle$ ($\pm z$); $X$: $\lvert\pm\rangle$ ($\pm x$); $Y$: $\lvert\pm i\rangle$ ($\pm y$).
2. $p(b_k) = |\langle b_k\lvert\psi\rangle|^2$; outcome $b_k$ collapses the state to $\lvert b_k\rangle$.
3. Rotate the target eigenbasis onto $Z$ first: apply $H$ to measure $X$, apply $S^\dagger$ then $H$ to measure $Y$, then read out in the computational basis.
4. $\langle\sigma\rangle = p_+ - p_-$; the three Pauli expectation values are the Cartesian coordinates of the Bloch vector $\vec r$.
5. A $\pm1$ observable has variance $1-\langle\sigma\rangle^2\le1$, so the mean's standard error is $\sqrt{\operatorname{Var}/N}\le1/\sqrt N$.

</details>

---

## Further Reading

- **[NC]** Nielsen & Chuang, §2.2.3–2.2.5 — measurement, observables, and changing measurement basis.
- **[Pre]** Preskill, *Ph219*, Ch. 2 — qubit observables and mutually unbiased bases.
- **[SDK]** `amazon-braket-sdk-python` — the `Circuit` gate set (`h`, `s`, `si`) and `measurement_counts`.

---

← Prev: [The Bloch Sphere](02-bloch-sphere.md) · Up: [Term 1](../README.md) · Next: [Projective Measurement](../c03-measurement/01-projective-measurement.md) →
