§
    ¹³Ùg/  ã                   óZ   — d Z ddlZddlZddlmZ ddlmZ ddlmZ d
d„Z	d„ Z
d„ Zd
d	„ZdS )z6
This module contains utility functions for circuits.
é    N)Ú_numpy_compat)ÚQiskitError)ÚCircuitErrorc                 ó|  — t          t          j        | j        d         ¦  «        ¦  «        }d|z  }t	          j        dgdggd|dz
  g¦  «        }|€|dz
  }n%t          |t          ¦  «        rt          |d¦  «        }t          |t           ¦  «        rd|cxk    r|k     sn t          d¦  «        ‚nt          d¦  «        ‚t	          j	        t	          j
        ||¦  «        ¦  «        }t	          j        t	          j        d|z  ¦  «        t	          j        |¦  «        |z
  ¦  «        t	          j        | |¦  «        z   }|S )aP  
    Compute the controlled version of the input matrix with qiskit ordering.
    This function computes the controlled unitary with :math:`n` control qubits
    and :math:`m` target qubits,

    .. math::

        V_n^j(U_{2^m}) = (U_{2^m} \otimes |j\rangle\!\langle j|) +
                         (I_{2^m} \otimes (I_{2^n} - |j\rangle\!\langle j|)).

    where :math:`|j\rangle \in \mathcal{H}^{2^n}` is the control state.

    Args:
        base_mat (ndarray): unitary to be controlled
        num_ctrl_qubits (int): number of controls for new unitary
        ctrl_state (int or str or None): The control state in decimal or as
            a bitstring (e.g. '111'). If None, use 2**num_ctrl_qubits-1.

    Returns:
        ndarray: controlled version of base matrix.

    Raises:
        QiskitError: unrecognized mode or invalid ctrl_state
    r   é   é   Nz&Invalid control state value specified.z%Invalid control state type specified.)ÚintÚmathÚlog2ÚshapeÚnumpyÚrepeatÚ
isinstanceÚstrr   ÚdiagÚrollÚkronÚeye)Úbase_matÚnum_ctrl_qubitsÚ
ctrl_stateÚ
num_targetÚctrl_dimÚ	ctrl_grndÚ	ctrl_projÚfull_mats           úK/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/_utils.pyÚ_compute_control_matrixr      sC  € õ2 •T”Y˜xœ~¨aÔ0Ñ1Ô1Ñ2Ô2€JØ�/Ñ!€HÝ”˜q˜c A 3˜Z¨!¨X¸©\Ð):Ñ;Ô;€IØÐØ ‘\ˆ
ˆ
Ý	�J¥Ñ	$Ô	$ð (Ý˜ QÑ'Ô'ˆ
Ý�*�cÑ"Ô"ð CØ�JÐ)Ð)Ò)Ð) Ò)Ð)Ð)Ð)ÝÐFÑGÔGÐGð *õ ÐAÑBÔBÐBÝ”
�5œ: i°Ñ<Ô<Ñ=Ô=€IÝŒz�%œ) A z¡MÑ2Ô2µE´I¸hÑ4GÔ4GÈ)Ñ4SÑTÔTÕW\ÔWaØ�)ñXô Xñ €Hð €Oó    c                 óÎ  — d}t          | t          ¦  «        ri	 t          | ¦  «        |k    sJ ‚t          | d¦  «        } nB# t          $ r}t          d| z   ¦  «        |‚d}~wt          $ r}t          d¦  «        |‚d}~ww xY wt          | t          ¦  «        r%d| cxk    r	d|z  k     rn n| }n9t          d¦  «        ‚| €	d|z  dz
  }nt          dt          | ¦  «        › �¦  «        ‚|S )	aK  Convert ctrl_state to int.

    Args:
        ctrl_state (None, str, int): ctrl_state. If None, set to 2**num_ctrl_qubits-1.
            If str, convert to int. If int, pass.
        num_ctrl_qubits (int): The number of control qubits.

    Return:
        int: ctrl_state

    Raises:
        CircuitError: invalid ctrl_state
    Nr   zinvalid control bit string: z5invalid control bit string: length != num_ctrl_qubitsr   z#invalid control state specificationr   z%invalid control state specification: )r   r   Úlenr	   Ú
ValueErrorr   ÚAssertionErrorÚrepr)r   r   Úctrl_state_stdÚexs       r   Ú_ctrl_state_to_intr'   D   s;  € ð €NÝ�*�cÑ"Ô"ð `ð	`Ý�z‘?”? oÒ5Ð5Ð5Ð5Ý˜Z¨Ñ+Ô+ˆJˆJøÝð 	Tð 	Tð 	TÝÐ=À
ÑJÑKÔKÐQSÐSøøøøÝð 	`ð 	`ð 	`ÝÐVÑWÔWÐ]_Ð_øøøøð	`øøøå�*�cÑ"Ô"ð WØ�
Ð/Ð/Ò/Ð/˜Q Ñ/Ò/Ð/Ð/Ð/Ð/Ø'ˆNˆNåÐDÑEÔEÐEØ	Ð	Ø˜OÑ+¨aÑ/ˆˆåÐUÅ4È
ÑCSÔCSÐUÐUÑVÔVÐVØÐs!   ™%? ¿
A>Á	AÁA>Á)A9Á9A>c                 ó¤   ‡‡— t          j        | t           j        ¬¦  «        Š‰                     d¬¦  «         dt          j        fˆfd„	Šˆfd„}|S )z±Class decorator that adds an ``__array__`` method to a :class:`.Gate` instance that returns a
    singleton nonwritable view onto the complex matrix described by ``base_array``.©ÚdtypeF©ÚwriteNc                 óH   •— |€‰j         n|}t          j        ‰||¬¦  «        S ©N©r*   Úcopy)r*   r   Úarray)Ú_selfr*   r0   Únonwritables      €r   Ú	__array__z"with_gate_array.<locals>.__array__m   s+   ø€ Ø%* ]�Ô!Ð!¸ˆÝŒ{˜;¨e¸$Ð?Ñ?Ô?Ð?r   c                 óT   •— t          | d¦  «        rt          d¦  «        ‚‰| _        | S ©Nr4   zBRefusing to decorate a class that already has '__array__' defined.©ÚhasattrÚRuntimeErrorr4   ©Úclsr4   s    €r   Ú	decoratorz"with_gate_array.<locals>.decoratorq   ó1   ø€ Ý�3˜Ñ$Ô$ð 	eÝÐcÑdÔdÐdØ!ˆŒØˆ
r   )r   r1   Ú
complex128Úsetflagsr   ÚCOPY_ONLY_IF_NEEDED)Ú
base_arrayr<   r4   r3   s     @@r   Úwith_gate_arrayrB   g   s�   øø€ õ ”+˜jµÔ0@ÐAÑAÔA€KØ×Ò˜uÐÑ%Ô%Ð%à#­-Ô*Kð @ð @ð @ð @ð @ð @ðð ð ð ð ð Ðr   c                 ó  ‡‡‡‡‡— t          j        | t           j        ¬¦  «        Šˆˆfd„Š|€1ˆfd„t          d‰z  ¦  «        D ¦   «         Šdt          j        fˆfd„	Šn"ˆfd„|D ¦   «         Šdt          j        fˆˆˆfd„	Šˆfd	„}|S )
a”  Class decorator that adds an ``__array__`` method to a :class:`.ControlledGate` instance that
    returns singleton nonwritable views onto a relevant precomputed complex matrix for the given
    control state.

    If ``cached_states`` is not given, then all possible control states are precomputed.  If it is
    given, it should be an iterable of integers, and only these control states will be cached.r)   c                 ó’   •— t          j        t          ‰‰| ¦  «        t           j        ¬¦  «        }|                     d¬¦  «         |S )Nr)   Fr+   )r   Úasarrayr   r>   r?   )ÚstateÚoutÚbaser   s     €€r   Úmatrix_for_control_statez<with_controlled_gate_array.<locals>.matrix_for_control_stateƒ   sI   ø€ ÝŒmÝ# D¨/¸5ÑAÔAÝÔ"ð
ñ 
ô 
ˆð 	�Š˜5ˆÑ!Ô!Ð!Øˆ
r   Nc                 ó&   •— g | ]} ‰|¦  «        ‘ŒS © rK   ©Ú.0rF   rI   s     €r   ú
<listcomp>z.with_controlled_gate_array.<locals>.<listcomp>Œ   s%   ø€ Ð_Ð_Ð_¸EÐ0Ð0°Ñ7Ô7Ð_Ð_Ð_r   r   c                 ób   •— ‰| j                  }|€|j        n|}t          j        |||¬¦  «        S r.   )r   r*   r   r1   )Úselfr*   r0   ÚarrÚnonwritabless       €r   r4   z-with_controlled_gate_array.<locals>.__array__Ž   s5   ø€ Ø˜tœÔ/ˆCØ!& �C”I�I°EˆEÝ”;˜s¨%°dÐ;Ñ;Ô;Ð;r   c                 ó(   •— i | ]}| ‰|¦  «        “ŒS rK   rK   rL   s     €r   ú
<dictcomp>z.with_controlled_gate_array.<locals>.<dictcomp>”   s'   ø€ ÐZÐZÐZÀ5˜Ð7Ð7¸Ñ>Ô>ÐZÐZÐZr   c                 ó  •— ‰                      | j        ¦  «        x}�"|€|j        n|}t          j        |||¬¦  «        S |du r|t
          j        urt          d¦  «        ‚t          j        t          ‰‰| j        ¦  «        |¬¦  «        S )Nr/   Fz,could not produce matrix without calculationr)   )
Úgetr   r*   r   r1   r   r@   r"   rE   r   )rP   r*   r0   rQ   rH   rR   r   s       €€€r   r4   z-with_controlled_gate_array.<locals>.__array__–   s–   ø€ Ø#×'Ò'¨¬Ñ8Ô8Ð8�ÐEØ%* ]˜œ	˜	¸�Ý”{ 3¨e¸$Ð?Ñ?Ô?Ð?à�uˆ}ˆ} ­]Ô-NÐ!NÐ!NÝ Ð!OÑPÔPÐPÝ”=Ý'¨¨o¸t¼ÑOÔOÐW\ðñ ô ð r   c                 óT   •— t          | d¦  «        rt          d¦  «        ‚‰| _        | S r6   r7   r:   s    €r   r<   z-with_controlled_gate_array.<locals>.decorator¡   r=   r   )r   rE   r>   Úranger   r@   )rA   r   Úcached_statesr<   r4   rH   rI   rR   s    `  @@@@r   Úwith_controlled_gate_arrayrZ   z   s  øøøøø€ õ Œ=˜­5Ô+;Ð<Ñ<Ô<€Dðð ð ð ð ð ð ÐØ_Ð_Ð_Ð_ÅUÈ1ÈoÑK]ÑE^ÔE^Ð_Ñ_Ô_ˆà"&­]Ô-Nð 	<ð 	<ð 	<ð 	<ð 	<ð 	<ð 	<ð [ÐZÐZÐZÈMÐZÑZÔZˆà"&­]Ô-Nð 		ð 		ð 		ð 		ð 		ð 		ð 		ð 		ðð ð ð ð ð Ðr   )N)Ú__doc__r
   r   Úqiskitr   Úqiskit.exceptionsr   Úqiskit.circuit.exceptionsr   r   r'   rB   rZ   rK   r   r   ú<module>r_      s«   ððð ð €€€Ø €€€à  Ð  Ð  Ð  Ð  Ð  Ø )Ð )Ð )Ð )Ð )Ð )Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2ð)ð )ð )ð )ðX ð  ð  ðFð ð ð&-ð -ð -ð -ð -ð -r   