ó
    Óz	iæm  ã                  óî   • S r SSKJr  SSKJrJrJr  SSKrSSKJ	r	J
r
JrJr  SSKJr  SSKJrJrJr  SSKJr  S	S
KJr  S	SKJr       S           SS jjr " S S\	5      r S       SS jjrg)zThe Grover operator.é    )Úannotations)ÚListÚOptionalÚUnionN)ÚQuantumCircuitÚQuantumRegisterÚAncillaRegisterÚAncillaQubit)ÚQiskitError)ÚStatevectorÚOperatorÚDensityMatrix)Údeprecate_funcé   )ÚMCXGate)ÚDiagonalGatec                ó  • [        U [        5      (       a  U R                  USS9nO[        U R                  US9n[        U [        5      (       a4  [        SU R                  -  5      nUR                  XvR                  5        OUR                  U SS9  U(       a  UR                  5         Uc@  [        UR                  5       VV	s/ s H  u  p‰[        U	[        5      (       a  M  UPM      nnn	Uc  UR                  U5        OUR                  UR                  5       SS9  U(       a  UR                  5         Uc  [        U5      n
UR!                  U5        U
S:X  a  UR#                  US   5        OG[%        U
S-
  5      nUR                  US   5        UR                  X³5        UR                  US   5        UR!                  U5        Oj[        U[&        [(        45      (       a?  [        UR                  R+                  5       5      nUR                  XvR                  5        OUR                  USS9  U(       a  UR                  5         Uc  UR                  U5        OUR                  USS9  [,        R.                  Ul        U$ s  sn	nf )	u‘  Construct 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, :math:`\mathcal{Q}`, consists of the phase oracle, :math:`\mathcal{S}_f`,
zero phase-shift or zero reflection, :math:`\mathcal{S}_0`, and an
input state preparation :math:`\mathcal{A}`:

.. math::
    \mathcal{Q} = \mathcal{A} \mathcal{S}_0 \mathcal{A}^\dagger \mathcal{S}_f

In the standard Grover search we have :math:`\mathcal{A} = H^{\otimes n}`:

.. math::
    \mathcal{Q} = H^{\otimes n} \mathcal{S}_0 H^{\otimes n} \mathcal{S}_f
                = D \mathcal{S_f}

The operation :math:`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 :math:`\mathcal{S}_f`)
and then the whole state is reflected around the mean (with :math:`D`).

This class allows setting a different state preparation, as in quantum amplitude
amplification (a generalization of Grover's algorithm), :math:`\mathcal{A}` might not be
a layer of Hardamard gates [3].

The action of the phase oracle :math:`\mathcal{S}_f` is defined as

.. math::
    \mathcal{S}_f: |x\rangle \mapsto (-1)^{f(x)}|x\rangle

where :math:`f(x) = 1` if :math:`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 :math:`\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

.. parsed-literal::

    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 :math:`\mathcal{A}` operator. Before the
Grover operator is applied in Grover's algorithm, the qubits are first prepared with one
application of the :math:`\mathcal{A}` operator (or Hadamard gates in the standard formulation).
Thus, we always have operation of the form
:math:`\mathcal{A} \mathcal{S}_f \mathcal{A}^\dagger`. Therefore it is possible to move
bitflip logic into :math:`\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 :math:`\mathcal{S}_0` is usually defined as

.. math::
    \mathcal{S}_0 = 2 |0\rangle^{\otimes n} \langle 0|^{\otimes n} - \mathbb{I}_n

where :math:`\mathbb{I}_n` is the identity on :math:`n` qubits.
By default, this class implements the negative version
:math:`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:

We can construct a Grover operator just from the phase oracle:

.. plot::
    :alt: Circuit diagram output by the previous code.
    :include-source:
    :context:

    from qiskit.circuit import QuantumCircuit
    from qiskit.circuit.library import grover_operator

    oracle = QuantumCircuit(2)
    oracle.z(0)  # good state = first qubit is |1>
    grover_op = grover_operator(oracle, insert_barriers=True)
    grover_op.draw("mpl")

We can also modify the state preparation:

.. plot::
    :alt: Circuit diagram output by the previous code.
    :include-source:
    :context: close-figs

    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 = grover_operator(oracle, state_preparation)
    grover_op.draw("mpl")

In addition, we can also mark which qubits the zero reflection should act on. This
is useful in case that some qubits are just used as scratch space but should not affect
the oracle:

.. plot::
    :alt: Circuit diagram output by the previous code.
    :include-source:
    :context: close-figs

    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 = grover_operator(oracle, state_preparation, reflection_qubits=reflection_qubits)
    grover_op.draw("mpl")


The oracle and the zero reflection can also be passed as :mod:`qiskit.quantum_info`
objects:

.. plot::
    :alt: Circuit diagram output by the previous code.
    :include-source:
    :context: close-figs

    from qiskit.quantum_info import Statevector, DensityMatrix, Operator

    mark_state = Statevector.from_label("011")
    reflection = 2 * DensityMatrix.from_label("000") - Operator.from_label("III")
    grover_op = grover_operator(oracle=mark_state, zero_reflection=reflection)
    grover_op.draw("mpl")

For a large number of qubits, the multi-controlled X gate used for the zero-reflection
can be synthesized in different fashions. Depending on the number of available qubits,
the compiler will choose a different implementation:

.. code-block:: python

    from qiskit import transpile, Qubit
    from qiskit.circuit import QuantumCircuit
    from qiskit.circuit.library import grover_operator

    oracle = QuantumCircuit(10)
    oracle.z(oracle.qubits)
    grover_op = grover_operator(oracle)

    # without extra qubit space, the MCX synthesis is expensive
    basis_gates = ["u", "cx"]
    tqc = transpile(grover_op, basis_gates=basis_gates)
    is_2q = lambda inst: len(inst.qubits) == 2
    print("2q depth w/o scratch qubits:", tqc.depth(filter_function=is_2q))  # > 350

    # add extra bits that can be used as scratch space
    grover_op.add_bits([Qubit() for _ in range(num_qubits)])
    tqc = transpile(grover_op, basis_gates=basis_gates)
    print("2q depth w/ scratch qubits:", tqc.depth(filter_function=is_2q)) # < 100

Args:
    oracle: 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: 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 :math:`\mathcal{A}`.
    zero_reflection: The reflection about the zero state, :math:`\mathcal{S}_0`.
    reflection_qubits: Qubits on which the zero reflection acts on.
    insert_barriers: Whether barriers should be inserted between the reflections and A.
    name: The name of the circuit.

References:

[1] L. K. Grover (1996), A fast quantum mechanical algorithm for database search,
`arXiv:quant-ph/9605043 <https://arxiv.org/abs/quant-ph/9605043>`_.

[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 <http://arxiv.org/abs/quant-ph/0005055>`_.
Údrop)ÚnameÚ	vars_mode©r   éÿÿÿÿT©Úinplacer   r   )Ú
isinstancer   Úcopy_empty_likeÚ
num_qubitsr   r   ÚdataÚappendÚqubitsÚcomposeÚbarrierÚ	enumerater
   ÚhÚinverseÚlenÚxÚzr   r   r   ÚdiagonalÚnumpyÚpiÚglobal_phase)ÚoracleÚstate_preparationÚzero_reflectionÚreflection_qubitsÚinsert_barriersr   Úcircuitr)   ÚiÚqubitÚnum_reflectionÚmcxs               Ú`/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/library/grover_operator.pyÚgrover_operatorr8      s!  € ôx &œ.×)Ñ)Ø×(Ñ(¨d¸fÐ(ÐE‰ä  ×!2Ñ!2¸Ñ>ˆô
 &œ+×&Ñ&Ü ¨¯©Ñ 3Ó4ˆØ‰x§¡Õ0à‰˜¨ˆÑ-æØ‰Ôð Ñ ä'¨¯©Ô7ô
Ú7‘(!¼zÈ%ÔQ]×?^AÑ7ð 	ñ 
ð Ñ Ø	‰	Ð#Õ$à‰Ð)×1Ñ1Ó3¸TˆÑBæØ‰Ôð ÑÜÐ.Ó/ˆà	‰	Ð#Ô$Ø˜QÓØI‰IØ! !Ñ$õô ˜.¨1Ñ,Ó-ˆCàI‰IÐ'¨Ñ+Ô,ØN‰N˜3Ô2ØI‰IÐ'¨Ñ+Ô,Ø	‰	Ð#Õ$ä	O¤h´Ð%>×	?Ñ	?Ü × 4Ñ 4× =Ñ =Ó ?Ó@ˆØ‰x§¡Õ0ð 	‰˜°ˆÑ6æØ‰Ôð Ñ Ø	‰	Ð#Õ$à‰Ð)°4ˆÑ8ô !Ÿ8™8€GÔà€Nùóa
s   ÃJÃ%Jc                  óÂ   ^ • \ rS rSrSr\" SSSS9      S               SU 4S jjj5       r\S 5       r\SS	 j5       r	\SS
 j5       r
\S 5       rS rSrU =r$ )ÚGroverOperatori   u.&  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, :math:`\mathcal{Q}`, consists of the phase oracle, :math:`\mathcal{S}_f`,
zero phase-shift or zero reflection, :math:`\mathcal{S}_0`, and an
input state preparation :math:`\mathcal{A}`:

.. math::
    \mathcal{Q} = \mathcal{A} \mathcal{S}_0 \mathcal{A}^\dagger \mathcal{S}_f

In the standard Grover search we have :math:`\mathcal{A} = H^{\otimes n}`:

.. math::
    \mathcal{Q} = H^{\otimes n} \mathcal{S}_0 H^{\otimes n} \mathcal{S}_f
                = D \mathcal{S_f}

The operation :math:`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 :math:`\mathcal{S}_f`)
and then the whole state is reflected around the mean (with :math:`D`).

This class allows setting a different state preparation, as in quantum amplitude
amplification (a generalization of Grover's algorithm), :math:`\mathcal{A}` might not be
a layer of Hardamard gates [3].

The action of the phase oracle :math:`\mathcal{S}_f` is defined as

.. math::
    \mathcal{S}_f: |x\rangle \mapsto (-1)^{f(x)}|x\rangle

where :math:`f(x) = 1` if :math:`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 :math:`\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

.. code-block:: text

    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 :math:`\mathcal{A}` operator. Before the
Grover operator is applied in Grover's algorithm, the qubits are first prepared with one
application of the :math:`\mathcal{A}` operator (or Hadamard gates in the standard formulation).
Thus, we always have operation of the form
:math:`\mathcal{A} \mathcal{S}_f \mathcal{A}^\dagger`. Therefore it is possible to move
bitflip logic into :math:`\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 :math:`\mathcal{S}_0` is usually defined as

.. math::
    \mathcal{S}_0 = 2 |0\rangle^{\otimes n} \langle 0|^{\otimes n} - \mathbb{I}_n

where :math:`\mathbb{I}_n` is the identity on :math:`n` qubits.
By default, this class implements the negative version
:math:`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 â”œ
    Â«         â””â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”˜â””â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”˜â””â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”˜â””â”€â”€â”€â”˜

.. seealso::

    The :func:`.grover_operator` implements the same functionality but keeping the
    :class:`.MCXGate` abstract, such that the compiler may choose the optimal decomposition.
    We recommend using :func:`.grover_operator` for performance reasons, which does not
    wrap the circuit into an opaque gate.

References:

[1] L. K. Grover (1996), A fast quantum mechanical algorithm for database search,
`arXiv:quant-ph/9605043 <https://arxiv.org/abs/quant-ph/9605043>`_.

[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 <http://arxiv.org/abs/quant-ph/0005055>`_.
z2.1z3Use qiskit.circuit.library.grover_operator instead.zin Qiskit 3.0)ÚsinceÚadditional_msgÚremoval_timelinec                ój  >• [         T	U ]  US9  [        U[        5      (       a  SSKJn  U" SUR                  -  5      nXl        [        U[        [        45      (       a&  SSKJn  U" UR                  R                  5       5      nX0l        X@l        X l        XPl        X`l        U R!                  5         g)aá  
Args:
    oracle: 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: 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 :math:`\mathcal{A}`.
    zero_reflection: The reflection about the zero state, :math:`\mathcal{S}_0`.
    reflection_qubits: Qubits on which the zero reflection acts on.
    insert_barriers: Whether barriers should be inserted between the reflections and A.
    mcx_mode: The mode to use for building the default zero reflection.
    name: The name of the circuit.
r   r   )ÚDiagonalr   N)ÚsuperÚ__init__r   r   Úqiskit.circuit.libraryr?   r   Ú_oracler   r   r)   Ú_zero_reflectionÚ_reflection_qubitsÚ_state_preparationÚ_insert_barriersÚ	_mcx_modeÚ_build)
Úselfr-   r.   r/   r0   r1   Úmcx_moder   r?   Ú	__class__s
            €r7   rA   ÚGroverOperator.__init__´  s•   ø€ ô8 	‰Ñ˜dÐÑ#ô fœk×*Ñ*Ý7á˜r f§k¡kÑ1Ó2ˆFØŒäo¬´-Ð'@×AÑAÝ7á& ×';Ñ';×'DÑ'DÓ'FÓGˆOØ /Ôà"3ÔØ"3ÔØ /ÔØ!Œð 	‰ó    c                ó¶   • U R                   b  U R                   $ U R                  R                  U R                  R                  -
  n[	        [        U5      5      $ )zHReflection qubits, on which S0 is applied (if S0 is not user-specified).)rE   r-   r   Únum_ancillasÚlistÚrange©rJ   Únum_state_qubitss     r7   r0   Ú GroverOperator.reflection_qubitsç  sL   € ð ×"Ñ"Ñ.Ø×*Ñ*Ð*àŸ;™;×1Ñ1°D·K±K×4LÑ4LÑLÐÜ”EÐ*Ó+Ó,Ð,rN   c                óÎ   • U R                   b  U R                   $ U R                  R                  U R                  R                  -
  n[        XR                  U R
                  5      $ )z3The subcircuit implementing the reflection about 0.)rD   r-   r   rP   r0   rH   rS   s     r7   r/   ÚGroverOperator.zero_reflectionð  sS   € ð × Ñ Ñ,Ø×(Ñ(Ð(àŸ;™;×1Ñ1°D·K±K×4LÑ4LÑLÐÜÐ 0×2HÑ2HÈ$Ï.É.ÓYÐYrN   c                óÜ   • U R                   b  U R                   $ U R                  R                  U R                  R                  -
  n[	        USS9nUR                  U R                  5        U$ )z8The subcircuit implementing the A operator or Hadamards.ÚHr   )rF   r-   r   rP   r   r$   r0   )rJ   rT   Ú	hadamardss      r7   r.   Ú GroverOperator.state_preparationù  s`   € ð ×"Ñ"Ñ.Ø×*Ñ*Ð*àŸ;™;×1Ñ1°D·K±K×4LÑ4LÑLÐÜ"Ð#3¸#Ñ>ˆ	à‰D×*Ñ*Ô+ØÐrN   c                ó   • U R                   $ )z9The oracle implementing a reflection about the bad state.)rC   )rJ   s    r7   r-   ÚGroverOperator.oracle  s   € ð |‰|ÐrN   c                óF  • U R                   R                  U R                   R                  -
  n[        [	        USS9SS9n[
        R                  " U R                   R                  U R                  R                  U R                  R                  /5      nUS:”  a  UR                  [        USS95        UR                  U R                   [        [        U R                   R                  5      5      SS9  U R                  (       a  UR                  5         UR                  U R                  R!                  5       [        [        U R                  R                  5      5      SS9  U R                  (       a  UR                  5         UR                  U R                  [        [        U R                  R                  5      5      SS9  U R                  (       a  UR                  5         UR                  U R                  [        [        U R                  R                  5      5      SS9  [
        R"                  Ul        U R                  " UR&                  6    UR)                  5       nU R                  X@R.                  SS9  g ! [*         a    UR-                  5       n N7f = f)	NÚstater   ÚQr   ÚancillaTr   )r    r   )r-   r   rP   r   r   r*   Úmaxr/   r.   Úadd_registerr	   r!   rQ   rR   rG   r"   r%   r+   r,   ÚqregsÚto_gater   Úto_instructionr    )rJ   rT   r2   rP   Úcircuit_wrappeds        r7   rI   ÚGroverOperator._build
  s
  € ØŸ;™;×1Ñ1°D·K±K×4LÑ4LÑLÐÜ ¤Ð1AÈÑ!PÐWZÑ[ˆÜ—y’yà—‘×(Ñ(Ø×$Ñ$×1Ñ1Ø×&Ñ&×3Ñ3ðó
ˆð ˜!ÓØ× Ñ ¤°ÀIÑ!NÔOà‰˜Ÿ™¤T¬%°·±×0FÑ0FÓ*GÓ%HÐRVˆÑWØ× × ØO‰OÔØ‰Ø×"Ñ"×*Ñ*Ó,Ü”t×-Ñ-×8Ñ8Ó9Ó:Øð 	ñ 	
ð
 × × ØO‰OÔØ‰Ø× Ñ ¤$¤u¨T×-AÑ-A×-LÑ-LÓ'MÓ"NÐX\ð 	ñ 	
ð × × ØO‰OÔØ‰Ø×"Ñ"¤D¬¨t×/EÑ/E×/PÑ/PÓ)QÓ$RÐ\`ð 	ñ 	
ô
  %Ÿx™xˆÔà×Ò˜7Ÿ=™=Ñ)ð	7Ø%Ÿo™oÓ/ˆOð 	‰_¯[©[À$ˆÒGøô ó 	7Ø%×4Ñ4Ó6ŠOð	7ús   ÉJ ÊJ ÊJ )rG   rH   rC   rE   rF   rD   )NNNFÚ	noancillar`   )r-   z"Union[QuantumCircuit, Statevector]r.   zOptional[QuantumCircuit]r/   z8Optional[Union[QuantumCircuit, DensityMatrix, Operator]]r0   zOptional[List[int]]r1   ÚboolrK   Ústrr   rk   ÚreturnÚNone)rl   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   rA   Úpropertyr0   r/   r.   r-   rI   Ú__static_attributes__Ú__classcell__)rL   s   @r7   r:   r:      sî   ø† ñQñf ØØLØ(ñð 7;ØTXØ15Ø %Ø#Øð,à2ð,ð 4ð,ð Rð	,ð
 /ð,ð ð,ð ð,ð ð,ð 
÷,óð
,ð\ ñ-ó ð-ð óZó ðZð ó	ó ð	ð ñó ð÷)Hð )HrN   r:   c                óâ  • [        U S5      n[        USS9n[        R                  " [	        U5      S-
  U5      nUS:”  a  [        US5      nUR                  U5        O[        S5      nUR                  U5        [	        U5      S:X  a  UR                  S5        OCUR                  US   5        UR                  US S US   US S  US9  UR                  US   5        UR                  U5        U$ )	Nr_   ÚS_0r   r   r   ra   r   )Úmode)r   r   r   Úget_num_ancilla_qubitsr&   r	   rc   r'   r(   r$   r6   )rT   r    rK   Úqr_stateÚ
reflectionrP   Ú
qr_ancillas          r7   rD   rD   7  sÚ   € ô Ð/°Ó9€HÜ ¨uÑ5€Jä×1Ò1´#°f³+À±/À8ÓL€LØaÓÜ$ \°9Ó=ˆ
Ø×Ñ 
Õ+ä$ QÓ'ˆ
à‡LLÔÜ
ˆ6ƒ{aÓØ‰Qà‰V˜B‘ZÔ Ø‰v˜c˜r{ F¨2¡J°
¹1°ÀHˆÑMØ‰V˜B‘ZÔ Ø‡LLÔàÐrN   )NNNFr`   )r-   zQuantumCircuit | Statevectorr.   zQuantumCircuit | Noner/   z0QuantumCircuit | DensityMatrix | Operator | Noner0   zlist[int] | Noner1   rj   r   rk   )N)rT   Úintr    z	List[int]rK   zOptional[str]rl   r   )rr   Ú
__future__r   Útypingr   r   r   r*   Úqiskit.circuitr   r   r	   r
   Úqiskit.exceptionsr   Úqiskit.quantum_infor   r   r   Úqiskit.utils.deprecationr   Ústandard_gatesr   Úgeneralized_gatesr   r8   r:   rD   © rN   r7   Ú<module>r‡      sÂ   ðñ å "ß (Ñ (Û ç YÓ YÝ )ß DÑ DÝ 3Ý #Ý +ð
 04ØHLØ*.Ø!ØðBØ(ðBà,ðBð FðBð (ð	Bð
 ðBð õBôJSH^ô SHðp IMðØðØ#,ðØ8EðàörN   