Solve utility-scale quantum optimization problems
Estimated QPU usage: 8 minutes (tested on IBM Sherbrooke)
This tutorial demonstrates how to implement the Quantum Approximate Optimization Algorithm (QAOA) – a hybrid (quantum-classical) iterative method – within the context of Qiskit patterns. You will first solve the Maximum-Cut (or Max-Cut) problem for a small graph and then learn how to execute it at utility scale. All the hardware executions in the notebook should run within the time limit for the freely-accessible Open Plan.
The Max-Cut problem is an optimization problem that is hard to solve (more specifically, it is an NP-hard problem) with a number of different applications in clustering, network science, and statistical physics. This tutorial considers a graph of nodes connected by vertices, and aims to partition it into separate graphs such that they are both as large as possible. Put another way, the goal of this problem is to partition the nodes of a graph into two sets such that the number of edges traversed by this cut is maximized.
Requirements
Before starting this tutorial, be sure you have the following installed:
- Qiskit SDK v1.0 or later, with visualization support (
pip install 'qiskit[visualization]' ) - Qiskit Runtime 0.22 or later (
pip install qiskit-ibm-runtime ) - Rustworkx graph library (
pip install rustworkx )
Part 1: Small-scale QAOA
The first part of this tutorial uses a small-scale Max-Cut problem to illustrate the steps to solve an optimization problem using a quantum computer.
To give some context before mapping this problem to a quantum algorithm, you can better understand how the Max-Cut problem becomes a classical combinatorial optimization problem by first considering the minimization of a function
where the input is a vector whose components correspond to each node of a graph. Then, constrain each of these components to be either or (which represent being included or not included in the cut). This small-scale example case uses a graph with nodes.
You could write a function of a pair of nodes which indicates whether the corresponding edge is in the cut. For example, the function is 1 only if one of either or are 1 (which means that the edge is in the cut) and zero otherwise. The problem of maximizing the edges in the cut can be formulated as
which can be rewritten as a minimization of the form
The minimum of in this case is when the number of edges traversed by the cut is maximal. As you can see, there is nothing relating to quantum computing yet. You need to reformulate this problem into something that a quantum computer can understand.
Initialize your problem by creating a graph with nodes.
Output:
Step 1. Map classical inputs to a quantum problem
The first step of the pattern is to map the classical problem (graph) into quantum circuits and operators. To do this, there are three main steps to take:
- Utilize a series of mathematical reformulations, to represent this problem using the Quadratic Unconstrained Binary Optimization (QUBO) problems notation.
- Rewrite the optimization problem as a Hamiltonian for which the ground state corresponds to the solution which minimizes the cost function.
- Create a quantum circuit which will prepare the ground state of this Hamiltonian via a process similar to quantum annealing.
Note: In the QAOA methodology, you ultimately want to have an operator (Hamiltonian) that represents the cost function of our hybrid algorithm, as well as a parametrized circuit (Ansatz) that represents quantum states with candidate solutions to the problem. You can sample from these candidate states and then evaluate them using the cost function.
Graph → optimization problem
The first step of the mapping is a notation change, The following expresses the problem in QUBO notation:
where is a matrix of real numbers, corresponds to the number of nodes in your graph, is the vector of binary variables introduced above, and indicates the transpose of the vector .
Optimization problem → Hamiltonian
You can then reformulate the QUBO problem as a Hamiltonian (here, a matrix that represents the energy of a system):
Reformulation steps from the QAOA problem to the Hamiltonian
To demonstrate how the QAOA problem can be rewritten in this way, first replace the binary variables to a new set of variables via
Here you can see that if is , then must be . When the 's are substituted for the 's in the optimization problem (), an equivalent formulation can be obtained.
Now if we define , remove the prefactor, and the constant term, we arrive at the two equivalent formulations of the same optimization problem.
Here, depends on . Note that to obtain we dropped the factor of 1/4 and a constant offset of which do not play a role in the optimization.
Now, to obtain a quantum formulation of the problem, promote the variables to a Pauli matrix, such as a matrix of the form
When you substitute these matrices in the optimization problem above, you obtain the following Hamiltonian
Also recall that the matrices are embedded in the quantum computer's computational space, i.e., a Hilbert space of size . Therefore, you should understand terms such as as the tensor product embedded in the Hilbert space. For example, in a problem with five decision variables the term is understood to mean where is the identity matrix.
This Hamiltonian is called the cost function Hamiltonian. It has the property that its ground state corresponds to the solution that minimizes the cost function . Therefore, to solve your optimization problem you now need to prepare the ground state of (or a state with a high overlap with it) on the quantum computer. Then, sampling from this state will, with a high probability, yield the solution to .
Output:
Cost Function Hamiltonian: SparsePauliOp(['IIIZZ', 'IIZIZ', 'ZIIIZ', 'IIZZI', 'IZZII', 'ZZIII'],
coeffs=[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j])
Hamiltonian → quantum circuit
The Hamiltonian contains the quantum definition of your problem. Now you can create a quantum circuit that will help sample good solutions from the quantum computer. The QAOA is inspired by quantum annealing and applies alternating layers of operators in the quantum circuit.
The general idea is to start in the ground state of a known system, above, and then steer the system into the ground state of the cost operator that you are interested in. This is done by applying the operators and with angles and .
The quantum circuit that you generate is parametrized by and , so you can try out different values of and and sample from the resulting state.
In this case, you will try an example with one QAOA layer that contains two parameters: and .
Output:
Output:
ParameterView([ParameterVectorElement(β[0]), ParameterVectorElement(β[1]), ParameterVectorElement(γ[0]), ParameterVectorElement(γ[1])])
Step 2. Optimize circuits for quantum hardware execution
The circuit above contains a series of abstractions useful to think about quantum algorithms, but not possible to run on the hardware. To be able to run on a QPU, the circuit needs to undergo a series of operations that make up the transpilation or circuit optimization step of the pattern.
The Qiskit library offers a series of transpilation passes that cater to a wide range of circuit transformations. You need to make sure that your circuit is optimized for your purpose.
Transpilation may involves several steps, such as:
- Initial mapping of the qubits in the circuit (such as decision variables) to physical qubits on the device.
- Unrolling of the instructions in the quantum circuit to the hardware-native instructions that the backend understands.
- Routing of any qubits in the circuit that interact to physical qubits that are adjacent with one another.
- Error suppression by adding single-qubit gates to suppress noise with dynamical decoupling.
More information about transpilation is available in our documentation.
The following code transforms and optimizes the abstract circuit into a format that is ready for execution on one of devices accessible through the cloud using the Qiskit IBM Runtime service.
Output:
<IBMBackend('ibm_kyiv')>
Step 3. Execute using Qiskit primitives
In the QAOA workflow, the optimal QAOA parameters are found in an iterative optimization loop, which runs a series of circuit evaluations and uses a classical optimizer to find the optimal and parameters. This execution loop is executed via the following steps:
- Define the initial parameters
- Instantiate a new
Session containing the optimization loop and the primitive used to sample the circuit - Once an optimal set of parameters is found, execute the circuit a final time to obtain a final distribution which will be used in the post-process step.
Define circuit with initial parameters
We start with arbitrary chosen parameters.
No output produced
Define backend and execution primitive
Use the Qiskit Runtime primitives to interact with IBM® backends. The two primitives are Sampler and Estimator, and the choice of primitive depends on what type of measurement you want to run on the quantum computer. For the minimization of , use the Estimator since the measurement of the cost function is simply the expectation value of .
Run
The primitives offer a variety of execution modes to schedule workloads on quantum devices, and a QAOA workflow runs iteratively in a session.
You can plug the sampler-based cost function into the SciPy minimizing routine to find the optimal parameters.
No output produced
Output:
message: Optimization terminated successfully.
success: True
status: 1
fun: -1.9252549900345741
x: [ 3.271e+00 2.874e+00 2.641e+00 9.339e-01]
nfev: 35
maxcv: 0.0
The optimizer was able to reduce the cost and find better parameters for the circuit.
Output:
Once you have found the optimal parameters for the circuit, you can assign these parameters and sample the final distribution obtained with the optimized parameters. Here is where the Sampler primitive should be used since it is the probability distribution of bitstring measurements which correspond to the optimal cut of the graph.
Note: This means preparing a quantum state in the computer and then measuring it. A measurement will collapse the state into a single computational basis state - for example,
Output:
Output:
{16: 0.0356, 18: 0.0753, 10: 0.0456, 21: 0.0456, 8: 0.0159, 19: 0.0288, 17: 0.0385, 1: 0.0234, 11: 0.0955, 31: 0.0038, 22: 0.0732, 28: 0.0137, 20: 0.08, 9: 0.0977, 6: 0.0138, 30: 0.0102, 2: 0.0127, 26: 0.0532, 13: 0.0354, 27: 0.0114, 0: 0.0087, 4: 0.0092, 5: 0.0432, 14: 0.0195, 25: 0.0196, 3: 0.0264, 24: 0.0128, 12: 0.0164, 15: 0.0094, 23: 0.009, 7: 0.0111, 29: 0.0054}
Step 4. Post-process, return result in classical format
The post-processing step interprets the sampling output to return a solution for your original problem. In this case, you are interested in the bitstring with the highest probability as this determines the optimal cut. The symmetries in the problem allow for four possible solutions, and the sampling process will return one of them with a slightly higher probability, but you can see in the plotted distribution below that four of the bitstrings are distictively more likely than the rest.
Output:
Result bitstring: [1, 0, 0, 1, 0]
Output:
/var/folders/dk/j4n5f_1d3xs7m8bq81g9gfk80000gn/T/ipykernel_76646/1639367914.py:19: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
ax.get_children()[int(p)].set_color("tab:purple")
Visualize best cut
From the optimal bit string, you can then visualize this cut on the original graph.
Output:
And calculate the value of the cut
Output:
The value of the cut is: 5
Part 2: scale it up!
You have access to many devices with over 100 qubits on IBM Quantum Platform. Select one on which to solve Max-Cut on a 100-node weighted graph. This is a "utility-scale" problem. The steps to build the workflow are followed the same as above, but with a much larger graph.
Output:
Step 1. Map classical inputs to a quantum problem
Graph → Hamiltonian
First, convert the graph you want to solve directly into a Hamiltonian that is suited for QAOA.
Output:
Cost Function Hamiltonian: SparsePauliOp(['IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZZ', 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZZIII', 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZZIIIIIII', 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZZIIIIIIIIII', 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZIIIIZIIIIIIIIIIII', 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZZIIIIIIIIIIIIIIIIII', 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZZIIIIIIIIIIIIIIIIIIIIII', 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZZIIIIIIIIIIIIIIIIIIIIIIII', 'I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
1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,
1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j])
Hamiltonian → quantum circuit
Output:
Step 2. Optimize problem for quantum execution
To scale the circuit optimization step to utility-scale problems, you can take advantage of the high performance transpilation strategies introduced in Qiskit SDK v1.0. Other tools include the new transpiler service with AI enhanced transpiler passes.
Output:
Step 3. Execute using Qiskit primitives
To run QAOA, you must know the optimal parameters and to put in the variational circuit. Optimize these parameters by running an optimization loop on the device. The cell submits jobs until the cost function value has converged and the optimal parameters for and are determined.
Find candidate solution by running the optimization on the device
First, run the optimization loop for the circuit parameters on a device.
Output:
message: Optimization terminated successfully.
success: True
status: 1
fun: -43.594336255533044
x: [ 2.751e+00 3.347e-01]
nfev: 30
maxcv: 0.0
Once the optimal parameters from running QAOA on the device have been found, assign the parameters to the circuit
Output:
Finally, execute the circuit with the optimal parameters to sample from the corresponding distribution.
No output produced
Step 4. Post-process, return result in classical format
Check that the cost minimized in the optimization loop has converged to a certain value. The code below uses the parameters corresponding to the minimum value of the cost.
Output:
Given that the likelihood of each solution is low, extract the solution that corresponds to the lowest cost.
Output:
Result bitstring: [0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0]
Next, visualize the cut. Nodes of the same color belong to the same group.
Output:
And calculate the the value of the cut
Output:
The value of the cut is: 84
Now you need to compute the objective value of each sample that you measured on the quantum computer. The sample with the lowest objective value is the solution returned by the quantum computer.
No output produced
No output produced
Finally, you can plot the cumulative distribution function to visualize how each sample contributes to the total probability distribution and the corresponding objective value. The horizontal spread shows the range of objective values of the samples in the final distribution. Ideally, you would see that the cumulative distribution function has "jumps" at the lower end of the objective function value axis. This would mean that few solutions with low cost have high probability of being sampled. A smooth, wide curve indicates that each sample is similarly likely, and they can have very different objective values, low or high.
Output:
Conclusion
This tutorial demonstrated how to solve an optimization problem with a quantum computer using the Qiskit patterns framework. The demonstration included a utility-scale example, with circuit sizes that cannot be exactly simulated classically. Currently, quantum computers do not outperform classical computers for combinatorial optimization because of noise. However, the hardware is steadily improving, and new algorithms for quantum computers are continually being developed. Indeed, much of the research working on quantum heuristics for combinatorial optimization is tested with classical simulations that only allow for a small number of qubits, typically around 20 qubits. Now, with larger qubit counts and devices with less noise, researchers will be able to start benchmarking these quantum heuristics at large problem sizes on quantum hardware.
Was this page helpful?