qiskit.aqua.algorithms.Grover¶

class
Grover
(oracle, good_state=None, state_preparation=None, iterations=1, sample_from_iterations=False, post_processing=None, grover_operator=None, quantum_instance=None, init_state=None, incremental=False, num_iterations=None, lam=None, rotation_counts=None, mct_mode=None)[código fonte]¶ Grover’s Search algorithm.
Grover’s Search [1, 2] is a well known quantum algorithm for that can be used for searching through unstructured collections of records for particular targets with quadratic speedup compared to classical algorithms.
Given a set \(X\) of \(N\) elements \(X=\{x_1,x_2,\ldots,x_N\}\) and a boolean function \(f : X \rightarrow \{0,1\}\), the goal of an unstructuredsearch problem is to find an element \(x^* \in X\) such that \(f(x^*)=1\).
The search is called unstructured because there are no guarantees as to how the database is ordered. On a sorted database, for instance, one could perform binary search to find an element in \(\mathbb{O}(\log N)\) worstcase time. Instead, in an unstructuredsearch problem, there is no prior knowledge about the contents of the database. With classical circuits, there is no alternative but to perform a linear number of queries to find the target element. Conversely, Grover’s Search algorithm allows to solve the unstructuredsearch problem on a quantum computer in \(\mathcal{O}(\sqrt{N})\) queries.
To carry out this search a socalled oracle is required, that flags a good element/state. The action of the oracle \(\mathcal{S}_f\) is
\[\mathcal{S}_f x\rangle = (1)^{f(x)} x\rangle,\]i.e. it flips the phase of the state \(x\rangle\) if \(x\) is a hit. The details of how \(S_f\) works are unimportant to the algorithm; Grover’s search algorithm treats the oracle as a black box.
This class supports oracles in form of
QuantumCircuit
orOracle
. For example theLogicalExpressionOracle
can take as input a SAT problem in DIMACS CNF format and be used with Grover algorithm to find a satisfiable assignment.With oracle at hand, Grover’s Search constructs the Grover operator to amplify the amplitudes of the good states:
\[\mathcal{Q} = H^{\otimes n} \mathcal{S}_0 H^{\otimes n} \mathcal{S}_f = D \mathcal{S}_f,\]where \(\mathcal{S}_0\) flips the phase of the allzero state and acts as identity on all other states. Sometimes the first three operands are summarized as diffusion operator, which implements a reflection over the equal superposition state.
If the number of solutions is known, we can calculate how often \(\mathcal{Q}\) should be applied to find a solution with very high probability, see the method optimal_num_iterations. If the number of solutions is unknown, the algorithm tries different powers of Grover’s operator, see the iterations argument, and after each iteration checks if a good state has been measured using good_state.
The generalization of Grover’s Search, Quantum Amplitude Amplification [3] uses a modified version of \(\mathcal{Q}\) where the diffusion operator does not reflect about the equal superposition state, but another state specified via an operator \(\mathcal{A}\):
\[\mathcal{Q} = \mathcal{A} \mathcal{S}_0 \mathcal{A}^\dagger \mathcal{S}_f.\]For more information, see the
GroverOperator
in the circuit library.Referências
 [1]: L. K. Grover (1996), A fast quantum mechanical algorithm for database search,
 [2]: I. Chuang & M. Nielsen, Quantum Computation and Quantum Information,
Cambridge: Cambridge University Press, 2000. Chapter 6.1.2.
 [3]: Brassard, G., Hoyer, P., Mosca, M., & Tapp, A. (2000).
Quantum Amplitude Amplification and Estimation. arXiv:quantph/0005055.
 Parâmetros
oracle (
Union
[Oracle
,QuantumCircuit
,Statevector
]) – The oracle to flip the phase of good states, \(\mathcal{S}_f\).good_state (
Union
[Callable
[[str
],bool
],List
[int
],List
[str
],Statevector
,None
]) – A callable to check if a given measurement corresponds to a good state. For convenience, a list of bitstrings, a list of integer or statevector can be passed instead of a function. 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\rangle\). If it is aStatevector
, it represents a superposition of all good states.state_preparation (
Optional
[QuantumCircuit
]) – The state preparation \(\mathcal{A}\). If None then Grover’s Search by default uses uniform superposition.iterations (
Union
[int
,List
[int
]]) – Specify the number of iterations/power of Grover’s operator to be checked. It the number of solutions is known, this should be an integer specifying the optimal number of iterations (seeoptimal_num_iterations
). Alternatively, this can be a list of powers to check.sample_from_iterations (
bool
) – If True, instead of taking the values initerations
as powers of the Grover operator, a random integer sample between 0 and smaller value than the iteration is used as a power, see [1], Section 4.post_processing (
Optional
[Callable
[[List
[int
]],List
[int
]]]) – An optional post processing applied to the top measurement. Can be used e.g. to convert from the bitrepresentation of the measurement [1, 0, 1] to a DIMACS CNF format [1, 2, 3].grover_operator (
Optional
[QuantumCircuit
]) – A circuit implementing the Grover operator \(\mathcal{Q}\). If None, the operator is constructed automatically using theGroverOperator
from the circuit library.quantum_instance (
Union
[QuantumInstance
,Backend
,BaseBackend
,None
]) – A Quantum Instance or Backend to run the circuits.init_state (
Optional
[InitialState
]) – DEPRECATED, usestate_preparation
instead. An optional initial quantum state. If None (default) then Grover’s Search by default uses uniform superposition to initialize its quantum state. However, an initial state may be supplied, if useful, for example, if the user has some prior knowledge regarding where the search target(s) might be located.incremental (
bool
) – DEPRECATED, useiterations
instead. Whether to use incremental search mode (True) or not (False). Supplied num_iterations is ignored when True and instead the search task will be carried out in successive rounds, using circuits built with incrementally higher number of iterations for the repetition of the amplitude amplification until a target is found or the maximal number \(\log N\) (\(N\) being the total number of elements in the set from the oracle used) of iterations is reached. The implementation follows Section 4 of [2].num_iterations (
Optional
[int
]) – DEPRECATED, useiterations
instead. How many times the marking and reflection phase subcircuit is repeated to amplify the amplitude(s) of the target(s). Has a minimum value of 1.lam (
Optional
[float
]) – DEPRECATED, useiterations
instead. For incremental search mode, the maximum number of repetition of amplitude amplification increases by factor lam in every round, \(R_{i+1} = lam \times R_{i}\). If this parameter is not set, the default value lam = 1.34 is used, which is proved to be optimal [1].rotation_counts (
Optional
[List
[int
]]) – DEPRECATED, useiterations
instead. For incremental mode, if rotation_counts is defined, parameter lam is ignored. rotation_counts is the list of integers that defines the number of repetition of amplitude amplification for each round.mct_mode (
Optional
[str
]) – DEPRECATED, pass a customgrover_operator
instead. MultiControl Toffoli mode (‘basic’  ‘basicdirtyancilla’  ‘advanced’  ‘noancilla’)
 Levanta
TypeError – If
init_state
is of unsupported type or is of typeInitialState` but the oracle is not of type ``Oracle
.AquaError – evaluate_classically() missing from the input oracle
TypeError – If
oracle
is of unsupported type.
Referências
 [1]: Boyer et al., Tight bounds on quantum searching

__init__
(oracle, good_state=None, state_preparation=None, iterations=1, sample_from_iterations=False, post_processing=None, grover_operator=None, quantum_instance=None, init_state=None, incremental=False, num_iterations=None, lam=None, rotation_counts=None, mct_mode=None)[código fonte]¶  Parâmetros
oracle (
Union
[Oracle
,QuantumCircuit
,Statevector
]) – The oracle to flip the phase of good states, \(\mathcal{S}_f\).good_state (
Union
[Callable
[[str
],bool
],List
[int
],List
[str
],Statevector
,None
]) – A callable to check if a given measurement corresponds to a good state. For convenience, a list of bitstrings, a list of integer or statevector can be passed instead of a function. 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\rangle\). If it is aStatevector
, it represents a superposition of all good states.state_preparation (
Optional
[QuantumCircuit
]) – The state preparation \(\mathcal{A}\). If None then Grover’s Search by default uses uniform superposition.iterations (
Union
[int
,List
[int
]]) – Specify the number of iterations/power of Grover’s operator to be checked. It the number of solutions is known, this should be an integer specifying the optimal number of iterations (seeoptimal_num_iterations
). Alternatively, this can be a list of powers to check.sample_from_iterations (
bool
) – If True, instead of taking the values initerations
as powers of the Grover operator, a random integer sample between 0 and smaller value than the iteration is used as a power, see [1], Section 4.post_processing (
Optional
[Callable
[[List
[int
]],List
[int
]]]) – An optional post processing applied to the top measurement. Can be used e.g. to convert from the bitrepresentation of the measurement [1, 0, 1] to a DIMACS CNF format [1, 2, 3].grover_operator (
Optional
[QuantumCircuit
]) – A circuit implementing the Grover operator \(\mathcal{Q}\). If None, the operator is constructed automatically using theGroverOperator
from the circuit library.quantum_instance (
Union
[QuantumInstance
,Backend
,BaseBackend
,None
]) – A Quantum Instance or Backend to run the circuits.init_state (
Optional
[InitialState
]) – DEPRECATED, usestate_preparation
instead. An optional initial quantum state. If None (default) then Grover’s Search by default uses uniform superposition to initialize its quantum state. However, an initial state may be supplied, if useful, for example, if the user has some prior knowledge regarding where the search target(s) might be located.incremental (
bool
) – DEPRECATED, useiterations
instead. Whether to use incremental search mode (True) or not (False). Supplied num_iterations is ignored when True and instead the search task will be carried out in successive rounds, using circuits built with incrementally higher number of iterations for the repetition of the amplitude amplification until a target is found or the maximal number \(\log N\) (\(N\) being the total number of elements in the set from the oracle used) of iterations is reached. The implementation follows Section 4 of [2].num_iterations (
Optional
[int
]) – DEPRECATED, useiterations
instead. How many times the marking and reflection phase subcircuit is repeated to amplify the amplitude(s) of the target(s). Has a minimum value of 1.lam (
Optional
[float
]) – DEPRECATED, useiterations
instead. For incremental search mode, the maximum number of repetition of amplitude amplification increases by factor lam in every round, \(R_{i+1} = lam \times R_{i}\). If this parameter is not set, the default value lam = 1.34 is used, which is proved to be optimal [1].rotation_counts (
Optional
[List
[int
]]) – DEPRECATED, useiterations
instead. For incremental mode, if rotation_counts is defined, parameter lam is ignored. rotation_counts is the list of integers that defines the number of repetition of amplitude amplification for each round.mct_mode (
Optional
[str
]) – DEPRECATED, pass a customgrover_operator
instead. MultiControl Toffoli mode (‘basic’  ‘basicdirtyancilla’  ‘advanced’  ‘noancilla’)
 Levanta
TypeError – If
init_state
is of unsupported type or is of typeInitialState` but the oracle is not of type ``Oracle
.AquaError – evaluate_classically() missing from the input oracle
TypeError – If
oracle
is of unsupported type.
Referências
 [1]: Boyer et al., Tight bounds on quantum searching
Methods
__init__
(oracle[, good_state, …]) type oracle
Union
[Oracle
,QuantumCircuit
,Statevector
]
construct_circuit
([power, measurement])Construct the circuit for Grover’s algorithm with
power
Grover operators.is_good_state
(bitstr)Check whether a provided bitstring is a good state or not.
optimal_num_iterations
(num_solutions, num_qubits)Return the optimal number of iterations, if the number of solutions is known.
post_processing
(measurement)Do the postprocessing to the measurement result
run
([quantum_instance])Execute the algorithm with selected backend.
set_backend
(backend, **kwargs)Sets backend with configuration.
Attributes
Returns backend.
Returns grover_operator.
Returns quantum instance.
Return a numpy random.

property
backend
¶ Returns backend.
 Tipo de retorno
Union
[Backend
,BaseBackend
]

construct_circuit
(power=None, measurement=False)[código fonte]¶ Construct the circuit for Grover’s algorithm with
power
Grover operators. Parâmetros
power (
Optional
[int
]) – The number of times the Grover operator is repeated. If None, this argument is set to the first item initerations
.measurement (
bool
) – Boolean flag to indicate if measurement should be included in the circuit.
 Retorna
the QuantumCircuit object for the constructed circuit
 Tipo de retorno

property
grover_operator
¶ Returns grover_operator.
 Tipo de retorno
QuantumCircuit

is_good_state
(bitstr)[código fonte]¶ Check whether a provided bitstring is a good state or not.
 Parâmetros
bitstr (
str
) – The measurement as bitstring. Tipo de retorno
bool
 Retorna
True if the measurement is a good state, False otherwise.

static
optimal_num_iterations
(num_solutions, num_qubits)[código fonte]¶ Return the optimal number of iterations, if the number of solutions is known.
 Parâmetros
num_solutions (
int
) – The number of solutions.num_qubits (
int
) – The number of qubits used to encode the states.
 Tipo de retorno
int
 Retorna
The optimal number of iterations for Grover’s algorithm to succeed.

post_processing
(measurement)[código fonte]¶ Do the postprocessing to the measurement result
 Parâmetros
measurement (
List
[int
]) – The measurement as list of int. Tipo de retorno
List
[int
] Retorna
Do the postprocessing based on the post_processing argument. If the post_processing argument is None and the Oracle class is used as its oracle, oracle.evaluate_classically is used as the post_processing. Otherwise, just return the input bitstr

property
quantum_instance
¶ Returns quantum instance.
 Tipo de retorno
Optional
[QuantumInstance
]

property
random
¶ Return a numpy random.

run
(quantum_instance=None, **kwargs)¶ Execute the algorithm with selected backend.
 Parâmetros
quantum_instance (
Union
[QuantumInstance
,Backend
,BaseBackend
,None
]) – the experimental setting.kwargs (dict) – kwargs
 Retorna
results of an algorithm.
 Tipo de retorno
dict
 Levanta
AquaError – If a quantum instance or backend has not been provided

set_backend
(backend, **kwargs)¶ Sets backend with configuration.
 Tipo de retorno
None