
    z	i'                    z    S 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Jr  SS	KJr   " S
 S\5      rg)Unitary gate.    )annotations)IteratorIterableN)ParameterExpression)CircuitError   )AnnotatedOperationControlModifierPowerModifier)Instructionc                     ^  \ rS rSrSr S         SU 4S jjjrSrSS jrSSS jjrSS jr	SS jr
    S       SS	 jjr\SS
 j5       r\SS j5       r\SS j5       rSS jrS rSrU =r$ )Gate   r   c                2   > SU l         [        TU ]	  XSX4S9  g)z
Args:
    name: The name of the gate.
    num_qubits: The number of qubits the gate acts on.
    params: A list of parameters.
    label: An optional label for the gate.
Nr   label)
definitionsuper__init__)selfname
num_qubitsparamsr   	__class__s        M/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/gate.pyr   Gate.__init__   s      1fB       c                x    [        U S5      (       a  U R                  [        S9$ [        S[	        U 5       35      e)a  Return a Numpy.array for the gate unitary matrix.

Returns:
    np.ndarray: if the Gate subclass has a matrix definition.

Raises:
    CircuitError: If a Gate subclass does not implement this method an
        exception will be raised when this base class method is called.
	__array__)dtypezto_matrix not defined for this )hasattrr!   complexr   type)r   s    r   	to_matrixGate.to_matrix0   s9     4%%>>>00<T$ZLIJJr   c                    SSK Jn  SSKJn  U(       d*  U" U" U 5      R	                  USS9U R
                   SU 3S9$ [        U [        U5      5      $ )a8  Raise this gate to the power of ``exponent``.

Implemented either as a unitary gate (ref. :class:`~.library.UnitaryGate`)
or as an annotated operation (ref. :class:`.AnnotatedOperation`). In the case of several standard
gates, such as :class:`.RXGate`, when the power of a gate can be expressed in terms of another
standard gate that is returned directly.

Args:
    exponent (float): the power to raise the gate to
    annotated (bool): indicates whether the power gate can be implemented
        as an annotated operation. In the case of several standard
        gates, such as :class:`.RXGate`, this argument is ignored when
        the power of a gate can be expressed in terms of another
        standard gate.

Returns:
    An operation implementing ``gate^exponent``

Raises:
    CircuitError: If gate is not unitary
r   )Operator)UnitaryGateT)assume_unitary^r   )qiskit.quantum_info.operatorsr)   0qiskit.circuit.library.generalized_gates.unitaryr*   powerr   r
   r   )r   exponent	annotatedr)   r*   s        r   r/   
Gate.power>   sY    . 	;P$$Xd$Cdii[XYZbYcKd  &dM(,CDDr   c                $    U R                  U5      $ N)r/   )r   r0   s     r   __pow__Gate.__pow___   s    zz(##r   c                    [        U R                   SU 3U R                  / S9nU R                  Ul        U R                  Ul        U$ )N*)r   r   r   )r   r   r   validate_parameterr   )r   r0   gates      r   _return_repeatGate._return_repeatb   sB    DII;az2tWYZ"&"9"9kkr   c                T    U(       d  SSK Jn  U" XX#5      $ [        U [        XS95      $ )a  Return the controlled version of itself.

Implemented either as a controlled gate (ref. :class:`.ControlledGate`)
or as an annotated operation (ref. :class:`.AnnotatedOperation`).

Args:
    num_ctrl_qubits: number of controls to add to gate (default: ``1``)
    label: optional gate label. Ignored if implemented as an annotated
        operation.
    ctrl_state: the control state in decimal or as a bitstring
        (e.g. ``'111'``). If ``None``, use ``2**num_ctrl_qubits-1``.
    annotated: indicates whether the controlled gate is implemented
        as an annotated gate. If ``None``, this is set to ``False``
        if the controlled gate can directly be constructed, and otherwise
        set to ``True``. This allows defering the construction process in case the
        synthesis of the controlled gate requires more information (e.g.
        values of unbound parameters).

Returns:
    Controlled version of the given operation.

Raises:
    QiskitError: unrecognized mode or invalid ctrl_state
r	   )add_control)num_ctrl_qubits
ctrl_state)_add_controlr>   r
   r   )r   r?   r   r@   r1   r>   s         r   controlGate.controlh   s0    > 1teHH &oo] r   c              #  ,   #    U  H
  nU// 4v   M     g7f)zHExpands a single argument.

For example: [q[0], q[1]] -> [q[0]], [q[1]]
N )qargarg0s     r   _broadcast_single_argumentGate._broadcast_single_argument   s      D&"* s   c              #  "  #    [        U 5      [        U5      :X  a  [        X5       H  u  p#X#// 4v   M     g [        U 5      S:X  a  U H  nU S   U// 4v   M     g [        U5      S:X  a  U  H  nX!S   // 4v   M     g [        SU  SU 35      e7f)Nr	   r   z4Not sure how to combine these two-qubit arguments:
 z
 )lenzipr   )qarg0qarg1rG   arg1s       r   _broadcast_2_argumentsGate._broadcast_2_arguments   s     u:U# "%/
lB&& 0Z1_ Qx&** Z1_ 1X&**  GwcRWQXY s   BBc              #     ^ #    [        U 4S jT  5       5      (       a  [        T 6  H  n[        U5      / 4v   M     g [        ST  S35      e7f)Nc              3  X   >#    U  H  n[        U5      [        TS    5      :H  v   M!     g7f)r   N)rK   ).0rF   qargss     r   	<genexpr>1Gate._broadcast_3_or_more_args.<locals>.<genexpr>   s"     <eds4yCaM)es   '*z0Not sure how to combine these qubit arguments:
 
)allrL   listr   )rU   args   ` r   _broadcast_3_or_more_argsGate._broadcast_3_or_more_args   sM     <e<<<E{3im# # !RSXRYY[\]]s   A	Ac           	     ,   [        U5      U R                  :w  d  U(       a1  [        S[        U5       S[        U5       SU R                   S35      e[        S U 5       5      (       a  [        S5      e[        U5      S:X  a  / / 4/$ [        U5      S:X  a  [        R                  US   5      $ [        U5      S	:X  a  [        R                  US   US   5      $ [        U5      S
:  a  [        R                  U5      $ [        S[        U5       S35      e)a  Validation and handling of the arguments and its relationship.

For example, ``cx([q[0],q[1]], q[2])`` means ``cx(q[0], q[2]); cx(q[1], q[2])``. This
method yields the arguments in the right grouping. In the given example::

    in: [[q[0],q[1]], q[2]],[]
    outs: [q[0], q[2]], []
          [q[1], q[2]], []

The general broadcasting rules are:

    * If len(qargs) == 1::

        [q[0], q[1]] -> [q[0]],[q[1]]

    * If len(qargs) == 2::

        [[q[0], q[1]], [r[0], r[1]]] -> [q[0], r[0]], [q[1], r[1]]
        [[q[0]], [r[0], r[1]]]       -> [q[0], r[0]], [q[0], r[1]]
        [[q[0], q[1]], [r[0]]]       -> [q[0], r[0]], [q[1], r[0]]

    * If len(qargs) >= 3::

        [q[0], q[1]], [r[0], r[1]],  ...] -> [q[0], r[0], ...], [q[1], r[1], ...]

Args:
    qargs: List of quantum bit arguments.
    cargs: List of classical bit arguments.

Returns:
    A tuple with single arguments.

Raises:
    CircuitError: If the input is not valid. For example, the number of
        arguments does not match the gate expectation.
zThe amount of qubit(z)/clbit(z1) arguments does not match the gate expectation (z).c              3  .   #    U  H  o(       + v   M     g 7fr4   rE   )rT   rF   s     r   rV   +Gate.broadcast_arguments.<locals>.<genexpr>   s     *EDxxEs   z&One or more of the arguments are emptyr   r	         zThis gate cannot handle z
 arguments)rK   r   r   anyr   rH   rP   r\   )r   rU   cargss      r   broadcast_argumentsGate.broadcast_arguments   s	   J u:(E&s5zl(3u:, G448OO3DBH 
 *E***GHHu:?R  u:?2258<<Z1_..uQxqBBZ1_11%88!9#e*ZPQQr   c                   [        U[        5      (       aL  [        UR                  5      S:  a  U$ UR	                  5       (       d  SU R
                   3n[        U5      eU$ [        U[        [        45      (       a  U$ [        U[        R                  [        R                  45      (       a  UR                  5       $ [        S[        U5       SU R
                   S35      e)z<Gate parameters should be int, float, or ParameterExpressionr   z.Bound parameter expression is complex in gate zInvalid param type z
 for gate .)
isinstancer   rK   
parametersis_realr   r   intfloatnpintegerfloatingitemr%   )r   	parametermsgs      r   r9   Gate.validate_parameter   s    i!4559''(1,  $$&&FtyykR"3''i#u..	BJJ#<==>>##!4T)_4EZPTPYPY{Z[\]]r   )r   r4   )
r   strr   rl   r   rZ   r   
str | NonereturnNone)rw   z
np.ndarray)F)r0   rm   r1   bool)r0   rm   rw   z'Gate')r	   NNN)r?   rl   r   rv   r@   zint | str | Noner1   zbool | None)rF   rZ   rw   Iterator[tuple[list, list]])rM   rZ   rN   rZ   rw   rz   )rU   rZ   rw   rz   )rU   rZ   rd   rZ   rw   zIterable[tuple[list, list]])__name__
__module____qualname____firstlineno____doc__r   __array_priority__r&   r/   r5   r;   rB   staticmethodrH   rP   r\   re   r9   __static_attributes____classcell__)r   s   @r   r   r      s     !CC C 	C
 C 
C C$ KEB$  ! '+!%(( ( %	(
 (T    * ^ ^9Rv^ ^r   r   )r   
__future__r   typingr   r   numpyrn   "qiskit.circuit.parameterexpressionr   qiskit.circuit.exceptionsr   annotated_operationr
   r   r   instructionr   r   rE   r   r   <module>r      s2     " %  B 2 S S $k^; k^r   