§
    ¹³Ùg—&  ã                  ó†   — d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ  G d„ de¦  «        ZdS )úControlled unitary gate.é    )ÚannotationsN)ÚOptionalÚUnion)ÚCircuitErroré   )ÚQuantumCircuit)ÚGate)ÚQuantumRegister)Ú_ctrl_state_to_intc                  ót  ‡ — e Zd ZdZ	 	 	 	 	 	 	 d,ddœd-ˆ fd„Zed.ˆ fd„¦   «         Zej        d/d„¦   «         Zed0d„¦   «         Zej        d„ ¦   «         Zed„ ¦   «         Z	e	j        d„ ¦   «         Z	ed1d „¦   «         Z
e
j        d2d"„¦   «         Z
ed#„ ¦   «         Zej        d$„ ¦   «         Zd3d%„Zd4d'„Zd5d6ˆ fd+„Zˆ xZS )7ÚControlledGater   Nr   )Ú_base_labelÚnameÚstrÚ
num_qubitsÚintÚparamsÚlistÚlabelúOptional[str]Únum_ctrl_qubitsúOptional[int]Ú
definitionúOptional['QuantumCircuit']Ú
ctrl_stateúOptional[Union[int, str]]Ú	base_gateúOptional[Gate]c               ó  •— |€dn|                      ¦   «         | _        t          ¦   «                              |||||	|
¬¦  «         d| _        || _        t          j        |¦  «        | _        d| _        d| _	        || _
        || _        dS )a7  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)r   ÚdurationÚunitr   )Úcopyr   ÚsuperÚ__init__Ú_num_ctrl_qubitsr   Údeepcopyr   Ú_ctrl_stateÚ
_open_ctrlr   Ú_name)Úselfr   r   r   r   r   r   r   r   r!   r"   r   Ú	__class__s               €úS/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/controlledgate.pyr%   zControlledGate.__init__   sˆ   ø€ ðP "+Ð!2˜˜¸	¿ºÑ8HÔ8HˆŒÝ‰Œ×Ò˜˜z¨6¸ÈÐX\ÐÑ]Ô]Ð]Ø !ˆÔØ.ˆÔÝœ-¨
Ñ3Ô3ˆŒØˆÔØˆŒØ$ˆŒØˆŒ
ˆ
ˆ
ó    Úreturnr	   c                óF  •— | j         �r|                      ¦   «         }d|_        t          | j        ¦  «        dd…                              | j        ¦  «        }t          | j        d¦  «        }t          |¦  «        }t          |ddd…         ¦  «        D ] \  }}|dk    r| 
                    |¦  «         Œ!|                     ||dd…         ¬¦  «         t          |ddd…         ¦  «        D ] \  }}|dk    r| 
                    |¦  «         Œ!|S t          ¦   «         j        S )zóReturn 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   zControlledGate.definitionq   s%  ø€ ð Œ?ñ 	&ØŸ/š/Ñ+Ô+ˆKØ%)ˆKÔ"Ý  ¤Ñ1Ô1°!°"°"Ô5×;Ò;¸DÔ<PÑQÔQˆNÝ" 4¤?°CÑ8Ô8ˆDÝ)¨$Ñ/Ô/ˆLÝ& ~°d°d¸°dÔ';Ñ<Ô<ð )ð )‘	��cØ˜#’:�:Ø —N’N 4Ñ(Ô(Ð(øØ×Ò °4¸¸¸´7ÐÑ;Ô;Ð;Ý& ~°d°d¸°dÔ';Ñ<Ô<ð )ð )‘	��cØ˜#’:�:Ø —N’N 4Ñ(Ô(Ð(øØÐå‘7”7Ô%Ð%r.   Úexcited_defú'QuantumCircuit'c                ó   — || _         dS )z‹Set controlled gate definition with closed controls.

        Args:
            excited_def: The circuit with all closed controls.
        N)Ú_definition)r+   rB   s     r-   r   zControlledGate.definition‰   s   € ð 'ˆÔÐÐr.   c                ó@   — | j         r| j        › d| j        › �S | j        S )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   zControlledGate.name’   s.   € ð Œ?ð 	Ø”jÐ5Ð5 D¤OÐ5Ð5Ð5à”:Ðr.   c                ó   — || _         dS )z}Set 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   zControlledGate.name¢   s   € ð
 ˆŒ
ˆ
ˆ
r.   c                ó   — | j         S )ztGet number of control qubits.

        Returns:
            int: The number of control qubits for the gate.
        )r&   rH   s    r-   r   zControlledGate.num_ctrl_qubits©   s   € ð Ô$Ð$r.   c                ó  — |t          |¦  «        k    rt          d¦  «        ‚t          |¦  «        }| j        t          | j        dd¦  «        z
  }|dk     s||k    r| j        €dnd}t          d|› d�¦  «        ‚|| _        dS )	zãSet 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]`.)r   r   r   Úgetattrr   r&   )r+   r   Úupper_limitÚlimits       r-   r   zControlledGate.num_ctrl_qubits²   s�   € ð �c /Ñ2Ô2Ò2Ð2ÝÐQÑRÔRÐRÝ˜oÑ.Ô.ˆð ”o­°´ÀÈaÑ(PÔ(PÑPˆØ˜QÒÐ /°KÒ"?Ð"?Ø$(¤NÐ$:�L�LÐ@cˆEÝÐYÈuÐYÐYÐYÑZÔZÐZØ /ˆÔÐÐr.   c                ó   — | j         S )z:Return the control state of the gate as a decimal integer.)r(   rH   s    r-   r   zControlledGate.ctrl_stateÇ   s   € ð ÔÐr.   úUnion[int, str, None]c                óp   — t          || j        ¦  «        | _        | j        d| j        z  dz
  k     | _        dS )z´Set the control state of this gate.

        Args:
            ctrl_state: The control state of the gate.

        Raises:
            CircuitError: ctrl_state is invalid.
        r1   r   N)r   r   r(   r   r)   )r+   r   s     r-   r   zControlledGate.ctrl_stateÌ   s8   € õ .¨j¸$Ô:NÑOÔOˆÔØœ/¨A¨tÔ/CÑ,CÀaÑ,GÒGˆŒˆˆr.   c                óF   — | j         r| j         j        S t          d¦  «        ‚)z»Get 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   rH   s    r-   r   zControlledGate.paramsÙ   s)   € ð Œ>ð 	bØ”>Ô(Ð(åÐ`ÑaÔaÐar.   c                óˆ   — | j         r-| j         j        r|| j         _        dS |rt          d¦  «        ‚dS t          d¦  «        ‚)zÊSet 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 gaterT   N)r   Úmutabler   r   )r+   Ú
parameterss     r-   r   zControlledGate.paramsè   sb   € ð Œ>ð 	bØŒ~Ô%ð SØ(2�”Ô%Ð%Ð%Øð SÝ"Ð#QÑRÔRÐRðSð Sõ Ð`ÑaÔaÐar.   c                ó¶   — t          j         | ¦  «        }| j                              ¦   «         |_        | j        rt          j        | j        |¦  «        |_        |S ©N)r#   r   rE   r'   )r+   ÚmemoÚcpys      r-   Ú__deepcopy__zControlledGate.__deepcopy__ú   sK   € ÝŒi˜‰oŒoˆØœ×+Ò+Ñ-Ô-ˆŒØÔð 	DÝ"œm¨DÔ,<¸dÑCÔCˆCŒOØˆ
r.   Úboolc                óì   — t          |t          ¦  «        o_| j        |j        k    oO| j        |j        k    o?| j        |j        k    o/| j        |j        k    o| j        |j        k    o| j        |j        k    S rY   )Ú
isinstancer   r   r   r   r   Ú
num_clbitsr   )r+   Úothers     r-   Ú__eq__zControlledGate.__eq__  sˆ   € å�u�nÑ-Ô-ð 4ØÔ$¨Ô(=Ò=ð4à” 5Ô#3Ò3ð4ð ” %¤/Ò1ð4ð ” 5Ô#3Ò3ð	4ð
 ” 5Ô#3Ò3ð4ð ” 5Ô#3Ò3ð	
r.   FÚ	annotatedú''ControlledGate' | 'AnnotatedOperation'c                óÂ   •— |s9| j                              ¦   «                              | j        | j        ¬¦  «        }n"t          ¦   «                              |¬¦  «        }|S )z5Invert this gate by calling inverse on the base gate.)r   )rc   )r   ÚinverseÚcontrolr   r   r$   )r+   rc   Úinverse_gater,   s      €r-   rf   zControlledGate.inverse  s_   ø€ àð 	@Øœ>×1Ò1Ñ3Ô3×;Ò;ØÔ$°´ð <ñ ô ˆLˆLõ !™7œ7Ÿ?š?°Y˜?Ñ?Ô?ˆLØÐr.   )Nr   NNNNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r/   r	   )rB   rC   )r/   r   )r/   r   )r   rQ   rY   )r/   r]   )F)rc   r]   r/   rd   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   Úpropertyr   Úsetterr   r   r   r   r\   rb   rf   Ú__classcell__)r,   s   @r-   r   r      s  ø€ € € € € Ø"Ð"ð  $Ø)*Ø15Ø04Ø$(ØØðPð ðPð Pð Pð Pð Pð Pð Pð Pðd ð&ð &ð &ð &ð &ñ „Xð&ð. Ôð'ð 'ð 'ñ Ôð'ð ðð ð ñ „Xðð 
„[ðð ñ „[ðð ð%ð %ñ „Xð%ð Ôð0ð 0ñ Ôð0ð( ð ð  ð  ñ „Xð ð Ôð
Hð 
Hð 
Hñ Ôð
Hð ðbð bñ „Xðbð „]ðbð bñ „]ðbð"ð ð ð ð	
ð 	
ð 	
ð 	
ðð ð ð ð ð ð ð ð ð ð r.   r   )rl   Ú
__future__r   r#   Útypingr   r   Úqiskit.circuit.exceptionsr   Úquantumcircuitr	   Úgater
   Úquantumregisterr   Ú_utilsr   r   © r.   r-   ú<module>rx      sÚ   ðð Ð à "Ð "Ð "Ð "Ð "Ð "Ø €€€Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "à 2Ð 2Ð 2Ð 2Ð 2Ð 2ð +Ð *Ð *Ð *Ð *Ð *Ø Ð Ð Ð Ð Ð Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø &Ð &Ð &Ð &Ð &Ð &ðxð xð xð xð x�Tñ xô xð xð xð xr.   