In this tutorial, we will learn how to solve the weighted MaxCut problem which is a combinatorial graph theory problem.

The MaxCut problem is an optimization problem in which the nodes of a given undirected graph have to be divided in two sets such that the number and weight of edges connecting a color node with another color node are maximized (the color is what distinguishes one set of nodes from the other) In the following diagram we will be using green and blue nodes. Read this article to find out more about the exact details of the problem.

Each graph consists of a set of edges and nodes. In the following image, the nodes are 1,2...6 and the edges are the lines joining the nodes

Given a graph with weights, which are essentially values assigned to each edge of the graph, the maxcut problem should output the following solution.

Where we will find a path or a cut separating the green and the blue nodes such that the edges going between the two sets have the biggest possible weight. The MaxCut problem can be solved using the Quantum Approximate Optimization Algorithm, which is what we will be exploring.

n_wires is a parameter which denotes number of nodes in the graph. This will be equal to the number of qubits we will be using in our circuit. We will also declare edges set which consists of the edges in the graph and the corresponding weights attached to each edge.

Our goal is to separate the whole set of nodes in two subsets X and Y such that the sum of weights of edges traversing between them have the maximum possible value. Or, we have to find a partition through the whole graph, such that sum of weights of edges that the partition passes becomes the maximum among all possible partitions.

The equation above specifies a particular partition of the graph where

represents the weight at each node i(if the partition passes through the node, 0 otherwise), where it is a piecewise function and the summation operator represents the sum of all the weighted edges.

We will define the partition in terms of computational basis states and operators acting on the states where the a th edge is the edge connecting jth and k nodes and will be equal to P of a if partition goes between jth and kth nodes and equal to 0 if doesn't pass.

We are converting the circuit into a superposition state by applying the hadamard gate on each qubit.

The circuit defined above can be represented as consisting of L layers where

are two gates on each layer associated with two different parameters a and b. The whole circuit will thus have 2L parameters. We will define it as a Rx gate as it is implemented on two nodes connected by an edge in the graph. U2 will consist of RZ gate in between two CNOT gates.

To measure the values of the qubits we will apply the hermitian operator where the eigenvalues of the operator are the values of the measured qubits.

Create a device on which to execute the circuit where the number of wires is same as number of nodes present in the graph or n_wires as we defined above.

To measure the expectation value, we will define a Pauli Z operator and take the kronecker product.

For compiling the entire circuit we will apply the functions and operators.

Initially, the state of each qubit is initialized to |0> by default. We will need to apply a Hadamard gate to create superposition and call the U1 and U2 gates. Then we will measure the expectation value of the edges using the kronecker product of the Pauli Z gate using the expval function.

We will optimize the circuit we just created by calling the AdagradOptimizer with step size 0.5. This optimizer uses the Adagrad algorithm which is a variant of gradient descent. We will then return lists of bitstrings that were measured after executing the circuit over multiple trials, check this for more information on why this technique works.

We will execute the circuit layer by layer. Since we have 3 nodes, we will create 6 layers.

Solution: The partitioned graph

We can try out different graphs for instance

This 5 qubit graph will get a partition.

Where we can verify that the partition between the different colored nodes is maximum as the sum of the edges that pass through the cut is the maximum.