
    z	i(                    ,   S r SSKJr  SSKrSSKJrJ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\R"                   " S S\5      5       r\R"                   " S S\5      5       r\R"                   " S S\5      5       r " S S\5      rS rg)zAnnotated Operations.    )annotationsN)UnionList)	Operation)ParameterValueType)_compute_control_matrix_ctrl_state_to_int)CircuitErrorc                      \ rS rSrSrSrg)Modifier   zWThe base class that all modifiers of :class:`~.AnnotatedOperation` should
inherit from. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       \/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/annotated_operation.pyr   r      s     	r   r   c                      \ rS rSrSrSrg)InverseModifier!   z;Inverse modifier: specifies that the operation is inverted.r   Nr   r   r   r   r   r   !   s    Er   r   c                  D    \ rS rSr% SrSrS\S'   SrS\S'   SSS	 jjrS
r	g)ControlModifier(   zyControl modifier: specifies that the operation is controlled by ``num_ctrl_qubits``
and has control state ``ctrl_state``.r   intnum_ctrl_qubitsNUnion[int, str, None]
ctrl_statec                0    Xl         [        X!5      U l        g )N)r   r	   r!   )selfr   r!   s      r   __init__ControlModifier.__init__0   s    .,ZIr   )r!   r   )r   N)r   r   r!   r    )
r   r   r   r   r   r   __annotations__r!   r$   r   r   r   r   r   r   (   s*    - OS(,J%,J Jr   r   c                  $    \ rS rSr% SrS\S'   Srg)PowerModifier5   zNPower modifier: specifies that the operation is raised to the power ``power``.floatpowerr   N)r   r   r   r   r   r&   r   r   r   r   r(   r(   5   s
    XLr   r(   c                      \ rS rSrSrSS jr\S 5       r\S 5       r\S 5       r	SS jr
SS jrS	 r    S         SS jjrSSS jjrSSS jjr\SS j5       r\R"                  SS j5       rSS jrSrg
)AnnotatedOperation<   zAnnotated operation.c                P    Xl          [        U[        5      (       a  UOU/U l        g)a  
Create a new AnnotatedOperation.

An "annotated operation" allows to add a list of modifiers to the
"base" operation. For now, the only supported modifiers are of
types :class:`~.InverseModifier`, :class:`~.ControlModifier` and
:class:`~.PowerModifier`.

An annotated operation can be viewed as an extension of
:class:`~.ControlledGate` (which also allows adding control to the
base operation). However, an important difference is that the
circuit definition of an annotated operation is not constructed when
the operation is declared, and instead happens during transpilation,
specifically during the :class:`~.HighLevelSynthesis` transpiler pass.

An annotated operation can be also viewed as a "higher-level"
or "more abstract" object that can be added to a quantum circuit.
This enables writing transpiler optimization passes that make use of
this higher-level representation, for instance removing a gate
that is immediately followed by its inverse.

Args:
    base_op: base operation being modified
    modifiers: ordered list of modifiers. Supported modifiers include
        ``InverseModifier``, ``ControlModifier`` and ``PowerModifier``.

Examples::

    op1 = AnnotatedOperation(SGate(), [InverseModifier(), ControlModifier(2)])

    op2_inner = AnnotatedGate(SGate(), InverseModifier())
    op2 = AnnotatedGate(op2_inner, ControlModifier(2))

Both op1 and op2 are semantically equivalent to an ``SGate()`` which is first
inverted and then controlled by 2 qubits.
N)base_op
isinstancer   	modifiers)r#   r0   r2   s      r   r$   AnnotatedOperation.__init__?   s)    J [&0D&A&A	{	8r   c                    g)z,Unique string identifier for operation type.	annotatedr   r#   s    r   nameAnnotatedOperation.namej   s     r   c                    SnU R                    H(  n[        U[        5      (       d  M  XR                  -  nM*     XR                  R
                  -   $ )zNumber of qubits.r   )r2   r1   r   r   r0   
num_qubits)r#   r   modifiers      r   r:   AnnotatedOperation.num_qubitso   sG     H(O44#;#;; ' !8!888r   c                .    U R                   R                  $ )zNumber of classical bits.)r0   
num_clbitsr6   s    r   r>   AnnotatedOperation.num_clbitsy   s     ||&&&r   c                    [        U[        5      =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ )z,Checks if two AnnotatedOperations are equal.)r1   r-   r2   r0   )r#   others     r   __eq__AnnotatedOperation.__eq__~   s>     u01 .%//1.-	
r   c                v    [        U R                  R                  5       U R                  R                  5       S9$ )z3Return a copy of the :class:`~.AnnotatedOperation`.r0   r2   )r-   r0   copyr2   r6   s    r   rF   AnnotatedOperation.copy   s)    !$,,*;*;*=I\I\I^__r   c                   SSK Jn  U" U R                  5      nU R                   H  n[	        U[
        5      (       a  UR                  S5      nM+  [	        U[        5      (       a3  U" [        UR                  UR                  UR                  5      5      nMs  [	        U[        5      (       a  UR                  UR                  5      nM  [        SU S35      e   U$ )z@Return a matrix representation (allowing to construct Operator).r   )OperatorUnknown modifier .)qiskit.quantum_info.operatorsrI   r0   r2   r1   r   r+   r   r   datar   r!   r(   r
   )r#   rI   operatorr;   s       r   	to_matrixAnnotatedOperation.to_matrix   s    :DLL)H(O44#>>"-Ho66#+ x'?'?ATAT
 Hm44#>>(..9"%6xj#BCC ' r   Nc                    U R                   R                  5       nUR                  [        XS95        [	        U R
                  U5      $ )a  
Return the controlled version of itself.

Implemented as an annotated operation, see  :class:`.AnnotatedOperation`.

Args:
    num_ctrl_qubits: number of controls to add to gate (default: ``1``)
    label: ignored (used for consistency with other control methods)
    ctrl_state: The control state in decimal or as a bitstring
        (e.g. ``'111'``). If ``None``, use ``2**num_ctrl_qubits-1``.
    annotated: ignored (used for consistency with other control methods)

Returns:
    Controlled version of the given operation.
)r   r!   )r2   rF   appendr   r-   r0   )r#   r   labelr!   r5   extended_modifierss         r   controlAnnotatedOperation.control   sA    . "^^002!!OS	
 "$,,0BCCr   c                    U R                   R                  5       nUR                  [        5       5        [	        U R
                  U5      $ )z
Return the inverse version of itself.

Implemented as an annotated operation, see  :class:`.AnnotatedOperation`.

Args:
    annotated: ignored (used for consistency with other inverse methods)

Returns:
    Inverse version of the given operation.
)r2   rF   rS   r   r-   r0   )r#   r5   rU   s      r   inverseAnnotatedOperation.inverse   s:     "^^002!!/"34!$,,0BCCr   c                    U R                   R                  5       nUR                  [        U5      5        [	        U R
                  U5      $ )a/  
Raise this gate to the power of ``exponent``.

Implemented as an annotated operation, see  :class:`.AnnotatedOperation`.

Args:
    exponent: the power to raise the gate to
    annotated: ignored (used for consistency with other power methods)

Returns:
    An operation implementing ``gate^exponent``
)r2   rF   rS   r(   r-   r0   )r#   exponentr5   rU   s       r   r+   AnnotatedOperation.power   s<     "^^002!!-"9:!$,,0BCCr   c                0    [        U R                  S/ 5      $ )z,The params of the underlying base operation.params)getattrr0   r6   s    r   r_   AnnotatedOperation.params   s     t||Xr22r   c                    [        U R                  S5      (       a  XR                  l        g [        SU R                   S35      e)Nr_   z6Cannot set attribute ``params`` on the base operation rL   )hasattrr0   r_   AttributeError)r#   values     r   r_   ra      s;    4<<**"'LL HVWX r   c                    [        U R                  S5      (       a  U R                  R                  U5      $ [        SU R                   S35      e)z7Validate a parameter for the underlying base operation.validate_parameterz1Cannot validate parameters on the base operation rL   )rc   r0   rg   rd   )r#   	parameters     r   rg   %AnnotatedOperation.validate_parameter   sG    4<<!566<<229==PQUQ]Q]P^^_`aar   rE   )r0   r   r2   zUnion[Modifier, List[Modifier]])returnbool)rj   z'AnnotatedOperation')   NNT)
r   r   rT   z
str | Noner!   zint | str | Noner5   rk   rj   r-   )T)r5   rk   )F)r\   r*   r5   rk   )rj   list[ParameterValueType])re   rm   )rh   r   rj   r   )r   r   r   r   r   r$   propertyr7   r:   r>   rB   rF   rP   rV   rY   r+   r_   setterrg   r   r   r   r   r-   r-   <   s    )8V   9 9 ' '
`.  ! '+DD D %	D
 D 
D:D"D$ 3 3 ]] br   r-   c                   SnSnSnU  H  n[        U[        5      (       a  US-  nM  [        U[        5      (       a+  X$R                  -  nX4R                  -  UR                  -  nM_  [        U[
        5      (       a  XR                  -  nM  [        SU S35      e   / nUS:  a  UR                  [        5       5        US-  nUS:w  a  UR                  [        U5      5        US:  a  UR                  [        X#5      5        U$ )a  
Returns the canonical representative of the modifier list. This is possible
since all the modifiers commute; also note that InverseModifier is a special
case of PowerModifier. The current solution is to compute the total number
of control qubits / control state and the total power. The InverseModifier
will be present if total power is negative, whereas the power modifier will
be present only with positive powers different from 1.
rl   r   rJ   rK   rL   )	r1   r   r   r   r!   r(   r+   r
   rS   )r2   r+   r   r!   r;   canonical_modifierss         r   _canonicalize_modifiersrr      s     EOJh00RKE/22777O$(@(@@HDWDWWJ-00^^#E!28*A>??  qy""?#45z""=#78""??#OPr   )r   
__future__r   dataclassestypingr   r   qiskit.circuit.operationr   "qiskit.circuit.parameterexpressionr   qiskit.circuit._utilsr   r	   qiskit.circuit.exceptionsr
   r   	dataclassr   r   r(   r-   rr   r   r   r   <module>r{      s     "   . A M 2	 	 	h 	 	 	Jh 	J 	J H  vb vbr"r   