Grover's algorithm
Estimated QPU usage: 4 seconds (tested on IBM Nairobi)
Background
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
Requirements
Before starting this tutorial, ensure that you have the following installed:
- Qiskit SDK 1.0 or later, with visualization support (
pip install 'qiskit[visualization]' ) - Qiskit Runtime (
pip install qiskit-ibm-runtime ) 0.22 or later
Step 1. Map classical inputs to a quantum problem
Grover's algorithm requires an oracle that specifies one or more marked computational basis states, where "marked" means a state with a phase of -1. A controlled-Z gate, or its multi-controlled generalization over qubits, marks the state (
Setup
Here we import the small number of tools we need for this tutorial.
No output produced
Output:
'ibm_kyoto'
No output produced
Specific Grover's instance
Now that we have the oracle function, we can define a specific instance of Grover search. In this example we will mark two computational states out of the eight available in a three-qubit computational space:
Output:
GroverOperator
The built-in Qiskit
Output:
Repeated applications of this
No output produced
Full Grover circuit
A complete Grover experiment starts with a Hadamard gate on each qubit; creating an even superposition of all computational basis states, followed the Grover operator (
Output:
Step 2. Optimize problem for quantum execution
Output:
Step 3. Execute using Qiskit Primitives
Amplitude amplification is a sampling problem that is suitable for execution with the
Note that the
No output produced
Step 4. Post-process, return result in classical format
Output:
Output:
'0.25.0'
Output:
'1.1.1'
Tutorial Survey
Tutorial Survey Please take one minute to provide feedback on this tutorial. Your insights will help us improve our content offerings and user experience. |
Was this page helpful?