Extended Examples¶
Formulating an Integer CSP as a QUBO Problem¶
As an example application of reformulating a given problem as a QUBO with the techniques of Reformulating a Problem, this example shows how any integer program may be converted to QUBO form. An example of an integer CSP is the map coloring problem described in Simple Map Coloring Problem.
Consider the integer program:
where \(D_i\) is the number of allowed values for variable \(y_i\). As a first step in translating this into a QUBO, the nonbinary valued \(y_i\) must be made binary valued. This can be accomplished either by introducing indicator variables or by writing \(y_i\) in binary. We write \(y_i\) in binary to limit the number of new Boolean valued variables required. Let \(d_i = \lceil \log_2 D_i \rceil\) and \(y_i = \ip{\vc{2}}{\vc{x}_i}\) where \(\vc{2}=[2^{d_i},\cdots, 2,1]\) is the vector of powers of two and \(\vc{x}_i = [x_{i,d_i}, \cdots, x_{i,1}, x_{i,0}]\) represents the bits of \(y_i\). The binary representation may allow for \(y_i\) values larger than \(D_i\) so we further impose the inequality constraints that \(\ip{\vc{2}}{\vc{x}_i}\le D_i1\). Thus, the original integer program may easily be converted to:
where the new \(\vc{B}\) and \(\vc{b}\) have the additional inequality constraints appended, and where \(\vc{c}\) has been suitably modified. In this form, it remains only to represent the equality and inequality constraints within the unconstrained QUBO. These are easily translated to quadratic penalties.
Thus, the binary program can be written as the QUBO objective:
where \(k\) runs over the equality constraints and \(i\) runs over the inequality constraints.
The only remaining question is how to set the sizes of the penalty weights \(\vc{m}^=\) and \(\vc{m}^\le\). Without problem specific knowledge, we approach this issue the same way it is done in Lagrangian methods by incrementally increasing the weights until the constraints are satisfied. If \(\vc{m}=[\vc{m}^=,\vc{m}^\le]\) is some setting of the penalty weights and \(\vc{x}(\vc{m}),\vc{\xi}(\vc{m})\) are the optimal variable settings for that choice of \(\vc{m}\), then we update the penalty weights in proportion to the constraint violations:
where \(\alpha>0\) determines the rate at which penalty weights are accumulated.
Further information
 [Bac2002] compares performance of two transformations from nonbinary to binary constraints used in CSPs: the dual transformation and the hidden (variable) transformation.
Factoring¶
The factoring problem is to decompose a number into a product of other numbers (factors) that give the original when multiplied together. [Bur2002] shows that factoring can be used as a generator of difficult optimization problems.
This section shows a complete example of solving a problem with QUBOs, using techniques that make best use of the DWave system. As here, it is generally true that in any given problem, precision, connectivity, and number of qubits may all be traded off against each other.
This example factors \(p\), an integer of \(\ell\) bits, as a product of a pair of integers \(a\) and \(b\), all represented in binary[1]. Factors \(a\) and \(b\) are assumed to be \(\ell_a\) and \(\ell_b\) bits respectively so that \(\ell=\ell_a+\ell_b\) or \(\ell=\ell_a+\ell_b1\).
[1] 

One approach is to cast this task as an optimization problem for which a reasonable objective is to minimize the difference:
This reformulation involves quartic interactions in the optimization variables so ancillary variables will need to be introduced to reduce the objective to quadratic as described in NonQuadratic (HigherDegree) Polynomials to Ising/QUBO. More problematically, there are a large number of interactions between pairs of variables and the magnitudes of these interactions varies from \(1\) to \(2^{2\ell_a+2\ell_b4}\). Thus, both connectivity and precision requirements are severe in this formulation of the problem as described in Overcoming Imprecisions of Qubit Biases and Coupling Strengths.
To ameliorate these difficulties, we adapt the ``inverse verification” idea to this factoring context. Representing factoring as a constraint satisfaction problem, as discussed in CSP Reformulation with Inverse Verification, greatly reduces precision requirements, and Boolean circuits (see Elementary Boolean Operations to QUBO) enable us to tuneably define the connectivity between optimization variables.
Multiplication Circuits as QUBOs¶
For factorization, the verification circuit is formed from the circuit which multiplies the inputs \(\vc{a}\) and \(\vc{b}\). We then fix the output of the circuit to the known bit string \(\vc{p}\), and minimize the energy of the penalty formulation of the circuit to find \(\vc{a}\) and \(\vc{b}\). The multiplication circuit is built from AND gates, half adders and full adders. The AND gate which realizes \(t=x_1 \wedge x_2\) is given by the penalty \(P_\wedge(x_1,x_2;t)= x_1 x_22(x_1+x_2)r + 3t\). The half adder takes two input bits \(x_1\) and \(x_2\) and gives two output bits \(s\) and \(c\) representing the sum and carry of the two bits. These are defined through \(s+2c=x_1+x_2\). Clearly, then the penalty function for this equality constraint is \(P_{2A}(x_1,x_2;s,c) = (s+2cx_1x_2)^2\). Similarly, the full adder adds three inputs bits and returns the sum and carry as \(s+2c=x_1+x_2+x_3\). The full adder penalty function is \(P_{3A}(x_1,x_2,x_3;s,c) = (s+2c=x_1+x_2+x_3)^2\). The penalty functions result in fully connected groups of 3, 4, and 5 variables respectively. Moreover, the range of coupling coefficients is small. The full multiplication circuit will be built from repeated use of these simple elements, and total precision requirements will remain small.
The multiplication circuit parallels standard multiplication circuits with logical gates replaced by the corresponding penalty functions. A simple circuit architecture is inspired by the explicit multiplication table. Consider the multiplication of two 4 bit integers. Schematically, the product is formed from the multiplication shown in Figure 45.
Multiplication of two 4bit integers \((a_3,a_2,a_1,a_0)\) and \((b_3,b_2,b_1,b_0)\) form the 8bit product \((p_7,p_6,p_5,p_4,p_3,p_2,p_1,p_0)\).
Each of the products \(a_i b_j\) is the logical AND of the two bits. We introduce the ancillary variables \(t_{i,j} \equiv a_i \wedge b_j\), \(s_b^l\) representing partial sums contributing to the \(b\) th output bit at level \(l\), and \(c_b^l\) representing carries from adding contributions to the \(b\) th output bit at level \(l\).
One circuit representation for the multiplication is formed from adding up the contributions to each output bit \(p_b\) as in Figure 45. As an example, the following QUBO objective realizes the multiplication of two 4bit integers \(a\) and \(b\) into their product \(p\).
This QUBO objective has energy 0 if and only if the binary encodings of \(a\) and \(b\) satisfy \(p=ab\).
The graphical representation of these QUBO contributions is perhaps simpler, and is shown in Figure 46.
Yellow ellipses represent \(\wedge\) penalties, green rounded boxes represent halfadder penalties, and red boxes represent fulladder penalties. Variables are represented by edges and connect appropriate penalty terms. The contributions to each bit are separated by a blue dashed line. The thick dashed blue line separating output bits \(p_3\) and \(p_4\) separates parts of the circuit having different connectivity rules.
This circuit can be run in ``reverse” by fixing the output bits \((p_7,\cdots,p_0)\) to the values defined by the number to be factored, and then optimizing over the remaining \(\vc{a}\) and \(\vc{b}\) variables as well as the intermediate sum and carry variables. As is visible from Figure 46 the connectivity between variables is much sparser than the naive \((p\ip{\vc{a}}{\vc{2}} \ip{\vc{2}}{\vc{b}})^2\) objective. Connectivity is defined by the sparse connections between small completely connected subgraphs of size 3, 4, and 5.
A Regularly Structured Factoring Lattice¶
To scale to large integers we need a regular circuit structure. One way to do this is to embed the ANDing of variables directly within the adder gates. Consequently, we define the new adder penalties \(\tilde{P}_{2A}(x_1^1,x_1^2,x_2; s,c)\) which realizes the constraint \(s+2c = x_1^1 x_1^2 + x_2\) and \(\tilde{P}_{3A}(x_1^1,x_1^2,x_2,x_3)\) which realizes \(s+2c = x_1^1 x_1^2 + x_2 + x_3\). By running the method described in CSP Reformulation with Penalty Functions, we rapidly find that the following penalties realize these constraints:
Note that no additional ancillary variables are needed to represent these nonlinear constraints. Thus, we can eliminate all the \(t_{i,j}\) variables representing the \(\wedge\) s of \(a_i b_j\). The price paid for this variable reduction is additional connectivity. The \(\tilde{P}_{2A}\) and \(\tilde{P}_{3A}\) gates are now connected graphs of size 5 and 6.
A regularly structured constraint/optimization network representing the multiplication of two 8bit numbers using the new gates is shown in Figure 47.
The dark black lines represent the desired \(\vc{a}\) and \(\vc{b}\) variables which thread through the lattice. From the product of two \(\ell\) bit integers into an integer of at most \(2\ell\) bits, the circuit has \(\mathcal{O}(\ell^2)\) variables and the maximal connectivity of any variable is \(\mathcal{O}(\ell)\) (this maximal connectivity occurs for the \(\vc{a}\) and \(\vc{b}\) variables). Again the \(\vc{p}\) variables will be fixed to the bits of the number to be factored.
Further, circuit improvements are possible. The resulting QUBO can be a difficult optimization problem to solve, with a great many local minima.