Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome back to our deep dive into recent advances in quantum computing.
In our previous lesson, we constructed the parameterized quantum circuit for QAOA, known as the ansatz. We learned how to map the MaxCut problem to a problem Hamiltonian and how to build the circuit by alternating evolutions under and a mixer Hamiltonian . We were left with a circuit dependent on a set of unknown angles.
Today, we will complete the picture by implementing the full QAOA workflow. This lesson directly addresses the learning outcome: Implement the full QAOA workflow, including the classical optimization loop, to solve a small instance of the MaxCut problem. We will learn how to find the optimal parameters and that guide the algorithm to the solution.
The process involves a hybrid quantum-classical approach, which should be conceptually familiar from your experience with training machine learning models. We will:
- Define the objective function for the optimization.
- Implement the classical optimization loop that uses a quantum computer (or simulator) to evaluate the objective function.
- Use the optimized parameters to run the circuit a final time and extract the solution to the MaxCut problem.
1. The Hybrid Optimization Framework
The QAOA is a variational algorithm. This means we don't run a single, fixed quantum circuit. Instead, we have a parameterized circuit (the ansatz) and we use a classical computer to find the best parameters. The "best" parameters are those that prepare a quantum state that minimizes the expectation value of the problem Hamiltonian, .
The objective function we seek to minimize is:
where is the state prepared by our -layer ansatz. Since the ground state of encodes the solution to our MaxCut problem by definition, minimizing this expectation value is equivalent to finding the best possible approximate solution the ansatz can represent.
Hybrid Classical-Quantum Simulation of MaxCut using ...
For a formal definition of the QAOA state and the objective function, let's refer to the research paper 'Hybrid Classical-Quantum Simulation of MaxCut using QAOA-in-QAOA'. Its 'Methods' section provides the precise mathematical formulation.
Please read section III-B, 'Quantum Approximate Optimization Algorithm'. Focus on Equation (2), which defines the ansatz state ψₚ, and Equation (3), which defines the objective function Fₚ. This formalizes the optimization goal.
The overall workflow is an iterative loop:
- Start with an initial guess for the parameters .
- On the quantum computer, prepare the state and measure the expectation value .
- Feed this expectation value (the cost) to a classical optimizer.
- The optimizer suggests a new set of parameters.
- Repeat until the cost converges to a minimum.
2. The Full Workflow in Practice
Let's now walk through how this hybrid loop is implemented. We'll see how to manage the quantum and classical parts, and how to get the final answer.
2.1. The Optimization Loop: Estimator and Optimizer
The core of the workflow is the interaction between the classical optimizer and the quantum device. To manage this, Qiskit provides two key tools called Primitives:
Estimator: This primitive is designed for the optimization phase. It takes a circuit and an observable (our ) as input and efficiently returns the expectation value, . It's the perfect tool for evaluating our cost function .Sampler: This primitive is for the final step. It takes a circuit and returns the raw measurement outcomes—a dictionary of bitstrings and their corresponding counts or probabilities. We use this after optimization to find the most likely solution.
Solving MaxCut with QAOA: Quantum Tutorial for 100+ Qubits
The Qiskit video 'Solving MaxCut with QAOA' provides an excellent conceptual walkthrough of this iterative process, explaining the roles of the Estimator, the Sampler, and the classical optimizer (COBYLA in this case).
Please watch from 18:48 to 23:01. This segment clearly illustrates the entire QAOA workflow, from initializing parameters to the iterative optimization loop and the final sampling step.
Now, let's see how to translate this into code. The IBM Quantum documentation provides a tutorial that serves as a perfect hands-on guide. It uses scipy.optimize.minimize as the classical optimizer and shows how to create a callback function that uses the Estimator to get the cost for each iteration.
Quantum approximate optimization algorithm
The 'Quantum approximate optimization algorithm' tutorial from IBM Quantum shows the complete implementation of this step.
Please read through the section 'Step 3: Execute using Qiskit primitives'. Pay close attention to the cost_func_estimator function, which is the bridge between the classical and quantum parts. Note how it takes the parameters from the optimizer, runs a job on the Estimator, and returns the resulting cost. Then, see how this function is passed to the minimize routine.
2.2. Extracting and Analyzing the Solution
Once the classical optimizer has converged, it returns the set of optimal parameters, . The optimization is complete. Now, we need to find the actual solution to the MaxCut problem.
To do this, we perform the final two steps of the workflow:
- Assign Parameters: We take our QAOA ansatz circuit and assign the optimal parameters to its gates.
- Sample the Output: We run this final, optimized circuit on the quantum device using the
Samplerprimitive. This gives us a probability distribution over all possible output bitstrings. The bitstring with the highest probability is our candidate for the optimal solution.
Quantum approximate optimization algorithm
The IBM Quantum tutorial continues by demonstrating exactly this process.
Continue reading the tutorial from where the minimize call finishes. Follow the logic for assigning the optimized parameters to the circuit and then using the Sampler to get the final distribution. Then, review 'Step 4: Post-process and return result...' to see how the most likely bitstring is extracted and visualized as a cut on the original graph.
It's important to remember that QAOA is an approximate algorithm. The solution it finds is not guaranteed to be the absolute best one, especially for a small number of layers (). The output distribution will likely show several bitstrings with high probabilities.
Solving MaxCut with QAOA: Quantum Tutorial for 100+ Qubits
To better understand how to interpret the results, let's return to the Qiskit video. It discusses the probabilistic nature of the output and what to make of non-optimal or degenerate solutions appearing in the final histogram.
Please watch from 23:01 to 27:05. This part provides valuable context on analyzing the results, sanity-checking the solution, and understanding potential sources of error, which is crucial for a practitioner.
3. Hands-on Implementation
You have now seen all the conceptual pieces of the full QAOA workflow. To truly master the implementation, the most effective next step is to execute the code yourself. The IBM Quantum tutorial is designed as an interactive notebook, allowing you to run each step and observe the outcome.
Quantum approximate optimization algorithm
I encourage you to now work through the tutorial notebook. This will solidify your understanding of how the different components—the graph, the Hamiltonian, the ansatz, the optimizer, and the primitives—fit together.
Open the tutorial on the IBM Quantum Platform (or a local environment with Qiskit installed) and run the cells for 'Part I. Small-scale QAOA'. As an interesting extension, you can also explore 'Part II. Scale it up!', which applies the exact same workflow to a 100-qubit problem, demonstrating the scalability of the pattern.
Conclusion
In this lesson, we have assembled the complete QAOA workflow for solving a combinatorial optimization problem. We moved beyond just constructing the ansatz to understanding and implementing the hybrid quantum-classical loop that makes the algorithm work.
Key Takeaways:
- The QAOA workflow is a hybrid method where a classical optimizer finds the best parameters for a parameterized quantum circuit.
- The objective is to minimize the expectation value of the problem Hamiltonian, , as its ground state encodes the problem's solution.
- The Qiskit
Estimatorprimitive is used within the optimization loop to efficiently calculate the cost () for each set of parameters. - After the optimal parameters are found, the
Samplerprimitive is used to measure the output distribution of the final circuit, from which the most probable solution bitstring is extracted. - QAOA is an approximate, heuristic algorithm; its performance depends on factors like the number of layers and the quality of the classical optimization.
Preview of the Next Lesson
We have now concluded our study of two major variational algorithms, VQE and QAOA. These are leading candidates for achieving useful results on near-term quantum hardware.
In our next lesson, we will pivot to a different, more advanced topic in Hamiltonian simulation: Derive the Linear Combination of Unitaries (LCU) lemma and analyze its application for non-unitary matrix simulation. This will take us from the realm of heuristic, variational methods towards more powerful techniques that are central to algorithms designed for future, fault-tolerant quantum computers.