Path: blob/main/notebooks/ch-applications/satisfiability-grover.ipynb
3855 views
Solving Satisfiability Problems using Grover's Algorithm
In this section, we demonstrate how to solve satisfiability problems using the implementation of Grover's algorithm in Qiskit Aqua.
1. Introduction
Grover's algorithm for unstructured search was introduced in an earlier section, with an example and implementation using Qiskit Terra. We saw that Grover search is a quantum algorithm that can be used to search for solutions to unstructured problems quadratically faster than its classical counterparts. Here, we are going to illustrate the use of Grover's algorithm to solve a particular combinatorial Boolean satisfiability problem.
In computer science, the Boolean satisfiability problem is the problem of determining if there exists an interpretation that satisfies a given Boolean formula. In other words, it asks whether the variables of a given Boolean formula can be consistently replaced by the values TRUE or FALSE in such a way that the formula evaluates to TRUE. If this is the case, the formula is called satisfiable. On the other hand, if no such assignment exists, the function expressed by the formula is FALSE for all possible variable assignments and the formula is unsatisfiable. This can be seen as a search problem, where the solution is the assignment where the Boolean formula is satisfied.
For unstructured search problems, Grover’s algorithm is optimal with its run time of [2]. In this chapter, we will look at solving a specific Boolean satisfiability problem (3-Satisfiability) using Grover’s algorithm, with the aforementioned run time of . Interestingly, at the time of writing, the best-known classical algorithm for 3-Satisfiability has an upper-bound of [3]. You may have heard that Grover’s algorithm can be used to speed up solutions to NP-complete problems, but these NP-complete problems do actually contain structure[4] and we can sometimes do better than the of Grover’s algorithm.
While it doesn’t make sense to use Grover’s algorithm on 3-sat problems, the techniques here can be applied to the more general case (k-SAT, discussed in the next section) for which Grover’s algorithm can outperform the best classical algorithm. Additionally, the techniques in Grover’s algorithm can theoretically be combined with the techniques used in the classical algorithms to gain an even better run time than either individually (including with 3-SAT).
2. 3-Satisfiability Problem
The 3-Satisfiability (3SAT) Problem is best explained with the following concrete problem. Let us consider a Boolean function with three Boolean variables as below:
In the above function, the terms on the right-hand side equation which are inside are called clauses; this function has 5 clauses. In a k-SAT problem, each clause has exactly k literals; our problem is a 3-SAT problem, so each clause has exactly three literals. For instance, the first clause has , and as its literals. The symbol is the Boolean NOT that negates (or, flips) the value of its succeeding literal. The symbols and are, respectively, the Boolean OR and AND. The Boolean is satisfiable if there is an assignment of that evaluates to (that is, evaluates to True).
A naive way to find such an assignment is by trying every possible combinations of input values of . Below is the table obtained from trying all possible combinations of . For ease of explanation, we interchangeably use and False, as well as and True.
| | | | | Comment | |------|-------|-------|-----|---------| | 0 | 0 | 0 | 1 | Solution | | 0 | 0 | 1 | 0 | Not a solution because is False | | 0 | 1 | 0 | 0 | Not a solution because is False | | 0 | 1 | 1 | 0 | Not a solution because is False | | 1 | 0 | 0 | 0 | Not a solution because is False | | 1 | 0 | 1 | 1 | Solution | | 1 | 1 | 0 | 1 | Solution | | 1 | 1 | 1 | 0 | Not a solution because is False |
From the table above, we can see that this 3-SAT problem instance has three satisfying solutions: or or .
In general, the Boolean function can have many clauses and more Boolean variables. Note that SAT problems can be always written in what is known as conjunctive normal form (CNF), that is, a conjunction of one or more clauses, where a clause is a disjunction of three literals; otherwise put, it is an AND of k ORs.
3. Qiskit Implementation
Let's now use Qiskit Aqua to solve the example 3SAT problem:
First we need to understand the input DIMACS CNF format that Qiskit Aqua uses for such problem, which looks like the following for the problem:
Lines that start with
care commentseg.
c example DIMACS CNF 3-SAT
The first non-comment line needs to be of the form
p cnf nbvar nbclauses, wherecnfindicates that the input is in CNF formatnbvaris the exact number of variables appearing in the filenbclausesis the exact number of clauses contained in the fileeg.
p cnf 3 5
Then there is a line for each clause, where
each clause is a sequence of distinct non-null numbers between
-nbvarandnbvarending with0on the same lineit cannot contain the opposite literals i and -i simultaneously
positive numbers denote the corresponding variables
negative numbers denote the negations of the corresponding variables
eg.
-1 2 3 0corresponds to the clause
Similarly the solutions to the problem or or can be written as 1 -2 3, or -1 -2 -3, or 1 2 -3.
With this example problem input, we create the corresponding oracle for our Grover search. In particular, we use the LogicalExpressionOracle component provided by Qiskit, which supports parsing DIMACS CNF format strings and constructing the corresponding oracle circuit.
We have a DIMACS file saved to the relative path examples/3sat.dimacs, let's open it and see what it looks like:
We can now use PhaseOracle to create an oracle circuit from this file:
Finally, we need a way of checking if the Grover circuit has produced the correct answer. To work with Qiskit's built-in tools, the callable needs to take a measurement string (e.g. 11010011) and return True if it's a correct solution, or False otherwise. Below is a simple class that does this:
And an example usage:
We can then use Qiskit's built in tools to solve this problem:
As seen above, a satisfying solution to the specified 3-SAT problem is obtained. And it is indeed one of the three satisfying solutions.
Since we used a simulator backend, the complete measurement result is also returned, as shown in the plot below, where it can be seen that the binary strings 000, 011, and 101 (note the bit order in each string), corresponding to the three satisfying solutions all have high probabilities associated with them.
We have seen that the simulator can find the solutions to the example problem. We would like to see what happens if we use the real quantum devices that have noise and imperfect gates. We can try this using Qiskit's mock backends:
Despite the noise, the simulated Melbourne device has an increased probability of measuring a correct answer compared to random guessing. It is still a challenge to design a scalable quantum circuit for Grover search to solve larger satisfiability and other optimization problems.
5. References
Giacomo Nannicini (2017), "An Introduction to Quantum Computing, Without the Physics", arXiv:1708.03684
Christof Zalka (1997) "Grover’s quantum searching algorithm is optimal", arXiv:quant-ph/9711070
T. D. Hansen, H. Kaplan, O. Zamir, U. Zwick, "Faster k-SAT algorithms using biased-PPSZ", https://dl.acm.org/doi/10.1145/3313276.3316359
N. J. Cerf, L. K. Grover, C. P. Williams, "Nested quantum search and NP-complete problems", arXiv:quant-ph/9806078