
    z	i3                        S r SSKJr  SSKrSSKrSSKJr  SSK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KJr  SSKJr  SSKJr  SSKJ r    " S S\5      r!S r"\" \!5        g)z,
Kraus representation of a Quantum Channel.
    )annotationsN)Number)circuit)QuantumCircuit)Instruction)QiskitError)is_identity_matrix)QuantumChannel)OpShape)Choi)SuperOp)	_to_kraus)generate_apidocs)BaseOperatorc                     ^  \ rS rSrSr  S     SU 4S jjjr\S 5       rSS jrSS jr	S r
S rS	 rSSS
 jjrSS jrSS jr\S 5       rS rS rSS jrS rSrU =r$ )Kraus%   a  Kraus representation of a quantum channel.

For a quantum channel :math:`\mathcal{E}`, the Kraus representation is
given by a set of matrices :math:`[A_0,...,A_{K-1}]` such that the
evolution of a :class:`~qiskit.quantum_info.DensityMatrix`
:math:`\rho` is given by

.. math::

    \mathcal{E}(\rho) = \sum_{i=0}^{K-1} A_i \rho A_i^\dagger

A general operator map :math:`\mathcal{G}` can also be written using the
generalized Kraus representation which is given by two sets of matrices
:math:`[A_0,...,A_{K-1}]`, :math:`[B_0,...,A_{B-1}]` such that

.. math::

    \mathcal{G}(\rho) = \sum_{i=0}^{K-1} A_i \rho B_i^\dagger

See reference [1] for further details.

References:
    1. C.J. Wood, J.D. Biamonte, D.G. Cory, *Tensor networks and graphical calculus
       for open quantum systems*, Quant. Inf. Comp. 15, 0579-0811 (2015).
       `arXiv:1111.6950 [quant-ph] <https://arxiv.org/abs/1111.6950>`_
c                B  > [        U[        [        [        R                  45      (       Ga4  [        U5      (       a0  [        R                  " U[        S9/S4nUS   S   R                  nGO[        U[        5      (       a  [        U5      S:  a  [        R                  " US   [        S9/nUS   R                  nUSS  HH  n[        R                  " U[        S9nUR                  U:w  a  [        S5      eUR                  U5        MJ     US4nGO'[        U[        5      (       Ga  [        U5      S:X  a  [        US   5      S:  a  [        R                  " US   S   [        S9/nUS   R                  nUS   SS  HH  n[        R                  " U[        S9nUR                  U:w  a  [        S5      eUR                  U5        MJ     US   c  US4nOb/ n	US    HH  n[        R                  " U[        S9nUR                  U:w  a  [        S5      eU	R                  U5        MJ     X4nO[        S5      e[        R                  " X2US   S   R                  S9n
O[        U[        [        45      (       a  [         R"                  " U5      nOU R%                  U5      nUR&                  n
U
R                  u  p[)        US	S
5      n[+        XR,                  X5      nUS   b"  [        R.                  " US   US   5      (       a  US   S4nOUn[0        TU ]e  XS9  g)a  Initialize a quantum channel Kraus operator.

Args:
    data: data to initialize superoperator.
    input_dims: the input subsystem dimensions.
    output_dims: the output subsystem dimensions.

Raises:
    QiskitError: if input data cannot be initialized as a list of Kraus matrices.

Additional Information:
    If the input or output dimensions are None, they will be
    automatically determined from the input data. If the input data is
    a list of Numpy arrays of shape :math:`(2^N,\,2^N)` qubit systems will be
    used. If the input does not correspond to an N-qubit channel, it
    will assign a single subsystem with dimension specified by the
    shape of the input.
dtypeNr      z)Kraus operators are different dimensions.   z Invalid input for Kraus channel.)dims_ldims_rshape_channel_repOperator)op_shape)
isinstancelisttuplenpndarray
_is_matrixasarraycomplexr   lenr   appendr   autor   r   r   _init_instruction_init_transformer	_op_shapegetattrr   _dataallclosesuper__init__)selfdata
input_dimsoutput_dimskrausr   iop
kraus_leftkraus_rightr   
output_dim	input_dimrep	__class__s                 e/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/quantum_info/operators/channel/kraus.pyr1   Kraus.__init__A   s   : dT5"**566 $**T9:DAa)) D$''CIMDG7;<aabAAW5Bxx5()*UVVLL$	 "  D%((SY!^DGq@P jja7CD
"1++aAAW5Bxx5()*UVV%%b)	 %
 7?'.E"$K!!WZZ988u,"-.Y"ZZ#**2.	 %
 (5E!"DEE||;QVWXQYZ[Q\QbQbcH $ =>> 006 --d3~~H$,NN!J$
;Cc::yEE 8r{{58U1X>>!Hd#D D1    c                X    U R                   S   c  U R                   S   $ U R                   $ )z*Return list of Kraus matrices for channel.r   r   )r.   )r2   s    r?   r3   
Kraus.data   s.     ::a=  ::a=  ::rA   c           	     (   U R                   S   b  gUc  U R                  nUc  U R                  nSnU R                   S    HE  nU[        R                  " [        R
                  " [        R                  " U5      5      U5      -  nMG     [        X2US9$ )z4Return True if completely-positive trace-preserving.r   Fy                r   )rtolatol)r.   rF   rE   r"   dot	transposeconjr	   )r2   rF   rE   accumr8   s        r?   is_cptpKraus.is_cptp   sy    ::a=$<99D<99D**Q-BRVVBLL5r::E  !%>>rA   c                6    [        U 5      R                  X5      $ N)r   _evolve)r2   stateqargss      r?   rO   Kraus._evolve   s    t}$$U22rA   c                   [         R                   " U 5      nU R                  u  p#U Vs/ s H  n[        R                  " U5      PM     nnUb  U Vs/ s H  oDR                  5       PM     nnX#4Ul        U$ s  snf s  snf rN   )copyr.   r"   rI   r2   retkraus_lkraus_rks        r?   	conjugateKraus.conjugate   sl    iio::'./w!2771:w/)01AvvxG1&	
	 01s    A7A<c                N   [         R                   " U 5      nU R                  R                  5       Ul        U R                  u  p#U Vs/ s H  n[        R                  " U5      PM     nnUb&  U Vs/ s H  n[        R                  " U5      PM     nnX#4Ul        U$ s  snf s  snf rN   )rT   r,   rH   r.   r"   rU   s        r?   rH   Kraus.transpose   s    iio002::,34Gq2<<?G40781r||AG8&	
	 58s    B1 B"c                   [         R                   " U 5      nU R                  R                  5       Ul        U R                  u  p#U Vs/ s H-  n[        R
                  " [        R                  " U5      5      PM/     nnUb:  U Vs/ s H-  n[        R
                  " [        R                  " U5      5      PM/     nnX#4Ul        U$ s  snf s  snf rN   )rT   r,   rH   r.   r"   rI   rU   s        r?   adjointKraus.adjoint   s    iio002::5<=W2772<<?+W=9@AArwwr||A/GA&	
	 >As   4C4C
c           	        Uc  [        USS 5      nUb"  [        [        U 5      R                  XUS95      $ [	        U[        5      (       d  [        U5      nU R
                  R                  UR
                  X#5      nUR                  5       nUR                  5       nU(       a  U R                  u  pxUR                  u  pOUR                  u  pxU R                  u  pU VVs/ s H"  o  H  n[        R                  " X5      PM     M$     nnnUc  U
c  S nOUc2  U VVs/ s H"  o  H  n[        R                  " X5      PM     M$     nnnOfU
c2  U VVs/ s H"  o  H  n[        R                  " X5      PM     M$     nnnO1U VVs/ s H"  o  H  n[        R                  " X5      PM     M$     nnn[        X4XV5      nXOl        U$ s  snnf s  snnf s  snnf s  snnf )NrQ   )rQ   front)r-   r   r   composer   r,   r   r   r.   r"   rG   )r2   otherrQ   rb   	new_shaper4   r5   ka_lka_rkb_lkb_rabkab_lkab_rrV   s                   r?   rc   Kraus.compose   s   =E7D1E..u.OPP%''%LENN**5??EI	%%'
&&(JDJD$JDJD'+:t!TTt:<DLE\+/>4aARVVA\\4E>E\+/>4aARVVA\\4E>E+/>4aARVVA\\4E>UNJ<!
 ; ?>>s   !)G)G)G)Gc                d    [        U[        5      (       d  [        U5      nU R                  X5      $ rN   r   r   _tensorr2   rd   s     r?   tensorKraus.tensor  s'    %''%LE||D((rA   c                d    [        U[        5      (       d  [        U5      nU R                  X5      $ rN   rp   rr   s     r?   expandKraus.expand  s'    %''%LE||E((rA   c           	        [         R                   " U5      nUR                  R                  UR                  5      Ul        UR                  u  pEUR                  u  pgU VV	s/ s H"  o  H  n	[        R
                  " X5      PM     M$     n
nn	Uc  Uc  S nO;Uc  UnUc  UnU VVs/ s H"  o  H  n[        R
                  " X5      PM     M$     nnnX4Ul        U$ s  sn	nf s  snnf rN   )rT   r,   rs   r.   r"   kron)clsrj   rk   rV   rf   rg   rh   ri   kakbrl   rm   s               r?   rq   Kraus._tensor  s    iil**1;;7 WW
WW
+/?4R$B$4?<DLE||,0?Dq$QRWWQ]$]DE?N	
 @ @s   ")C&)C"c                z    [        USS 5      n[        U[        5      (       d  [        U5      nU R	                  XS9$ NrQ   rQ   r-   r   r
   r   _addr2   rd   rQ   s      r?   __add__Kraus.__add__   s7    w-%00KEyyy,,rA   c                ~    [        USS 5      n[        U[        5      (       d  [        U5      nU R	                  U* US9$ r   r   r   s      r?   __sub__Kraus.__sub__&  s;    w-%00KEyy%uy--rA   c                D    [        [        U 5      R                  XS95      $ )Nr   )r   r   r   r   s      r?   r   
Kraus._add,  s     T$Z__U_899rA   c                   [        U[        5      (       d  [        S5      e[        R                  " U 5      n[        U[        5      (       d  US:  a4  [        [        U 5      R                  U5      5      R                  Ul        U$ [        R                  " U5      nS nU R                  S    Vs/ s H  oSU-  PM	     nnU R                  S   b!  U R                  S    Vs/ s H  oSU-  PM	     nnXd4Ul        U$ s  snf s  snf )Nzother is not a numberr   r   )r   r   r   rT   r&   r   r   	_multiplyr.   mathsqrt)r2   rd   rV   valrX   rY   rW   s          r?   r   Kraus._multiply2  s    %((566iio eW%%d4j2259:@@CIJ ii$(JJqM2Mq7M2::a=$(,

161QwG6&	
	 36s   /C: C? )NN)r3   zLQuantumCircuit | circuit.instruction.Instruction | BaseOperator | np.ndarrayr4   tuple | Noner5   r   rN   )NF)rd   r   rQ   zlist | Nonerb   boolreturnr   )rd   r   r   r   )__name__
__module____qualname____firstlineno____doc__r1   propertyr3   rK   rO   rZ   rH   r_   rc   rs   rv   classmethodrq   r   r   r   r   __static_attributes____classcell__)r>   s   @r?   r   r   %   s    < $($(	c2Zc2 !c2 "	c2 c2J  ?3@)
)
  &-.: rA   r   c                    [        U [        R                  5      (       d  [        R                  " U [        S9n U R
                  S:H  $ )Nr   r   )r   r"   r#   arrayobjectndim)r3   s    r?   r$   r$   H  s1    dBJJ''xxF+99>rA   )#r   
__future__r   rT   r   numbersr   numpyr"   qiskitr   qiskit.circuit.quantumcircuitr   qiskit.circuit.instructionr   qiskit.exceptionsr   (qiskit.quantum_info.operators.predicatesr	   5qiskit.quantum_info.operators.channel.quantum_channelr
   &qiskit.quantum_info.operators.op_shaper   *qiskit.quantum_info.operators.channel.choir   -qiskit.quantum_info.operators.channel.superopr   5qiskit.quantum_info.operators.channel.transformationsr   $qiskit.quantum_info.operators.mixinsr   +qiskit.quantum_info.operators.base_operatorr   r   r$   r   rA   r?   <module>r      s]    #      8 2 ) G P : ; A K A D`N `F	  rA   