
    z	i):                        S r SSKJr  SSKrSSKJr  SSKJrJr  SSK	r	SSK
Jr  SSKJr  SSKJrJr  SS	KJr   " S
 S\5      r " S S\5      rS rS rg)zTwo-pulse single-qubit gate.    )annotationsN)exp)OptionalUnion)ControlledGate)Gate)ParameterValueTypeParameterExpression)StandardGatec                     ^  \ rS rSrSr\R                  r S
       SU 4S jjjrSSS jjr	    S       SU 4S jjjr
S rSS jrS rS	rU =r$ )U3Gate   uM  Generic single-qubit rotation gate with 3 Euler angles.

.. warning::

   This gate is deprecated. Instead, the following replacements should be used

   .. math::

       U3(\theta, \phi, \lambda) = U(\theta, \phi, \lambda)

   .. code-block:: python

      circuit = QuantumCircuit(1)
      circuit.u(theta, phi, lambda)

Circuit symbol:

.. code-block:: text

         ┌───────────┐
    q_0: ┤ U3(ϴ,φ,λ) ├
         └───────────┘

Matrix representation:

.. math::

    \newcommand{\rotationangle}{\frac{\theta}{2}}

    U3(\theta, \phi, \lambda) =
        \begin{pmatrix}
            \cos\left(\rotationangle\right) & -e^{i\lambda}\sin\left(\rotationangle\right) \\
            e^{i\phi}\sin\left(\rotationangle\right) &
            e^{i(\phi+\lambda)}\cos\left(\rotationangle\right)
        \end{pmatrix}

.. note::

    The matrix representation shown here differs from the `OpenQASM 2.0 specification
    <https://doi.org/10.48550/arXiv.1707.03429>`_ by a global phase of
    :math:`e^{i(\phi+\lambda)/2}`.

Examples:

.. math::

    U3(\theta, \phi, \lambda) = P(\phi) R_Y(\theta) P(\lambda)
    = e^{i\frac{\phi + \lambda}{2}} R_Z(\phi) R_Y(\theta) R_Z(\lambda)

.. math::

    U3\left(\theta, -\frac{\pi}{2}, \frac{\pi}{2}\right) = RX(\theta)

.. math::

    U3(\theta, 0, 0) = RY(\theta)
c                *   > [         TU ]  SSXU/US9  g)a0  
Args:
    theta: The angle :math:`\theta corresponding to the :math:`R_Y(\theta)` rotation.
    phi: The angle :math:`\phi` corresponding to the :math:`R_Z(\phi)` rotation.
    lam: The angle :math:`\lambda` corresponding to the :math:`R_Z(\lambda)` rotation.
    label: An optional label for the gate.
u3   labelN)super__init__)selfthetaphilamr   	__class__s        b/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/library/standard_gates/u3.pyr   U3Gate.__init__X   s      	q5s"35A    c                p    [        U R                  S   * U R                  S   * U R                  S   * 5      $ )a  Return inverted U3 gate.

:math:`U3(\theta,\phi,\lambda)^{\dagger} =U3(-\theta,-\lambda,-\phi))`

Args:
    annotated: when set to ``True``, this is typically used to return an
        :class:`.AnnotatedOperation` with an inverse modifier set instead of a concrete
        :class:`.Gate`. However, for this class this argument is ignored as the inverse
        of this gate is always a :class:`.U3Gate` with inverse parameter values.

Returns:
    U3Gate: inverse gate.
r      r   )r   paramsr   	annotateds     r   inverseU3Gate.inverseh   s1     t{{1~oAQHHr   c                   > U(       d8  US:X  a2  [        U R                  X#S.6nU R                  UR                  l        U$ Uc  [	        S U R                   5       5      n[
        TU ]  UUUUS9nU$ )a  Return a (multi-)controlled-U3 gate.

Args:
    num_ctrl_qubits: number of control qubits.
    label: An optional label for the gate [Default: ``None``]
    ctrl_state: control state expressed as integer,
        string (e.g.``'110'``), or ``None``. If ``None``, use all 1s.
    annotated: indicates whether the controlled gate should be implemented
        as an annotated gate. If ``None``, this is set to ``True`` if
        the gate contains free parameters and more than one control qubit, in which
        case it cannot yet be synthesized. Otherwise it is set to ``False``.

Returns:
    ControlledGate: controlled version of this gate.
r   )r   
ctrl_statec              3  B   #    U  H  n[        U[        5      v   M     g 7fN)
isinstancer
   ).0ps     r   	<genexpr>!U3Gate.control.<locals>.<genexpr>   s     XKq
1.A B BKs   )num_ctrl_qubitsr   r&   r"   )CU3Gater    r   	base_gateanyr   control)r   r.   r   r&   r"   gater   s         r   r2   U3Gate.controlx   sx    , _1DKKuLD#'::DNN    XDKKXX	7? /%#	 # D r   c                    SSK Jn  UR                  [        R                  R                  U R                  5      SU R                  S9U l        gzDefault definitionr   )QuantumCircuitT)legacy_qubitsnameN)	qiskit.circuitr7   _from_circuit_datar   U3_get_definitionr    r9   
definitionr   r7   s     r   _defineU3Gate._define   sA     	2 );;OO++DKK8SWS\S\ < 
r   c           	        USL a  [        S5      eU R                  u  p4n[        U5      [        U5      [        U5      pTn[        R                  " US-  5      n[        R
                  " US-  5      n[        R                  " U[        SU-  5      * U-  /[        SU-  5      U-  [        SXE-   -  5      U-  //U=(       d    [        S9$ )z%Return a Numpy.array for the U3 gate.F9unable to avoid copy while creating an array as requestedr                 ?dtype)

ValueErrorr    floatmathcossinnumpyarrayr   complexr   rF   copyr   r   r   rJ   rK   s           r   	__array__U3Gate.__array__   s    5=XYY++C,c
E#JChhuqy!hhuqy!{{s28}ns*+R#X$c"	*:&;c&AB "7
 	
r   c                R    [        U[        5      =(       a    U R                  U5      $ r(   )r)   r   _compare_parametersr   others     r   __eq__U3Gate.__eq__   s    %(LT-E-Ee-LLr   r>   r(   )r   r	   r   r	   r   r	   r   Optional[str]Fr"   bool)r   NNN)r.   intr   z
str | Noner&   zstr | int | Noner"   zbool | NoneNN)__name__
__module____qualname____firstlineno____doc__r   r<   _standard_gater   r#   r2   r@   rQ   rW   __static_attributes____classcell__r   s   @r   r   r      s    8t "__N  $B!B  B  	B
 B B I$  ! '+!%%% % %	%
 % %N

 M Mr   r   c                     ^  \ rS rSrSr\R                  r  SSS.         SU 4S jjjjrS r	SSS jjr
SS jrS	 rS
rU =r$ )r/      ua	  Controlled-U3 gate (3-parameter two-qubit gate).

This is a controlled version of the U3 gate (generic single qubit rotation).
It is restricted to 3 parameters, and so cannot cover generic two-qubit
controlled gates).

.. warning::

   This gate is deprecated. Instead, the :class:`.CUGate` should be used

   .. math::

       CU3(\theta, \phi, \lambda) = CU(\theta, \phi, \lambda, 0)

   .. code-block:: python

      circuit = QuantumCircuit(2)
      gamma = 0
      circuit.cu(theta, phi, lambda, gamma, 0, 1)




Circuit symbol:

.. code-block:: text

    q_0: ──────■──────
         ┌─────┴─────┐
    q_1: ┤ U3(ϴ,φ,λ) ├
         └───────────┘

Matrix representation:

.. math::

    \newcommand{\rotationangle}{\frac{\theta}{2}}

    CU3(\theta, \phi, \lambda)\ q_0, q_1 =
        I \otimes |0\rangle\langle 0| +
        U3(\theta,\phi,\lambda) \otimes |1\rangle\langle 1| =
        \begin{pmatrix}
            1 & 0                   & 0 & 0 \\
            0 & \cos(\rotationangle)           & 0 & -e^{i\lambda}\sin(\rotationangle) \\
            0 & 0                   & 1 & 0 \\
            0 & e^{i\phi}\sin(\rotationangle)  & 0 & e^{i(\phi+\lambda)}\cos(\rotationangle)
        \end{pmatrix}

.. note::

    In Qiskit's convention, higher qubit indices are more significant
    (little endian convention). In many textbooks, controlled gates are
    presented with the assumption of more significant qubits as control,
    which in our case would be q_1. Thus a textbook matrix for this
    gate will be:

    .. code-block:: text

             ┌───────────┐
        q_0: ┤ U3(ϴ,φ,λ) ├
             └─────┬─────┘
        q_1: ──────■──────

    .. math::

        \newcommand{\rotationangle}{\frac{\theta}{2}}

        CU3(\theta, \phi, \lambda)\ q_1, q_0 =
            |0\rangle\langle 0| \otimes I +
            |1\rangle\langle 1| \otimes U3(\theta,\phi,\lambda) =
            \begin{pmatrix}
                1 & 0   & 0                  & 0 \\
                0 & 1   & 0                  & 0 \\
                0 & 0   & \cos(\rotationangle)          & -e^{i\lambda}\sin(\rotationangle) \\
                0 & 0   & e^{i\phi}\sin(\rotationangle) & e^{i(\phi+\lambda)}\cos(\rotationangle)
            \end{pmatrix}
N)_base_labelc               @   > [         TU ]  SSXU/SUU[        XX6S9S9  g)zCreate new CU3 gate.cu3r   r   r   )r.   r   r&   r0   N)r   r   r   )r   r   r   r   r   r&   rk   r   s          r   r   CU3Gate.__init__  s8     	!U@ 	 	
r   c                    SSK Jn  UR                  [        R                  R                  U R                  5      SU R                  S9U l        gr6   )	r:   r7   r;   r   CU3r=   r    r9   r>   r?   s     r   r@   CU3Gate._define&  sC     	2 );;,,T[[9TXT]T] < 
r   c                    [        U R                  S   * U R                  S   * U R                  S   * U R                  S9$ )a  Return inverted CU3 gate.

:math:`CU3(\theta,\phi,\lambda)^{\dagger} =CU3(-\theta,-\phi,-\lambda))`

Args:
    annotated: when set to ``True``, this is typically used to return an
        :class:`.AnnotatedOperation` with an inverse modifier set instead of a concrete
        :class:`.Gate`. However, for this class this argument is ignored as the inverse
        of this gate is always a :class:`.CU3Gate` with inverse
        parameter values.

Returns:
    CU3Gate: inverse gate.
r   r   r   )r&   )r/   r    r&   r!   s     r   r#   CU3Gate.inverse5  s<     [[^Odkk!n_t{{1~o$//
 	
r   c                   USL a  [        S5      eU R                  u  p4n[        U5      [        U5      [        U5      pTn[        R                  " US-  5      n[        R
                  " US-  5      nU R                  (       ab  [        R                  " / SQSUS[        SU-  5      * U-  // SQS[        SU-  5      U-  S[        SXE-   -  5      U-  //U=(       d    [        S9$ [        R                  " US[        SU-  5      * U-  S// S	Q[        SU-  5      U-  S[        SXE-   -  5      U-  S// S
Q/U=(       d    [        S9$ )z&Return a numpy.array for the CU3 gate.FrC   r   )r   r   r   r   r   rD   )r   r   r   r   rE   )r   r   r   r   )r   r   r   r   )rG   r    rH   rI   rJ   rK   r&   rL   rM   r   rN   rO   s           r   rQ   CU3Gate.__array__H  s<   5=XYY++C,c
E#JChhuqy!hhuqy!??;; QR#X 45 BH+QB#)4D0E0KL	 &w  ;;!c"s(m^c115 c]S(!Ssy1A-BS-H!L 	 &w r   c                    [        U[        5      =(       a1    U R                  UR                  :H  =(       a    U R                  U5      $ r(   )r)   r/   r&   rT   rU   s     r   rW   CU3Gate.__eq__e  s;    ug& 05#3#330((/	
r   rY   r_   )
r   r	   r   r	   r   r	   r   rZ   r&   zOptional[Union[str, int]]r[   r\   )r`   ra   rb   rc   rd   r   rp   re   r   r@   r#   rQ   rW   rf   rg   rh   s   @r   r/   r/      s|    L\ "%%N  $04
 
!
  
  	

 
 .
 
*

&:
 
r   r/   c           	         U S::  a  [        S5      eS/n[        U 5       H'  nU[        U5       Vs/ s H
  o3SU-  -   PM     sn-  nM)     U Vs/ s H  n[        USU  S35      PM     sn$ s  snf s  snf )z-Generate the gray code for ``num_bits`` bits.r   z2Cannot generate the gray code for less than 1 bit.r   0b)rG   rangereversedformat)num_bitsresultixs       r   _generate_gray_coder   m  s|    1}MNNSF8_Xf%56%5q!t8%566 0671F1(1o&77 77s   A,A1c                   SSK Jn  / nU SU X   pe[        U5      nSnU GH:  n	SU	;  a  M  Uc  U	n[        U	5      R	                  S5      n
[        X5       VVs/ s H	  u  pX:g  PM     nnnSU;   a  UR	                  S5      nOSnUbx  X:w  a   UR                  U" 5       X^   XZ   // 45        OS[        U	5       VVs/ s H  u  pUS:X  d  M  UPM     nnnUSS  H#  nUR                  U" 5       UU   XZ   // 45        M%     U	R                  S5      S-  S:X  a'  UR                  UR                  5       XZ   U// 45        OUR                  X%U
   U// 45        U	nGM=     U$ s  snnf s  snnf )a+  Apply the gate to the last qubit in the register ``q``, controlled on all
preceding qubits. This function uses the gray code to propagate down to the last qubit.

Ported and adapted from Aqua (github.com/Qiskit/qiskit-aqua),
commit 769ca8d, file qiskit/aqua/circuits/gates/multi_control_u1_gate.py.
r   )CXGateN1Tr   r   )
r   r   r   listindexzipappend	enumeratecountr#   )qr.   r3   r   rule
q_controlsq_target	gray_codelast_patternpatternlm_posr   jcompposr   indicesidxs                     r   _gray_code_chainr   w  s    D-o.0B#O4ILg"Lg$$S) $'w#=>#=41#=>4<**T"CC?}VX
9K'LbQR)27);H);qCx1);H"12;CKKJsOZ=O+PRT UV ' ==!Q&KK**<h)GLMKK6 2H=rBC7 : K) ? Is   E!<E'E')rd   
__future__r   rI   cmathr   typingr   r   rL   qiskit.circuit.controlledgater   qiskit.circuit.gater   "qiskit.circuit.parameterexpressionr	   r
   qiskit._accelerate.circuitr   r   r/   r   r    r   r   <module>r      sO    # "   "  8 $ V 3bMT bMJj
n j
Z8+r   