
    z	i/                     \    S r SSKrSSKrSSKJr  SSKJr  SSKJr  S
S jr	S r
S rS
S	 jrg)z6
This module contains utility functions for circuits.
    N)_numpy_compat)QiskitError)CircuitErrorc                 ~   [        [        R                  " U R                  S   5      5      nSU-  n[        R
                  " S/S//SUS-
  /5      nUc  US-
  nO![        U[        5      (       a  [        US5      n[        U[         5      (       a  SUs=::  a  U:  d  O  [        S5      eO[        S5      e[        R                  " [        R                  " XR5      5      n[        R                  " [        R                  " SU-  5      [        R                  " U5      U-
  5      [        R                  " X5      -   nU$ )a  
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         z&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           O/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/_utils.py_compute_control_matrixr      s   2 TYYx~~a012J/!HqcA3Z!X\):;I\
	J	$	$Q'
*c""J))FGG * ABB

5::i<=Izz%))AzM2EIIh4G)4STW\WaWaX H O    c                    Sn[        U [        5      (       a   [        U 5      U:X  d   e[        U S5      n [        U [        5      (       a"  SU s=::  a	  SU-  :  a  O  OU nU$ [        S5      eU c
  SU-  S-
  nU$ [        S[        U 5       35      e! [         a  n[        SU -   5      UeSnAf[         a  n[        S5      UeSnAff = f)	a'  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Z+J
 *c""
/Q//'N  DEE		O+a/  B4
CSBTUVV  	T=
JKQSS 	`VW]__	`s#   B 
CB**C7CCc                    ^^ [         R                  " U [         R                  S9mTR                  SS9  S[        R
                  4U4S jjmU4S jnU$ )zClass 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                 P   > Uc  TR                   OUn[        R                  " TXS9$ Nr*   copy)r*   r   array)_selfr*   r0   nonwritables      r   	__array__"with_gate_array.<locals>.__array__m   s%    %*]!!{{;e??r   c                 N   > [        U S5      (       a  [        S5      eTU l        U $ Nr4   zBRefusing to decorate a class that already has '__array__' defined.hasattrRuntimeErrorr4   clsr4   s    r   	decorator"with_gate_array.<locals>.decoratorq   (    3$$cdd!
r   )r   r1   
complex128setflagsr   COPY_ONLY_IF_NEEDED)
base_arrayr=   r4   r3   s     @@r   with_gate_arrayrD   g   sJ     ++j0@0@AKu%#-*K*K @ r   c                 h  ^^^^ [         R                  " U [         R                  S9mUU4S jnUc=  [        ST-  5       Vs/ s H
  oC" U5      PM     snmS[        R
                  4U4S jjmO3U Vs0 s H  oDU" U5      _M     snmS[        R
                  4UUU4S jjmU4S jnU$ s  snf s  snf )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                    > [         R                  " [        TTU 5      [         R                  S9nUR	                  SS9  U$ )Nr)   Fr+   )r   asarrayr   r@   rA   )stateoutbaser   s     r   matrix_for_control_state<with_controlled_gate_array.<locals>.matrix_for_control_state   s;    mm#D/5A""
 	5!
r   Nr   c                 n   > TU R                      nUc  UR                  OUn[        R                  " X1US9$ r.   )r   r*   r   r1   )selfr*   r0   arrnonwritabless       r   r4   -with_controlled_gate_array.<locals>.__array__   s1    t/C!&CIIEE;;sd;;r   c                 &  > TR                  U R                  5      =nb&  Uc  UR                  OUn[        R                  " X1US9$ USL a  U[
        R                  La  [        S5      e[        R                  " [        TTU R                  5      US9$ )Nr/   Fz,could not produce matrix without calculationr)   )
getr   r*   r   r1   r   rB   r"   rG   r   )rN   r*   r0   rO   rJ   rP   r   s       r   r4   rQ      s    #''88E%*]		{{3$??u}]-N-N!N !OPP=='otOW\ r   c                 N   > [        U S5      (       a  [        S5      eTU l        U $ r7   r8   r;   s    r   r=   -with_controlled_gate_array.<locals>.decorator   r?   r   )r   rG   r@   ranger   rB   )	rC   r   cached_statesrK   rH   r=   r4   rJ   rP   s	    `    @@@r   with_controlled_gate_arrayrX   z   s     ==5+;+;<D EJ1oK]E^_E^E07E^_"&]-N-N 	< 	< MZZM57>>MZ"&]-N-N 		 		 7 ` [s   B*3B/)N)__doc__r
   r   qiskitr   qiskit.exceptionsr   qiskit.circuit.exceptionsr   r   r'   rD   rX    r   r   <module>r^      s1        ) 2)X F&-r   