§
    ¹³Ùg6o  ã                  óÊ   — d Z ddlmZ ddlmZmZmZ ddlZddlm	Z	m
Z
mZmZ ddlmZ ddlmZmZmZ ddlmZ d	d
lmZ d	dlmZ 	 	 	 	 	 d&d'd„Z G d„ de	¦  «        Z	 d(d)d%„ZdS )*zThe Grover operator.é    )Úannotations)ÚListÚOptionalÚUnionN)ÚQuantumCircuitÚQuantumRegisterÚAncillaRegisterÚAncillaQubit)ÚQiskitError)ÚStatevectorÚOperatorÚDensityMatrix)Údeprecate_funcé   )ÚMCXGate)ÚDiagonalGateFÚQÚoracleúQuantumCircuit | StatevectorÚstate_preparationúQuantumCircuit | NoneÚzero_reflectionú0QuantumCircuit | DensityMatrix | Operator | NoneÚreflection_qubitsúlist[int] | NoneÚinsert_barriersÚboolÚnameÚstrc                ór  — t          | t          ¦  «        r|                      |d¬¦  «        }nt          | j        |¬¦  «        }t          | t          ¦  «        r3t          d| j        z  ¦  «        }|                     ||j        ¦  «         n| 	                    | d¬¦  «         |r| 
                    ¦   «          |€d„ t          |j        ¦  «        D ¦   «         }|€|                     |¦  «         n)| 	                    |                     ¦   «         d¬¦  «         |r| 
                    ¦   «          |€ºt          |¦  «        }|                     |¦  «         |d	k    r|                     |d
         ¦  «         n^t#          |d	z
  ¦  «        }	|                     |d         ¦  «         |                     |	|¦  «         |                     |d         ¦  «         |                     |¦  «         nut          |t$          t&          f¦  «        rBt          |j                             ¦   «         ¦  «        }|                     ||j        ¦  «         n| 	                    |d¬¦  «         |r| 
                    ¦   «          |€|                     |¦  «         n| 	                    |d¬¦  «         t*          j        |_        |S )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)r   Ú	vars_mode©r   éÿÿÿÿT©ÚinplaceNc                óB   — g | ]\  }}t          |t          ¦  «        °|‘ŒS © )Ú
isinstancer
   )Ú.0ÚiÚqubits      ú\/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/library/grover_operator.pyú
<listcomp>z#grover_operator.<locals>.<listcomp>ê   s<   € ð 
ð 
ð 
Ù�!�U½zÈ%ÕQ]Ñ?^Ô?^ð
Øð
ð 
ð 
ó    r   r   )r)   r   Úcopy_empty_likeÚ
num_qubitsr   r   ÚdataÚappendÚqubitsÚcomposeÚbarrierÚ	enumerateÚhÚinverseÚlenÚxÚzr   r   r   ÚdiagonalÚnumpyÚpiÚglobal_phase)
r   r   r   r   r   r   Úcircuitr=   Únum_reflectionÚmcxs
             r-   Úgrover_operatorrD      sÇ  € õr �&�.Ñ)Ô)ð ?Ø×(Ò(¨d¸fÐ(ÑEÔEˆˆå  Ô!2¸Ð>Ñ>Ô>ˆõ
 �&�+Ñ&Ô&ð .Ý ¨¬Ñ 3Ñ4Ô4ˆØ�Š�x ¤Ñ0Ô0Ð0Ð0à�Š˜¨ˆÑ-Ô-Ð-àð Ø�ŠÑÔÐð Ð ð
ð 
Ý'¨¬Ñ7Ô7ð
ñ 
ô 
Ðð Ð Ø�	Š	Ð#Ñ$Ô$Ð$Ð$à�ŠÐ)×1Ò1Ñ3Ô3¸TˆÑBÔBÐBàð Ø�ŠÑÔÐð ÐÝÐ.Ñ/Ô/ˆà�	Š	Ð#Ñ$Ô$Ð$Ø˜QÒÐØ�IŠIØ! !Ô$ñô ð ð õ ˜.¨1Ñ,Ñ-Ô-ˆCà�IŠIÐ'¨Ô+Ñ,Ô,Ð,Ø�NŠN˜3Ð 1Ñ2Ô2Ð2Ø�IŠIÐ'¨Ô+Ñ,Ô,Ð,Ø�	Š	Ð#Ñ$Ô$Ð$Ð$å	�O¥hµÐ%>Ñ	?Ô	?ð 7Ý Ô 4× =Ò =Ñ ?Ô ?Ñ@Ô@ˆØ�Š�x ¤Ñ0Ô0Ð0Ð0ð 	�Š˜°ˆÑ6Ô6Ð6àð Ø�ŠÑÔÐð Ð Ø�	Š	Ð#Ñ$Ô$Ð$Ð$à�ŠÐ)°4ˆÑ8Ô8Ð8õ !œ8€GÔà€Nr/   c                  ó¼   ‡ — e Zd ZdZ eddd¬¦  «        	 	 	 	 	 	 d d!ˆ fd„¦   «         Zed„ ¦   «         Zed"d„¦   «         Zed"d„¦   «         Z	ed„ ¦   «         Z
d„ Zˆ xZS )#ÚGroverOperatoru6(  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1.3z3Use qiskit.circuit.library.grover_operator instead.T)ÚsinceÚadditional_msgÚpendingNFÚ	noancillar   r   ú"Union[QuantumCircuit, Statevector]r   úOptional[QuantumCircuit]r   ú8Optional[Union[QuantumCircuit, DensityMatrix, Operator]]r   úOptional[List[int]]r   r   Úmcx_moder   r   ÚreturnÚNonec                ó¬  •— t          ¦   «                              |¬¦  «         t          |t          ¦  «        rddlm}  |d|j        z  ¦  «        }|| _        t          |t          t          f¦  «        r(ddlm}  ||j         
                    ¦   «         ¦  «        }|| _        || _        || _        || _        || _        |                      ¦   «          dS )aA  
        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.libraryrS   r2   Ú_oracler   r   r=   Ú_zero_reflectionÚ_reflection_qubitsÚ_state_preparationÚ_insert_barriersÚ	_mcx_modeÚ_build)
Úselfr   r   r   r   r   rO   r   rS   Ú	__class__s
            €r-   rU   zGroverOperator.__init__®  så   ø€ õ8 	‰Œ×Ò˜dÐÑ#Ô#Ð#õ �f�kÑ*Ô*ð 	3Ø7Ð7Ð7Ð7Ð7Ð7à�X˜r f¤kÑ1Ñ2Ô2ˆFØˆŒå�o­µ-Ð'@ÑAÔAð 	HØ7Ð7Ð7Ð7Ð7Ð7à&˜h Ô';×'DÒ'DÑ'FÔ'FÑGÔGˆOØ /ˆÔà"3ˆÔØ"3ˆÔØ /ˆÔØ!ˆŒð 	�Š‰Œˆˆˆr/   c                óˆ   — | j         �| j         S | j        j        | j        j        z
  }t	          t          |¦  «        ¦  «        S )zHReflection qubits, on which S0 is applied (if S0 is not user-specified).)rY   r   r1   Únum_ancillasÚlistÚrange©r^   Únum_state_qubitss     r-   r   z GroverOperator.reflection_qubitsá  sB   € ð Ô"Ð.ØÔ*Ð*àœ;Ô1°D´KÔ4LÑLÐÝ•EÐ*Ñ+Ô+Ñ,Ô,Ð,r/   r   c                ó†   — | j         �| j         S | j        j        | j        j        z
  }t          || j        | j        ¦  «        S )z3The subcircuit implementing the reflection about 0.)rX   r   r1   ra   r   r\   rd   s     r-   r   zGroverOperator.zero_reflectionê  sC   € ð Ô Ð,ØÔ(Ð(àœ;Ô1°D´KÔ4LÑLÐÝÐ 0°$Ô2HÈ$Ì.ÑYÔYÐYr/   c                óª   — | j         �| j         S | j        j        | j        j        z
  }t	          |d¬¦  «        }|                     | j        ¦  «         |S )z8The subcircuit implementing the A operator or Hadamards.NÚHr#   )rZ   r   r1   ra   r   r8   r   )r^   re   Ú	hadamardss      r-   r   z GroverOperator.state_preparationó  sY   € ð Ô"Ð.ØÔ*Ð*àœ;Ô1°D´KÔ4LÑLÐÝ"Ð#3¸#Ð>Ñ>Ô>ˆ	à�Š�DÔ*Ñ+Ô+Ð+ØÐr/   c                ó   — | j         S )z9The oracle implementing a reflection about the bad state.)rW   )r^   s    r-   r   zGroverOperator.oracleÿ  s   € ð Œ|Ðr/   c                óò  — | j         j        | j         j        z
  }t          t	          |d¬¦  «        d¬¦  «        }t          j        | j         j        | j        j        | j        j        g¦  «        }|dk    r$| 	                    t          |d¬¦  «        ¦  «         |                     | j         t          t          | j         j        ¦  «        ¦  «        d¬¦  «         | j        r|                     ¦   «          |                     | j                             ¦   «         t          t          | j        j        ¦  «        ¦  «        d¬¦  «         | j        r|                     ¦   «          |                     | j        t          t          | j        j        ¦  «        ¦  «        d¬¦  «         | j        r|                     ¦   «          |                     | j        t          t          | j        j        ¦  «        ¦  «        d¬¦  «         t
          j        |_         | j	        |j        Ž  	 |                     ¦   «         }n$# t*          $ r |                     ¦   «         }Y nw xY w|                      || j        d¬¦  «         d S )	NÚstater#   r   r   ÚancillaTr%   )r4   r&   )r   r1   ra   r   r   r>   Úmaxr   r   Úadd_registerr	   r5   rb   rc   r[   r6   r9   r?   r@   ÚqregsÚto_gater   Úto_instructionr4   )r^   re   rA   ra   Úcircuit_wrappeds        r-   r]   zGroverOperator._build  sb  € Øœ;Ô1°D´KÔ4LÑLÐÝ ¥Ð1AÈÐ!PÑ!PÔ!PÐWZÐ[Ñ[Ô[ˆÝ”yà”Ô(ØÔ$Ô1ØÔ&Ô3ðñ
ô 
ˆð ˜!ÒÐØ× Ò ¥°ÀIÐ!NÑ!NÔ!NÑOÔOÐOà�Š˜œ¥T­%°´Ô0FÑ*GÔ*GÑ%HÔ%HÐRVˆÑWÔWÐWØÔ ð 	Ø�OŠOÑÔÐØ�ŠØÔ"×*Ò*Ñ,Ô,Ý•�tÔ-Ô8Ñ9Ô9Ñ:Ô:Øð 	ñ 	
ô 	
ð 	
ð
 Ô ð 	Ø�OŠOÑÔÐØ�ŠØÔ ¥$¥u¨TÔ-AÔ-LÑ'MÔ'MÑ"NÔ"NÐX\ð 	ñ 	
ô 	
ð 	
ð Ô ð 	Ø�OŠOÑÔÐØ�ŠØÔ"¥D­¨tÔ/EÔ/PÑ)QÔ)QÑ$RÔ$RÐ\`ð 	ñ 	
ô 	
ð 	
õ
  %œxˆÔàˆÔ˜7œ=Ð)Ð)ð	7Ø%ŸošoÑ/Ô/ˆOˆOøÝð 	7ð 	7ð 	7Ø%×4Ò4Ñ6Ô6ˆOˆOˆOð	7øøøð 	�Š�_¨T¬[À$ˆÑGÔGÐGÐGÐGs   È!H6 È6IÉI)NNNFrJ   r   )r   rK   r   rL   r   rM   r   rN   r   r   rO   r   r   r   rP   rQ   )rP   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rU   Úpropertyr   r   r   r   r]   Ú__classcell__)r_   s   @r-   rF   rF     s  ø€ € € € € ðNð Nð` €^ØØLØðñ ô ð 7;ØTXØ15Ø %Ø#Øð,ð ,ð ,ð ,ð ,ð ,ñô ð
,ð\ ð-ð -ñ „Xð-ð ðZð Zð Zñ „XðZð ð	ð 	ð 	ñ „Xð	ð ðð ñ „Xðð)Hð )Hð )Hð )Hð )Hð )Hð )Hr/   rF   re   Úintr4   ú	List[int]rO   úOptional[str]rP   r   c                óx  — t          | d¦  «        }t          |d¬¦  «        }t          j        t	          |¦  «        dz
  |¦  «        }|dk    r&t          |d¦  «        }|                     |¦  «         nt          d¦  «        }|                     |¦  «         t	          |¦  «        dk    r|                     d¦  «         ne| 	                    |d         ¦  «         | 
                    |d d…         |d         |d d …         |¬¦  «         | 	                    |d         ¦  «         |                     |¦  «         |S )	Nrl   ÚS_0r#   r   r   rm   r$   )Úmode)r   r   r   Úget_num_ancilla_qubitsr:   r	   ro   r;   r<   r8   rC   )re   r4   rO   Úqr_stateÚ
reflectionra   Ú
qr_ancillas          r-   rX   rX   1  s&  € õ Ð/°Ñ9Ô9€HÝ ¨uÐ5Ñ5Ô5€JåÔ1µ#°f±+´+À±/À8ÑLÔL€LØ�aÒÐÝ$ \°9Ñ=Ô=ˆ
Ø×Ò 
Ñ+Ô+Ð+Ð+å$ QÑ'Ô'ˆ
à‡L‚L�ÑÔÐÝ
ˆ6�{„{�aÒÐØ�Š�Q‰Œˆˆà�Š�V˜B”ZÑ Ô Ð Ø�Š�v˜c˜r˜c”{ F¨2¤J°
¸1¸1¸1´ÀHˆÑMÔMÐMØ�Š�V˜B”ZÑ Ô Ð Ø‡L‚L�ÑÔÐàÐr/   )NNNFr   )r   r   r   r   r   r   r   r   r   r   r   r   )N)re   rz   r4   r{   rO   r|   rP   r   )rw   Ú
__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   rD   rF   rX   r(   r/   r-   ú<module>rŒ      se  ðð Ð à "Ð "Ð "Ð "Ð "Ð "Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ø €€€à YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YÐ YØ )Ð )Ð )Ð )Ð )Ð )Ø DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DØ 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø #Ð #Ð #Ð #Ð #Ð #Ø +Ð +Ð +Ð +Ð +Ð +ð
 04ØHLØ*.Ø!Øðð ð ð ð ðDPHð PHð PHð PHð PH�^ñ PHô PHð PHðj IMðð ð ð ð ð ð r/   