Professional Documents
Culture Documents
Grover's Algorithm and Amplitude Amplification
Grover's Algorithm and Amplitude Amplification
3 documentation
English
Table of Contents
• NOTE
Qiskit implements Grover’s algorithm in the Grover class. This class also includes the generalized version, Amplitude Amplification [3], and allows setting individual
iterations and other meta-settings to Grover’s algorithm.
References:
[1]: L. K. Grover, A fast quantum mechanical algorithm for database search. Proceedings 28th Annual Symposium on the Theory of Computing (STOC) 1996, pp. 212-219.
https://arxiv.org/abs/quant-ph/9605043
[2]: A. Gilliam, S. Woerner, C. Gonciulea, Grover Adaptive Search for Constrained Polynomial Binary Optimization. https://arxiv.org/abs/1912.04088
[3]: Brassard, G., Hoyer, P., Mosca, M., & Tapp, A. (2000). Quantum Amplitude Amplification and Estimation. http://arxiv.org/abs/quant-ph/0005055
Grover ’s algorithm ¶
Grover’s algorithm uses the Grover operator Q to amplify the amplitudes of the good states:
†
Q = AS0 A Sf
Here, * A is the initial search state for the algorithm, which is just Hadamards, H ⊗n for the textbook Grover search, but can be more elaborate for Amplitude Amplification
* S0 is the reflection about the all 0 state
−|x⟩, x ≠ 0
|x⟩ ↦ {
|x⟩, x = 0
f (x)
|x⟩ ↦ (−1) |x⟩
In a nutshell, Grover’s algorithm applies different powers of Q and after each execution checks whether a good solution has been found.
Note that the oracle for Grover must be a phase-flip oracle. That is, it multiplies the amplitudes of the of “good states” by a factor of −1. We explain later how to convert a
bit-flip oracle to a phase-flip oracle.
# specify the oracle that marks the state '11' as a good solution
oracle = QuantumCircuit(2)
oracle.cz(0, 1)
# now we can have a look at the Grover operator that is used in running the algorithm
# (Algorithm circuits are wrapped in a gate to appear in composition as a block
# so we have to decompose() the op to see it expanded into its component gates.)
problem.grover_operator.decompose().draw(output='mpl')
[1]:
Then, we specify a backend and call the run method of Grover with a backend to execute the circuits. The returned result type is a GroverResult .
If the search was successful, the oracle_evaluation attribute of the result will be True . In this case, the most sampled measurement, top_measurement , is one of the “good
states”. Otherwise, oracle_evaluation will be False.
grover = Grover(sampler=Sampler())
result = grover.amplify(problem)
print('Result type:', type(result))
print()
print('Success!' if result.oracle_evaluation else 'Failure!')
print('Top measurement:', result.top_measurement)
Success!
Top measurement: 11
In the example, the result of top_measurement is 11 which is one of “good state”. Thus, we succeeded to find the answer by using Grover .
grover = Grover(sampler=Sampler())
result = grover.amplify(problem)
print('Result type:', type(result))
print()
print('Success!' if result.oracle_evaluation else 'Failure!')
print('Top measurement:', result.top_measurement)
Success!
Top measurement: 11
[4]: problem.grover_operator.oracle.decompose().draw(output='mpl')
https://qiskit.org/documentation/tutorials/algorithms/06_grover.html#Amplitude-amplification 1/4
7/27/23, 11:25 PM Grover’s Algorithm and Amplitude Amplification — Qiskit 0.43.3 documentation
[4]:
Qiskit allows for an easy construction of more complex oracles: * PhaseOracle : for parsing logical expressions such as '~a | b' . This is especially useful for solving 3-SAT
problems and is shown in the accompanying Grover Examples tutorial.
Here we’ll use the PhaseOracle for the simple example of finding the state |11⟩, which corresponds to 'a & b' .
Which we observe that this oracle implements a phase flip when the state is |11⟩
As mentioned above, Grover’s algorithm requires a phase-flip oracle. A bit-flip oracle flips the state of an auxiliary qubit if the other qubits satisfy the condition. To use
these types of oracles with Grover’s we need to convert the bit-flip oracle to a phase-flip oracle by sandwiching the auxiliary qubit of the bit-flip oracle by X and H gates.
Note: This transformation from a bit-flip to a phase-flip oracle holds generally and you can use this to convert your oracle to the right representation.
Amplitude amplification ¶
Grover’s algorithm uses Hadamard gates to create the uniform superposition of all the states at the beginning of the Grover operator Q. If some information on the good
states is available, it might be useful to not start in a uniform superposition but only initialize specific states. This, generalized, version of Grover’s algorithm is referred to
Amplitude Amplification.
In Qiskit, the initial superposition state can easily be adjusted by setting the state_preparation argument.
State preparation ¶
A state_preparation argument is used to specify a quantum circuit that prepares a quantum state for the start point of the amplitude amplification. By default, a circuit with
H
⊗n
is used to prepare uniform superposition (so it will be Grover’s search). The diffusion circuit of the amplitude amplification reflects state_preparation automatically.
# Specifying `state_preparation`
# to prepare a superposition of |01>, |10>, and |11>
oracle = QuantumCircuit(3)
oracle.ccz(0, 1, 2)
# we only care about the first two bits being in state 1, thus add both possibilities for the last qubit
problem = AmplificationProblem(oracle, state_preparation=state_preparation, is_good_state=['110', '111'])
# state_preparation
print('state preparation circuit:')
problem.grover_operator.state_preparation.draw(output='mpl')
[6]:
Success!
Top measurement: 111
Full flexibility ¶
For more advanced use, it is also possible to specify the entire Grover operator by setting the grover_operator argument. This might be useful if you know more efficient
implementation for Q than the default construction via zero reflection, oracle and state preparation.
The qiskit.circuit.library.GroverOperator can be a good starting point and offers more options for an automated construction of the Grover operator. You can for instance
* set the mcx_mode * ignore qubits in the zero reflection by setting reflection_qubits * explicitly exchange the Sf , S0 and A operations using the oracle , zero_reflection
and state_preparation arguments
For instance, imagine the good state is a three qubit state |111⟩ but we used 2 additional qubits as auxiliary qubits.
https://qiskit.org/documentation/tutorials/algorithms/06_grover.html#Amplitude-amplification 2/4
7/27/23, 11:25 PM Grover’s Algorithm and Amplitude Amplification — Qiskit 0.43.3 documentation
[8]:
Then, per default, the Grover operator implements the zero reflection on all five qubits.
[9]:
[10]:
Grover has arguments other than oracle and state_preparation . We will explain them in this section.
Specifying good_state ¶
good_state is used to check whether the measurement result is correct or not internally. It can be a list of binary strings, a list of integer, Statevector , and Callable. If the
input is a list of bitstrings, each bitstrings in the list represents a good state. If the input is a list of integer, each integer represent the index of the good state to be |1⟩. If it
is a Statevector , it represents a superposition of all good states.
True
True
True
oracle = QuantumCircuit(2)
oracle.cz(0, 1)
problem = AmplificationProblem(oracle, is_good_state=good_state)
print(problem.is_good_state('11'))
True
Additionally there is the sample_from_iterations argument. When it is True , instead of the specific power in iterations , a random integer between 0 and the value in
iteration is used as the power Grover’s operator. This approach is useful when we don’t even know the number of solution.
https://qiskit.org/documentation/tutorials/algorithms/06_grover.html#Amplitude-amplification 3/4
7/27/23, 11:25 PM Grover’s Algorithm and Amplitude Amplification — Qiskit 0.43.3 documentation
For more details of the algorithm using sample_from_iterations , see [4].
References:
When the number of solutions is known, we can also use a static method optimal_num_iterations to find the optimal number of iterations. Note that the output iterations is
an approximate value. When the number of qubits is small, the output iterations may not be optimal. In addition, the calculation of this value assumes the standard
uniform superposition state preparation and may not be accurate for other state preparations.
[18]: 12
Applying post_processing ¶
We can apply an optional post processing to the top measurement for ease of readability. It can be used e.g. to convert from the bit-representation of the measurement
[1, 0, 1] to a DIMACS CNF format [1, -2, 3] .
oracle = QuantumCircuit(2)
oracle.cz(0, 1)
problem = AmplificationProblem(oracle, is_good_state=['11'], post_processing=to_DIAMACS_CNF_format)
problem.post_processing([1, 0, 1])
Version Information
Previous Next
https://qiskit.org/documentation/tutorials/algorithms/06_grover.html#Amplitude-amplification 4/4