How to Implement VQE and QAOA Across Qiskit, PennyLane, and Cirq — Step-by-Step Workflow + Reusable Experiment Template
VQEQAOAVariational AlgorithmsQuantum OptimizationQiskitPennyLaneCirqExperiment Tracking

How to Implement VQE and QAOA Across Qiskit, PennyLane, and Cirq — Step-by-Step Workflow + Reusable Experiment Template

SSmartQubit Editorial Team
2026-11-11
7 min read

A reusable checklist for choosing, implementing, and comparing VQE and QAOA experiments across Qiskit, PennyLane, and Cirq.

How to Implement VQE and QAOA Across Qiskit, PennyLane, and Cirq

This practical workflow helps you choose between VQE and QAOA, build a comparable experiment in Qiskit, PennyLane, or Cirq, and record results that remain useful when you change ansatzes, optimizers, simulators, or hardware targets.

Overview

VQE and QAOA are variational quantum algorithms: a classical optimizer updates circuit parameters while a quantum circuit estimates an objective. The useful engineering question is not which algorithm sounds more advanced, but which problem representation, circuit structure, and evaluation method match your experiment.

Use VQE when the objective is naturally expressed as the expectation value of a Hamiltonian. Typical examples include estimating a low-energy state or testing a chemistry-inspired model. Use QAOA when the problem is combinatorial and can be mapped to a cost function, such as a small constraint or graph optimization problem. These categories overlap in implementation: both require parameterized circuits, repeated measurements, classical optimization, and careful baseline comparisons.

The SDK should not determine the scientific design. Qiskit is often useful when you want detailed circuit construction, transpilation, and backend workflows. PennyLane is designed around differentiable quantum programming and can fit naturally into hybrid quantum-AI experiments. Cirq provides a circuit-oriented approach with explicit control over gates and execution. API details change, so treat the code below as a cross-SDK design pattern and verify syntax against the installed version.

Before coding, define five invariants that should remain the same across frameworks:

  • The problem instance and qubit mapping.
  • The objective function and sign convention.
  • The ansatz or QAOA depth being tested.
  • The optimizer budget and stopping rule.
  • The measurement, shot, seed, and result-recording policy.

For broader context, compare this workflow with the developer tutorial on variational quantum algorithms and use the resource-estimation checklist before selecting a backend.

Checklist by scenario

Scenario 1: You are testing a Hamiltonian objective with VQE

  1. Write the Hamiltonian explicitly. Store each Pauli term, coefficient, and qubit index in a structured form. Do not hide the sign convention inside an SDK-specific helper.
  2. Select a small reference instance. Choose a case that can be solved classically or checked by exact diagonalization. A known reference lets you distinguish implementation errors from optimizer difficulty.
  3. Choose an ansatz deliberately. Start with a shallow hardware-efficient circuit or a problem-informed ansatz. Record its gate pattern, entanglement layout, parameter count, and initialization rule.
  4. Measure each required term. The energy is a weighted sum of expectation values. Confirm that basis changes and qubit ordering produce the intended Pauli measurements.
  5. Optimize with a fixed budget. Record the maximum number of objective evaluations, not only optimizer iterations. Circuit evaluations can differ substantially between SDKs.
  6. Validate the final state. Compare energy, feasibility or symmetry checks, parameter history, and—where practical—the state or probability distribution.

Scenario 2: You are testing a graph or binary optimization problem with QAOA

  1. Define the binary objective first. Write the cost function in ordinary mathematical notation before converting it into a cost Hamiltonian.
  2. Check penalty terms. If constraints are encoded with penalties, verify that the penalty scale does not overwhelm the original objective or make every feasible state look equivalent.
  3. Map variables to qubits. Document whether bit value zero or one represents the selected state, and keep that convention consistent when decoding samples.
  4. Choose depth p as an experiment variable. Begin with a shallow circuit, then compare deeper circuits rather than silently changing depth during optimization.
  5. Separate cost from feasibility. Report the best measured cost, probability of feasible solutions, and the best feasible sample. A low expected cost is not automatically a useful answer.
  6. Compare with a classical baseline. For small instances, exhaustive search or a simple local heuristic provides an essential correctness check.

Scenario 3: You are porting the same experiment across Qiskit, PennyLane, and Cirq

  1. Build a framework-neutral specification containing qubit count, initial state, gate sequence, parameter order, objective terms, shots, seed policy, and optimizer settings.
  2. Implement state preparation and the variational circuit separately from measurement and optimization.
  3. Use the same parameter vector ordering in every implementation. A silent permutation can look like optimizer instability.
  4. Test one circuit layer at a time. Compare statevector results on a simulator before introducing finite shots.
  5. Use the same objective sign. Some workflows minimize energy while others maximize a score; convert explicitly.
  6. Export results to a common schema rather than comparing screenshots or SDK-native objects.

Reusable experiment template

experiment_id: "vqe-or-qaoa-example"
problem:
  type: "hamiltonian | graph | binary"
  instance_id: "small-reference-01"
  qubits: 4
algorithm:
  name: "VQE | QAOA"
  ansatz: "describe gate pattern"
  depth: 1
  parameter_initialization: "seeded random | zeros | problem-informed"
optimization:
  method: "record exact optimizer name"
  max_objective_evaluations: 200
  tolerance: "record configured value"
execution:
  mode: "statevector | shot-based simulator | hardware"
  shots: 1024
  seed: "record if supported"
  transpilation_or_compilation: "record settings"
results:
  objective_value: null
  best_feasible_value: null
  feasibility_rate: null
  circuit_depth: null
  two_qubit_gate_count: null
  runtime: null
  notes: "noise, retries, warnings, and deviations"

Use this template for every run, including failed runs. For measurement uncertainty and confidence intervals, see the guide to interpreting shots and statistical validation.

What to double-check

Ansatz and parameterization

Count parameters and confirm their order. Check whether the initial state is compatible with known symmetries or constraints. A deeper ansatz may express more states, but it also increases circuit cost and can make optimization harder. When comparing SDKs, keep the circuit topology fixed before experimenting with framework-specific improvements.

Objective construction

Print individual Hamiltonian contributions or cost terms for a test parameter vector. This catches reversed signs, incorrect coefficients, missing terms, and qubit-index conventions. For QAOA, decode several computational-basis samples by hand and verify that their costs match the implementation.

Optimizer behavior

Record objective evaluations, not just the final value. Shot noise can cause a nominally improving run to fluctuate. Run multiple initializations when the conclusion depends on one optimization trace. Do not compare optimizers using different evaluation budgets or different stopping conditions.

Simulator versus hardware

First establish a noiseless or high-precision simulator reference. Then add finite shots, a noise model if relevant, and finally hardware execution when access and budget permit. Preserve the circuit after compilation and record depth, two-qubit gates, measurement mapping, queue or execution issues, and any mitigation applied. The cloud execution workflow provides a useful checklist for separating local testing from remote runs.

Noise mitigation and validation

Noise mitigation should be treated as an experimental condition, not an invisible cleanup step. Record the technique, calibration settings, extra circuits, and whether the reported result is raw or mitigated. Always retain raw counts. Compare against a classical baseline and repeat the most important runs to assess stability.

Common mistakes

  • Choosing VQE or QAOA by name alone. Start with the mathematical structure of the problem and the measurement objective.
  • Changing several variables at once. If depth, optimizer, shots, and backend all change together, you cannot explain a different result.
  • Ignoring qubit and bit ordering. SDK conventions can differ, especially when converting measured bitstrings into problem variables.
  • Reporting only the best sample. Include the distribution, feasibility rate, objective history, and number of shots so the result can be interpreted.
  • Using too few shots for a fine comparison. If two configurations differ by less than the measurement uncertainty, call the comparison inconclusive rather than declaring a winner.
  • Skipping classical validation. A circuit can run successfully while implementing the wrong Hamiltonian or cost function.
  • Porting API calls without porting assumptions. A successful translation between Qiskit, PennyLane, and Cirq does not prove that gradients, measurements, compilation, or parameter ordering are equivalent.
  • Failing to pin and record environments. Store Python, SDK, plugin, backend, and device information. Consult the SDK migration guide before updating a project.

When to revisit

Revisit this experiment template before a new planning or benchmarking cycle, whenever the workflow changes, and after a major SDK or backend update. Re-run the small reference instance when you change any of the following: ansatz structure, optimizer, parameter initialization, qubit mapping, measurement method, shot count, compiler settings, simulator, noise model, or hardware target.

A practical maintenance routine is:

  1. Run a deterministic or tightly controlled simulator smoke test.
  2. Compare each SDK implementation with the stored reference objective and circuit metadata.
  3. Repeat shot-based tests using the documented seed policy.
  4. Run the classical baseline and check feasibility decoding.
  5. Update the experiment record with changed versions, backend details, and deviations.
  6. Only then rerun larger instances or cloud hardware experiments.

Keep a small regression suite containing one VQE instance, one QAOA instance, one measurement-order test, and one invalid-input test. This catches the most expensive mistakes early. For projects connecting variational circuits to machine learning, pair this checklist with the quantum machine learning reproducibility guide.

The goal is not to make every framework produce identical raw output. The goal is to make differences explainable. If the problem definition, circuit specification, evaluation budget, and result schema are stable, Qiskit, PennyLane, and Cirq can serve as interchangeable development environments for a disciplined VQE or QAOA experiment.

Related Topics

#VQE#QAOA#Variational Algorithms#Quantum Optimization#Qiskit#PennyLane#Cirq#Experiment Tracking
S

SmartQubit Editorial Team

Quantum Developer Education

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.