Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
In this workshop, we will solve the MaxCut problem using qmod’s quantum types, such as QBit, and the phase function. First, we will have introduction to the Maximum Cut (MaxCut) problem.

Guidance for the Workshop:

The # TODO is there for you to do yourself. **The # Solution start and # Solution end are only for helping you. Please delete the Solution and try doing it yourself…**

Introduction

The “Maximum Cut Problem” (MaxCut) [1] is an example of combinatorial optimization problem. It refers to finding a partition of a graph into two sets, such that the number of edges between the two sets is maximal. This optimization problem is the cardinal example in the context of Quantum Approximate Optimization Algorithm [2], since it is an unconstrained problem whose objective function in terms quantum gates can be derived easily. With the Classiq platform, things are even simpler, as the objective function is inserted in its arithmetic form.

Mathematical Formulation

Given a graph G=(V,E)G=(V,E) with ∣V∣=n|V|=n nodes and EE edges, a cut is defined as a partition of the graph into two complementary subsets of nodes. In the MaxCut problem we are looking for a cut where the number of edges between the two subsets is maximal. We can represent a cut of the graph by a binary vector xx of size nn, where we assign 0 and 1 to nodes in the first and second subsets, respectively. The number of connecting edges for a given cut is simply given by summing over xi(1−xj)+xj(1−xi)x_i (1-x_j)+x_j (1-x_i) for every pair of connected nodes (i,j)(i,j).

Solving with the Classiq Platform

We go through the steps of solving the problem with the Classiq platform, using QAOA algorithm [2].

Creating and Plotting the Graph, the MaxCut Problem

output

Create the Classical Optimization Function

First, build a function that whether to count an edge or not. If the two nodes are from different groups, the function edge_cut will return 1, otherwise, it should return
  1. The values of the nodes are binary.
Implement a function that returns the total cost for a cut on the graph.

Generic Building Blocks for QAOA Circuit

The mixer layer will apply RX gates with parameter beta to all qubits in the qba array.

The Phase Function

In the QAOA ansatz, the cost layer gives a phase to any solution type. Therefore, we will use the phase function which takes an expression (phase_expr) and a parameter (coefficient) as arguments. See more here. For example:
The phase function rotates each computational basis state about the Z axis with the angle coefficient relative the value of the expression phase_expr. Namely, it adds a phase to each quantum state ∣x⟩→ef(x1,x2,...,xn)∣x⟩|x\rangle \rightarrow e^{f(x_1,x_2,...,x_n)}|x\rangle with respect to the value that f(x1,x2,...,xn)f(x_1,x_2,...,x_n) returns. The quantum variables x1,x2,...,xnx_1,x_2,...,x_n constitute the quantum state ∣x⟩|x\rangle.

Problem Specifc Building Blocks

Synthesizing and Visualizing

Output:
Output:

Execution and Post Processing

For the hybrid execution, we use ExecutionSession, which can evaluate the circuit in multiple methods, such as sampling the circuit, giving specific values for the parameters, and evaluating to a specific Hamiltonian, which is very common in chemical applications. In QAOA, we will use the estimate_cost method, which samples the cost function and returns their average cost from all measurements. That helps to optimize easily.
A good initialization of QAOA is linearly approach the γ\gamma values from 0 to 1 and the β\beta values from 1 to
  1. This approach showed better solutions because it is similar to the adiabatic evolution with big steps rather than small evolution.
Some suggest the value 0.75 bring better solutions.
Record the steps of the optimization.
Build the classical cost function that take a single state and return its cut.
Use the ExecutionSession to calculate the cost of all measurements at once using the estimate_cost method.
In the QAOA algorithm, the classical cost function is converted to a Hamiltonian and the estimate_cost method give the ⟨HC{⟩} \langle H_C\{\displaystyle \rangle \}. But you can define anything with the estimate_cost, not necessarily the expectation value of the Hamiltonian. Define the callback function to store the intermediate parameters or any intermediate parameter you wish.
Now, make the optimization part using the minimize function from scipy. You need to combine the objective function, which includes the quantum program, the classical optimizer, and the callback function. To achieve better convergence, you need to define the type of optimizer (such as COBYLA), the number of iterations, and other parameters, depending on the type of optimizer.
The maxiter is the number of iterations. As the number of layers is larger, we need more iterations to converge to a good solution because there are more parameters. After we finish the optimization and find good parameters, we will use them once again to find the optimized solution.
Output:

Plot the Result

Let’s plot the result of the best found solution.
output

Plot the Convergence Graph

output

Changes to Play With

  1. Change the type of the optimizer from the scipy minimize function.
  2. Each optimizer has another type step size.
For example, in the COBYLA optimizer, it is called rhobeg. The step size determines how far the optimizer will go in each iteration. You can change it to see how it affects the convergence.
  1. Do the same optimization process for a random Erdos Renyi graph.
  2. Build a weighted graph in which each edge has a different weight, and solve the maxcut with it.
This type of graphs are more realistic for real-life applications and also NP-hard.

References

[1]: Maximum Cut Problem (Wikipedia) [2]: Farhi, Edward, Jeffrey Goldstone, and Sam Gutmann. “A quantum approximate optimization algorithm.” arXiv preprint arXiv:1411.4028 (2014). [3]: Barkoutsos, Panagiotis Kl, et al. “Improving variational quantum optimization using CVaR.” Quantum 4 (2020): 256.