Continuous Integration:
Static Analysis & coverage:
This repository implements some of the examples given in "Quantum Bridge Analytics I: A Tutorial on Formulating and Using QUBO Models" by Glover et al. [1]. It should be noted that [1] operates with symmetric QUBO matrices while the D-Wave binary quadratic model represents the QUBO variables as an upper-diagonal/upper-triangular matrix. A symmetric matrix Q is easily transformed to upper-diagonal form by replacing qij with qij + qji, for all i and j with j > i. All qij with j < i are replaced by 0. So we are just doubling all values above the main diagonal (as the matrix is symmetric) and setting all values below the main diagonal to 0
The variant of the number partitioning problem we look at here involves partitioning a set of numbers into two subsets such that the subset sums are as close to each other as possible. The formulation of the binary quadratic model is based on the QUBO optimization problem described in [1]. You can find more background on the hardness of the problem in [2].
Consider a set of numbers S = {s1, s2, ..., sn} and let xi = 1 if si is assigned to subset 1; 0 otherwise. The sum for subset 1 is then given by sum1 = Σsixi, 1≤i≤n, and the sum for subset 2 is given by sum2 = Σsi - Σsixi, 1≤i≤n.
The difference in the sums is then diff = Σsi - 2Σsixi = c - 2Σsixi, 1≤i≤n. (where c denotes the (constant) sum of all the numbers in the set).
The difference is minimized by minimizing: diff2 = (c - 2Σsixi)2, 1≤i≤n = xTQx
Where x is the (n-)vector deciding whether a given number belongs to set 1 or set 2, and Q is a symmetric n X n matrix where qii = si(si - c) and qij = qji = sisj (to convince myself I scribbled down the math - note that x = x2 as x either has the value 0 or 1).
If you run the implementation given in example_partition_problem.py
you should see the example set s = [25, 7, 13, 31, 42, 17, 21, 10]
partitioned into either s1 = [25, 7, 13, 17, 21]
and s2 = [31, 42, 10]
, or s1 = [7, 13, 42, 21]
and s2 = [25, 31, 17, 10]
. Both are perfect partitions.
The maximum cut for an unweighted undirected graph G=(V, E), is defined as the problem of partitioning (cutting) the set of vertices V into two complementary subsets, S and Sc, such that the number of (cut) edges between S and Sc is as large as possible.
The maximum cut decision problem is known to be NP-complete. The weighted version of the decision problem constitutes the 21st of Karp's NP-complete problems [4]. And Hohmann and Kern showed in [5] that, given a graph and a cut, deciding whether or not the given cut is maximal is as hard as solving the maximum cut problem - so there's no feasible way of verifying our results.
Below a maximum cut is illustrated for a simple graph of 5 vertices (nodes) and 6 edges. The illustation is taken from [3] and the nodes have been "color coded" for clarity.
Exactly the same cut can also be illustrated as follows.
The problem can be modelled by introducing binary variables satisfying xi = 1 if vertex i is in subset S, and xi = 0 if i ∈ Sc. An edge is "severed" by the cut if one of its endpoints is in subset S while the other is in Sc. This implies that the quantity (xi + xj - 2xixj) can be used to decide if an edge (i, j) is part of the cut or not. If the edge (i, j) is part of the cut then exactly one of xi and xj equals 1 while the other is 0 - in this case (xi + xj - 2xixj) is equal to 1. If (i, j) is not part of the cut, then both xi and xj have the value 1 or both are 0 - in both cases (xi + xj - 2xixj) is equal to 0.
Thus the problem of maximizing the number of edges in the cut can be formulated as:
maximize(Σ xi + xj - 2xixj, (i, j) ∈ E) (the sum is over all edges in the graph)
Since the D-Wave system is a "minimizing sort of creature" we need to reformulate the expression as a minimization problem. This is done by multiplying the expression by -1; i.e. we need to solve the following to find the maximum cut:
minimize(Σ -xi - xj + 2xixj, (i, j) ∈ E)
This is an instance of:
minimize(xTQx)
Where x is the (n-)vector deciding whether a given node belongs to S or Sc, and Q is a symmetric n X n matrix where qii = -(|(•, i) ∈ E| + |(i, •) ∈ E|) (i.e. the total number of edges having an endpoint in node i, negated) and qij = qji = 1 for (i, j) ∈ E.
An example of the QUBO formulation of the maximum cut for a graph can be found here.
Given an undirected graph, G=(V, E), a vertex cover is a subset of the vertices (nodes), V, such that each edge in E is incident to at least one vertex in the subset, i.e. each edge has at least one of its endpoints in the vertex cover. The minimum vertex cover problem seeks to find a cover with a minimum number of vertices in the subset.
For the graph above - illustrating the maximum cut problem - examples of a minimum vertex cover include (1, 2, 3) and (0, 1, 4).
A standard optimization model for the minimum vertex cover problem can be formulated as follows. Let xi = 1 if vertex i is part of the subset of nodes constituting the minimum vertex cover; otherwise xi = 0. Then the constrained optimization model for the problem is:
minimize(Σ xi , i ∈ V) (the sum is over all nodes in the graph)
subject to the constraints:
xi + xj ≥ 1 for all (i, j) ∈ E
Note that the constraints ensure that at least one of the endpoints of each edge is part of the cover while the objective function seeks to find the cover using the minimum number of vertices.
We need to reformulate the problem as unconstrained in order to solve it as a QUBO. This is done by introducing the concept of penalties. A penalty, P, is a positive, scalar value that must be chosen sufficiently large in order to ensure that it corresponds to the classical constraint. For the MVC problem we need a way of penalizing the situation where an edge has none of its endpoints in the cover. This is done as follows. Let
P = ⌈1.5 * |V|⌉ (i.e. 1.5 times the number of nodes, rounded up)
then the penalty for an edge is calculated as follows:
P(1 - xi - xj + xixj)
meaning that if none of the endpoints are in the minimum vertex cover (xi = xj = 0) then the edge is penalized with the value P, otherwise the penalty is 0. We can now reformulate our optimization model in an unconstrained form:
minimize(Σ xi , i ∈ V + PΣ(1 - xi - xj + xixj), (i, j)i ∈ E)
where P is the positive, scalar penalty.
This can be seen to be an instance of:
minimize(xTQx)
Where x is the (n-)vector deciding whether a given node belongs to the minimum vertex cover, and Q is a symmetric n X n matrix where qii = (1 - P(|(•, i) ∈ E| + |(i, •) ∈ E|)) (i.e. 1 minus P times the total number of edges having an endpoint in node i) and qij = qji = P/2 for (i, j) ∈ E.
(in no particular order - just added on the go)
[1] Fred Glover, Gary Kochenberger, Yu Du (2019), "Quantum Bridge Analytics I: A Tutorial on Formulating and Using QUBO Models", arXiv:1811.11538
[2] Stephan Mertens (2003), "The Easiest Hard Problem: Number Partitioning", arXiv:cond-mat/0310317
[3] Abhijith et al. (2020), "Quantum Algorithm Implementations for Beginners", arXiv:1804.03719
[4] Karp, R. M. (1972), "Reducibility among combinatorial problems", in Miller, R. E.; Thatcher, J. W. (eds.), Complexity of Computer Computations, New York: Plenum Press, pp. 85–103.
[5] Hohmann, C., Kern, W. (1990), "Optimization and optimality test for the Max-Cut Problem.", ZOR - Methods and Models of Operations Research 34, 195–206