Hands-On: Implementing a Hybrid QAOA Agent to Improve Last-Mile Delivery
Hands-on code lab: build a hybrid QAOA + heuristic agent to optimize last-mile routes with Qiskit, simulation, and production metrics.
Hook: Why last-mile teams need a hybrid QAOA agent now
Last-mile routing teams face relentless pressure: tighter SLAs, rising labor costs, and complex urban constraints. Many organizations see the promise of agentic and hybrid AI for logistics, but struggle to adopt it—42% of logistics leaders said in a late-2025 survey they are holding back on Agentic AI adoption even as 2026 becomes a test-and-learn year for advanced optimization. If you're an IT/DevOps or developer team trying to pilot quantum-enhanced routing, this hands-on code lab walks you through a pragmatic hybrid approach: combine proven heuristics for scale with QAOA for small, high-value subproblems to push last-mile performance without waiting for fault-tolerant quantum hardware.
The big idea (inverted-pyramid summary)
Goal: build a hybrid agent that uses classical clustering and routing heuristics to generate feasible last-mile routes, then applies QAOA to improve short runs (clusters of size ≤ 8) where quantum solvers are feasible. This yields measurable improvements in per-route distance and end-to-end service time while keeping compute practical for IT teams.
- Why hybrid? Quantum routines today are best at small, dense combinatorial subproblems. Heuristics scale. Combining them lets you capture the best of both.
- Tooling: Qiskit (QAOA + Qiskit Optimization), Qiskit Runtime and cloud backends matured through late 2024–2025; for early pilots you can run QAOA on Aer or try cloud runtime for short queue times. Alternatives: PennyLane or Cirq for interchangeable QAOA components.
- Results you’ll measure: total distance, % improvement vs baseline, solver wall-clock, solver circuit count, and fallback rate.
Prerequisites and environment
Target audience: developers and IT/DevOps with Python experience. Run this lab on a workstation or CI runner with Python 3.9+ and a GPU if you plan large classical preprocessing. Minimal packages:
pip install qiskit qiskit-optimization numpy scikit-learn networkx matplotlib tqdm
Notes (2026 context): Qiskit Runtime and cloud backends matured through late 2024–2025; for early pilots you can run QAOA on Aer or try cloud runtime for short queue times. Use a simulator for reproducible results during evaluation before any cloud quantum runs.
Step 1 — Dataset setup: synthetic last-mile city
We'll generate a compact last-mile problem: depot + N customers in a city grid. This mirrors real-world micro-fulfillment where routes often contain 5–12 stops.
import numpy as np
from sklearn.cluster import KMeans
import math
np.random.seed(42)
N = 20 # customers
M = 4 # vehicles / clusters
coords = np.random.rand(N, 2) * 10 # 10x10 city block
depot = np.array([5.0, 5.0])
# distance matrix
pts = np.vstack([depot, coords])
D = np.sqrt(((pts[:, None, :] - pts[None, :, :])**2).sum(axis=2))
We keep the depot at index 0 and customers at 1..N. For production you would load real geo-coordinates and compute road distances via an API or graph model.
Step 2 — Classical baseline: clustering + NN + 2-opt
A pragmatic baseline: cluster customers into M clusters (one per vehicle), create a nearest-neighbor route in each cluster, then apply 2-opt local search. This is easy to run at scale and forms the fallback when quantum parts fail or are unavailable.
def cluster_customers(coords, M):
kmeans = KMeans(n_clusters=M, random_state=1).fit(coords)
labels = kmeans.labels_
clusters = {i: [] for i in range(M)}
for idx, lab in enumerate(labels):
clusters[lab].append(idx + 1) # +1 since 0 is depot
return clusters
def nearest_neighbor_route(cluster_nodes, D):
route = [0] # start at depot
unvisited = set(cluster_nodes)
cur = 0
while unvisited:
nxt = min(unvisited, key=lambda x: D[cur, x])
route.append(nxt)
unvisited.remove(nxt)
cur = nxt
route.append(0)
return route
def route_distance(route, D):
return sum(D[route[i], route[i+1]] for i in range(len(route)-1))
def two_opt(route, D):
best = route
improved = True
while improved:
improved = False
for i in range(1, len(route)-2):
for j in range(i+1, len(route)-1):
new = best[:i] + best[i:j+1][::-1] + best[j+1:]
if route_distance(new, D) < route_distance(best, D):
best = new
improved = True
route = best
return best
clusters = cluster_customers(coords, M)
heuristic_routes = []
for c_nodes in clusters.values():
r = nearest_neighbor_route(c_nodes, D)
r = two_opt(r, D)
heuristic_routes.append(r)
baseline_total = sum(route_distance(r, D) for r in heuristic_routes)
print('Baseline total distance', baseline_total)
Step 3 — Hybrid design: where QAOA fits
Design principle: use QAOA to optimize short subroutes where quantum circuits stay tractable. Practically this means:
- Keep cluster sizes for QAOA <= K (we use K=7 or 8). Larger clusters stay classical.
- Convert the small-TSP into a QUBO and pass it to QAOA via Qiskit Optimization.
- Run QAOA on Aer (statevector or qasm) for development; later test on cloud runtime or hardware for latency and noise impact.
Mapping small TSP to QUBO (quick overview)
TSP can be encoded as a QuadraticProgram: binary variables x_{i,t} indicate whether city i is visited at position t. Constraints ensure one city per position and one position per city. The objective minimizes total tour cost. For small N this is tractable; for larger N the QUBO grows O(N^2).
Step 4 — QAOA implementation with Qiskit (small clusters)
We use Qiskit Optimization to build the QuadraticProgram and Qiskit Algorithms' QAOA on an Aer backend. This example targets clusters with up to 7 customers (8 including depot).
from qiskit_optimization import QuadraticProgram
from qiskit_optimization.translators import from_docplex_mp
from qiskit.algorithms import QAOA
from qiskit.algorithms.optimizers import COBYLA
from qiskit import Aer
from qiskit.utils import algorithm_globals
from qiskit.opflow import PauliSumOp
# helper to build TSP QP for a set of nodes (including depot as index 0)
def build_tsp_quadratic_program(nodes, D):
# nodes: list of node indices (0..N). We will encode a tour that starts and ends at depot(0).
n = len(nodes)
qp = QuadraticProgram()
# variable x_i_p = 1 if node i is at position p
for i in range(n):
for p in range(n):
qp.binary_var(name=f'x_{i}_{p}')
# constraints: each position has exactly one node
for p in range(n):
qp.linear_constraint(
{f'x_{i}_{p}': 1 for i in range(n)}, sense='==', rhs=1, name=f'pos_{p}'
)
# each node appears exactly once
for i in range(n):
qp.linear_constraint(
{f'x_{i}_{p}': 1 for p in range(n)}, sense='==', rhs=1, name=f'node_{i}'
)
# objective: sum distances between consecutive positions
# mapping back to original node indices
for p in range(n):
q = (p + 1) % n
for i in range(n):
for j in range(n):
cost = D[nodes[i], nodes[j]]
qp.minimize(quadratic={(f'x_{i}_{p}', f'x_{j}_{q}'): cost})
return qp
# solve with QAOA on Aer (small circuit)
backend = Aer.get_backend('aer_simulator_statevector')
algorithm_globals.random_seed = 123
def solve_subtour_with_qaoa(nodes, D, p=1):
qp = build_tsp_quadratic_program(nodes, D)
qubo = qp.to_ising()[0] # get operator
# Build QAOA
optimizer = COBYLA(maxiter=200)
qaoa = QAOA(optimizer=optimizer, reps=p, quantum_instance=backend)
result = qaoa.compute_minimum_eigenvalue(qubo[0])
# In practice, decode bitstring -> tour (omitted detailed decoding for brevity)
return result
Notes: This code shows the core pieces; in production you must decode binary outputs to a valid permutation and enforce depot anchoring. For clusters that include depot at position 0, you can simplify variables by fixing depot positions.
Step 5 — Integrating QAOA into the agent loop
Agent pseudocode for one optimization iteration:
- Cluster customers (classical)
- For each cluster: if cluster_size ≤ K_quantum then call QAOA solver, decode best tour; else use NN+2-opt
- Assemble routes, compute metrics
- Log solver times, fallback occurrences, and solution quality
hybrid_routes = []
K_quantum = 7
for c_nodes in clusters.values():
nodes = [0] + c_nodes # include depot
if len(c_nodes) <= K_quantum:
res = solve_subtour_with_qaoa(nodes, D)
# decode 'res' -> route (decode omitted here)
route = decode_qaoa_result_to_route(res, nodes)
else:
route = nearest_neighbor_route(c_nodes, D)
route = two_opt(route, D)
hybrid_routes.append(route)
hybrid_total = sum(route_distance(r, D) for r in hybrid_routes)
print('Hybrid total distance', hybrid_total)
print('Improvement', (baseline_total - hybrid_total) / baseline_total)
Step 6 — Metrics & evaluation for IT/DevOps
Measure both solution quality and operational metrics:
- Quality metrics: total distance, average route length, % improvement vs baseline, per-route savings, service-time impact (if you have time windows and speeds).
- Operational metrics: solver wall-clock (ms/sec), number of quantum circuit executions, circuit depth, shots, memory, and fallback rate (how often the agent reverts to classical path). For wall-clock and outage-sensitive metrics, follow a network observability mindset when instrumenting runtimes.
- Resilience metrics: success rate of QAOA runs, variance under noise simulation, and end-to-end SLA compliance. Track trust and supply-chain quality of telemetry providers using published trust score frameworks.
Example logging schema (for observability):
{
'run_id': '2026-01-17-001',
'baseline_total': 123.4,
'hybrid_total': 119.2,
'improvement_pct': 3.4,
'qaoa': {
'calls': 6,
'avg_wallclock_ms': 1200,
'fallbacks': 0
}
}
Step 7 — Simulation study: multiple seeds and statistical confidence
Run the hybrid pipeline across many random seeds (or real-day datasets) and compute mean improvement and confidence intervals. For early pilots you’ll want to show statistically significant gains before moving to cloud quantum hardware.
from tqdm import trange
runs = 50
improvements = []
for s in trange(runs):
np.random.seed(1000 + s)
# regenerate coords, cluster, baseline, hybrid as above
# compute improvement and append
improvements.append(improvement)
mean_imp = np.mean(improvements)
std_imp = np.std(improvements)
print('Mean improvement', mean_imp, '+/-', 1.96 * std_imp / np.sqrt(runs))
Practical tips and pitfalls (experience-driven)
- Keep quantum inputs small: QAOA circuits grow quickly. Target clusters ≤ 8 to limit qubit and gate counts.
- Warm-start with heuristics: initialize parameters or seed solutions using classical heuristics to speed up convergence.
- Use simulators first: validate correctness and measure deterministic gains with Aer before committing cloud budget.
- Cache quantum answers: many last-mile subproblems repeat—cache QAOA solutions and reuse across runs to reduce redundant runtime and cost.
- Monitor fallback rate: a high fallback rate indicates either noisy hardware or a mismatch between cluster sizing and quantum capability.
2026 Trends and future-proofing your agent
By early 2026, we see three relevant trends:
- Hybrid agent pilots are accelerating: many logistics teams are running controlled pilots rather than full deployments—aligns with late-2025 survey findings that 2026 would be a test-and-learn year.
- Quantum-classical runtime improvements: providers improved low-latency runtimes and noise mitigation, making short QAOA runs feasible in CI/CD experiments. Instrument these runtimes the same way you would instrument any cloud stack and integrate with your edge & message-broker topology for resilient orchestration.
- Tooling convergence: frameworks like Qiskit, PennyLane, and Cirq now offer smoother translation from optimization models (QuadraticProgram) to parameterized quantum circuits, simplifying hybrid orchestration. Also plan KPI reporting using a reusable dashboard and KPI approach so non-technical stakeholders can evaluate impact.
Actionable recommendation: build your agent with abstraction layers so you can swap QAOA implementations (Qiskit -> PennyLane) and backends (simulator -> cloud). Measure both solution quality and operational cost—quantum value must exceed orchestration and inference latency.
Advanced strategies and future predictions
For teams looking to push further in 2026:
- Adaptive clustering: dynamically size clusters by expected quantum gain; learn this mapping using lightweight meta-models.
- Multi-objective QAOA: extend QUBO to include fairness constraints (driver workload balance) or time windows—this increases problem size but captures real last-mile KPIs.
- Agentic orchestration: allow a supervisory agent to propose which subproblems to send to quantum solvers based on recent history and cost/latency trade-offs. This is the practical bridge to broader Agentic AI adoption.
Case study (fictional but realistic)
In a 2025 pilot, a regional delivery operator used the hybrid approach on 10 delivery zones. They ran the hybrid agent overnight to reoptimize small clusters and reported:
- Average per-zone distance improvement: 4.1%
- Operational overhead: 12 min per zone for QAOA runs on cloud simulator (batched)
- Net improvement when accounting for labor and fuel: positive on high-density urban zones where routes were short and stop-dense
Key lesson: target dense micro-zones where per-stop improvements compound into measurable savings.
Checklist for production pilots (IT/DevOps)
- Instrument baseline heuristics and collect per-route telemetry. Evaluate telemetry providers and their trust frameworks (trust scores).
- Define cluster sizing policy and K_quantum threshold.
- Implement QAOA with a simulator, add decoding and validation pipelines.
- Implement caching and circuit reuse to reduce runtime and cloud cost. Refer to published caching strategies when designing caches for repeated subproblems.
- Define SLOs: acceptable latency, fallback rate, minimum improvement to justify runs.
- Run A/B tests on held-out zones and analyze statistical significance. Integrate experiment telemetry with your KPI stack (dashboards).
"42% of logistics leaders were holding back on Agentic AI at the end of 2025—2026 is the year to pilot smart, hybrid approaches that combine trusted heuristics and quantum-enhanced modules."
(Reference: Ortec/industry survey, late 2025)
Limitations and ethical considerations
Be transparent about when quantum solvers are used and monitor for regressions. Ensure the hybrid agent does not prioritize cost savings at the expense of driver safety or legally mandated breaks. Also consider energy cost of cloud quantum simulations in sustainability reporting. For governance and compliance guidance related to quantum systems, see work on regulatory and ethical considerations.
Actionable takeaways
- Start small: target clusters ≤ 8 nodes for QAOA-based improvements.
- Use classical clustering and local search as robust fallbacks.
- Measure both solution quality and operational metrics—wall-clock and fallback rate matter in production.
- Instrument and run repeated experiments across varied zones to build statistical confidence.
- Design your agent to be backend-agnostic so you can upgrade quantum runtimes without refactoring routing logic. Consider your cloud and hosting strategy in light of the overall evolution of cloud-native hosting.
Next steps & call-to-action
If you manage a last-mile optimization team, pick two pilot zones this week: one dense urban micro-zone and one suburban longer-route zone. Implement the hybrid loop from this lab using Aer for testing. Log the metrics suggested here, run 50+ seeds, and prepare an executive summary comparing baseline vs hybrid performance. If you want a ready-to-run starter repo tailored to your fleet size (incl. Qiskit and PennyLane variants) or a 2-hour workshop to put this into your CI pipeline, contact our team at askqbit.com/trials to book a pilot consultation.
Related Reading
- Regulatory and ethical considerations for quantum-augmented agents
- Caching strategies for platform estimation and reuse
- Edge+Cloud Telemetry: integrating edge devices for observability
- How to build DevEx platforms to support agentic workflows
- The Placebo Effect and Premium Desserts: Do Fancy Gadgets Make Ice Cream Taste Better?
- Festival to Marketplace: How EO Media’s Content Slate Signals Winning Genres for 2026
- Wearables, Stress Tracking and Adaptogens: When Your Watch Says 'Relax', Which Herb to Reach For
- How to Cut Your Spotify Bill Today: Workarounds, Gift Cards and Family Hacks
- EV Owners Who Rent: Designing a Sofa-Centric Entryway for Bike and Charger Access
Related Topics
Unknown
Contributor
Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.
Up Next
More stories handpicked for you