Understanding VQE and QAOA Through Practical Examples
Variational Quantum Algorithms (VQAs) represent a powerful approach in quantum computing that combines quantum circuits with classical optimization. Today, we’ll explore two fundamental algorithms - the Variational Quantum Eigensolver (VQE) and the Quantum Approximate Optimization Algorithm (QAOA) - through concrete examples with Python implementation.
What Are Variational Quantum Algorithms?
VQAs work by parameterizing quantum circuits and using classical optimizers to find the optimal parameters that minimize a cost function. This hybrid quantum-classical approach makes them particularly suitable for near-term quantum devices.
Problem Setup: Finding the Ground State Energy
Let’s start with VQE by solving a classic problem: finding the ground state energy of a Hamiltonian. We’ll use the following Hamiltonian:
$$H = 0.5 \cdot Z_0 + 0.8 \cdot Z_1 + 0.3 \cdot X_0 \cdot X_1$$
where $Z$ and $X$ are Pauli operators.
For QAOA, we’ll solve a Max-Cut problem on a simple graph, which can be formulated as:
$$C = \sum_{(i,j) \in E} \frac{1 - Z_i Z_j}{2}$$
Complete Python Implementation
1 | import numpy as np |
Code Explanation
VQE Implementation Details
1. Ansatz Construction (create_ansatz method)
The ansatz is a parameterized quantum circuit that prepares trial quantum states. Our implementation uses:
- Rotation layers: Each qubit undergoes $R_Y(\theta)$ and $R_Z(\phi)$ rotations
- Entanglement layers: CNOT gates create quantum correlations between qubits
- Depth parameter: Controls circuit expressiveness
The rotation gates are defined as:
$$R_Y(\theta) = \begin{pmatrix} \cos(\theta/2) & -\sin(\theta/2) \ \sin(\theta/2) & \cos(\theta/2) \end{pmatrix}$$
$$R_Z(\phi) = \begin{pmatrix} e^{-i\phi/2} & 0 \ 0 & e^{i\phi/2} \end{pmatrix}$$
2. Expectation Value Measurement (measure_expectation method)
For each Pauli operator string in the Hamiltonian, we compute:
$$\langle H \rangle = \langle \psi(\theta) | H | \psi(\theta) \rangle$$
where $|\psi(\theta)\rangle$ is the state prepared by the ansatz.
3. Classical Optimization (optimize method)
Uses the COBYLA (Constrained Optimization BY Linear Approximation) algorithm to find parameters that minimize the energy expectation value. The optimization iteratively:
- Evaluates the cost function for current parameters
- Updates parameters based on gradient estimates
- Continues until convergence or maximum iterations
QAOA Implementation Details
1. Mixer Hamiltonian (apply_mixer method)
The mixer Hamiltonian is:
$$H_M = \sum_{i} X_i$$
Applied as $e^{-i\beta H_M}$, which creates superposition and allows exploration of the solution space.
2. Cost Hamiltonian (apply_cost method)
For Max-Cut, the cost Hamiltonian is:
$$H_C = \sum_{(i,j) \in E} \frac{1 - Z_i Z_j}{2}$$
This encodes the optimization objective directly into the quantum circuit.
3. QAOA Circuit Structure
The circuit alternates between cost and mixer layers:
$$|\psi(\gamma, \beta)\rangle = e^{-i\beta_p H_M} e^{-i\gamma_p H_C} \cdots e^{-i\beta_1 H_M} e^{-i\gamma_1 H_C} |+\rangle^{\otimes n}$$
Optimization Landscape Visualization
The 3D visualization shows:
- Surface plot: How cost varies with parameters
- Contour plot: Level curves for easier identification of minima
- Multiple local minima: Demonstrating the non-convex nature of the optimization
Performance Optimization
Key optimizations implemented:
- Vectorized operations: Using NumPy arrays instead of loops where possible
- State representation: Efficient complex number arrays for quantum states
- Sparse matrix operations: Only computing necessary expectation values
- Reduced grid resolution: 30×30 points for landscape plots balances detail and speed
Results Interpretation
VQE Results:
- The algorithm finds the ground state energy of the given Hamiltonian
- Convergence typically occurs within 50-100 iterations
- The energy decreases monotonically as optimization progresses
QAOA Results:
- Solves the Max-Cut problem on a triangle graph
- The maximum cut value represents the best bipartition of graph vertices
- For a triangle, the optimal cut value is 2 (cutting 2 out of 3 edges)
Optimization Landscape:
- Shows the rugged nature of the cost function
- Multiple local minima demonstrate why good initialization matters
- The global minimum corresponds to optimal parameters
Execution Results
====================================================================== VARIATIONAL QUANTUM ALGORITHMS: VQE AND QAOA ====================================================================== ====================================================================== VQE: Finding Ground State Energy ====================================================================== Initial parameters: [2.35330497 5.97351416 4.59925358 3.76148219] Initial energy: 0.370085 COBYLA Method: Final energy: -1.392839 Optimal parameters: [3.83274445 5.31063155 6.88284135 5.19585113] Iterations: 78 Powell Method: Final energy: -1.392837 Optimal parameters: [1.04628889 4.28374903 4.14776851 4.93366944] Iterations: 157 Nelder-Mead Method: Final energy: -1.392839 Optimal parameters: [2.65333757 3.8409344 5.95374019 3.90264057] Iterations: 190 ====================================================================== QAOA: Solving MaxCut Problem on Triangle Graph ====================================================================== Initial parameters: [0.98029403 0.98014248 0.3649501 5.44234523] Initial MaxCut value: 1.839420 Optimization Results: Final MaxCut value: 2.000000 Optimal parameters: [0.74732167 0.90539016 0.38911248 5.48714665] Iterations: 50 Final state probabilities: |000>: 0.0000 (cut value: 0) |001>: 0.1667 (cut value: 2) |010>: 0.1667 (cut value: 2) |011>: 0.1667 (cut value: 2) |100>: 0.1667 (cut value: 2) |101>: 0.1667 (cut value: 2) |110>: 0.1667 (cut value: 2) |111>: 0.0000 (cut value: 0) ====================================================================== Generating Visualizations... ====================================================================== Saved: vqe_qaoa_results.png

Saved: qaoa_3d_landscape.png

====================================================================== ANALYSIS COMPLETE ======================================================================
The visualizations clearly demonstrate how both VQE and QAOA navigate complex parameter spaces to find optimal solutions, showcasing the power of hybrid quantum-classical optimization algorithms.