Quantum in 7 Sessions › Session 6
Session 6 — Near-term methods: Hamiltonians and variational circuits
Session 6 of 7Self-studySimulator only
On this page: Read · Key ideas · Worked example · Try it · Second experiment · Common mistakes · Check yourself · Project
1. Read
- Chapter 8 — Hamiltonians, Dynamics, and Trotterization
- Chapter 9 — Variational and Hybrid Quantum Algorithms
- Chapter 10 — Quantum Machine Learning
Read the chapters first. The summary below is a reminder, not a replacement.
2. Key ideas
- A Hamiltonian describes the energy of a system. Measuring an operator many times gives its expectation value, the average result.
- A parameterized circuit has gates with adjustable angles. Different angles prepare different trial states.
- The hybrid loop: the quantum circuit prepares a trial state and estimates the energy; a classical optimizer chooses the next angles. VQE, QAOA, and variational classifiers all follow this pattern.
- At teaching sizes a classical computer solves these problems easily. A small demonstration is never evidence of quantum advantage, so always compare with an exact or classical answer.
3. Worked example
Work through this with pen and paper before touching the code.
From counts to an expectation value. The state Ry(θ)|0⟩ is cos(θ/2)|0⟩ + sin(θ/2)|1⟩.
- P(0) = cos²(θ/2) and P(1) = sin²(θ/2).
- Z gives +1 for outcome 0 and −1 for outcome 1, so ⟨Z⟩ = P(0) − P(1) = cos²(θ/2) − sin²(θ/2) = cos θ.
- Suppose 1,000 shots give 250 zeros and 750 ones. Then ⟨Z⟩ ≈ 0.25 − 0.75 = −0.5.
- cos θ = −0.5 means θ = 2π/3, or 120°.
This is how an energy is estimated on real hardware: from counts, not from the state itself.
4. Try it
A one-qubit version of the variational idea: find the angle θ that minimizes the expectation value of Z for the state Ry(θ)|0⟩. Write down what you expect before you run the code.
import numpy as np
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector, SparsePauliOp
Z = SparsePauliOp("Z")
def energy(theta):
qc = QuantumCircuit(1)
qc.ry(theta, 0) # the trial state
return Statevector(qc).expectation_value(Z).real # <Z> = cos(theta)
# The "classical optimizer": try many angles and keep the best one
thetas = np.linspace(0, 2 * np.pi, 41)
values = [energy(t) for t in thetas]
best = int(np.argmin(values))
print("lowest <Z> found:", round(values[best], 3))
print("at theta =", round(thetas[best], 3), "(pi =", round(np.pi, 3), ")")
Output (counts will vary slightly from run to run):
lowest <Z> found: -1.0 at theta = 3.142 (pi = 3.142 )
What to notice
The lowest value is −1 at θ = π, where Ry(θ) has rotated |0⟩ into |1⟩. For this state ⟨Z⟩ = cos θ, so you can check the answer by hand. That hand calculation is the classical baseline; in a real problem you would compare against exact diagonalization or brute force.
5. Second experiment
Repeat the scan with the operator X instead of Z. Predict first.
import numpy as np
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector, SparsePauliOp
X = SparsePauliOp("X")
thetas = np.linspace(0, 2 * np.pi, 41)
values = []
for t in thetas:
qc = QuantumCircuit(1)
qc.ry(t, 0)
values.append(Statevector(qc).expectation_value(X).real) # <X> = sin(theta)
best = int(np.argmin(values))
print("lowest <X>:", round(values[best], 3), "at theta =", round(thetas[best], 3),
"(3*pi/2 =", round(1.5 * np.pi, 3), ")")
Output:
lowest <X>: -1.0 at theta = 4.712 (3*pi/2 = 4.712 )
What it shows
The minimum is −1 at θ = 3π/2. For this state ⟨X⟩ = sin θ, so the result can be checked by hand. A different operator has its minimum at a different angle: the “best” circuit depends on the question you ask.
6. Common mistakes and tips
Common mistakes
- Treating a variational result as exact. It is an estimate with sampling error, and the optimizer may stop in a local minimum.
- Skipping the classical baseline. At teaching sizes the exact answer is always available, so compare against it.
- Calling a small successful demonstration “quantum advantage”.
Tips
- Plot the whole energy curve for a one-parameter problem before trusting any optimizer.
- Estimate ⟨Z⟩ as P(0) − P(1). Its sampling error is at most about 1/√shots.
- Change one thing at a time: the ansatz, the optimizer, or the number of shots.
7. Check yourself
Answer each question on paper, then open the answer to compare.
- Replace Z by X in the code. At which angle is ⟨X⟩ lowest? Work it out first.
Show answer
At θ = 3π/2, where ⟨X⟩ = sin θ = −1. See the second experiment.
- On a real device you would estimate ⟨Z⟩ from counts as P(0) − P(1). How does the number of shots affect the optimizer?
Show answer
Each energy estimate carries random error of roughly 1/√shots. If two angles differ in energy by less than that, the optimizer cannot tell which is better and may wander.
- Why is “my VQE found the right energy for a tiny molecule” not a claim of advantage?
Show answer
A classical computer solves a tiny instance exactly and instantly. Getting the right answer shows the method works, not that it beats classical computing.
8. Do a project
Pick one. Each follows the routine from Guide 1: predict, run the baseline, change one variable, analyze.
← Session 5Session 7: Communication, benchmarking, and your own project →
For instructors: Teacher notes for Session 6