Curriculum / Real-World Quantum Python / VQE from Scratch
VQE from Scratch
Implement the Variational Quantum Eigensolver end-to-end: ansatz, energy estimation, and classical optimization.
VQE from Scratch
The Variational Quantum Eigensolver (VQE) is the flagship NISQ algorithm. It finds the ground state energy of a Hamiltonian by minimizing ⟨ψ(θ)|H|ψ(θ)⟩ over circuit parameters θ, using a classical optimizer in the outer loop.
The Variational Principle
The variational principle from quantum mechanics states: for any normalized state |ψ⟩,
⟨ψ|H|ψ⟩ ≥ E₀
where E₀ is the ground state energy. Equality holds only when |ψ⟩ = |ψ₀⟩ (the ground state). This means the expectation value is a cost function we can minimize with classical optimization.
Algorithm Structure
Step 1: Ansatz, Choose a parameterized quantum circuit |ψ(θ)⟩. For hardware: a Hardware-Efficient Ansatz (HEA) uses alternating RY/RZ layers and entangling CX gates. For chemistry: UCCSD (Unitary Coupled Cluster) mirrors the true quantum chemistry structure.
Step 2: Energy Estimation, Decompose H into Pauli strings: H = Σᵢ cᵢ Pᵢ where each Pᵢ is a tensor product of {I, X, Y, Z}. Measure ⟨Pᵢ⟩ by appending a basis rotation to the circuit and counting outcomes. Sum: ⟨H⟩ = Σᵢ cᵢ ⟨Pᵢ⟩.
Step 3: Gradient, Use the parameter-shift rule: ∂⟨H⟩/∂θᵢ = [⟨H⟩(θᵢ+π/2) − ⟨H⟩(θᵢ−π/2)] / 2
This requires two quantum circuit evaluations per parameter. For n parameters: 2n circuit runs per gradient step.
Step 4: Optimize, Run gradient descent or COBYLA with the quantum function as the cost: θ ← θ − α ∇⟨H⟩(θ)
Repeat until convergence.
Hamiltonian: Heisenberg Model
The Hamiltonian in this lesson is H = Z⊗Z + X⊗X + Y⊗Y (the isotropic Heisenberg model, 2 qubits). This has a known ground state energy of −3, attained by the spin singlet (|01⟩−|10⟩)/√2 (the state |00⟩−|11⟩ is a triplet with eigenvalue +1).
Measuring ⟨H⟩ requires three separate circuits:
- •⟨Z⊗Z⟩: measure in Z basis directly
- •⟨X⊗X⟩: apply H⊗H before measuring
- •⟨Y⊗Y⟩: apply S†H⊗S†H before measuring
Convergence and Barren Plateaus
VQE converges when the gradient norm drops below a tolerance. Key practical concerns:
- •Barren plateaus: for deep ansätze on many qubits, gradients vanish exponentially, the optimizer gets stuck. Use shallow circuits or layer-by-layer training.
- •Shot noise: with finite shots, energy estimates have statistical noise. Use enough shots (≥1000) per energy evaluation.
- •Local minima: gradient descent can get stuck. Use random restarts or adaptive learning rates.
This is the opening of the lesson. The full walkthrough, the interactive circuit, and the graded challenge continue inside myqubit.
How this lesson works
A hands-on coding challenge. You write Qiskit-compatible Python in the browser editor, run it instantly via WebAssembly, watch the circuit and Bloch sphere react, and pass automatic output checks. The AI tutor Qubitus gives Socratic hints if you get stuck.
Part of: Real-World Quantum Python
Write production-quality quantum Python, circuit optimization, hybrid algorithms, cloud backends, noise modeling, and software engineering patterns.
This lesson is part of Pro
Unlock Real-World Quantum Python and all 10 tracks with Pro: $12.99/month, $79/year, or $97 lifetime. Start with the free track first if you are new.