Grover's algorithm
Ang pahinang ito ay hindi pa naisalin. Nakikita mo ang orihinal na bersyon sa Ingles.
For this Qiskit in Classrooms module, students must have a working Python environment with the following packages installed:
qiskitv2.1.0 or newerqiskit-ibm-runtimev0.40.1 or newerqiskit-aerv0.17.0 or newerqiskit.visualizationnumpypylatexenc
To set up and install the packages above, see the Install Qiskit guide. In order to run jobs on real quantum computers, students will need to set up an account with IBM Quantum® by following the steps in the Set up your IBM Cloud account guide.
This module was tested and used 12 seconds of QPU time. This is a good-faith estimate; your actual usage may vary.
# Uncomment and modify this line as needed to install dependencies
#!pip install 'qiskit>=2.1.0' 'qiskit-ibm-runtime>=0.40.1' 'qiskit-aer>=0.17.0' 'numpy' 'pylatexenc'
Introduction
Grover's algorithm is a foundational quantum algorithm that addresses the unstructured search problem: given a set of items and a way to check if any given item is the one you're looking for, how quickly can you find the desired item? In classical computing, if the data is unsorted and there is no structure to exploit, the best approach is to check each item one by one, leading to a query complexity of — on average, you'll need to check about half the items before finding the target.

Grover's algorithm, introduced by Lov Grover in 1996, demonstrates how a quantum computer can solve this problem much more efficiently, requiring only steps to find the marked item with high probability. This represents a quadratic speedup over classical methods, which is significant for large datasets.
The algorithm operates in the following context:
- Problem setup: You have a function that returns 1 if is the item you want, and 0 otherwise. This function is often called an oracle or black box, since you can only learn about the data by querying .
- Usefulness of quantum: While classical algorithms for this problem require, on average, queries, Grover's algorithm can find the solution in roughly queries, which is much faster for large .
- How it works (at a high level):
- The quantum computer first creates a superposition of all possible states, representing all possible items at once.
- It then repeatedly applies a sequence of quantum operations (the Grover iteration) that amplifies the probability of the correct answer and diminishes the others.
- After enough iterations, measuring the quantum state yields the correct answer with high probability.
Here is a very basic diagram of Grover's algorithm that skips over a lot of nuance. For a more detailed diagram, see this paper.

A few things to note about Grover's algorithm:
- It is optimal for unstructured search: no quantum algorithm can solve the problem with fewer than queries.
- It provides only a quadratic, not exponential, speedup — unlike some other quantum algorithms (for example, Shor's algorithm for factoring).
- It has practical implications, such as potentially speeding up brute-force attacks on cryptographic systems, though the speedup is not enough to break most modern encryption by itself.
For undergraduate students familiar with basic computing concepts and query models, Grover's algorithm offers a clear illustration of how quantum computing can outperform classical approaches for certain problems, even when the improvement is "only" quadratic. It also serves as a gateway to understanding more advanced quantum algorithms and the broader potential of quantum computing.
Amplitude amplification is a general purpose quantum algorithm, or subroutine, that can be used to obtain a quadratic speedup over a handful of classical algorithms. Grover’s algorithm was the first to demonstrate this speedup on unstructured search problems. Formulating a Grover's search problem requires an oracle function that marks one or more computational basis states as the states we are interested in finding, and an amplification circuit that increases the amplitude of marked states, consequently suppressing the remaining states.
Here, we demonstrate how to construct Grover oracles and use the GroverOperator from the Qiskit circuit library to easily set up a Grover's search instance. The runtime Sampler primitive allows seamless execution of Grover circuits.
Math
Suppose there exists a function that maps binary strings to a single binary variable, meaning
One example defined on is
Another example defined on is
You are tasked with finding quantum states corresponding to those arguments of that are mapped to 1. In other words, find all such that (or if there is no solution, report that). We would refer to non-solutions as . Of course, we will do this on a quantum computer, using quantum states, so it is useful to express these binary strings as states:
Using the quantum state (Dirac) notation, we are looking for one or more special states in a set of possible states, where is the number of qubits, and with non-solutions denoted
We can think of the function as being provided by an oracle: a black-box that we can query to determine its effect on a state In practice, we will often know the function, but it may be very complicated to implement, meaning that reducing the number of queries or applications of could be important. Alternatively, we can imagine a paradigm in which one person is querying an oracle controlled by another person, such that we don't know the oracle function, we only know its action on particular states from querying.
This is an "unstructured search problem, in that there is nothing special about that aids us in our search. The outputs are not sorted nor are solutions known to cluster, and so on. Consider use old, paper phone books as an analogy. This unstructured search would be like scanning through it looking for a certain number, and not like looking through an alphabetized list of names.
In the case where a single solution is sought, classically, this requires a number of queries that is linear in . Clearly you might find a solution on the first try, or you might find no solutions in the first guesses, such that you need to query the input to see if there is any solution at all. Since the functions have no exploitable structure, you will require guesses on average. Grover's algorithm requires a number of queries or computations of that scales like
Sketch of circuits in Grover's algorithm
A full mathematical walkthrough of Grover's algorithm can be found, for example, in Fundamentals of quantum algorithms, a course by John Watrous on IBM Quantum Learning. A condensed treatment is provided in an appendix at the end of this module. But for now, we will only review the overall structure of the quantum circuit that implements Grover's algorithm.
Grover's algorithm can be broken down into the following stages:
- Preparation of an initial superposition (applying Hadamard gates to all qubits)
- "Marking" the target state(s) with a phase flip
- A "diffusion" stage in which Hadamard gates and a phase flip are applied to all qubits.
- Possible repetitions of the marking and diffusion stages to maximize the probability of measuring the target state
- Measurement
Often, the marking gate and the diffusion layers consisting of and are collectively referred to as the "Grover operator". In this diagram, only a single repetition of the Grover operator is shown.
Hadamard gates are well-known and used widely throughout quantum computing. The Hadamard gate creates superposition states. Specifically it is defined by
Its operation on any other state is defined through linearity. In particularly, a layer of Hadamard gates allows us to go from the initial state with all qubits in (denoted ) to a state where each qubit has some probability of being measured in either or this lets us probe the space of all possible states differently from in classical computing.
An important corollary property of the Hadamard gate is that acting a second time can undo such superposition states: