§
    ¹³Ùg¾  ã                  óf   — d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	  G d„ de¦  «        Z
	 ddd„ZdS )zPhase estimation circuit.é    )Úannotations)ÚQuantumCircuitÚQuantumRegister)Údeprecate_func)ÚQFTc                  óR   ‡ — e Zd ZdZ eddd¬¦  «        	 	 ddˆ fd„¦   «         Zˆ xZS )ÚPhaseEstimationuG  Phase Estimation circuit.

    In the Quantum Phase Estimation (QPE) algorithm [1, 2, 3], the Phase Estimation circuit is used
    to estimate the phase :math:`\phi` of an eigenvalue :math:`e^{2\pi i\phi}` of a unitary operator
    :math:`U`, provided with the corresponding eigenstate :math:`|\psi\rangle`.
    That is

    .. math::

        U|\psi\rangle = e^{2\pi i\phi} |\psi\rangle

    This estimation (and thereby this circuit) is a central routine to several well-known
    algorithms, such as Shor's algorithm or Quantum Amplitude Estimation.

    **References:**

    [1]: Kitaev, A. Y. (1995). Quantum measurements and the Abelian Stabilizer Problem. 1â€“22.
        `quant-ph/9511026 <http://arxiv.org/abs/quant-ph/9511026>`_

    [2]: Michael A. Nielsen and Isaac L. Chuang. 2011.
         Quantum Computation and Quantum Information: 10th Anniversary Edition (10th ed.).
         Cambridge University Press, New York, NY, USA.

    [3]: Qiskit
        `textbook <https://github.com/Qiskit/textbook/blob/main/notebooks/ch-algorithms/
        quantum-phase-estimation.ipynb>`_

    z1.3z4Use qiskit.circuit.library.phase_estimation instead.T)ÚsinceÚadditional_msgÚpendingNÚQPEÚnum_evaluation_qubitsÚintÚunitaryr   ÚiqftúQuantumCircuit | NoneÚnameÚstrÚreturnÚNonec                óŒ  •— t          |d¦  «        }t          |j        d¦  «        }t          |||¬¦  «        }|€$t          |dd¬¦  «                             ¦   «         }|                     |¦  «         t          |¦  «        D ]N}|                     |                     d|z  ¦  «         	                    ¦   «         |g|dd…         z   d¬	¦  «         ŒO|                     ||dd…         d¬	¦  «          t          ¦   «         j        |j        d
|j        iŽ |                      |                     ¦   «         | j        d¬	¦  «         dS )aH  
        Args:
            num_evaluation_qubits: The number of evaluation qubits.
            unitary: The unitary operation :math:`U` which will be repeated and controlled.
            iqft: A inverse Quantum Fourier Transform, per default the inverse of
                :class:`~qiskit.circuit.library.QFT` is used. Note that the QFT should not include
                the usual swaps!
            name: The name of the circuit.

        .. note::

            The inverse QFT should not include a swap of the qubit order.

        Reference Circuit:
            .. plot::
               :alt: Diagram illustrating the previously described circuit.

               from qiskit.circuit import QuantumCircuit
               from qiskit.circuit.library import PhaseEstimation
               from qiskit.visualization.library import _generate_circuit_library_visualization
               unitary = QuantumCircuit(2)
               unitary.x(0)
               unitary.y(1)
               circuit = PhaseEstimation(3, unitary)
               _generate_circuit_library_visualization(circuit)
        ÚevalÚq©r   NTF)ÚinverseÚdo_swapsé   ©ÚqubitsÚinplacer   )r   Ú
num_qubitsr   r   Úreverse_bitsÚhÚrangeÚcomposeÚpowerÚcontrolÚsuperÚ__init__Úqregsr   Úto_gater   )
Úselfr   r   r   r   Úqr_evalÚqr_stateÚcircuitÚjÚ	__class__s
            €ú]/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/library/phase_estimation.pyr)   zPhaseEstimation.__init__4   sB  ø€ õL "Ð"7¸Ñ@Ô@ˆÝ" 7Ô#5°sÑ;Ô;ˆÝ  ¨(¸Ð>Ñ>Ô>ˆàˆ<ÝÐ,°dÀUÐKÑKÔK×XÒXÑZÔZˆDà�	Š	�'ÑÔÐåÐ,Ñ-Ô-ð 	cð 	cˆAØ�OŠO˜GŸMšM¨!¨Q©$Ñ/Ô/×7Ò7Ñ9Ô9À1À#ÈÐQRÐQRÐQRÌÑBSÐ]aˆOÑbÔbÐbÐbà�Š˜ W¨Q¨Q¨Q¤Z¸ˆÑ>Ô>Ð>à�‰ŒÔ˜'œ-Ð;¨g¬lÐ;Ð;Ð;Ø�Š�W—_’_Ñ&Ô&¨t¬{ÀDˆÑIÔIÐIÐIÐIó    )Nr   )
r   r   r   r   r   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r)   Ú__classcell__)r1   s   @r2   r	   r	      sˆ   ø€ € € € € ðð ð: €^ØØMØðñ ô ð '+Øð0Jð 0Jð 0Jð 0Jð 0Jð 0Jñô ð
0Jð 0Jð 0Jð 0Jð 0Jr3   r	   r   r   r   r   r   r   r   r   c                ó|  — ddl m}m} t          | d¦  «        }t          |j        d¦  «        }t          |||¬¦  «        }|                     |¦  «         t          | ¦  «        D ]N}|                     | 	                    d|z  ¦  «         
                    ¦   «         |g|dd…         z   d¬	¦  «         ŒO|                      || ¦  «                             ¦   «         |dd…         ¦  «         t          t          t          | ¦  «        ¦  «        ¦  «        }	|                      ||	¦  «        |dd…         ¦  «         |S )
u�  Phase Estimation circuit.

    In the Quantum Phase Estimation (QPE) algorithm [1, 2, 3], the Phase Estimation circuit is used
    to estimate the phase :math:`\phi` of an eigenvalue :math:`e^{2\pi i\phi}` of a unitary operator
    :math:`U`, provided with the corresponding eigenstate :math:`|\psi\rangle`.
    That is

    .. math::

        U|\psi\rangle = e^{2\pi i\phi} |\psi\rangle

    This estimation (and thereby this circuit) is a central routine to several well-known
    algorithms, such as Shor's algorithm or Quantum Amplitude Estimation.

    Args:
        num_evaluation_qubits: The number of evaluation qubits.
        unitary: The unitary operation :math:`U` which will be repeated and controlled.
        name: The name of the circuit.

    **Reference Circuit:**

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

       from qiskit.circuit import QuantumCircuit
       from qiskit.circuit.library import phase_estimation
       unitary = QuantumCircuit(2)
       unitary.x(0)
       unitary.y(1)
       circuit = phase_estimation(3, unitary)
       circuit.draw('mpl')

    **References:**

    [1]: Kitaev, A. Y. (1995). Quantum measurements and the Abelian Stabilizer Problem. 1â€“22.
        `quant-ph/9511026 <http://arxiv.org/abs/quant-ph/9511026>`_

    [2]: Michael A. Nielsen and Isaac L. Chuang. 2011.
         Quantum Computation and Quantum Information: 10th Anniversary Edition (10th ed.).
         Cambridge University Press, New York, NY, USA.

    [3]: Qiskit
        `textbook <https://github.com/Qiskit/textbook/blob/main/notebooks/ch-algorithms/
        quantum-phase-estimation.ipynb>`_

    r   )ÚPermutationGateÚQFTGater   r   r   r   NTr   )Úqiskit.circuit.libraryr:   r;   r   r!   r   r#   r$   r%   r&   r'   Úappendr   ÚlistÚreversed)
r   r   r   r:   r;   r-   r.   r/   r0   Úreversal_patterns
             r2   Úphase_estimationrA   l   sE  € ðj @Ð?Ð?Ð?Ð?Ð?Ð?Ð?åÐ3°VÑ<Ô<€GÝ˜wÔ1°3Ñ7Ô7€HÝ˜W h°TÐ:Ñ:Ô:€Gà‡I‚IˆgÑÔÐåÐ(Ñ)Ô)ð _ð _ˆØ�Š˜Ÿš a¨¡dÑ+Ô+×3Ò3Ñ5Ô5¸q¸cÀHÈQÈQÈQÄKÑ>OÐY]ˆÑ^Ô^Ð^Ð^à‡N‚N�7�7Ð0Ñ1Ô1×9Ò9Ñ;Ô;¸WÀQÀQÀQ¼ZÑHÔHÐHå�H¥UÐ+@Ñ%AÔ%AÑBÔBÑCÔCÐØ‡N‚N�?�?Ð#3Ñ4Ô4°g¸a¸a¸a´jÑAÔAÐAà€Nr3   N)r   )r   r   r   r   r   r   r   r   )r7   Ú
__future__r   Úqiskit.circuitr   r   Úqiskit.utils.deprecationr   r<   r   r	   rA   © r3   r2   ú<module>rF      sÅ   ðð  Ð à "Ð "Ð "Ð "Ð "Ð "à :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø &Ð &Ð &Ð &Ð &Ð &ðSJð SJð SJð SJð SJ�nñ SJô SJð SJðr ðEð Eð Eð Eð Eð Eð Er3   