
    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  SS	KJr  SS
KJr   " S S\5      rg)Controlled unitary gate.    )annotationsN)OptionalUnion)CircuitError   )QuantumRegister)QuantumCircuit)Gate)_ctrl_state_to_intc                    ^  \ rS rSrSr     SSS.               SU 4S jjjjr\SU 4S jj5       r\R                  SS j5       r\SS j5       r	\	R                  S	 5       r	\S
 5       r
\
R                  S 5       r
\SS j5       r\R                  SS j5       r\S 5       r\R                  S 5       rSS jrSS jrSSU 4S jjjrSrU =r$ )ControlledGate   r   N)_base_labelc	                  > Uc  SOUR                  5       U l        [        T
U ]  XX4S9  SU l        XPl        [         R                  " U5      U l        SU l        SU l	        Xpl
        Xl        g)a  Create a new ControlledGate. In the new gate the first ``num_ctrl_qubits``
of the gate are the controls.

Args:
    name: The name of the gate.
    num_qubits: The number of qubits the gate acts on.
    params: A list of parameters for the gate.
    label: An optional label for the gate.
    num_ctrl_qubits: Number of control qubits.
    definition: A list of gate rules for implementing this gate. The
        elements of the list are tuples of (:meth:`~qiskit.circuit.Gate`, [qubit_list],
        [clbit_list]).
    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.
    base_gate: Gate object to be controlled.

Raises:
    CircuitError: If ``num_ctrl_qubits`` >= ``num_qubits``.
    CircuitError: ctrl_state < 0 or ctrl_state > 2**num_ctrl_qubits.

Examples:

Create a controlled standard gate and apply it to a circuit.

.. plot::
   :alt: Circuit diagram output by the previous code.
   :include-source:

   from qiskit import QuantumCircuit, QuantumRegister
   from qiskit.circuit.library.standard_gates import HGate

   qr = QuantumRegister(3)
   qc = QuantumCircuit(qr)
   c3h_gate = HGate().control(2)
   qc.append(c3h_gate, qr)
   qc.draw('mpl')

Create a controlled custom gate and apply it to a circuit.

.. plot::
   :alt: Circuit diagram output by the previous code.
   :include-source:

   from qiskit import QuantumCircuit, QuantumRegister
   from qiskit.circuit.library.standard_gates import HGate

   qc1 = QuantumCircuit(2)
   qc1.x(0)
   qc1.h(1)
   custom = qc1.to_gate().control(2)

   qc2 = QuantumCircuit(4)
   qc2.append(custom, [0, 3, 1, 2])
   qc2.draw('mpl')
N)labelr   )copy	base_gatesuper__init___num_ctrl_qubitsnum_ctrl_qubitsdeepcopy
definition_ctrl_state
_open_ctrl
ctrl_state_name)selfname
num_qubitsparamsr   r   r   r   r   r   	__class__s             W/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/controlledgate.pyr   ControlledGate.__init__   sd    L "+!2	8H6? !.--
3$
    c                   > U R                   (       a  U R                  5       nSUl        [        U R                  5      SS R	                  U R
                  5      n[        U R                  S5      n[        U5      n[        USSS2   5       H  u  pVUS:X  d  M  UR                  U5        M      UR                  XSS S9  [        USSS2   5       H  u  pVUS:X  d  M  UR                  U5        M      U$ [        TU ]4  $ )zReturn definition in terms of other basic gates. If the gate has
open controls, as determined from :attr:`ctrl_state`, the returned
definition is conjugated with X without changing the internal
``_definition``.
N   q0)qargs)r   
to_mutabler   binzfillr   r	   r!   r
   	enumeratexappendr   r   )r   closed_gatebit_ctrl_stateqregqc_open_ctrlqindvalr#   s          r$   r   ControlledGate.definitiono   s     ??//+K%)K" 1!"5;;D<P<PQN"4??C8D)$/L&~dd';<	#: NN4( = 7;&~dd';<	#: NN4( =  7%%r&   c                    Xl         g)zsSet controlled gate definition with closed controls.

Args:
    excited_def: The circuit with all closed controls.
N)_definition)r   excited_defs     r$   r   r9      s
     'r&   c                r    U R                   (       a  U R                   SU R                   3$ U R                  $ )a  Get name of gate. If the gate has open controls the gate name
will become:

   <original_name_o<ctrl_state>

where <original_name> is the gate name for the default case of
closed control qubits and <ctrl_state> is the integer value of
the control state for the gate.
_o)r   r   r   r   s    r$   r    ControlledGate.name   s/     ??jj\DOO#455::r&   c                    Xl         g)zmSet the name of the gate. Note the reported name may differ
from the set name if the gate has open controls.
N)r   )r   name_strs     r$   r    r@      s	    
 
r&   c                    U R                   $ )z\Get number of control qubits.

Returns:
    int: The number of control qubits for the gate.
)r   r?   s    r$   r   ControlledGate.num_ctrl_qubits   s     $$$r&   c                    U[        U5      :w  a  [        S5      e[        U5      nU R                  [        U R                  SS5      -
  nUS:  d  X:  a   U R                  c  SOSn[        SU S35      eXl        g)	zSet the number of control qubits.

Args:
    num_ctrl_qubits (int): The number of control qubits.

Raises:
    CircuitError: ``num_ctrl_qubits`` is not an integer in ``[1, num_qubits]``.
z0The number of control qubits must be an integer.r!   r   r   Nz!num_qubits - base_gate.num_qubitsz-The number of control qubits must be in `[1, z]`.)intr   r!   getattrr   r   )r   r   upper_limitlimits       r$   r   rD      s~     c/22QRRo. ooa(PPQ/"?$(NN$:L@cE!NugUXYZZ /r&   c                    U R                   $ )z:Return the control state of the gate as a decimal integer.)r   r?   s    r$   r   ControlledGate.ctrl_state   s     r&   c                    [        XR                  5      U l        U R                  SU R                  -  S-
  :  U l        g)zSet the control state of this gate.

Args:
    ctrl_state: The control state of the gate.

Raises:
    CircuitError: ctrl_state is invalid.
r(   r   N)r   r   r   r   r   )r   r   s     r$   r   rK      s7     .j:N:NO//At/C/C,Ca,GGr&   c                f    U R                   (       a  U R                   R                  $ [        S5      e)zGet parameters from base_gate.

Returns:
    list: List of gate parameters.

Raises:
    CircuitError: Controlled gate does not define a base gate
?Controlled gate does not define base gate for extracting params)r   r"   r   r?   s    r$   r"   ControlledGate.params   s'     >>>>(((`aar&   c                    U R                   (       a?  U R                   R                  (       a  XR                   l        gU(       a  [        S5      eg[        S5      e)zSet base gate parameters.

Args:
    parameters (list): The list of parameters to set.

Raises:
    CircuitError: If controlled gate does not define a base gate.
z,cannot set parameters on immutable base gaterN   N)r   mutabler"   r   )r   
parameterss     r$   r"   rO      sD     >>~~%%(2%"#QRR  `aar&   c                    [         R                   " U 5      nU R                  R                  5       Ul        U R                  (       a&  [         R                  " U R                  U5      Ul        U$ N)r   r   r;   r   )r   memocpys      r$   __deepcopy__ControlledGate.__deepcopy__   sI    iio++-"mmD,<,<dCCO
r&   c                   [        U[        5      =(       a    U R                  UR                  :H  =(       a    U R                  UR                  :H  =(       ay    U R                  UR                  :H  =(       aY    U R
                  UR
                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ rT   )
isinstancer   r   r   r   r!   
num_clbitsr   )r   others     r$   __eq__ControlledGate.__eq__   s    un- 4$$(=(==45#3#334 %//14 5#3#33	4
 5#3#334 5#3#33	
r&   c                   > U(       d>  U R                   R                  5       R                  U R                  U R                  S9nU$ [
        TU ]  US9nU$ )z5Invert this gate by calling inverse on the base gate.)r   )	annotated)r   inversecontrolr   r   r   )r   r`   inverse_gater#   s      r$   ra   ControlledGate.inverse
  sW    >>113;;$$ < L
  !7?Y??Lr&   )	r   r;   r   r   r   r   r   r   r   )Nr   NNN)r    strr!   rF   r"   listr   zOptional[str]r   zOptional[int]r   zOptional['QuantumCircuit']r   zOptional[Union[int, str]]r   zOptional[Gate])returnr
   )r<   z'QuantumCircuit')rg   re   )rg   rF   )r   zUnion[int, str, None]rT   )rg   bool)F)r`   rh   rg   z''ControlledGate' | 'AnnotatedOperation')__name__
__module____qualname____firstlineno____doc__r   propertyr   setterr    r   r   r"   rW   r]   ra   __static_attributes____classcell__)r#   s   @r$   r   r      s   "  $)*1504$(N NN N 	N
 N 'N /N .N "N N` & &. ' '   
[[  % % 0 0(     
H 
H b b ]]b b"	
 r&   r   )rm   
__future__r   r   typingr   r   qiskit.circuit.exceptionsr    r	   quantumcircuitr
   gater   _utilsr   r    r&   r$   <module>rz      s2     "  " 2  *  &vT vr&   