OQCL   HomeStart Here7 SessionsTextbookProjectsQUEST LabSubmitUse CasesInstructor GuideReferences

Quantum in 7 Sessions › Session 6

1234567

Session 6 — Near-term methods: Hamiltonians and variational circuits

Session 6 of 7Self-studySimulator only

By the end of this session you can say what an expectation value is, describe the hybrid loop used by VQE and QAOA, and explain why a classical baseline is always required.

On this page: Read · Key ideas · Worked example · Try it · Second experiment · Common mistakes · Check yourself · Project

1. Read

Read the chapters first. The summary below is a reminder, not a replacement.

2. Key ideas

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⟩.

  1. P(0) = cos²(θ/2) and P(1) = sin²(θ/2).
  2. Z gives +1 for outcome 0 and −1 for outcome 1, so ⟨Z⟩ = P(0) − P(1) = cos²(θ/2) − sin²(θ/2) = cos θ.
  3. Suppose 1,000 shots give 250 zeros and 750 ones. Then ⟨Z⟩ ≈ 0.25 − 0.75 = −0.5.
  4. 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

Tips

7. Check yourself

Answer each question on paper, then open the answer to compare.

  1. 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.

  2. 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.

  3. 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