
    {	i+0                         S r SSK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
KJr  SSKJr  SSKJr   " S S\
5      rg)z$
DensityMatrix quantum state class.
    N)QuantumCircuitInstruction)QiskitError)DensityMatrix)is_hermitian_matrix)AerSimulator   )AerStatevector)AerState   )AerError)BASIS_GATESc                      ^  \ rS rSrSrSU 4S jjrSS jrS rS rS r	SS jr
S	 rS
 rS rS rSS jr\S 5       r\S 5       r\S 5       r\S 5       rSU 4S jjr\S 5       r\S 5       rSS jrSrU =r$ )AerDensityMatrix   zCAerDensityMatrix class
This class inherits :class:`DensityMatrix`.
c                 J  > SU;   a  UR                  S5      U l        GOjSU;  a  SUS'   OUS   S:w  a  US   n[        SU S35      e[        U[        [
        45      (       a  [        R                  USU5      u  pGO[        U[        5      (       aA  U R                  [        R                  " U[        S95      n[        R                  X5      u  pGO[        U[        R                  5      (       a*  U R                  U5      n[        R                  X5      u  pGOf[        U[        5      (       aA  UR                  nUc  UR                  R                   nUR"                  R%                  5       nGO[        U[&        5      (       a:  [        R                  [        R                  " UR(                  [        S9U5      u  pO[+        US5      (       aF  UR-                  5       n[        R                  UR(                  U5      u  pUc  UR/                  5       nOj[+        US	5      (       a>  [        R                  [        R0                  " UR3                  5       [        S9U5      u  pO[        S
UR4                   SU 35      eXPl        [6        TU ]q  XS9  SU l        X0l        g)a  
Args:
    data (np.array or list or Statevector or AerStatevector or DensityMatrix or
          AerDensityMatrix or QuantumCircuit or qiskit.circuit.Instruction):
        Data from which the densitymatrix can be constructed. This can be either a complex
        vector, another densitymatrix or statevector or a ``QuantumCircuit`` or
        ``Instruction`` (``Operator`` is not supported in the current implementation).
        If the data is a circuit or instruction, the densitymatrix is constructed by
        assuming that all qubits are initialized to the zero state.
    dims (int or tuple or list): Optional. The subsystem dimension of
                                 the state (See additional information).
    configs (kwargs): configurations of :class:`AerDensityMatrix`. `_aer_state` and `method`
        are valid.

Raises:
    AerError: if input data is not valid.
Additional Information:
    The ``dims`` kwarg is used to ``AerDensityMatrix`` constructor.

_aer_statemethoddensity_matrixzMethod z is not supportedNdtypeto_operator	to_matrixz"Input data is not supported: type=z, data=dims)popr   r   
isinstancer   r   r   _from_instructionlist_from_1d_arraynparraycomplex_from_ndarrayndarray	_op_shape_dims_l_datacopyr   datahasattrr   output_dimsasarrayr   	__class__super__init___result_configs)selfr*   r   configsr   	aer_stateopr.   s          j/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_aer/quantum_info/states/aer_densitymatrix.pyr0   AerDensityMatrix.__init__$   s/   ( 7"%kk,7DOw&$4!"&66 *0ABCC$ =>>"2"D"DT4QX"YiD$''**288D+HI"2"@"@"OiD"**--**40"2"@"@"OiD"233 OO	<>>11Dzz(D-00"2"@"@HHTYYg6#i }-- %%'"2"@"@'"R<>>+D{++ #3"@"@JJt~~/w?#i !CDNNCSSZ[_Z`abb'O)    c                     Ub  [        U[        5      (       a  U R                  R                  U5        g[	        SUR
                   SU 35      e)z'Set the seed for the quantum state RNG.Nz!This seed is not supported: type=z, value=)r   intr   set_seedr   r.   )r3   values     r7   seedAerDensityMatrix.seedj   sE    =Juc22OO$$U+>u>OxX]W^_``r9   c                 r    U R                   c  U R                  R                  5       U l         U R                   $ N)r1   r   last_resultr3   s    r7   _last_resultAerDensityMatrix._last_resultq   s*    <<??668DL||r9   c                 `    U R                  5       c  [        S5      eU R                  5       S   $ )z<Return result metadata of an operation that executed lastly.z2AerState was not used and metadata does not exist.metadata)rD   r   rC   s    r7   rG   AerDensityMatrix.metadatav   s1    &OPP  ":..r9   c                 .    [         R                  " U 5      $ rA   )r)   deepcopyrC   s    r7   __copy__AerDensityMatrix.__copy__|   s    }}T""r9   c                     [        U R                  R                  5       40 U R                  D6n[        R                  " U R
                  5      Ul        [        R                  " U R                  5      Ul        U$ rA   )r   r(   r)   r2   rJ   r&   _rng_generator)r3   _memorets      r7   __deepcopy__AerDensityMatrix.__deepcopy__   sP    tzz0BDMMBdnn5!]]4+>+>?
r9   c                 n    [        [        R                  " U R                  5      U R	                  5       S9$ )Nr   )r   r!   conjr(   r   rC   s    r7   	conjugateAerDensityMatrix.conjugate   s"    

 3$))+FFr9   c                 &   [        U[        5      (       d  [        U5      n[        R                  " U 5      n[        R                  " U R
                  UR
                  5      Ul        U R                  R                  UR                  5      Ul        U$ )u   Return the tensor product state self ⊗ other.
Args:
    other (AerDensityMatrix): a quantum state object.
Returns:
    AerDensityMatrix: the tensor product operator self ⊗ other.
Raises:
    QiskitError: if other is not a quantum state.
)r   r   r)   r!   kronr(   r&   tensorr3   otherrP   s      r7   rY   AerDensityMatrix.tensor   sb     %!122$U+EiioGGDJJ4	--eoo>
r9   c                 &   [        U[        5      (       d  [        U5      n[        R                  " U 5      n[        R                  " UR
                  U R
                  5      Ul        U R                  R                  UR                  5      Ul        U$ )u   Return the tensor product state other ⊗ self.
Args:
    other (AerDensityMatrix): a quantum state object.
Returns:
    AerDensityMatrix: the tensor product state other ⊗ self.
Raises:
    QiskitError: if other is not a quantum state.
)r   r   r)   r!   rX   r(   r&   expandrZ   s      r7   r^   AerDensityMatrix.expand   sb     %!122$U+EiioGGEKK4	--eoo>
r9   c                     [        U[        5      (       d  [        U5      nU R                  R                  UR                  5        [        R                  " U 5      nU R
                  UR
                  -   Ul        U$ )a  Return the linear combination self + other.
Args:
    other (AerDensityMatrix): a quantum state object.
Returns:
    AerDensityMatrix: the linear combination self + other.
Raises:
    QiskitError: if other is not a quantum state, or has
                 incompatible dimensions.
)r   r   r&   _validate_addr)   r*   r(   rZ   s      r7   _addAerDensityMatrix._add   sX     %!122$U+E$$U__5iioII

*	
r9   c                    Uc+  [         R                  " U R                  R                  5      nO[         R                  " U5      nU R                  R                  5         U R                  R                  5         U R                  R                  U R                  SS9  U R                  R                  X15      nU R                  R                  5       U l        U$ )NF)r)   )r!   aranger   
num_qubitsr"   closerenew
initializer(   sample_memorymove_to_ndarray)r3   shotsqargsqubitssampless        r7   rj   AerDensityMatrix.sample_memory   s    =YYt99:FXXe_F""4::E":////>__446
r9   c                 ,   U R                   nU R                  nUS:X  a  US   US   :X  a   U $ US:X  a-  [        R                  " U [        R                  " U 5      5      n U $ US:X  a$  US   S:X  a  [        R
                  " XS   5      n U $ [        S5      e)N   r   r	   z4Invalid AerDensityMatrix input: not a square matrix.)ndimshaper!   outerrT   reshaper   )r*   rs   rt   s      r7   r    AerDensityMatrix._from_1d_array   s     yy

19qU1X-  QY88D"''$-0D
 	 QY58q=::d!H-D  TUUr9   c                    [        SS9n[        R                  " 5       nUR                  5        H.  u  pEUR	                  U5      (       d  M  UR                  XE5        M0     [        U 5      S:X  a  [        S5      e[        [        R                  " [        U 5      5      5      nUR                  U5        UR                  U S9  UR                  5       U4$ )Nr   r   r   z"initial data must be larger than 0)r*   )r   r   _default_optionsitemsget	configurelenr   r;   r!   log2allocate_qubitsri   rk   )	init_datar4   r5   options
config_keyconfig_valuerf   s          r7   r$   AerDensityMatrix._from_ndarray   s    $45	//1(/$J{{:&&##J= )8 y>Q?@@Y01
!!*-),((*I55r9   c                     [        U5      $ rA   )r   )clsinstructions     r7   from_instruction!AerDensityMatrix.from_instruction   s    ,,r9   c                 l   [        SS9nUR                  5        H  u  pEUR                  XE5        M     [        S   n/ SQnUR	                  U R
                  5        U R
                  nUb  UR                  USS9  OUR                  5         [        U [        5      (       a+  U R                  S:w  a  UR                  U R                  5        [        U [        5      (       a"  [        R                  " X0[        U5      Xg5        O![        R                  " X0[        U5      Xg5        UR                  5       U4$ )Nr   ry   )resetkrausbarrierT)r*   r)   r   )r   r{   r}   r   r   rf   ri   r   r   global_phaseapply_global_phaser
   _aer_evolve_circuitrange_aer_evolve_instructionrk   )	instr   r4   r5   r   r   basis_gatescustom_instsrf   s	            r7   r   "AerDensityMatrix._from_instruction   s   $45	(/$J
9 )8 ""234!!$//2__
   id ;  "dN++0A0AQ0F(():):;dN++..z!2K 22z!2K ((*I55r9   c                 b   > [         TU ]  US9n[        U[        5      (       a  [	        U5      nU$ )N)rm   )r/   r   r   r   r   )r3   rm   r   r.   s      r7   r   AerDensityMatrix.reset  s2     U3nm44-n=Nr9   c                 @    [        [        R                  " U5      5      $ rA   )r   r
   
from_label)r   labels     r7   r   AerDensityMatrix.from_label  s     9 9% @AAr9   c                     [         R                  " U5      n[         R                  " X"4[        S9nSX0U 4'   [	        X1S9$ )Nr   g      ?r   )r!   prodzerosr#   r   )ir   sizestates       r7   from_intAerDensityMatrix.from_int  s7    wwt}$W5d11r9   c                    Uc  U R                   nUc  U R                  n[        U R                  XS9(       d  [	        S5      e[
        R                  R                  U R                  5      u  p4U[        U5      U:     n[        U5      S:w  d  [
        R                  " US   SXS9(       d  [	        S5      eUSS2[
        R                  " U5      4   n[        U5      $ )a  Return a statevector from a pure density matrix.
Args:
    atol (float): Absolute tolerance for checking operation validity.
    rtol (float): Relative tolerance for checking operation validity.
Returns:
    AerStatevector: The pure density matrix's corresponding statevector.
        Corresponds to the eigenvector of the only non-zero eigenvalue.
Raises:
    QiskitError: if the state is not pure.
N)atolrtolz+Not a valid density matrix (non-hermitian).r	   r   z"Density matrix is not a pure state)r   r   r   r*   r   r!   linalgeigabsr~   iscloseargmaxr
   )r3   r   r   evalsevecsnonzero_evalspsis          r7   to_statevectorAerDensityMatrix.to_statevector  s     <99D<99D"4994CKLLyy}}TYY/c%j4/0}""**]15Eqt*_BCCAryy''(c""r9   )r   r2   r(   r1   rA   )NN)__name__
__module____qualname____firstlineno____doc__r0   r>   rD   rG   rK   rQ   rU   rY   r^   rb   rj   staticmethodr    r$   classmethodr   r   r   r   r   r   __static_attributes____classcell__)r.   s   @r7   r   r      s    D La
/#G  "   6 6$ - - 6 6> B B 2 2# #r9   r   )r   r)   numpyr!   qiskit.circuitr   r   qiskit.exceptionsr   qiskit.quantum_info.statesr   (qiskit.quantum_info.operators.predicatesr   
qiskit_aerr   aer_statevectorr
   r5   r   backends.aerbackendr   backends.backend_utilsr   r    r9   r7   <module>r      s;      6 ) 4 H # +  + 1Z#} Z#r9   