Practical implementation of a quantum backtracking algorithm

by   Simon Martiel, et al.

In previous work, Montanaro presented a method to obtain quantum speedups for backtracking algorithms, a general meta-algorithm to solve constraint satisfaction problems (CSPs). In this work, we derive a space efficient implementation of this method. Assume that we want to solve a CSP with m constraints on n variables and that the union of the domains in which these variables take their value is of cardinality d. Then, we show that the implementation of Montanaro's backtracking algorithm can be done by using O(n log d) data qubits. We detail an implementation of the predicate associated to the CSP with an additional register of O(log m) qubits. We explicit our implementation for graph coloring and SAT problems, and present simulation results. Finally, we discuss the impact of the usage of static and dynamic variable ordering heuristics in the quantum setting.



There are no comments yet.


page 1

page 2

page 3

page 4


Exponential-time quantum algorithms for graph coloring problems

The fastest known classical algorithm deciding the k-colorability of n-v...

Efficient quantum algorithm for dissipative nonlinear differential equations

While there has been extensive previous work on efficient quantum algori...

CCCP Algorithms to Minimize the Bethe free energy of 3-SAT Problem

The k-sat problem is a prototypical constraint satisfaction problem. The...

Streaming approximation resistance of every ordering CSP

An ordering constraint satisfaction problem (OCSP) is given by a positiv...

Planning for Compilation of a Quantum Algorithm for Graph Coloring

The problem of compiling general quantum algorithms for implementation o...

Approximability of all finite CSPs in the dynamic streaming setting

A constraint satisfaction problem (CSP), Max-CSP( F), is specified by a ...

Optimizing quantum heuristics with meta-learning

Variational quantum algorithms, a class of quantum heuristics, are promi...
This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

Quantum computing. Quantum computing is one of the most promising emerging computation technology. Theory promises algorithmic speed ups ranging from quadratic, for unstructured problems, up to exponential for some particular key problems. Besides the some problems such as integer factoring and its obvious applications in cryptology, very few applicable large scale algorithms have been derived or studied. In 2015 especially, Montanaro [22] presented a general method to obtain speedups of backtracking-based algorithms, relying on Belovs’ previous work [5] (merged in [6]). The algorithm uses Quantum Walks, a well developed quantum algorithmic tool intensively studied in the scope of search algorithms [1, 2, 9, 18, 20, 25, 26].

Constraint satisfaction problems. CSPs form a very general class of problems, that encompass a large set of practical problems. The most famous examples are the Boolean satisfiability problem (SAT) [17] and the graph coloring problem [21]. Both have applications in many fields, like scheduling [19] or timetabling [12]

for graph coloring, and in computer science or artificial intelligence, among others, for SAT (refer to

[17] for a wide list of applications of SAT).

CSPs have been widely studied and a well known tool taking advantage of their structure to solve them is backtracking. For example, the DPLL [10, 11] backtracking-based algorithm has been introduced in 1962 and is currently the procedure at the basis of some of the most efficient SAT solvers [14, 16, 27]. Since backtracking algorithms explore a tree whose vertices are partial solutions to the associated CSP [27], one can think of using a quantum walk to obtain a speedup.

Quantum backtracking. In 2017, Ambainis and Kokainis [3] have dealt with Montanaro’s algorithm in depth and in 2018, Aono, Nguyen and Shen [4]

used these works to speed up the two most efficient forms of enumeration (a lattice algorithm) known. They claim that it affects the security estimates of several lattice-based submissions to the NIST post-quantum standardization process

[8]. In turn, Montanaro [24] has presented how to get a quantum speedup of branch-and-bound algorithms by using Ambainis-Kokainis’ work and his own. Thus, Montanaro’s algorithm is of high interest in computing science and cryptography. In 2018, Campbell, Khurana and Montanaro [7] reviewed its complexity when applied to the graph coloring problem and SAT, assuming access to a very large amount of qubits, since they aggressively optimized the circuit depth. The aim of this work is to investigate a memory efficient implementation of this backtracking algorithm, in order to be able to validate the algorithm on small instances via classical emulation.

The paper is organized as follows. Section 2 introduces definitions. We discuss the choice of heuristic in section 3. Then, section 4 is about the use of the predicate, how to implement it and how to check some generic constraints. We conclude by presenting some of the results we got for graph coloring, thanks to a simulator, in section 5.

2 Preliminaries

Thereafter, we will denote by a constraint satisfaction problem defined by a triple , where is a set of variables, is a set of values and is a set of constraints. We will also use to denote the extended domain in which each variable can take its value, standing for "the variable has no value".

Let be an assignment of values to the variables of a CSP . It will be said to be a solution to if it verifies all the constraints in ; complete if , partial otherwise; valid if it is partial and can be extended to a solution; invalid if it is partial and not a solution.

2.1 Backtracking algorithm

A standard approach for solving a CSP is the technique of backtracking. For this, we assume that we have access to a predicate which can receive an assignment (complete or partial) of values as argument and returns "true" if is a solution, "indeterminate" if it is valid, "false" otherwise. We also assume access to a heuristic which specifies which variable should be instantiated next. The general algorithm is presented as algorithm 1.

1:An assignment of values , access to and to .
2:if  is true then output and return
3:if  is false then return
5:for  do
6:      with the -th entry replaced with .
7:     return Algorithm 1
Algorithm 1 General backtracking algorithm

2.2 Montanaro’s algorithm

The algorithm presented in [22] is based on a quantum walk on trees. The idea is summarized as follows. Consider a rooted tree with vertices, labeled , the vertex being the root of . Hereafter, (resp.

) will denote the set of vertices at an even (resp. odd) distance from

and will mean that is a child of in the tree. The quantum walk operates on the space spanned by , and starts in the state . It is based on a set of diffusion operators , where is the identity if is a solution, otherwise, diffuses on the subspace spanned by . A step of the walk consists in applying the operator , where :


Thanks to these operators, an algorithm for detecting a solution in a tree can be established (algorithm 2). It is the phase estimation of the operator which allows the quantum walker to go through the paths leading to a solution in . By applying the detection algorithm to wisely chosen vertices of , it is possible to construct an hybrid algorithm for finding a solution (algorithm 3). Montanaro has shown that it finds a solution in time complexity and if there exists a unique solution, it is shown to be . For more details on the quantum walk, refer to [22].

1:Operators ,

, a failure probability

, upper bounds on the depth and the number of vertices . Let be universal constants to be determined.
2:Repeat times:
3:     Apply phase estimation to the operator with precision .
4:     If

 the eigenvalue is 1 

then accept      
5:If number of acceptances  then return "Solution exists"
6:     else return "No solution"
Algorithm 2 Detecting a solution (algorithm 2 of [22])
1:Apply algorithm 2 to the entire tree.
2:If it outputs "No solution" then return
3:Apply algorithm 2 to each child of the root until one outputs "Solution exists".
4:If this child is a solution then output its label and return
5:     else go back to step 3 with this child as the root.
Algorithm 3 Finding a solution (described in subsection 2.1 of [22])

3 Variable ordering heuristics

In order to be able to test the algorithm with the means we have, our glance was initially towards the choice of the heuristic. In fact, the largest qubit overhead in the implementation comes from the heuristic implementation, since one has to store variable indexes inside the quantum memory ( qubits are needed). Moreover, even though the depth overhead of the heuristic is asymptotically negligible compared to the rest of the algorithm, it seems that for instances of reasonable size, this overhead is not negligible.

Since we are interested in optimizing the number of qubits, we chose to deal with static variable ordering (SVO) heuristics, which can be classically precomputed at the start of the meta-algorithm. This has two main benefits: only data qubits are required to store an assignment and we do not have to produce a reversible implementation of a dynamic variable ordering (DVO) heuristic.

In [7] and [23], the implementation of Montanaro’s algorithm has been optimized in depth and its complexity has been studied considering the use of a DVO heuristic. However, the benefit of implementing a DVO heuristic is unclear in the range of parameters for which it is claimed in [7] that a graph could be colored in one day (up to approximately 150 vertices). Let (resp. ) be the number of vertices in the backtracking tree associated with a SVO (resp. DVO) heuristic and (resp. ) the number of calls to made by Montanaro’s algorithm. If we denote the depth of the overhead due to the implementation of a DVO heuristic by and the depth of the operators and without heuristic by , we have that using a dynamic heuristic is more efficient than using a static one if: , i.e. if . This is asymptotically true but thanks to the script computing algorithm 3 complexity to solve graph coloring given in [23], one can compute an estimate of (Fig. 1). In the considered range of parameters, we can see that using a DVO heuristic would be better than using a SVO one if . Unfortunately, as far as we know, no DVO heuristic has been proved to verify such a bound. Thus, the time-saving trick allowed by a DVO heuristic may not be obvious and it might be worth using a SVO heuristic for "small" instances.

Figure 1: according to for graph coloring given in [23].

The modified algorithm for and is presented as algorithm 4.

1:A basis state corresponding to a partial assignment . Ancilla registers : , , storing a tuple , where , , initialized to , .
2:If is odd, swap with .
3:Compute .
4:If is true, go to step 8.
5:If , subtract 1 from .
6:For each , if is not false, set .
7:If , , else, . Perform on .
8:Revert steps 5 and 4.
9:Revert steps 2 and 1.

is similar, except that: step 1 is preceded by the check "If , return"; "odd" is replaced with "even" in step 1; and the check "If is removed from step 6.

Algorithm 4 Implementation of the operator

In order to test our implementation of Montanaro’s algorithm, we used the following SVO heuristics:

  • Maximum cardinality (MC) [13]: chooses the variable with the largest number of neighbors to be the first one and then orders the others by choosing at each step the most connected one with previously ordered ones;

  • Maximum degree (MD) [13]: orders the variables in the decreasing order of the size of their neighborhood;

  • Minimum width (MW) [15]: orders the variables from last to first by choosing at each step the one having the minimum number of neighbors in the subproblem where previously ordered variables have been deleted.

To get an idea of the efficiency of these heuristics compared to the naive one, we emulated Montanaro’s algorithm on the graph coloring problem by replacing algorithm 2 with a backtracking one in algorithm 3. For fixed values of and , we generated 1000 random graphs for a varying number of edges, ran algorithm 3 on these graphs and counted the number of calls to the emulated algorithm in average. The same pattern arose when took different values between 10 and 20 and between 3 and 6 (Fig. 2). We observed that the MD heuristic offered the best (and optimal) results and that MC and MW heuristics led to very similar results. Of course, we can not extrapolate for higher values of and .

Figure 2: Mean number of calls to algorithm 2 depending on the number of edges in a graph with 15 nodes and 4 colors.

4 Generic implementation

Thereafter, we will use the following notations : , and .

The most straightforward way of implementing the predicate is to compute a logical of the result of the evaluation of each constraint over the current variable assignment. This would lead to a circuit using work qubits. We present another solution, a quantum counter, using only qubits.

In the case of the partial predicate, an index stored in a quantum register indicates that the first variables have been assigned a value. Therefore, if is sufficient to check the constraints that depends on at least one variable in . To find out if a constraint has to be checked, we use a system of comparison of values, one of which is quantum and the other classical. The overall process requires additions on qubits.

Similarly, if we apply the detection algorithm to a vertex located at the -th level of the backtracking tree, it is unnecessary to check the constraints that depend solely on the variables in .

Thanks to these optimizations, the deeper we go in the backtracking tree, the more qubits, quantum gates and time are saved.

4.1 How to implement a predicate

Let be a CSP. For all , the binary representation of the value assigned to is denoted by . The symbol will be encoded by 0.

The circuit (a)a is used to verify if an assignment is solution to , by checking for all the constraint . An ancillary register is used as a counter. In order to check , a subroutine depending on the set of involved variables in will be used and will increase the counter if is not verified. Once all the constraints have been checked, the counter will be equal to the number of constraints that are violated. Thus, if it is 0, we set an ancillary qubit to 1. Then, we reverse the checking operations to reset the counter to 0. At the end of the circuit, we have in the ancillary qubit the wanted value: .

We also want to be able to verify if a partial assignment is valid for . In this aim, we have to check for all the constraint if . In the following, , will denote and .

In order to compute the comparison operator, we add a bit (most significant one) to (call it , initialize it to ) and use the following procedure ((b)b):

  1. If , subtract it from , otherwise, add it to ;

  2. If then some overflow will occur and thus, will be flipped. Use it to control the checking operation.

In circuit (c)c, the ancillary qubit is set to 1. Thus, the comparison operator will flip if , i.e. if . Thanks to we can control the checking operation. For the rest of the circuit, the idea is the same as the one of the circuit (a)a.

Within the scope of an optimization of the depth of our implementation, note that constraint checking operations can easily be parallelized (figure 8). We just have to divide in sets of constraints and use a copy of the registers and a counter for checking each one. This involves using qubits but the depth of the predicate would be divided by (we just have then to fan-out/fan-in registers to copies, which can be done in depth ).

(a) (a)
(b) (c)
(c) (b)
Figure 3: Circuit (a) computes . If the assignment is a solution, then , else . Circuit (b) is a comparison operator. The operation is done modulo . Circuit (c) computes . If the partial assignment is not invalid, then , else .

4.2 How to check a constraint

The circuit (a)a is used to check if the variable has been assigned a value (e.g. if the -th vertex of a graph has been colored). It uses the ancillary register which contains a bit string corresponding to the counter. Since we have chosen that the value 0 means that , we just have to check that is different from 0. The counter is increased by 1 if has not been assigned a value.

The circuit (b)b is used to check if the variables and have different values (e.g. if the coloring of the edge between the -th and the -th vertices of a graph is well colored). It uses the ancillary register , which is increased by 1 if (i.e. ). For that, we apply a bit-wise XOR to the values of the two variables (the result is stocked in the register of the second value).

Figure 4: Circuit (a) checks if the -th variable has been assigned a value. If , , otherwise . Circuit (b) checks if the -th and the -th variables have different values. If , , otherwise .

For the specific case of boolean variables, we suggest to represent the bit 0 by the quantum state , the bit 1 by the quantum state and the unassigned symbol by . In the following, we will denote the left qubit by an in exponent and the right qubit by an in exponent. Our suggestion stems from the fact that thanks to this choice, it will be simpler to manipulate the variables. The left qubit will allow us to do the negation of the boolean variable without having a side effect on the unassigned values. The right qubit will allow us to distinguish 1 from and 0, which will be useful to check a disjunction of literals.

Figure 5: Circuit (a) is the negation of a boolean variable. The value associated with is the negation of the one associated with . Circuit (b) checks if the disjunction of the literals is True or False. If , , otherwise .

4.3 General structure

Thanks to the precedent subsections, step 2 and most of step 5 of algorithm 4 can be realized. The rest of the implementation is most straightforward, circuits of the operators and are given in appendix (resp. Fig. 9 and 10). Note that contrary to what is said in [7], we just need to add one control qubit to three (resp. one) controlled-Z gates in step 6 to control the whole operator (resp. ).

5 Simulation results

Now, we present some results of our simulations for the problem of graph coloring. In order to check the constraints, the circuits (a)a and (b)b can be used. The colors red (circle), green (square) and blue (triangle) will be denoted by , and . We will denote any partial assignment which is not trivially false by and if the values are of importance by .

(a) (a)
(b) (c)
(c) (b)
(d) (d)
Figure 6: Fig. (a): a graph. Fig. (b): its associated classical backtracking tree. Tab. (c): results of our simulations. Fig. (d): some of these results as a graph.

Fig. (b)b (resp. (b)b) shows the classical backtracking tree associated to the graph in Fig. (a)a (resp. (a)a), assuming, without loss of generality, that the first vertex has been colored in blue.

By applying the algorithm 3 on the root of the complete tree associated to the graph in Fig. (a)a, it is supposed to detect that there is no possible coloring for this graph with two colors. Thus, the subtree presented here may not be visited thanks to the quantum part. For the second example, the algorithm 3 will travel along the dark gray tree (if it outputs all the solutions in Fig. (b)b).

Tab. (c)c (resp. (c)c) presents some probabilities of obtaining eigenvalue 1 depending on the precision used for the phase estimation in the algorithm 2 and the partial assignment at the root of the subtree on which it is applied. Some of these probabilities (y-axis) depending on the precision (x-axis) are presented in Fig. (d)d (resp. (d)d).

For the first example, we look at the results where the algorithm has been applied on . We can see that for a precision smaller than 4, the algorithm 2 will fail with high probability, since the threshold probability of acceptance fixed in [22] is (blue line). For the second example, we look at the results where the algorithm has been applied on (plus signs) and (crosses). We can see that for a precision equal to 1, the algorithm 2 will fail with high probability.

(a) (a)
(b) (c)
(c) (b)
(d) (d)
Figure 7: Fig. (a): a graph. Fig. (b): a subtree of its associated classical backtracking tree (). Tab. (c): results of our simulations. Fig. (d): some of these results as a graph.

6 Conclusion

In this paper, we discussed our implementation of Montanaro’s algorithm, but an improved quantum algorithm for backtracking has been introduced by Ambainis and Kokainis [3], reducing the queries complexity from to , where is the number of vertices of actually explored by a classical backtracking algorithm. Nevertheless, Montanaro’s algorithm can not be left out since it is a component of Ambainis-Kokainis’ algorithm.

While Campbell, Khurana and Montanaro [7] assumed access to an extremely large number of physical qubits to propose a depth optimized method to implement Montanaro’s algorithm, we have presented techniques minimizing the space usage. For that, we especially looked at the implementation of the predicate and the heuristic. We have proposed the use of a quantum counter for the former and highlighted the fact that up to a certain point, the latter might not be quantumly implemented. However, these propositions are not asymptotically competitive, although our implementation of the predicate could be parallelized to be efficient and could lead to a trade-off between the space usage and the time usage. As far as the heuristic is concerned, it would be interesting to establish a precise resource estimation and define to what extent a SVO heuristic would present benefits compared to a DVO one.

Acknowledgments. This work was supported by Atos. The implementation was developed in python using Atos’ pyAQASM library. All simulations were performed on the Atos Quantum Learning Machine. We acknowledge support from the French ANR project ANR-18-CE47-0010 (QUDATA), the QuantERA ERA-NET Cofund in Quantum Technologies implemented within the European Union’s Horizon 2020 Program (QuantAlgo project), and the French ANR project ANR-18-QUAN-0017 (QuantAlgo Project).


  • [1] A. Ambainis. Quantum walks and their algorithmic applications. International Journal of Quantum Information 01(04) 507–518, 2003.
  • [2] A. Ambainis. Quantum walk algorithm for element distinctness. SIAM J. Comput. 37(1) 210–239, 2007.
  • [3] A. Ambainis and M. Kokainis. Quantum algorithm for tree size estimation, with applications to backtracking and 2-player games. ACM, 2017.
  • [4] Y. Aono, P. Q. Nguyen, and Y. Shen. Quantum Lattice Enumeration and Tweaking Discrete Pruning. Springer, 2018.
  • [5] A. Belovs. Quantum walks and electric networks. arXiv:1302.3143, 2013.
  • [6] A. Belovs, A. M. Childs, S. Jeffery, R. Kothari, and F. Magniez. Time-efficient quantum walks for 3-distinctness. Springer, 2013.
  • [7] E. Campbell, A. Khurana, and A. Montanaro. Applying quantum algorithms to constraint satisfaction problems. Quantum 3(167), 2018.
  • [8] L. Chen, S. Jordan, Y.-K. Liu, D. Moody, R. Peralta, R. Perlner, and D. Smith-Tone. Report on post-quantum cryptography. NISTIR 8105, 2016.
  • [9] A. M. Childs, R. Cleve, E. Deotto, E. Farhi, S. Gutmann, and D. A. Spielman. Exponential algorithmic speedup by a quantum walk. ACM, 2003.
  • [10] M. Davis, G. Logemann, and D. Loveland. A machine program for theorem-proving. Communications of the ACM 5(7) 394–397, 1962.
  • [11] M. Davis and H. Putnam. A computing procedure for quantification theory. JACM 7(3) 201–215, 1960.
  • [12] D. de Werra. An introduction to timetabling. European Journal of Operational Research 19(2) 151–162, 1985.
  • [13] R. Dechter and I. Meiri. Experimental evaluation of preprocessing techniques in constraint satisfaction problems. Morgan Kaufmann Publishers Inc., 1989.
  • [14] N. Eén and N. Sörensson. An extensible SAT-solver. Springer, 2004.
  • [15] E. C. Freuder. A sufficient condition for backtrack-free search. JACM 29(1) 24–32, 1982.
  • [16] C. P. Gomes, H. Kautz, A. Sabharwal, and B. Selman. Satisfiability solvers. Elsevier, 2008.
  • [17] J. Gu, P. W. Purdom, J. Franco, and B. W. Wah. Algorithms for the satisfiability (SAT) problem: a survey. Amer Mathematical Society, 1997.
  • [18] J. Kempe. Quantum random walks: an introductory overview. Contemporary Physics 44(4) 307–327, 2003.
  • [19] F. T. Leighton. A graph coloring algorithm for large scheduling problems. Journal of research of the NBS 84(6), 1979.
  • [20] F. Magniez, A. Nayak, J. Roland, and M. Santha. Search via quantum walk. Theory of Computing, 2007.
  • [21] E. Malaguti and P. Toth. A survey on vertex coloring problems. International Transactions in Operational Research 17, 2010.
  • [22] A. Montanaro. Quantum walk speedup of backtracking algorithms. Theory of Computing 14(15) 1–24, 2015.
  • [23] A. Montanaro. Data from Quantum algorithms for CSPs. 07 2019.
  • [24] A. Montanaro. Quantum speedup of branch-and-bound algorithms. arXiv:1906.10375, 2019.
  • [25] M. Santha. Quantum walk based search algorithms. Springer, 2008.
  • [26] M. Szegedy.

    Quantum speed-up of Markov chain based algorithms

    IEEE, 2004.
  • [27] P. van Beek. Backtracking search algorithms. Elsevier, 2006.


Figure 8: Example of parallelization of the predicate for . is a partition of and . The checking operation of an element in is denoted by Check.
Figure 9: Circuit corresponding to algorithm 4 for . Steps 7 and 8 are not represented since they consist in reversing steps 5, 4, 2 and 1. Step 3 is realized by controlling cZ (controlled Z) operations in step 6 with . In the same way, it is sufficient to control the three cZ in step 6 to control the whole operator, due to the reversibility of the other steps.
Figure 10: Circuit corresponding to algorithm 4 for . Steps 7 and 8 are not represented since they consist in reversing steps 5, 4, 2 and 1. Steps "" and 3 are realized by controlling cZ (controlled Z) operation in step 6 with and respectively. In the same way, it is sufficient to control the cZ operation in step 6 to control the whole operator, due to the reversibility of the other steps.