GroverOperator#
- class qiskit.circuit.library.GroverOperator(oracle, state_preparation=None, zero_reflection=None, reflection_qubits=None, insert_barriers=False, mcx_mode='noancilla', name='Q')[Quellcode]#
Bases:
QuantumCircuit
The Grover operator.
Groverβs search algorithm [1, 2] consists of repeated applications of the so-called Grover operator used to amplify the amplitudes of the desired output states. This operator, \(\mathcal{Q}\), consists of the phase oracle, \(\mathcal{S}_f\), zero phase-shift or zero reflection, \(\mathcal{S}_0\), and an input state preparation \(\mathcal{A}\):
\[\mathcal{Q} = \mathcal{A} \mathcal{S}_0 \mathcal{A}^\dagger \mathcal{S}_f\]In the standard Grover search we have \(\mathcal{A} = H^{\otimes n}\):
\[\mathcal{Q} = H^{\otimes n} \mathcal{S}_0 H^{\otimes n} \mathcal{S}_f = D \mathcal{S_f}\]The operation \(D = H^{\otimes n} \mathcal{S}_0 H^{\otimes n}\) is also referred to as diffusion operator. In this formulation we can see that Groverβs operator consists of two steps: first, the phase oracle multiplies the good states by -1 (with \(\mathcal{S}_f\)) and then the whole state is reflected around the mean (with \(D\)).
This class allows setting a different state preparation, as in quantum amplitude amplification (a generalization of Groverβs algorithm), \(\mathcal{A}\) might not be a layer of Hardamard gates [3].
The action of the phase oracle \(\mathcal{S}_f\) is defined as
\[\mathcal{S}_f: |x\rangle \mapsto (-1)^{f(x)}|x\rangle\]where \(f(x) = 1\) if \(x\) is a good state and 0 otherwise. To highlight the fact that this oracle flips the phase of the good states and does not flip the state of a result qubit, we call \(\mathcal{S}_f\) a phase oracle.
Note that you can easily construct a phase oracle from a bitflip oracle by sandwiching the controlled X gate on the result qubit by a X and H gate. For instance
Bitflip oracle Phaseflip oracle q_0: βββ ββ q_0: βββββββββββββ ββββββββββββ βββ΄ββ βββββββββββββ΄ββββββββββββ out: β€ X β out: β€ X ββ€ H ββ€ X ββ€ H ββ€ X β βββββ βββββββββββββββββββββββββ
There is some flexibility in defining the oracle and \(\mathcal{A}\) operator. Before the Grover operator is applied in Groverβs algorithm, the qubits are first prepared with one application of the \(\mathcal{A}\) operator (or Hadamard gates in the standard formulation). Thus, we always have operation of the form \(\mathcal{A} \mathcal{S}_f \mathcal{A}^\dagger\). Therefore it is possible to move bitflip logic into \(\mathcal{A}\) and leaving the oracle only to do phaseflips via Z gates based on the bitflips. One possible use-case for this are oracles that do not uncompute the state qubits.
The zero reflection \(\mathcal{S}_0\) is usually defined as
\[\mathcal{S}_0 = 2 |0\rangle^{\otimes n} \langle 0|^{\otimes n} - \mathbb{I}_n\]where \(\mathbb{I}_n\) is the identity on \(n\) qubits. By default, this class implements the negative version \(2 |0\rangle^{\otimes n} \langle 0|^{\otimes n} - \mathbb{I}_n\), since this can simply be implemented with a multi-controlled Z sandwiched by X gates on the target qubit and the introduced global phase does not matter for Groverβs algorithm.
Examples
>>> from qiskit.circuit import QuantumCircuit >>> from qiskit.circuit.library import GroverOperator >>> oracle = QuantumCircuit(2) >>> oracle.z(0) # good state = first qubit is |1> >>> grover_op = GroverOperator(oracle, insert_barriers=True) >>> grover_op.decompose().draw() βββββ β βββββ β βββββ βββββ β βββββ state_0: β€ Z βββββ€ H βββββ€ X βββββββββ βββ€ X ββββββββββ€ H β βββββ β βββββ€ β βββββ€ββββββββ΄βββββββ€βββββ β βββββ€ state_1: βββββββββ€ H βββββ€ X ββ€ H ββ€ X ββ€ H ββ€ X βββββ€ H β β βββββ β βββββββββββββββββββββββββ β βββββ
>>> oracle = QuantumCircuit(1) >>> oracle.z(0) # the qubit state |1> is the good state >>> state_preparation = QuantumCircuit(1) >>> state_preparation.ry(0.2, 0) # non-uniform state preparation >>> grover_op = GroverOperator(oracle, state_preparation) >>> grover_op.decompose().draw() βββββββββββββββββββββββββββββββββββββββββββ state_0: β€ Z ββ€ RY(-0.2) ββ€ X ββ€ Z ββ€ X ββ€ RY(0.2) β βββββββββββββββββββββββββββββββββββββββββββ
>>> oracle = QuantumCircuit(4) >>> oracle.z(3) >>> reflection_qubits = [0, 3] >>> state_preparation = QuantumCircuit(4) >>> state_preparation.cry(0.1, 0, 3) >>> state_preparation.ry(0.5, 3) >>> grover_op = GroverOperator(oracle, state_preparation, ... reflection_qubits=reflection_qubits) >>> grover_op.decompose().draw() βββββ βββββ state_0: βββββββββββββββββββββββ βββββββ€ X βββββββββ βββ€ X ββββββββββββ ββββββββββββββββ β βββββ β βββββ β state_1: βββββββββββββββββββββββΌβββββββββββββββββββΌββββββββββββββββββΌββββββββββββββββ β β β state_2: βββββββββββββββββββββββΌβββββββββββββββββββΌββββββββββββββββββΌββββββββββββββββ βββββββββββββββββββββββ΄βββββββββββββββββββ΄ββββββββββββββββββ΄ββββββββββββββββ state_3: β€ Z ββ€ RY(-0.5) ββ€ RY(-0.1) ββ€ X ββ€ H ββ€ X ββ€ H ββ€ X ββ€ RY(0.1) ββ€ RY(0.5) β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
>>> mark_state = Statevector.from_label('011') >>> diffuse_operator = 2 * DensityMatrix.from_label('000') - Operator.from_label('III') >>> grover_op = GroverOperator(oracle=mark_state, zero_reflection=diffuse_operator) >>> grover_op.decompose().draw(fold=70) βββββββββββββββββββ βββββ Β» state_0: β€0 ββββββββ€ H βββββββββββββββββββββββββββΒ» β ββββββββ΄ββββ΄ββββββ βββββ Β» state_1: β€1 UCRZ(0,pi,0,0) ββ€0 βββββββ€ H βββββββββββΒ» β ββ UCRZ(pi/2,0) βββββββ΄ββββ΄ββββββββββΒ» state_2: β€2 ββ€1 ββ€ UCRZ(-pi/4) ββ€ H βΒ» ββββββββββββββββββββββββββββββββββββββββββββββββββββββββΒ» Β« βββββββββββββββββββ βββββ Β«state_0: β€0 ββββββββ€ H ββββββββββββββββββββββββββ Β« β ββββββββ΄ββββ΄ββββββ βββββ Β«state_1: β€1 UCRZ(pi,0,0,0) ββ€0 ββββββ€ H βββββββββββ Β« β ββ UCRZ(pi/2,0) ββββββ΄ββββ΄ββββββββββ Β«state_2: β€2 ββ€1 ββ€ UCRZ(pi/4) ββ€ H β Β« βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
References
- [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:quant-ph/0005055.
- Parameter:
oracle (Union[QuantumCircuit, Statevector]) β The phase oracle implementing a reflection about the bad state. Note that this is not a bitflip oracle, see the docstring for more information.
state_preparation (Optional[QuantumCircuit]) β The operator preparing the good and bad state. For Groverβs algorithm, this is a n-qubit Hadamard gate and for amplitude amplification or estimation the operator \(\mathcal{A}\).
zero_reflection (Optional[Union[QuantumCircuit, DensityMatrix, Operator]]) β The reflection about the zero state, \(\mathcal{S}_0\).
reflection_qubits (Optional[List[int]]) β Qubits on which the zero reflection acts on.
insert_barriers (bool) β Whether barriers should be inserted between the reflections and A.
mcx_mode (str) β The mode to use for building the default zero reflection.
name (str) β The name of the circuit.
Attributes
- ancillas#
Returns a list of ancilla bits in the order that the registers were added.
- calibrations#
Return calibration dictionary.
The custom pulse definition of a given gate is of the form
{'gate_name': {(qubits, params): schedule}}
- clbits#
Returns a list of classical bits in the order that the registers were added.
- data#
Return the circuit data (instructions and context).
- RΓΌckgabe:
a list-like object containing the
CircuitInstruction
s for each instruction.- RΓΌckgabetyp:
QuantumCircuitData
- extension_lib = 'include "qelib1.inc";'#
- global_phase#
Return the global phase of the circuit in radians.
- header = 'OPENQASM 2.0;'#
- instances = 127#
- layout#
Return any associated layout information about the circuit
This attribute contains an optional
TranspileLayout
object. This is typically set on the output fromtranspile()
orPassManager.run()
to retain information about the permutations caused on the input circuit by transpilation.There are two types of permutations caused by the
transpile()
function, an initial layout which permutes the qubits based on the selected physical qubits on theTarget
, and a final layout which is an output permutation caused bySwapGate
s inserted during routing.
- metadata#
The user provided metadata associated with the circuit.
The metadata for the circuit is a user provided
dict
of metadata for the circuit. It will not be used to influence the execution or operation of the circuit, but it is expected to be passed between all transforms of the circuit (ie transpilation) and that providers will associate any circuit metadata with the results it returns from execution of that circuit.
- num_ancillas#
Return the number of ancilla qubits.
- num_clbits#
Return number of classical bits.
- num_parameters#
The number of parameter objects in the circuit.
- num_qubits#
Return number of qubits.
- op_start_times#
Return a list of operation start times.
This attribute is enabled once one of scheduling analysis passes runs on the quantum circuit.
- RΓΌckgabe:
List of integers representing instruction start times. The index corresponds to the index of instruction in
QuantumCircuit.data
.- Verursacht:
AttributeError β When circuit is not scheduled.
- oracle#
The oracle implementing a reflection about the bad state.
- parameters#
The parameters defined in the circuit.
This attribute returns the
Parameter
objects in the circuit sorted alphabetically. Note that parameters instantiated with aParameterVector
are still sorted numerically.Examples
The snippet below shows that insertion order of parameters does not matter.
>>> from qiskit.circuit import QuantumCircuit, Parameter >>> a, b, elephant = Parameter("a"), Parameter("b"), Parameter("elephant") >>> circuit = QuantumCircuit(1) >>> circuit.rx(b, 0) >>> circuit.rz(elephant, 0) >>> circuit.ry(a, 0) >>> circuit.parameters # sorted alphabetically! ParameterView([Parameter(a), Parameter(b), Parameter(elephant)])
Bear in mind that alphabetical sorting might be unintuitive when it comes to numbers. The literal β10β comes before β2β in strict alphabetical sorting.
>>> from qiskit.circuit import QuantumCircuit, Parameter >>> angles = [Parameter("angle_1"), Parameter("angle_2"), Parameter("angle_10")] >>> circuit = QuantumCircuit(1) >>> circuit.u(*angles, 0) >>> circuit.draw() βββββββββββββββββββββββββββββββ q: β€ U(angle_1,angle_2,angle_10) β βββββββββββββββββββββββββββββββ >>> circuit.parameters ParameterView([Parameter(angle_1), Parameter(angle_10), Parameter(angle_2)])
To respect numerical sorting, a
ParameterVector
can be used.>>> from qiskit.circuit import QuantumCircuit, Parameter, ParameterVector >>> x = ParameterVector("x", 12) >>> circuit = QuantumCircuit(1) >>> for x_i in x: ... circuit.rx(x_i, 0) >>> circuit.parameters ParameterView([ ParameterVectorElement(x[0]), ParameterVectorElement(x[1]), ParameterVectorElement(x[2]), ParameterVectorElement(x[3]), ..., ParameterVectorElement(x[11]) ])
- RΓΌckgabe:
The sorted
Parameter
objects in the circuit.
- prefix = 'circuit'#
- qubits#
Returns a list of quantum bits in the order that the registers were added.
- reflection_qubits#
Reflection qubits, on which S0 is applied (if S0 is not user-specified).
- state_preparation#
The subcircuit implementing the A operator or Hadamards.
- zero_reflection#
The subcircuit implementing the reflection about 0.