Skip to content

thiagorgs/qaoa-maxcut

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

QAOA for Maximum Cut: Quantum Combinatorial Optimization

Author: Thiago Girao - PhD candidate in Physics, researching quantum information and quantum computing

Overview

This repository contains a complete, production-quality implementation of the Quantum Approximate Optimization Algorithm (QAOA) applied to the Maximum Cut (MaxCut) problem. QAOA is a hybrid quantum-classical algorithm that leverages current near-term quantum devices to solve combinatorial optimization problems.

The MaxCut problem is a fundamental NP-hard problem in computer science: given an undirected graph, partition the vertices into two sets to maximize the number of edges between the sets. This implementation demonstrates how variational quantum algorithms can approximate solutions to such hard problems.

The Maximum Cut Problem

Problem Definition

Given an undirected graph G = (V, E), the Maximum Cut problem seeks to partition vertices into two disjoint sets S and S̄ such that the number of edges between S and S̄ is maximized:

MaxCut = max |{(u,v) ∈ E : u ∈ S, v ∈ S̄}|

Classical Hardness

MaxCut is NP-hard, meaning there is no known polynomial-time algorithm to solve it for large instances. The best classical polynomial-time approximation algorithm (Goemans-Williamson) achieves 0.878 approximation ratio.

Quantum Relevance

QAOA provides an alternative approach using quantum computing, potentially offering better approximation ratios for certain problem instances. Even on current NISQ (Noisy Intermediate-Scale Quantum) devices, QAOA can provide useful approximate solutions.

QAOA Algorithm

Mathematical Formulation

QAOA is a variational quantum algorithm that works as follows:

1. Cost Hamiltonian

For MaxCut, the cost Hamiltonian is:

H_C = (1/2) * Σ_{(i,j) ∈ E} (I - Z_i Z_j)

This Hamiltonian has eigenvalues aligned with the MaxCut objective. A bitstring where Z_i Z_j = -1 (qubits in different partitions) contributes positively to the energy.

2. Mixer Hamiltonian

The mixer Hamiltonian enables quantum tunneling between different regions of the search space:

H_M = Σ_i X_i

This is the transverse field that allows the quantum state to explore different computational basis states.

3. QAOA Circuit (p layers)

The QAOA state after p layers is:

|ψ(γ, β)⟩ = e^{-i β_p H_M} e^{-i γ_p H_C} ... e^{-i β_1 H_M} e^{-i γ_1 H_C} |+⟩^⊗n

Where:

  • |+⟩ = (|0⟩ + |1⟩)/√2 is the initial equal superposition
  • γ = (γ_1, ..., γ_p) are cost function angles
  • β = (β_1, ..., β_p) are mixer angles
  • p is the depth (number of layers)

4. Classical Optimization

The expected value of the cost Hamiltonian is:

E(γ, β) = ⟨ψ(γ, β)| H_C |ψ(γ, β)⟩

A classical optimizer (COBYLA in this implementation) minimizes -E(γ, β) to find optimal parameters.

Approximation Quality

The approximation ratio r is:

r = E_QAOA / E_MAX

where E_QAOA is the energy found by QAOA and E_MAX is the optimal value. QAOA guarantees r ≥ 0.692 for MaxCut with p=1, and the ratio typically improves with larger p.

Implementation Details

Core Components

  1. Cost Hamiltonian Construction (create_maxcut_hamiltonian)

    • Builds SparsePauliOp representation of the MaxCut cost function
    • Efficient for both sparse and dense graphs
  2. QAOA Circuit Building (build_qaoa_circuit)

    • Constructs parametrized quantum circuits
    • Implements exp(-i γ H_C) using controlled-Z gates
    • Implements exp(-i β H_M) using RX rotations
    • Supports arbitrary number of layers p
  3. Expectation Value Computation (compute_expectation)

    • Uses Qiskit's Statevector simulator for exact simulation
    • Computes ⟨ψ| H_C |ψ⟩ efficiently
  4. Classical Optimization (optimize_qaoa)

    • Uses scipy.optimize.minimize with COBYLA method
    • Tracks convergence via callback function
    • Supports random restarts for global optimization
  5. Solution Extraction (extract_solution)

    • Samples most probable bitstring from QAOA state
    • Returns probability of the solution
  6. Exact Solver (brute_force_maxcut)

    • Computes exact solution for small graphs (n ≤ 20)
    • Used for validation and benchmarking

Visualizations

  1. Graph with Cut (plot_graph_with_cut)

    • Visualizes graph with partition coloring
    • Green edges indicate cut edges
    • Shows exact and approximate solution quality
  2. Optimization Landscape (plot_optimization_landscape)

    • 2D heatmap of energy vs (gamma, beta) for p=1
    • Reveals structure of parameter space
  3. Convergence Plot (plot_convergence)

    • Tracks optimization progress over iterations
    • Useful for hyperparameter tuning

Technologies

  • Qiskit (≥1.0): IBM's quantum computing framework

    • QuantumCircuit for circuit building
    • Statevector for state representation and simulation
    • SparsePauliOp for Hamiltonian representation
  • NumPy (≥1.24): Numerical computations

  • SciPy (≥1.10): Optimization algorithms

  • NetworkX (≥3.0): Graph representation and analysis

  • Matplotlib (≥3.7): Visualization

Installation & Usage

Setup

# Clone the repository
git clone <repository-url>
cd 04-qaoa-maxcut

# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Running the Code

python qaoa_maxcut.py

What the Script Does

The script demonstrates QAOA on three test cases:

  1. 6-node Random 3-regular Graph

    • Solves with QAOA at depths p=1, 2, 3
    • Compares with exact brute-force solution
    • Computes approximation ratios
    • Generates convergence plots and visualizations
    • Plots optimization landscape for p=1
  2. 12-node Cycle Graph

    • Demonstrates QAOA on a larger instance
    • Uses p=2 layers
    • Shows practical applicability
  3. Complete Graph K_5

    • MaxCut has known optimal value (5)
    • Demonstrates QAOA performance on dense graphs
    • Uses p=2 layers

Expected Output

The script produces:

Console Output:

  • Iteration counts and convergence status
  • Best energies found during optimization
  • QAOA bitstring solutions
  • Cut values and approximation ratios
  • Summary statistics

Generated Files:

  • maxcut_exact.png: Exact solution visualization
  • maxcut_qaoa_p1.png, maxcut_qaoa_p2.png, maxcut_qaoa_p3.png: QAOA solutions at different depths
  • maxcut_cycle.png: Solution on 12-node cycle
  • maxcut_k5.png: Solution on K_5
  • convergence_p1.png, convergence_p2.png, convergence_p3.png: Optimization convergence
  • optimization_landscape.png: Energy landscape for p=1

Customization

Using Your Own Graph

import networkx as nx
from qaoa_maxcut import optimize_qaoa, extract_solution, plot_graph_with_cut

# Create or load your graph
G = nx.read_edgelist('your_graph.edgelist')

# Run QAOA
result = optimize_qaoa(G, p=2)

# Extract and visualize
circuit = build_qaoa_circuit(G, result['gamma'], result['beta'], p=2)
bitstring, prob = extract_solution(circuit)
plot_graph_with_cut(G, bitstring, "Your Graph", "output.png")

Adjusting QAOA Depth

Increasing p typically improves approximation quality at the cost of:

  • Longer classical optimization time
  • Deeper quantum circuits (more noise on real hardware)
  • More parameters to optimize (2p parameters total)
result = optimize_qaoa(G, p=3)  # Deeper circuit

Trying Different Optimizers

QAOA optimization works with any scipy optimizer:

result = optimize_qaoa(G, p=2, method='L-BFGS-B')  # Bounded optimization
result = optimize_qaoa(G, p=2, method='SLSQP')     # Sequential Least Squares

Performance Characteristics

Computational Complexity

  • Circuit Simulation: O(2^n) where n is number of qubits
  • Per Iteration: O(n_edges × p) for circuit construction + O(2^n) for state simulation
  • Optimization: Typically converges in 100-500 iterations
  • Total Time: O(n^2 × 2^n) for n ≤ 12-15 qubits

Scalability

Current implementation uses statevector simulation, which scales exponentially:

  • n ≤ 10: Very fast (< 1 second)
  • n = 15: Few seconds
  • n = 20: Minutes
  • n > 20: Infeasible on classical simulators

For larger instances, consider:

  • Noise-aware simulation (Qiskit Aer)
  • Approximate simulation methods
  • Real quantum hardware via IBM Quantum

Theoretical Background & References

Key Papers

  1. Farhi, E., Goldstone, J., & Gutmann, S. (2014) A Quantum Approximate Optimization Algorithm arXiv:1411.4028

    • Original QAOA paper introducing the algorithm
  2. Hadfield, S., Wang, Z., O'Gorman, B., et al. (2019) From the Perturbed Single Layer to the Collective Optimal QAOA arXiv:1910.08980

    • Analysis of QAOA performance
  3. Zhou, L., Wang, S.-T., Choi, S., et al. (2020) Quantum Approximate Optimization Algorithm: Performance, Mechanism, and Implementation on Near-Term Devices Physical Review X 10, 021067

    • Comprehensive review and implementation guide

Classical References

  • Goemans, M. X., & Williamson, D. P. (1995). "Improved approximation algorithms for maximum cut and satisfiability problems using semidefinite programming." Journal of the ACM, 42(6), 1115-1145.
  • Karp, R. M. (1972). "Reducibility among combinatorial problems." In Complexity of computer computations.

Quantum Computing Background

QAOA bridges quantum and classical computing:

  • Quantum Advantage: Explores parameter space using quantum superposition
  • Variational: Uses classical optimizer to find good parameters
  • NISQ-Friendly: Works with shallow circuits suitable for near-term devices
  • Hybrid: Combines quantum circuit evaluation with classical optimization

This approach is considered one of the most promising near-term applications of quantum computers.

Code Quality

This implementation emphasizes:

  • Correctness: Validated against exact solutions for small instances
  • Clarity: Well-documented functions with comprehensive docstrings
  • Efficiency: Optimized Hamiltonian construction and simulation
  • Reproducibility: Fixed random seeds, detailed output
  • Extensibility: Modular design for custom graphs and optimizers

Troubleshooting

ImportError: No module named 'qiskit'

Install dependencies:

pip install -r requirements.txt

Slow Performance

For large graphs (n > 15), statevector simulation becomes slow. Consider:

  • Reducing graph size for testing
  • Using sparse graphs (fewer edges)
  • Using only p=1 or p=2 layers

Optimization Not Converging

Try:

  • Increasing maxiter in scipy.optimize.minimize
  • Using different optimizer (e.g., 'SLSQP')
  • Checking graph structure (disconnected components often harder)

Future Enhancements

Potential extensions of this implementation:

  1. Noise Models: Add quantum noise simulation for realistic NISQ analysis
  2. Warm Starting: Use classical approximate solutions to initialize QAOA
  3. Parameter Transferability: Analyze parameter reuse across graph families
  4. Hybrid Algorithms: Combine QAOA with classical heuristics
  5. Hardware Compilation: Prepare circuits for specific quantum backends
  6. Detailed Analytics: More sophisticated performance benchmarking

License

This implementation is provided as-is for educational and research purposes.

Contact

For questions or collaborations:

  • Thiago Girao
  • PhD candidate in Physics
  • Focus: Quantum Information and Quantum Computing

Last Updated: March 2026 QAOA Implementation Version: 1.0

About

QAOA applied to the Maximum Cut combinatorial optimization problem with Qiskit

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages