
    z	i).                        S r SSKJr  SSKJr  SSK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
KJrJrJrJr  S	SKJr  / SQr          SS jr   S         SS jjrS rS rS rg)z&Add control to operation if supported.    )annotations)pi)CircuitError)UnitaryGate)PassManager)BasisTranslatorUnrollCustomDefinitions)SessionEquivalenceLibrary   )ControlledGateGateQuantumRegisterQuantumCircuit)_ctrl_state_to_int)puxzyhsxsxdgrxryrzcxczc                    [        U [        5      (       a  U R                  5         [        XX#S9nU R                  b:  UR
                  R                  5       Ul        U R                  UR
                  l        U$ )a/  Return the controlled version of the gate.

This function first checks whether the gate's name corresponds to a known
method for generating its controlled version. Currently, these methods exist
for gates in ``EFFICIENTLY_CONTROLLED_GATES``.

For gates not in ``EFFICIENTLY_CONTROLLED_GATES``, the function calls the unroller
to decompose the gate into gates in ``EFFICIENTLY_CONTROLLED_GATES``,
and then generates the controlled version by controlling every gate in this
decomposition.

Open controls are implemented by conjugating the control line with X gates.

This function is meant to be called from the
:method:`qiskit.circuit.gate.Gate.control()` method.

Args:
    operation: The operation to be controlled.
    num_ctrl_qubits: The number of controls to add to gate.
    label: An optional gate label.
    ctrl_state: The control state in decimal or as a bitstring
        (e.g. '111'). If specified as a bitstring the length
        must equal num_ctrl_qubits, MSB on left. If None, use
        2**num_ctrl_qubits-1.

Returns:
    Controlled version of gate.

)num_ctrl_qubitslabel
ctrl_state)
isinstancer   _definecontrolr    	base_gate
to_mutable)	operationr   r    r!   cgates        U/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/_add_control.pyadd_controlr*   2   s]    F )[))IecE"//446 )L    Nc                   SSK Jn  [        X15      n[        USS9n[        U R                  SS9n[        XVSU R                   3S9n[        XR                  5      (       a#  U R                  nU R                  5       n SU l	        Sn	U R                  [        ;   a  [        XpXV5        GO)[        XR                  5      (       a  U R                  5       n SU l	        [        U [        S9n
U
R                  R                  (       a  XR                  R                  -  n	U
R                  nUR                   UR"                  4 VVVs0 s H  n[%        U5        H  u  pX_M	     M     nnnnUR&                   HZ  nUR(                  UR                   nn[+        U5      S	:X  a  XoUS         nOU Vs/ s H
  nXoU      PM     nn[        UUUU5        M\     U	(       a9  [+        U5      S
:  a  UR-                  X5        OUR/                  XSS US   5        [        XR                  5      (       aJ  WU l	        XR0                  -   nU R                  U-  U-  nU R2                  R                  nU R2                  nOUnUnU R                  nU nUS
:  a  SUS 3nOSU-  R5                  S5      nU U 3nUR                  UUR                  U R6                  UUUUUS9nU$ s  snnnf s  snf )a#  Return the controlled version of the gate.

This function first checks whether the gate's name corresponds to a known
method for generating its controlled version. Currently, these methods exist
for gates in ``EFFICIENTLY_CONTROLLED_GATES``.

For gates not in ``EFFICIENTLY_CONTROLLED_GATES``, the function calls the unroller
to decompose the gate into gates in ``EFFICIENTLY_CONTROLLED_GATES``,
and then generates the controlled version by controlling every gate in this
decomposition.

Open controls are implemented by conjugating the control line with X gates.

Args:
    operation: The gate used to create the ControlledGate.
    num_ctrl_qubits: The number of controls to add to gate (default=1).
    label: An optional gate label.
    ctrl_state: The control state in decimal or as
        a bitstring (e.g. '111'). If specified as a bitstring the length
        must equal num_ctrl_qubits, MSB on left. If None, use
        2**num_ctrl_qubits-1.

Returns:
    Controlled version of gate.

Raises:
    CircuitError: gate contains non-gate in definition
r   )controlledgater$   nametargetc_Nbasis_gatesr      cdz{0})r    r   
definitionr!   r%   )qiskit.circuitr-   r   r   
num_qubitsr   r/   r"   r   r!   r&   EFFICIENTLY_CONTROLLED_GATESapply_basic_controlled_gate_unroll_gater8   global_phasequbitsclbits	enumeratedatar'   lenr   mcpr   r%   formatparams)r'   r   r    r!   r-   	q_controlq_targetcontrolled_circoriginal_ctrl_stater>   unrolled_gater8   bitsindexbitbit_indicesinstructiongateqargsr0   qargnew_num_ctrl_qubitsnew_ctrl_state	base_namer%   ctrl_substrnew_namer(   s                               r)   r$   r$   _   s   H .#J@Ji@Iy33(CH$YINNCS?TUO)::;;'22((*	#	L~~55#O	Ti!>!>??!,,.I#'I $Y<XY##0044AAAL"--
 $**J,=,=>
>'o
 J- > 	 
 &??K%//1C1C%D5zQ!eAh"78BGH%$(t#45%H'y&Q + y>Al6nimL)::;;2	-0I0II"--@:M'',,	''	-#NN		 Q-a0122::3?yk*H))""+"! * 	E Lk
 Is   !K,K!c                	   [        U5      nUR                  S:X  a  U R                  X#5        gUR                  S:X  aA  U R                  UR                  R
                  S   R                  R                  S   UUSS9  gUR                  S:X  aB  U R                  UR                  R
                  S   R                  R                  S   UUSSS9  gUR                  S	:X  aA  U R                  UR                  R
                  S   R                  R                  S   UUSS9  gUR                  S
:X  a4  SSK
Jn  U R                  U" UR                  S   U5      USS U/-   5        gUR                  S:X  a   U R                  USS US   /-   US   5        gUR                  S:X  aH  U R                  US   5        U R                  USS US   /-   US   5        U R                  US   5        gUR                  S:X  Ga  UR                  u  pgnUS:X  a;  US:X  a  US:X  a  U R                  XS   U5        gU R                  XgUSUS   U5        gU[         * S-  :X  a  U[         S-  :X  a  U R                  XbUSS9  gUS:X  a  US:X  a  U R                  UUUSS9  gUS:X  a  US:X  a  U R#                  XU5        gU R                  XUSS9  U R                  XbUSS9  U R                  XrUSS9  U R#                  Xx-   S-  USS US   5        gUR                  S:X  a4  U R                  U5        U R                  X#5        U R                  U5        gUR                  S:X  a4  U R%                  U5        U R                  X#5        U R'                  U5        gUR                  S:X  ax  U R'                  U5        U R                  U5        U R)                  U5        U R                  X#5        U R+                  U5        U R                  U5        U R%                  U5        gUR                  S:X  a<  U R                  U5        U R#                  [         S-  X#5        U R                  U5        gUR                  S:X  a?  U R                  U5        U R#                  S[         -  S-  X#5        U R                  U5        g[-        SU S35      e)zApply a controlled version of ``gate`` to the circuit.

This implements multi-control operations for every gate in
``EFFICIENTLY_CONTROLLED_GATES``.

r   r   r   F)use_basis_gatesr   	noancilla)moderZ   r   r   )MCPhaseGateNr   r   r   r   r4   r   r   r   r   r      zGate z not in supported basis.)rC   r/   mcxmcrxr8   rB   r'   rF   mcrymcrzqiskit.circuit.libraryr]   appendr   cpcur   rD   sdgsttdgr   )	circuitrQ   controlsr0   r   r]   thetaphilambs	            r)   r<   r<      s1    (mOyyCH%	d	OO  #--44Q7!	 	 	
 
d	OO  #--44Q7! 	 	
 
d	OO  #--44Q7!	 	 	
 
c	6A8QK6("	
 
d	QK6!9+%1I	
 
d			&)QK6!9+%1I	
 			&)	c	;;DazcQh

4!f5

5tQVDrcAg~$"q&.UfeLdai$)	   !qDF3TVUKUfeLSFEJSZ1,hqrlHQKH	c			&H%		&	c	FH%		&	c			&		&		&H%F		&F	d			&BFH-		&	f			&AFQJ1		&U4&(@ABBr+   c                    [        U S5      (       a  U R                  b  U R                  $ [        U R                  5      n[	        XR
                  S9nUR                  X5        U$ )z,Converts a gate instance to a QuantumCircuitr8   r.   )hasattrr8   r   r:   r   r/   rd   )r'   qrqcs      r)   _gate_to_circuitrt   =  sU    y,''I,@,@,L###	--	.B		0BIIiIr+   c                    [        U 5      n[        [        [        US9[	        [        US9/5      nUR                  U5      nUR                  5       $ )z7Unrolls a gate, possibly composite, to the target basisr2   )target_basis)rt   r   r	   selr   runto_gate)r'   r3   circpmopqcs        r)   r=   r=   H  sJ    I&D	#C[ACk:	

B 66$<D<<>r+   )
r'   Gate | ControlledGater   intr    
str | Noner!   str | int | Nonereturnr   )r   NN)
r'   r}   r   z
int | Noner    r   r!   r   r   r   )__doc__
__future__r   mathr   qiskit.circuit.exceptionsr   rc   r   qiskit.transpilerr   qiskit.transpiler.passes.basisr   r	   "qiskit.circuit.equivalence_libraryr
   rw    r   r   r   r   _utilsr   r;   r*   r$   r<   rt   r=    r+   r)   <module>r      s    - "  2 . ) S O C C &  "*$** * !	*
 *^ #$#'	s$ss s !	s
 sleCP
r+   