
    z	i6;                        S r SSKJr  SSKrSSK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 S\5      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S jrS rSrU =r$ )UGate   uh  Generic single-qubit rotation in terms of ZYZ Euler angles.

The action of this gate can be related to the standard ZYZ Euler decomposition by

.. math::

    U(\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).

Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
with the :meth:`~qiskit.circuit.QuantumCircuit.u` method.

Circuit symbol:

.. code-block:: text

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

Matrix representation:

.. math::

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

    U(\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 is the same as in the `OpenQASM 3.0 specification
    <https://openqasm.com/language/gates.html#built-in-gates>`_,
    which 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::

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

.. math::

    U(\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.
u   labelN)super__init__)selfthetaphilamr   	__class__s        a/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/library/standard_gates/u.pyr   UGate.__init__S   s      	a%c!2%@    c                p    [        U R                  S   * U R                  S   * U R                  S   * 5      $ )a  Return inverted U gate.

:math:`U(\theta,\phi,\lambda)^{\dagger} =U(-\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:`.UGate` with inverse parameter values.

Returns:
    UGate: inverse gate.
r      r   )r   paramsr   	annotateds     r   inverseUGate.inversec   s1     dkk!n_t{{1~oAGGr   c           	     $  > U(       dX  US:X  aR  [        U R                  S   U R                  S   U R                  S   SUUS9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-U 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   r   )r   
ctrl_statec              3  B   #    U  H  n[        U[        5      v   M     g 7fN)
isinstancer
   ).0ps     r   	<genexpr> UGate.control.<locals>.<genexpr>   s     XKq
1.A B BKs   )num_ctrl_qubitsr   r&   r"   )CUGater    r   	base_gateanyr   control)r   r.   r   r&   r"   gater   s         r   r2   UGate.controls   s    , _1AAA%D $(::DNN    XDKKXX	7? /%#	 # D r   c           	     b   USL a  [        S5      eS U R                   5       u  p4n[        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 U gate.F9unable to avoid copy while creating an array as requestedc              3  8   #    U  H  n[        U5      v   M     g 7fr(   floatr*   params     r   r,   "UGate.__array__.<locals>.<genexpr>   s     A[E5<<[   r                 ?dtype)	
ValueErrorr    mathcossinnumpyarrayr   complex)r   r@   copyr   r   r   rC   rD   s           r   	__array__UGate.__array__   s    5=XYYAT[[AChhuqy!hhuqy!{{s28}ns*+R#X$c"	*:&;c&AB "7
 	
r   c                P    [        U[        5      (       a  U R                  U5      $ g)NF)r)   r   _compare_parametersr   others     r   __eq__UGate.__eq__   s#    eU##++E22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   U_standard_gater   r#   r2   rI   rO   __static_attributes____classcell__r   s   @r   r   r      s    1f "^^N  $A!A  A  	A
 A A H$  ! '+!%,, , %	,
 , ,\
 r   r   c                  8   ^  \ rS rSrSrU 4S jrU 4S jrSrU =r$ )_CUGateParams   )_gatec                D   > [         TU ]  UR                  5        Xl        g r(   )r   r   _paramsre   )r   r3   r   s     r   r   _CUGateParams.__init__   s    &
r   c                  > [         TU ]  X5        X R                  R                  U'   [	        U[
        5      (       ab  [        [        UR                  S5      6 5       H=  u  p4US:  a  SU-   nUS:  d  M  X#   U R                  R                  R                  U'   M?     g US:  a  SU-   nUS:  a#  X R                  R                  R                  U'   g g )N   r      )r   __setitem__re   rg   r)   slice	enumeraterangeindicesr0   r    )r   keyvalueibase_keyr   s        r   rl   _CUGateParams.__setitem__   s    C'"'

3c5!!  )A)?@a< 8|Ha<<AHDJJ((//9	  A Qw#gQw38

$$++C0 r   )	rX   rY   rZ   r[   	__slots__r   rl   r_   r`   ra   s   @r   rc   rc      s     I9 9r   rc   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	 5       r\R                  U 4S
 j5       rSU 4S jjrS rSrU =r$ )r/      u  Controlled-U gate (4-parameter two-qubit gate).

This is a controlled version of the U gate (generic single qubit rotation),
including a possible global phase :math:`e^{i\gamma}` of the U gate.

Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
with the :meth:`~qiskit.circuit.QuantumCircuit.cu` method.

Circuit symbol:

.. code-block:: text

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

Matrix representation:

.. math::

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

    CU(\theta, \phi, \lambda, \gamma)\ q_0, q_1 =
        I \otimes |0\rangle\langle 0| +
        e^{i\gamma} U(\theta,\phi,\lambda) \otimes |1\rangle\langle 1| =
        \begin{pmatrix}
            1 & 0 & 0 & 0 \\
            0 & e^{i\gamma}\cos(\rotationangle) &
            0 & -e^{i(\gamma + \lambda)}\sin(\rotationangle) \\
            0 & 0 & 1 & 0 \\
            0 & e^{i(\gamma+\phi)}\sin(\rotationangle) &
            0 & e^{i(\gamma+\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: ┤ U(ϴ,φ,λ,γ) ├
             └─────┬──────┘
        q_1: ──────■───────

    .. math::

        \newcommand{\rotationangle}{\frac{\theta}{2}}
        CU(\theta, \phi, \lambda, \gamma)\ q_1, q_0 =
        |0\rangle\langle 0| \otimes I +
        e^{i\gamma}|1\rangle\langle 1| \otimes U(\theta,\phi,\lambda) =
        \begin{pmatrix}
        1 & 0 & 0 & 0 \\
        0 & 1 & 0 & 0 \\
        0 & 0 & e^{i\gamma} \cos(\rotationangle) & -e^{i(\gamma + \lambda)}\sin(\rotationangle) \\
        0 & 0 &
        e^{i(\gamma + \phi)}\sin(\rotationangle) & e^{i(\gamma + \phi+\lambda)}\cos(\rotationangle)
        \end{pmatrix}
N)_base_labelc               @   > [         TU ]  SSXX4/SUU[        XX7S9S9  g)zCreate new CU gate.cur   r   r   )r.   r   r&   r0   N)r   r   r   )	r   r   r   r   gammar   r&   ry   r   s	           r   r   CUGate.__init__  s8     	$!E? 	 	
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.circuitr   _from_circuit_datar   CU_get_definitionr    r   
definition)r   r   s     r   _defineCUGate._define5  sA     	2 );;OO++DKK8SWS\S\ < 
r   c                    [        U R                  S   * U R                  S   * U R                  S   * U R                  S   * U R                  S9$ )a  Return inverted CU gate.

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

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:`.CUGate` with inverse parameter
        values.

Returns:
    CUGate: inverse gate.
r   r   r   rk   )r&   )r/   r    r&   r!   s     r   r#   CUGate.inverseD  sL     [[^O[[^O[[^O[[^O
 	
r   c           	     \   USL a  [        S5      eS U R                   5       u  p4pV[        R                  " US-  5      n[        R                  " US-  5      n[
        R                  " SU-  5      U-  n	[
        R                  " SXe-   -  5      * U-  n
[
        R                  " SXd-   -  5      U-  n[
        R                  " SXd-   U-   -  5      U-  nU R                  (       a%  [        R                  " / SQSU	SU
// SQSUSU//US	9$ [        R                  " U	SU
S// S
QUSUS// SQ/US	9$ )z%Return a numpy.array for the CU gate.Fr6   c              3  8   #    U  H  n[        U5      v   M     g 7fr(   r8   r:   s     r   r,   #CUGate.__array__.<locals>.<genexpr>_  s     !HK5%,,Kr=   r   r>   )r   r   r   r   r   )r   r   r   r   r?   )r   r   r   r   )r   r   r   r   )
rA   r    rB   rC   rD   cmathr   r&   rE   rF   )r   r@   rH   r   r   r   r|   rC   rD   abcds                r   rI   CUGate.__array__[  s"   5=XYY!HDKK!HChhuqy!hhuqy!IIb5j!C'YYrU[)**S0IIbEK()C/IIbEK#-./#5??;;1a|\Aq!Q<HPU  ;;Q1|aAq\<HPU r   c                    [        U 5      $ r(   )rc   )r   s    r   r    CUGate.paramso  s    T""r   c                z   > [         [        [        U 5      ]  R	                  X5        US S U R
                  l        g )N)r   r   typer    fsetr0   )r   
parametersr   s     r   r    r   s  s0    
 	nd4j055dG *3Br   c                   > Ub  UO0 n[         TU ]  U5      n[        R                  " UR                  U5      Ul        U$ r(   )r   __deepcopy___copydeepcopyrg   )r   memooutr   s      r   r   CUGate.__deepcopy__{  s;     'tRg"4(nnS[[$7
r   c                    [        U[        5      =(       a1    U R                  UR                  :H  =(       a    U R                  U5      $ r(   )r)   r/   r&   rL   rM   s     r   rO   CUGate.__eq__  s;    uf% 05#3#330((/	
r   )r   rW   )r   r	   r   r	   r   r	   r|   r	   r   rR   r&   zOptional[Union[str, int]]rS   rT   r(   )rX   rY   rZ   r[   r\   r   r   r^   r   r   r#   rI   propertyr    setterr   rO   r_   r`   ra   s   @r   r/   r/      s    ?B "__N  $04
 
!
  
  	

 "
 
 .
 
,

.( # # ]]0 0
 
r   r/   )r\   
__future__r   r   rH   r   rB   r   typingr   r   rE   qiskit.circuit.controlledgater   qiskit.circuit.gater   "qiskit.circuit.parameterexpressionr	   r
   qiskit._accelerate.circuitr   r   listrc   r/   rQ   r   r   <module>r      sU    # "     "  8 $ V 3VD Vr"9D "9Jm
^ m
r   