Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
Grover’s Search Algorithm, introduced by Lov Grover in 1996 [1], is one of the canonical quantum algorithms. It provides a quadratic speedup for searching an unstructured database and is often considered alongside Shor’s algorithm as a cornerstone of quantum computing. The search algorithm has applications in various fields, such as in cybersecurity.
  • Input: A Boolean function (oracle) f:{0,1}n→{0,1}f: \{0,1\}^n \rightarrow \{0,1\} marking “solutions” among N=2nN = 2^n possible items.
  • Promise: At least one marked element exists in the search space.
  • Output: With high probability, the algorithm outputs a marked element xx (i.e., f(x)=1f(x) = 1).
Complexity: The algorithm requires O(N)O(\sqrt{N}) oracle queries to obtain the result, while the query complexity for classical search is O(N)O(N).
Keywords: Search and optimization, Unstructured search, Quadratic speedup, Amplitude amplification, Graph problems, SAT problems, Oracle/Query complexity.
Grover’s Search algorithm starts with preparing the search space ∣s⟩|s\rangle, and then repeats over a combination of two reflection operations: Uf∣x⟩=(−1)f(x)∣x⟩usually called an "Oracle", adds a minus phase to marked states, andU_f|x\rangle = (-1)^{f(x)}|x\rangle \qquad \text{usually called an "Oracle", adds a minus phase to marked states, and} Us=2∣s⟩⟨s∣−1usually called a "Diffuser", reflects around the full search space.U_s = 2|s\rangle\langle s|-1 \qquad \text{usually called a "Diffuser", reflects around the full search space.} The number of times we need to repeat the combination of these two reflections (sometimes called a “Grover Operator”) depends on the number of solutions: for kk solutions in a search space of size NN, we shall perform r∼πN/kr\sim \pi \sqrt{N/k} iterations. In practice, however, the number of solutions is often unknown. In that case, one can perform a quantum counting algorithm prior to the search, or run the search algorithm repeatedly, with an increasing number of repetitions ri∼π42ir_i\sim \frac{\pi}{4} \sqrt{2^i} for i=0,1,…i=0,1,\dots. Both approaches do not affect the complexity of the search algorithm. In this notebook we take the second approach. For a given problem, Grover’s algorithm depends on two functions, one for preparing the search space and another for applying the oracle, as well as on the number of repetitions. Below we address two canonical search problems, a 3-SAT problem and a Max-Cut problem on a graph. In this notebook we work with the grover_operator function, and define a classical postprocess function for iterating over different rr values and obtianing the marked states. Using the phase_oracle quantum function from the Classiq open-library together with the Qmod language for high-level problem definitions helps avoid low-level implementation details that are typically required on other platforms.

Screenshot 2025-09-11 at 10.17.19.png

Classical Postprocess Function

Below we define a function that runs a quantum program of the Grover’s search algorithm, for an increasing number of repetitions, until a marked state is found.

Example: 3-SAT Problem

The 3-SAT problem [2] is a famous NP-Complete\text{NP-Complete} problem, a solution of which allows solving any problem in the complexity class NP\text{NP}. We treat two different 3-SAT problems, a small one and a larger one. For illustration, we define a function for printing the truth table of SAT problems.
We start with a small problem:

Small 3-SAT Formula

We specify a 3-SAT formula in the so-called Conjunctive Normal Form (CNF), that requires a solution: (x1∨x2∨x3)∧(¬x1∨x2∨x3)∧(¬x1∨¬x2∨¬x3)∧(¬x1∨¬x2∨x3)∧(x1∨x2∨¬x3)∧(¬x1∨x2∨¬x3)(x_1 \lor x_2 \lor x_3) \land (\neg x_1 \lor x_2 \lor x_3) \land (\neg x_1 \lor \neg x_2 \lor \neg x_3) \land (\neg x_1 \lor \neg x_2 \lor x_3) \land (x_1 \lor x_2 \lor \neg x_3) \land (\neg x_1 \lor x_2 \lor \neg x_3)
We can see that the formula has two possible solutions:
Output:
We define the Grover search model for finding the solution. To specify the model, we use the standard phase_oracle that transforms ‘digital’ oracle; i.e., ∣x⟩∣0⟩→∣x⟩∣f(x)⟩|x\rangle|0\rangle \rightarrow |x\rangle|f(x)\rangle to a phase oracle ∣x⟩→(−1)f(x)∣x⟩|x\rangle \rightarrow (-1)^{f(x)}|x\rangle. The predicate that we pass to the phase oracle is simply given by the 3-CNF formula defined above.
Output:
We execute our repeat until success function:
Output:
Output:
Output:
Output:
We can see that a single iteration was needed to find solutions with high probability:
Output:

Large 3-SAT Formula

We continue with a larger example:
Output:
The procedure is identical to the small use-case, just changing small_3sat_formula to large_3sat_formula.
Output:
Output:
Output:
Output:
Output:
Output:
We can print the five most probable solutions:
We can see that the amplitude of the two “marked” solutions is amplified.

Example: Graph Cut Search Problem

The “Maximum Cut Problem” (MaxCut) [3] is an example of a combinatorial optimization problem. It refers to finding a partition of a graph into two sets, such that the number of edges between the two sets is the maximum. The MaxCut problem is defined as follows: Given a graph G=(V,E)G=(V,E) with ∣V∣=n|V|=n nodes and EE edges, a cut is defined as a partition of the graph into two complementary subsets of nodes. The gaol is to find a cut where the number of edges between the two subsets is the maximum. We can represent a cut, and the number of its connecting edges as follows:
  • x∈{0,1}nx\in \{0,1\}^n is a binary vector of size nn that represents a cut: assigning 0 and 1 to nodes in the first and second subsets, respectively.
  • C(x)=∑(i,j)xi(1−xj)+xj(1−xi)=∑(i,j)xi⊕xjC(x)=\sum_{(i,j)}x_i (1-x_j)+x_j (1-x_i)=\sum_{(i,j)}x_i \oplus x_j gives the number of connecting edges for a given cut.
The MaxCut problem cannot be cast directly into a Grover’s search algorithm, as we task to find the maximum of a function, rather than some “marked” solutions. One approach is to apply Grover’s algorithms for formulas of the form C(x)≥TC(x)\geq T, for some fixed value of TT: We initialize a threshold TT, then run Grover’s algorithm with an oracle that marks cuts with C(x)≥TC(x)\geq T to amplify promising candidates. If a better cut is found, we update TT and repeat until no improvement is likely (or a preset budget is reached). In this notebook we show a solution for a single instance of this procedure, i.e., taking one example with some value for TT. We initiate a specific graph whose maximum cut is 5:
output Constructing a Grover’s search algorithm is done in similar to the 3-SAT examples, we only require to define a predicate formula. In this example we set the cut size (the value of TT) to
Output:
Output:
Output:
Output:
Output:
Upon printing the result, we see that our execution of Grover’s algorithm successfully found the satisfying assignments for the input formula:
The satisfying assignments are ~100 times more probable than the unsatisfying assignments. We print the corresponding graph for one of them:
output

References

[1]: L. K. Grover, “A fast quantum mechanical algorithm for database search”, Proceedings of the 28th Annual ACM Symposium on Theory of Computing (STOC ‘96), pp. 212-219, 1996. [2]: The 3-SAT problem [3]: The Maximum Cut problem