
    z	i,                       S r SSKJr  SSKrSSKrSSKJ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"  \(       a  SSK#J$r$   " S S\5      r%\ " \%5        g)z2
Stinespring representation of a Quantum Channel.
    )annotationsN)Number)TYPE_CHECKING)QuantumCircuit)Instruction)QiskitError)is_identity_matrix)QuantumChannel)OpShape)Kraus)Choi)SuperOp)_to_stinespring)generate_apidocs)BaseOperator)circuitc                     ^  \ 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       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$ )Stinespring)   u  Stinespring representation of a quantum channel.

The Stinespring representation of a quantum channel :math:`\mathcal{E}`
is a rectangular matrix :math:`A` such that the evolution of a
:class:`~qiskit.quantum_info.DensityMatrix` :math:`\rho` is given by

.. math::

    \mathcal{E}(ρ) = \mbox{Tr}_2\left[A ρ A^\dagger\right]

where :math:`\mbox{Tr}_2` is the :func:`partial_trace` over subsystem 2.

A general operator map :math:`\mathcal{G}` can also be written using the
generalized Stinespring representation which is given by two matrices
:math:`A`, :math:`B` such that

.. math::

    \mathcal{G}(ρ) = \mbox{Tr}_2\left[A ρ B^\dagger\right]

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                8  > [        U[        [        [        R                  45      (       Ga3  [        U[        5      (       d  [        R
                  " U[        S9S4n[        U[        5      (       al  [        U5      S:X  a]  US   c  [        R
                  " US   [        S9S4nO8[        R
                  " US   [        S9[        R
                  " US   [        S94nWS   R                  u  pVUS   b  US   R                  XV4:w  a  [        S5      eUnU(       a  [        R                  " U5      nOUnXX-  S:w  a  [        S5      e[        R                  " X2X4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*                  Xx5      nUS   b  US   US   :H  R-                  5       (       a  US   S4nOUn[.        TU ]a  XS9  g)a-  Initialize a quantum channel Stinespring 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
                 a list of Kraus matrices.

Additional Information:
    If the input or output dimensions are None, they will be
    automatically determined from the input data. This can fail for the
    Stinespring operator if the output dimension cannot be automatically
    determined.
)dtypeN      r   zInvalid Stinespring input.zInvalid output_dim)dims_ldims_rshape_channel_repOperator)op_shape)
isinstancelisttuplenpndarrayasarraycomplexlenr   r   prodr   autor   r   r   _init_instruction_init_transformer	_op_shapegetattrr   _dataallsuper__init__)selfdata
input_dimsoutput_dimsstinedim_left	dim_right	input_dim
output_dimr   rep	__class__s              k/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/quantum_info/operators/channel/stinespring.pyr1   Stinespring.__init__F   s   4 dT5"**566dE**D8$?$&&3t9>7?ZZQw?FEZZQw?DQRG[bAcdE"'(..HQx#8>>h%::%&BCC!IWW[1
&
$)!"677||"j=TH $ =>> 006 --d3~~H$,NN!J $
;C#CYKE 8aE!H 499;;!Hd#D D1    c                X    U R                   S   c  U R                   S   $ U R                   $ )Nr   r   )r.   )r2   s    r=   r3   Stinespring.data   s*     ::a= ::a= ::r?   c                N   Uc  U R                   nUc  U R                  nU R                  S   b  g[        R                  " [        R
                  " [        R                  " U R                  S   5      5      U R                  S   5      n[        X0R                  U R                   S9$ )z4Return True if completely-positive trace-preserving.r   Fr   )rtolatol)rD   rC   r.   r#   dot	transposeconjr	   )r2   rD   rC   checks       r=   is_cptpStinespring.is_cptp   sv    <99D<99D::a=$r||BGGDJJqM$:;TZZ]K!%iidiiHHr?   c                6    [        U 5      R                  X5      $ N)r   _evolve)r2   stateqargss      r=   rM   Stinespring._evolve   s    t}$$U22r?   c                    [         R                   " U 5      n[        R                  " U R                  S   5      nS nU R                  S   b#  [        R                  " U R                  S   5      nX#4Ul        U$ )Nr   r   )copyr#   	conjugater.   )r2   retstine_lstine_rs       r=   rS   Stinespring.conjugate   s]    iio,,tzz!}-::a=$ll4::a=1G&	
r?   c                   [         R                   " U 5      nU R                  R                  5       Ul        U R                  u  p#U R                  S   R
                  S   U-  nS S /n[        U R                  5       HR  u  pgUc  M
  [        R                  " [        R                  " [        R                  " XsXB45      S5      X$-  U45      XV'   MT     US   US   4Ul        U$ )Nr   )r   r   r   r   )	rR   r,   rF   dimr.   r   	enumerater#   reshape)r2   rT   dindoutdtrr6   imats           r=   rF   Stinespring.transpose   s    iio002HH	jjm!!!$,t

+FA::LLC1A!BINQTQZ\`Pa ,
 1XuQx(	
r?   c                    Uc  [        USS 5      nUb"  [        [        U 5      R                  XUS95      $ [        [	        U 5      R                  XS95      $ )NrO   )rO   front)rc   )r-   r   r   composer   )r2   otherrO   rc   s       r=   rd   Stinespring.compose   s[     =E7D1Ewt}44Uu4UVV 5;..u.BCCr?   c                d    [        U[        5      (       d  [        U5      nU R                  X5      $ rL   r    r   _tensorr2   re   s     r=   tensorStinespring.tensor   s(    %--&E||D((r?   c                d    [        U[        5      (       d  [        U5      nU R                  X5      $ rL   rh   rj   s     r=   expandStinespring.expand   s(    %--&E||E((r?   c                   UR                   u  p4UR                   u  pVUR                  u  pxUR                  u  pUR                  S   U-  nUR                  S   U
-  nXXXy-  4nX-  U
-  U-  Xy-  4n[        R                  " X55      n[        R
                  " [        R                  " [        R
                  " X5      S5      U5      nUc  Uc  S nObUc  UnOUc  Un[        R                  " XF5      n[        R
                  " [        R                  " [        R
                  " UU5      S5      U5      n[        R                  " U5      nUR                  R                  UR                  5      Ul        UU4Ul         U$ )Nr   )r   r   r         )
r.   rY   r   r#   kronr[   rF   rR   r,   rk   )clsabsa_lsa_rsb_lsb_rdin_adout_adin_bdout_bdtr_adtr_bshape_in	shape_outsab_lsab_rrT   s                     r=   ri   Stinespring._tensor   sC    WW
WW
 

1'

1'6%-@^f,u4emD	#

2<<

5(C_UW`a <DLE|GGD'EJJRZZx8/JIE iil**1;;7EN	
r?   c                z    [        USS 5      n[        U[        5      (       d  [        U5      nU R	                  XS9$ NrO   rO   r-   r    r
   r   _addr2   re   rO   s      r=   __add__Stinespring.__add__   s7    w-%00KEyyy,,r?   c                ~    [        USS 5      n[        U[        5      (       d  [        U5      nU R	                  U* US9$ r   r   r   s      r=   __sub__Stinespring.__sub__  s;    w-%00KEyy%uy--r?   c                D    [        [        U 5      R                  XS95      $ )Nr   )r   r   r   r   s      r=   r   Stinespring._add
  s     4:??5?>??r?   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U R                  u  pEX0R                  S   -  nS nU R                  S   b  X0R                  S   -  nXE4Ul        U$ )Nzother is not a numberr   r   )r    r   r   rR   r&   r   r   	_multiplyr.   mathsqrt)r2   re   rT   numrU   rV   s         r=   r   Stinespring._multiply  s    %((566iio eW%%#DJ$8$8$?@FFCIJ ii::

1%::a=$JJqM)G&	
r?    )NN)r3   zLQuantumCircuit | circuit.instruction.Instruction | BaseOperator | np.ndarrayr4   int | tuple | Noner5   r   rL   )NF)re   r   rO   zlist | Nonerc   boolreturnr   )re   r   r   r   )__name__
__module____qualname____firstlineno____doc__r1   propertyr3   rI   rM   rS   rF   rd   rk   rn   classmethodri   r   r   r   r   __static_attributes____classcell__)r<   s   @r=   r   r   )   s    > *.*.	L2ZL2 'L2 (	L2 L2\  	I3 LQ	D 	D)4	DDH	D		D)
)
 ! !F-.@
 r?   r   )&r   
__future__r   rR   r   numbersr   typingr   numpyr#   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.krausr   *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   qiskitr   r   r   r?   r=   <module>r      sa    #       8 2 ) G P : = ; A Q A D{. {~  r?   