
    {	iN                     ^    S r SSKJr  SSKrSSKJr  SSKJr   " S S\5      r	 " S	 S
5      r
g)z4
State class that handles internal C++ state safely
    )EnumN)AerStateWrapper   )AerErrorc                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)
_STATE         r             N)__name__
__module____qualname____firstlineno__INITIALIZING	ALLOCATEDMAPPEDMOVEDRELEASEDCLOSED__static_attributes__r       b/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_aer/quantum_info/states/aer_state.pyr   r      s     LIFEHFr   r   c                      \ rS rSrSr0 r\S 5       r\S 5       r\S 5       r	S r
S rS rS	 rS
 rS rS rS rS rS rS rS rS rS rS<S jrS rS rS=S jrS rS rS rS r\ S 5       r!S r"S r#S  r$S! r%S" r&S# r'S$ r(S% r)S& r*S' r+S( r,S) r-S* r.S+ r/S, r0S- r1S. r2S/ r3S0 r4S1 r5S2 r6S3 r7S4 r8S5 r9S6 r:S7 r;S=S8 jr<S>S9 jr=S>S: jr>S;r?g)?AerState    z&Internal class to access state of Aer.c                 <    U [         R                  [        U 5      '   g Nr   _data_in_useiddatas    r   _in_useAerState._in_use%   s    *.bh'r   c                 8    [         R                  [        U 5      	 g r!   r"   r%   s    r   _not_in_useAerState._not_in_use)   s    !!"T(+r   c                 :    [        U 5      [        R                  ;   $ r!   )r$   r   r#   r%   s    r   
_is_in_useAerState._is_in_use-   s    $x80000r   c                     [         R                  U l        [        5       U l        SU l        SU l        SU l        0 U l        SU l	        UR                  5        H  u  p#U R                  X#5        M     g)z+State that handles cpp quantum state safelyN)r   r   _stater   _native_state
_init_data_moved_data_last_qubit_configs_methoditems	configure)selfkwargskeyvalues       r   __init__AerState.__init__1   s]    )),. ,,.JCNN3& )r   c                 x    U R                  5         [        R                  U l        SU l        SU l        SU l        g)zRenew AerState for reuseNr0   )_assert_closedr   r   r1   r3   r4   r5   r:   s    r   renewAerState.renew?   s2    ))r   c                 V    U R                   [        R                  :w  a  [        S5      eg )Nz!AerState was already initialized.)r1   r   r   r   rB   s    r   _assert_initializingAerState._assert_initializingG   s%    ;;&--->?? .r   c                     U R                   [        R                  :X  a  [        S5      eU R                   [        R                  :X  a  [        S5      eg )N&AerState has not been initialized yet.!AerState has already been closed.)r1   r   r   r   r   rB   s    r   $_assert_allocated_or_mapped_or_moved-AerState._assert_allocated_or_mapped_or_movedK   sA    ;;&---CDD;;&--'>?? (r   c                     U R                   [        R                  :X  a  [        S5      eU R                   [        R                  :X  a  [        S5      eU R                   [        R
                  :X  a  [        S5      eg )NrI   z AerState has already been moved.rJ   )r1   r   r   r   r   r   rB   s    r   _assert_allocated_or_mapped$AerState._assert_allocated_or_mappedQ   s]    ;;&---CDD;;&,,&=>>;;&--'>?? (r   c                     U R                   [        R                  :X  a  [        S5      eU R                   [        R                  :X  a  [        S5      eU R                   [        R
                  :X  a  [        S5      eg )NrI   z AerState has not been moved yet.rJ   )r1   r   r   r   r   r   rB   s    r   _assert_mapped_or_moved AerState._assert_mapped_or_movedY   s_    ;;&---CDD;;&***=>>;;&--'>?? (r   c                 V    U R                   [        R                  :w  a  [        S5      eg )NzAerState is not closed.)r1   r   r   r   rB   s    r   rA   AerState._assert_closeda   s#    ;;&--'455 (r   c                     U R                   [        R                  :w  a  [        S5      e[        R                  U l         g )Nz>unexpected state transition: {self._state}->{_STATE.ALLOCATED})r1   r   r   r   r   rB   s    r   
_allocatedAerState._allocatede   s-    ;;&---[\\&&r   c                     U R                   [        R                  :w  a  [        S5      e[        R                  U l         g )Nz;unexpected state transition: {self._state}->{_STATE.MAPPED})r1   r   r   r   r   rB   s    r   _mappedAerState._mappedj   s+    ;;&---XYYmmr   c                     U R                   [        R                  :w  a  [        S5      e[        R                  U l         g )Nz=unexpected state transition: {self._state}->{_STATE.RELEASED})r1   r   r   r   r   rB   s    r   	_releasedAerState._releasedo   s)    ;;&--'Z[[oor   c                     U R                   [        R                  :w  a  [        S5      e[        R                  U l         g )Nz:unexpected state transition: {self._state}->{_STATE.MOVED})r1   r   r   r   r   rB   s    r   _movedAerState._movedt   s+    ;;&***WXXllr   c                     U R                   [        R                  [        R                  [        R                  4;  a  [        S5      e[        R                  U l         g )Nz;unexpected state transition: {self._state}->{_STATE.CLOSED})r1   r   r   r   r   r   r   rB   s    r   _closedAerState._closedy   s8    ;;v||V]]FOOLLXYYmmr   c                    U R                  5         [        U[        5      (       d  [        S5      e[        U[        5      (       d  [        U5      nX R                  U'   U R
                  R                  X5        US:X  a  X l        gg)z2configure AerState with options of `AerSimulator`.z%AerState is configured with a str keymethodN)rF   
isinstancestrr   r6   r2   r9   r7   )r:   r<   r=   s      r   r9   AerState.configure~   sn    !!##s##BCC%%%JE"c$$S0(? L r   c                 6    U R                   R                  5       $ )zreturn configuration)r6   copyrB   s    r   configurationAerState.configuration   s    }}!!##r   Nc                 F   U R                  5         U R                  (       d  [        S5      eUc+  U R                  R	                  5         U R                  5         g[        U[        R                  5      (       a  U R                  X5      $ [        SUR                   35      e)zinitialize state.zmethod is not configured yet.Tzunsupported init data: )rF   r7   r   r2   
initializerV   rf   npndarray_initialize_with_ndarray	__class__)r:   r&   rj   s      r   rn   AerState.initialize   s    !!#||:;;<))+OObjj))00<<4T^^4DEFFr   c                    [         R                  U5      (       a  U(       d  [        S5      e[        [        R
                  " [        U5      5      5      n[        U5      [        R                  " SU5      :w  a  [        S5      eSnU R                  S:X  a  U R                  R                  X1U5      nO[U R                  S:X  aK  UR                  [        U5      [        U5      4:w  a  [        S5      eU R                  R                  X1U5      nU(       aF  U(       d-  Xl        [         R                  U5        U R                  5         GO6U R!                  5         GO$U R                  R#                  U5        U R                  R%                  5         UR&                  R(                  (       d1  UR&                  R*                  (       d  [        R,                  " U5      nU R                  S:X  a&  U R                  R/                  [1        U5      U5        O[U R                  S:X  a&  U R                  R3                  [1        U5      U5        O%U R                  R/                  [1        U5      U5        U R!                  5         SnUS	-
  U l        U$ )
Nzanother AerState owns this datar   z(length of init data must be power of twoFstatevectordensity_matrixz1shape of init data must be a pair of power of twoTr
   )r   r-   r   intro   log2lenpowerr7   r2   initialize_statevectorshapeinitialize_density_matrixr3   r'   rY   rV   reallocate_qubitsrn   flagsc_contiguousf_contiguousascontiguousarrayapply_initializerangeset_density_matrixr5   )r:   r&   rj   num_of_qubitsinits        r   rq   !AerState._initialize_with_ndarray   s   t$$T<==BGGCI./t9M22EFF<<=(%%<<]RVWD\\--zzc$iT33RSS%%??UYZD"&  &! 00?))+::**4::3J3J++D1||},""33E-4H$O!11""55eM6JDQ""33E-4H$OOOD(1,r   c                     U R                   $ )zreturn method to simulate)r7   rB   s    r   re   AerState.method   s    ||r   c                 v    Uc  U R                   R                  5         gU R                   R                  U5        g)z&initialize seed with a specified valueN)r2   set_random_seedset_seed)r:   r=   s     r   r   AerState.set_seed   s-    =..0''.r   c                    U R                  5         U R                  [        R                  :X  a  U R                  R                  5         U R                  5         U R                  [        R                  :X  a9  U R                  R                  5         [        R                  U R                  5        U R                  R                  5         U R                  5         g)z#Safely release all releated memory.N)rK   r1   r   r   r2   move_to_ndarrayrQ   r   move_to_bufferr   r*   r3   clearrb   rB   s    r   closeAerState.close   s    113;;&***..0$$&;;&--' --/  1  "r   c                    U R                  5         U R                  [        R                  :X  aW  U R                  nU R
                  R                  5         [        R                  U R                  5        U R                  5         U$ U R                  [        R                  :X  a  U R                  nU$ U R                  [        R                  :X  a  U R                  nU$ U R                  S:X  a   U R
                  R                  5       U l        OU R
                  R                  5       U l        U R                  nU R!                  5         U$ )zKmove memory to ndarray if it is allocated, otherwise return mapped ndarray.rv   )rK   r1   r   r   r3   r2   r   r   r*   r\   r   r   r4   r7   move_to_matrixr   r_   )r:   rets     r   r   AerState.move_to_ndarray   s    113;;&--'//C --/  1NN 
 [[FOO+//C 
 [[FLL(""C 
 ||//#'#5#5#D#D#F #'#5#5#E#E#G ""CKKM
r   c                     U R                  5         US::  a  [        SU 35      eU R                  R                  U5      nU[	        U5      S-
     U l        g)zallocate qubits.r   zinvalid number of qubits: r
   N)rF   r   r2   allocate_qubitsry   r5   )r:   r   	allocateds      r   r   AerState.allocate_qubits   sU    !!#A7GHH&&66}E	$S^a%78r   c                     [        US5      (       a  U H  nU R                  U5        M     g US:  d  XR                  :  a  [        SU 35      eg )N__iter__r   zinvalid qubit: index=)hasattr_assert_in_allocated_qubitsr5   r   )r:   qubitqs      r   r   $AerState._assert_in_allocated_qubits  sQ    5*%%003 QY%"2"2225':;; 3r   c                      U R                   S-   $ )z#return a number of allocate qubits.r
   )r5   rB   s    r   
num_qubitsAerState.num_qubits  s     !##r   c                 V    U R                  5         U R                  R                  5       $ )zapply all buffered operations.
Some gate operations are not evaluated immediately.
This method guarantees that all called operations are evaluated.
)rN   r2   flushrB   s    r   r   AerState.flush  s%    
 	((*!!''))r   c                 V    U R                  5         U R                  R                  5       $ )z3return a result of a operation fluhsed in the last.)rK   r2   last_resultrB   s    r   r   AerState.last_result  s#    113!!--//r   c                 Z    U R                  5         U R                  R                  U5        g)zapply global phaseN)rN   r2   apply_global_phase)r:   phases     r   r   AerState.apply_global_phase  s"    ((*--e4r   c                    U R                  5         U R                  U5        [        R                  " U[        S9nUR
                  S   nUR
                  S   n[        [        R                  " U5      5      nX4:w  d	  SU-  U:w  a  [        S5      e[        U5      U:w  a  [        S5      eU R                  R                  X5        g)zapply a unitary gate.dtyper   r
   r   (Input matrix is not an N-qubit operator.)Input matrix and qubits are insonsistent.N)rN   r   ro   arraycomplexr|   rw   rx   r   ry   r2   apply_unitary)r:   qubitsr&   	input_dim
output_dimr   s         r   r   AerState.apply_unitary#  s    ((*((0xxG,JJqM	ZZ]
+,
"amy&@EFFv;*$FGG((6r   c                    U R                  5         U R                  U5        U R                  U5        [        U[        5      (       d  [	        S5      e[        U5      S[        U5      -  :w  a  [	        S5      eU Vs/ s H  n[        R                  " U[        S9PM     nnUS   R                  u  pV[        [        R                  " U5      5      nXV:w  d	  SU-  U:w  a  [	        S5      eUSS  H+  nUR                  US   R                  :w  d  M"  [	        S5      e   [        U5      U:w  a  [	        S	5      eU R                  R                  XU5        gs  snf )
zapply a multiplexer operation.z Input must be a list of ndarray.r   z2Input length must be 2 ** number of control gates.r   r   r   r
   Nr   )rN   r   rf   listr   ry   ro   r   r   r|   rw   rx   r2   apply_multiplexer)r:   control_qubitstarget_qubitsmatsmatr   r   num_target_qubitss           r   r   AerState.apply_multiplexer4  s.   ((*((8((7$%%=>>t9c.112OPP8<=G,= $Q		 23"a):&:i&GEFF8CyyDGMM)IJJ  }!22FGG,,^DQ >s   =#Ec                 f   U R                  5         U R                  U5        [        R                  " U[        S9nUR
                  S   n[        [        R                  " U5      5      nSU-  U:w  a  [        S5      e[        U5      U:w  a  [        S5      eU R                  R                  X5        g)z"apply a diagonal matrix operation.r   r   r   z(Input vector is not an N-qubit operator.z)Input vector and qubits are insonsistent.N)rN   r   ro   r   r   r|   rw   rx   r   ry   r2   apply_diagonal)r:   r   diagr   r   s        r   r   AerState.apply_diagonalL  s    ((*((0xxG,JJqM	+,
j=I%EFFv;*$FGG))&7r   c                 |    U R                  5         U R                  U5        U R                  R                  U5        g)zapply a x operation.N)rN   r   r2   apply_xr:   target_qubits     r   r   AerState.apply_x\  0    ((*((6""<0r   c                     U R                  5         U R                  U5        U R                  U5        U R                  R                  X/5        g)zapply a cx operation.N)rN   r   r2   apply_cxr:   control_qubitr   s      r   r   AerState.apply_cxc  A    ((*((7((6##]$ABr   c                     U R                  5         U R                  U5        U R                  U5        U R                  R                  X/-   5        g)zapply a mcx operation.N)rN   r   r2   	apply_mcxr:   r   r   s      r   r   AerState.apply_mcxk  C    ((*((8((6$$^n%DEr   c                 |    U R                  5         U R                  U5        U R                  R                  U5        g)zapply a y operation.N)rN   r   r2   apply_yr   s     r   r   AerState.apply_ys  r   r   c                     U R                  5         U R                  U5        U R                  U5        U R                  R                  X/5        g)zapply a cy operation.N)rN   r   r2   apply_cyr   s      r   r   AerState.apply_cyz  r   r   c                     U R                  5         U R                  U5        U R                  U5        U R                  R                  X/-   5        g)zapply a mcy operation.N)rN   r   r2   	apply_mcyr   s      r   r   AerState.apply_mcy  r   r   c                 |    U R                  5         U R                  U5        U R                  R                  U5        g)zapply a z operation.N)rN   r   r2   apply_zr   s     r   r   AerState.apply_z  r   r   c                     U R                  5         U R                  U5        U R                  U5        U R                  R                  X/5        g)zapply a cz operation.N)rN   r   r2   apply_czr   s      r   r   AerState.apply_cz  r   r   c                     U R                  5         U R                  U5        U R                  U5        U R                  R                  X/-   5        g)zapply a mcz operation.N)rN   r   r2   	apply_mczr   s      r   r   AerState.apply_mcz  r   r   c                     U R                  5         U R                  U5        U R                  U5        U R                  R                  X/-   U5        g)zapply a mcphase operation.N)rN   r   r2   apply_mcphase)r:   r   r   r   s       r   r   AerState.apply_mcphase  sE    ((*((8((6((.)H%Pr   c                 |    U R                  5         U R                  U5        U R                  R                  U5        g)zapply a h operation.N)rN   r   r2   apply_hr   s     r   r   AerState.apply_h  r   r   c                 ~    U R                  5         U R                  U5        U R                  R                  XX45        g)zapply a u operation.N)rN   r   r2   apply_u)r:   r   thetaphilambs        r   r   AerState.apply_u  s2    ((*((6""<Br   c                     U R                  5         U R                  U5        U R                  U5        U R                  R                  X/X4XV5        g)zapply a cu operation.N)rN   r   r2   apply_cu)r:   r   r   r   r   r   gammas          r   r   AerState.apply_cu  sE    ((*((7((6##]$A5t[r   c                     U R                  5         U R                  U5        U R                  U5        U R                  R                  X/-   X4XV5        g)zapply a mcu operation.N)rN   r   r2   	apply_mcu)r:   r   r   r   r   r   r   s          r   r   AerState.apply_mcu  sH    ((*((8((6$$^n%DeRV^r   c                     U R                  5         U R                  U5        U R                  U5        U R                  U5        U R                  R                  XU/-   5        g)zapply a mcswap operation.N)rN   r   r2   apply_mcswap)r:   r   qubit0qubit1s       r   r  AerState.apply_mcswap  sT    ((*((8((0((0''&9I(IJr   c                 z    U R                  5         U R                  U5        U R                  R                  U5      $ )zapply a measure operation.)rN   r   r2   apply_measurer:   r   s     r   r  AerState.apply_measure  s3    ((*((0!!//77r   c                 z    U R                  5         U R                  U5        U R                  R                  X5      $ )zapply an initialize operation.)rN   r   r2   r   )r:   r   vecs      r   r   AerState.apply_initialize  s3    ((*((0!!226??r   c                 z    U R                  5         U R                  U5        U R                  R                  U5      $ )zapply a reset operation.)rN   r   r2   apply_resetr  s     r   r  AerState.apply_reset  s3    ((*((0!!--f55r   c                 z    U R                  5         U R                  U5        U R                  R                  X5      $ )zapply a kraus operation.)rN   r   r2   apply_kraus)r:   r   krausopss      r   r  AerState.apply_kraus  s3    ((*((0!!--f??r   c                 X    U R                  5         U R                  R                  U5      $ )z"return a probability of `outcome`.)rN   r2   probability)r:   outcomes     r   r  AerState.probability  s%    ((*!!--g66r   c                     U R                  5         Uc  [        U R                  S-   5      nOU R                  U5        U R                  R                  U5      $ )z!return probabilities of `qubits`.r
   )rN   r   r5   r   r2   probabilitiesr  s     r   r  AerState.probabilities  sO    ((*>4++a/0F,,V4 !!//77r   c                     U R                  5         Uc  [        U R                  S-   5      nOU R                  U5        U R                  R                  X5      $ zsamples all the qubits.r
   )rN   r   r5   r   r2   sample_countsr:   r   shotss      r   r  AerState.sample_counts  M    ((*>4++a/0F,,V4!!//>>r   c                     U R                  5         Uc  [        U R                  S-   5      nOU R                  U5        U R                  R                  X5      $ r  )rN   r   r5   r   r2   sample_memoryr  s      r   r"  AerState.sample_memory  r   r   )r6   r3   r5   r7   r4   r2   r1   )NTr!   )Ni   )@r   r   r   r   __doc__r#   staticmethodr'   r*   r-   r>   rC   rF   rK   rN   rQ   rA   rV   rY   r\   r_   rb   r9   rk   rn   rq   re   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r  r  r  r"  r   r   r   r   r   r       sk   0L/ / , , 1 1'@@@@6'
$
&
#
$
!$G 'R/ 09< $ $*0
5
7"R08 1CF1CF1CFQ1C\_K8@6@7	8??r   r   )r$  enumr   numpyro   'qiskit_aer.backends.controller_wrappersr   backends.aerbackendr   r   r   r   r   r   <module>r+     s3      D +T m? m?r   