This QAOA tutorial shows how to model Max-Cut, build a parameterized circuit, optimize its angles, and decide whether the result is improving. The workflow uses repeatable measurements—best cut value, approximation ratio, circuit depth, shots, and optimizer iterations—so you can compare a simulator run with later hardware experiments without treating a single noisy result as proof of success.
Overview
Max-Cut is a useful first quantum optimization problem because its input is simple: an undirected graph with vertices and edges. A cut assigns every vertex to one of two groups. An edge is cut when its endpoints belong to different groups. The objective is to maximize the number, or weighted value, of cut edges.
QAOA, the Quantum Approximate Optimization Algorithm, alternates two operations. The cost unitary encodes the graph objective, while the mixer unitary explores different bit-string assignments. A circuit with depth p has parameter sets gamma[0:p] and beta[0:p]. A classical optimizer changes these parameters after evaluating the circuit until the measured objective stops improving.
For a graph with binary variables x_i, one practical way to evaluate a sampled solution is directly:
cut_value(bitstring) = sum(weight for (i, j, weight) in edges
if bitstring[i] != bitstring[j])
This classical evaluator is important. It gives you a transparent reference for every measured bit string, supports weighted graphs, and avoids confusing a circuit's energy convention with the business objective. Before writing a circuit, also run a classical baseline on the same graph. For small graphs, brute force is enough; for larger graphs, use a suitable classical heuristic or optimizer.
How to estimate QAOA performance
Do not judge a QAOA run only by the optimizer's final floating-point objective. Estimate performance from the same outputs you will use in a real experiment.
- Define the graph. Store edges as pairs of vertex indexes and include a weight for each edge.
- Calculate the reference. Find the best known cut value, preferably the exact optimum for a small test graph.
- Choose depth. Start with
p = 1. Increase depth only when the additional circuit cost is justified by better validation results. - Prepare parameters. Use several initial points rather than trusting one random seed. Record the seed and initialization rule.
- Optimize with a fixed evaluation method. During simulation, you may evaluate expectation values exactly or with shots. For hardware-oriented comparisons, shot-based evaluation is usually the more representative choice.
- Sample final bit strings. Convert counts into cut values and inspect both the best observed solution and the distribution of outcomes.
Useful metrics include:
- Best cut value: the highest objective found in the measured samples.
- Mean cut value: the probability-weighted average across sampled bit strings.
- Approximation ratio:
best observed value / reference value. State clearly whether the denominator is an exact optimum or only the best known classical value. - Success probability: the fraction of shots producing a target value, such as the exact optimum.
- Optimization stability: whether repeated seeds reach similar values and parameter regions.
- Resource use: qubit count, two-qubit gate count, transpiled depth, optimizer evaluations, and total shots.
If you are estimating sampling effort, a simple first approximation is total shots = number of evaluations × shots per evaluation. If every evaluation measures several circuits—for example, separate Pauli terms—multiply by that circuit count as well. This helps you forecast runtime and cloud usage without assuming a provider's pricing model. For a broader planning checklist, see the quantum circuit resource estimation guide and the quantum cloud job cost and runtime planning guide.
Inputs and assumptions
Graph representation
Use a consistent vertex-to-qubit mapping. If vertex three maps to qubit three in your graph code but to another position after circuit construction, the measured bit strings will be scored incorrectly. Keep one explicit mapping and test it with hand-checked assignments.
For an unweighted edge (i, j), the Max-Cut cost term is commonly represented using the Pauli-Z expression (1 - Z_i Z_j) / 2. A weighted edge multiplies that term by its weight. Some SDKs maximize the cost directly, while others minimize an energy whose sign is reversed. Confirm the convention with a two-vertex test before running an optimizer.
Parameter and optimizer choices
QAOA parameter tuning is sensitive to initialization, depth, optimizer tolerances, and noisy objective evaluations. A practical baseline is to compare a gradient-free optimizer with a small number of random or hand-designed initial points. Keep the optimizer budget fixed when comparing configurations; otherwise, a result may improve simply because it received more evaluations.
Use a simulator to debug graph encoding and circuit construction. A statevector simulator is convenient for inspecting ideal probabilities, while a shot-based simulator exposes sampling variation. The quantum simulator selection guide explains how those choices affect experimentation.
Illustrative Qiskit-style workflow
SDK primitives and optimizer APIs change, so treat the following as implementation structure rather than a version-pinned drop-in script:
edges = [(0, 1, 1.0), (1, 2, 1.0), (0, 2, 1.0)]
num_qubits = 3
p = 1
# 1. Build a cost Hamiltonian from each edge.
# 2. Create a circuit with Hadamards on all qubits.
# 3. For each layer, apply the cost unitary using gamma[layer].
# 4. Apply an X-based mixer using beta[layer].
# 5. Add measurements and return counts.
def objective(params, shots=1024):
circuit = build_qaoa_circuit(edges, params, p)
counts = run_shot_based(circuit, shots=shots)
return sum(probability * cut_value(bitstring, edges)
for bitstring, probability in normalized(counts).items())
best = classical_optimizer(objective, initial_params, budget=100)
counts = run_shot_based(build_qaoa_circuit(edges, best.params, p), shots=4096)
ranked = rank_by_cut_value(counts, edges)
In a concrete Qiskit implementation, verify the current primitive, transpiler, and backend interfaces in the installed SDK documentation. Pin the environment for a reproducible experiment, draw the circuit before execution, and inspect the transpiled circuit before comparing depth. The Qiskit transpiler guide is useful when moving from an abstract circuit to a real backend.
Worked examples
Triangle graph
Consider three vertices with unit-weight edges between every pair. No two-group assignment can cut all three edges, so the exact optimum is two. There are multiple optimal assignments, such as 001, 010, and 100, depending on bit ordering and which vertex is placed alone.
For a first test, use p = 1, a fixed shot count, and several parameter initializations. A results table might look like this:
| Run | Depth | Shots | Best cut | Mean cut | Approximation ratio | Notes |
|---|---|---|---|---|---|---|
| A | 1 | 1,024 | 2 | record value | 1.00 | ideal simulator |
| B | 1 | 1,024 | record value | record value | best / 2 | shot-based simulator |
| C | 2 | 1,024 | record value | record value | best / 2 | compare added depth |
Replace each placeholder with measured data; do not report a ratio without naming the reference value. If the best cut is two but the mean is much lower, QAOA has found an optimal sample without concentrating strongly on optimal assignments. That distinction matters when the application needs reliable sampling rather than a single favorable outcome.
A weighted four-vertex graph
Next, use edges such as (0,1,3), (1,2,1), (2,3,2), and (0,3,1). The classical evaluator now matters because maximizing the number of cut edges is not equivalent to maximizing total weight. Enumerate all sixteen assignments for a small validation graph, identify the exact weighted optimum, and then score QAOA samples with the same weighted function.
Compare p = 1 and p = 2 using the same seeds, shots, and optimizer budget. If depth two improves the ideal simulator but loses after transpilation or noise, record that result rather than hiding it. The goal is a reproducible trade-off between objective quality and circuit resources, not depth for its own sake.
When to recalculate
Recalculate whenever the graph changes, edge weights change, the QAOA depth changes, or the execution target changes from an ideal simulator to a shot-based simulator or real hardware. Also rerun the experiment after changing the SDK version, transpilation settings, backend configuration, optimizer budget, parameter initialization, or shot count. These changes can alter both the circuit and the measured distribution.
Before each comparison, save the graph, qubit mapping, parameter values, random seed, SDK environment, circuit depth, gate counts, shots, optimizer evaluations, and raw counts. For hardware experiments, track queue and execution details separately from algorithmic quality. If noise is material, compare mitigation methods only after establishing an unmitigated baseline; relevant options are discussed in the quantum error mitigation guide.
Finally, use a short checklist: validate the cost convention, verify bit ordering, run a classical baseline, test multiple initializations, report mean and best values, inspect the transpiled circuit, and compare resource use. Once this Max-Cut workflow is stable, you can reuse the same pattern for other combinatorial objectives: define a cost function, encode it in a Hamiltonian, alternate cost and mixer layers, optimize parameters, sample solutions, and evaluate them with an independent classical scorer.