
    z	iE=                       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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K J!r!  SSK"J#r#   " S S\5      r$\" \$5        g)z3
Superoperator representation of a Quantum Channel.    )annotationsN)TYPE_CHECKING)_numpy_compat)Instruction)QuantumCircuit)QiskitError)BaseOperator)QuantumChannel)_bipartite_tensor_to_superop)generate_apidocs)OpShape)Operator)circuitDensityMatrix)Statevectorc                     ^  \ rS rSrSr  S     SU 4S jjjrS\R                  4S jr\	S 5       r
\	S 5       rS rS	 rS
 rSS jrSS jr\S 5       rSSS jjrSS jr\S 5       r\S 5       rSS jrSrU =r$ )SuperOp)   a  Superoperator representation of a quantum channel.

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

.. math::

    |\mathcal{E}(\rho)\rangle\!\rangle = S |\rho\rangle\!\rangle

where the double-ket notation :math:`|A\rangle\!\rangle` denotes a vector
formed by stacking the columns of the matrix :math:`A`
*(column-vectorization)*.

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>`_
Nc                  > [        U[        [        R                  45      (       a  [        R                  " U[
        S9nUR                  u  pV[        [        R                  " U5      5      n[        [        R                  " U5      5      nUS-  U:w  d	  US-  U:w  a  [        S5      e[        R                  " X2X4S9n	O{[        U[        [        45      (       a  U R                  U5      nOU R!                  U5      nUR"                  n	UR$                  u  px['        USS5      n
[)        XR*                  Xx5      n[,        TU ]]  XIS9  g)	a  Initialize a quantum channel Superoperator 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
                 superoperator.

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 Numpy array of shape (4**N, 4**N) qubit systems will be used. If
    the input operator is not an N-qubit operator, it will assign a
    single subsystem with dimension specified by the shape of the input.
)dtype   z!Invalid shape for SuperOp matrix.)dims_ldims_rshape_channel_repr   )op_shapeN)
isinstancelistnpndarrayasarraycomplexr   intmathsqrtr   r   autor   r   _init_instruction_init_transformer	_op_shapedimgetattrr   _datasuper__init__)selfdata
input_dimsoutput_dims	super_matdoutdin	input_dim
output_dimr   rep	__class__s              g/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/quantum_info/operators/channel/superop.pyr0   SuperOp.__init__@   s   4 dT2::.//

4w7I!IDDIIcN+ITYYt_-J1}$	1(;!"EFF||"j=TH $ =>> --d3 --d3 ~~H$(HH!I$
;C#CYKI6    c                v    Uc  U R                   R                  OUn[        R                  " U R                   XS9$ )N)r   copy)r2   r   r!   array)r1   r   r@   s      r<   	__array__SuperOp.__array__   s*    #(=		exx		::r>   c                    S[        [        U R                  R                  5       5      5      -  S[        [        U R                  R	                  5       5      5      -  -   $ )z3Return the tensor shape of the superoperator matrixr   )tuplereversedr+   r   r   r1   s    r<   _tensor_shapeSuperOp._tensor_shape   sQ     5$.."7"7"9:;;a%T^^**,-C
 ?
 
 	
r>   c                ^    U R                   U R                   U R                  U R                  4$ )z%Return the shape for bipartite matrix)_output_dim
_input_dimrG   s    r<   _bipartite_shapeSuperOp._bipartite_shape   s'       $"2"2DOOT__UUr>   c                |    [         R                  " U 5      n[        R                  " U R                  5      Ul        U$ N)_copyr@   r!   conjr.   r1   rets     r<   	conjugateSuperOp.conjugate   s)    jjGGDJJ'	
r>   c                    [         R                  " U 5      n[        R                  " U R                  5      Ul        U R
                  R                  5       Ul        U$ rP   )rQ   r@   r!   	transposer.   r+   rS   s     r<   rX   SuperOp.transpose   s=    jjLL,	002
r>   c                    [         R                  " U 5      n[        R                  " [        R                  " U R
                  5      5      Ul        U R                  R	                  5       Ul        U$ rP   )rQ   r@   r!   rR   rX   r.   r+   rS   s     r<   adjointSuperOp.adjoint   sF    jjGGBLL45	002
r>   c                d    [        U[        5      (       d  [        U5      nU R                  X5      $ rP   r   r   _tensorr1   others     r<   tensorSuperOp.tensor   s'    %))ENE||D((r>   c                d    [        U[        5      (       d  [        U5      nU R                  X5      $ rP   r^   r`   s     r<   expandSuperOp.expand   s'    %))ENE||E((r>   c                    [         R                  " U5      nUR                  R                  UR                  5      Ul        [	        UR
                  UR                  UR                  UR                  S9Ul        U$ )N)shape1shape2)rQ   r@   r+   rb   r   r.   r2   rM   )clsabrT   s       r<   r_   SuperOp._tensor   sY    jjm**1;;7%GGQVVA$6$6q?Q?Q
	 
r>   c           	     H   Uc  [        USS 5      n[        U[        5      (       d  [        U5      nU R                  R	                  UR                  X#5      nUR                  5       nUR                  5       nUcr  U(       a,  [        R                  " U R                  UR                  5      nO+[        R                  " UR                  U R                  5      n[        XuU5      nXHl        U$ U R                  R                  u  pU(       a
  U
nSU	-  nSnOU	nSnSn[        R                  " U R                  U R                  5      n[        R                  " UR                  UR                  5      nU Vs/ s H  nSU-  S-
  U-
  PM     snU Vs/ s H  nUS-
  U-
  PM     sn-   n[        R                  " U5      S-  [        R                  " U5      S-  /n[        R                  " [        R                   " XUX5      U5      n[        XuU5      nXHl        U$ s  snf s  snf )Nqargsr   Tr   F   )r-   r   r   r+   composer   r   r!   dotr.   r2   	num_qargsreshaperH   prodr   _einsum_matmul)r1   ra   ro   front	new_shaper3   r4   r2   rT   num_qargs_lnum_qargs_rnum_indicesshift	right_mulrb   matqubitindicesfinal_shapes                      r<   rq   SuperOp.compose   s   =E7D1E%))ENE NN**5??EI	%%'
&&( =vvdjj%**5vvejj$**5$K8C%MJ $(>>#;#; %KOEI%KEI DIIt'9'9:jjU%8%89<ABE51{?Q&.EB16F
16K!Oe#F
 
 ww{+q0"''*2E2JKzz##F%K[
 d4!
 C F
s   5HHc                   SSK Jn  [        X5      (       d  U" U5      nUc  UR                  R                  S   U R                  R                  S   :w  a  [        S5      e[        R                  " UR                  SS9n[        R                  " [        R                  " U R                  U5      U R                  U R                  4SS9nU" XPR                  5       S9$ UR                  U5      U R                  5       :w  a  [        S5      e[        R                  " UR                  UR                  R                  5      n[        R                  " U R                  U R                   5      n[#        UR                  5       5      nU Vs/ s H
  oS-
  U-
  PM     snU Vs/ s H  nS	U-  S-
  U-
  PM     sn-   n	[$        R&                  " XeU	5      n[)        UR                  5       5      n
U R                  5       n[+        U5       H  u  pX   X'   M     [        R,                  " U
5      n[        R                  " XmU45      nU" XjS9$ s  snf s  snf )
a  Evolve a quantum state by the quantum channel.

Args:
    state (DensityMatrix or Statevector): The input state.
    qargs (list): a list of quantum state subsystem positions to apply
                   the quantum channel on.

Returns:
    DensityMatrix: the output quantum state as a density matrix.

Raises:
    QiskitError: if the quantum channel dimension does not match the
                 specified quantum state subsystem dimensions.
r   r   rp   zBOperator input dimension is not equal to density matrix dimension.F)order)dimszLOperator input dimensions are not equal to statevector subsystem dimensions.r   )(qiskit.quantum_info.states.densitymatrixr   r   r+   r   r   r!   ravelr2   rt   rr   rK   r4   r   r3   tensor_shaperH   lenr   rv   r    	enumerateru   )r1   statero   r   vecr~   rb   r{   r   r   new_dimsr4   inew_dims                 r<   _evolveSuperOp._evolve   s   " 	K%//!%(E=$$Q'4>>+?+?+BB!X 
 ((5::S1C**tyy#&)9)94;K;K(LTWC !+;+;+=>> ::e 11^  EJJ(D(DEjjD$6$67%**,'8=>u?U*>5:B
5:EAOa%'UB
 
 ((g>

%&&(!%(HA)nHO )''(#Fg$67V33 ? B
s   
I	!Ic                    [        U[        5      (       a  UR                  5       n[        [        R
                  " SUR                  -  5      5      nUR                  U5        U$ )z5Convert a QuantumCircuit or Instruction to a SuperOp.   )r   r   to_instructionr   r!   eye
num_qubits_append_instruction)rj   instructionops      r<   r)   SuperOp._init_instruction)  sR     k>22%446K RVVA{55567
{+	r>   c           	         [        U[        5      (       d  [        S5      eSnUR                  S:X  a+  [	        [
        R                  " / SQ/ SQ/ SQ/ SQ/5      5      nUR                  S:X  a4  UR                  n[        US   5      n[	        [        SUS4XD5      5      nU$ [        US	5      (       a:   UR                  5       /n[        US   5      n[	        [        SUS4XD5      5      nU$ U$ ! [         a     U$ f = f)
z<Return superop for instruction if defined or None otherwise.zInput is not an instruction.Nreset)rp   r   r   rp   )r   r   r   r   krausr   Kraus	to_matrix)r   r   r   namer   r!   rA   paramsr   r   hasattrr   )rj   objchanr   r,   s        r<   _instruction_to_superopSuperOp._instruction_to_superop5  s     #{++<==88w 288\<|$\]^D88wJJEeAh-C;wsHID  S+&&)%(m{7UDM3LM t  s   47C/ /
C=<C=c                ^   SSK Jn  U R                  U5      nUb!  U R                  XBS9nUR                  U l        g[        X5      (       a  gUR                  c  [        SUR                   35      e[        UR                  [        5      (       d.  [        UR                   S[        UR                  5       S35      e[        UR                  R                  5       VVs0 s H  u  pgXv_M	     nnnUR                  R                   H  n	U	R                  (       a"  [        SU	R                  R                   35      eUc  U	R                   V
s/ s H  oU
   PM	     nn
O U	R                   V
s/ s H	  oX      PM     nn
U R!                  U	R                  US9  M     gs  snnf s  sn
f s  sn
f )	z4Update the current Operator by apply an instruction.r   )BarrierN)ro   zCannot apply Instruction: z instruction definition is z; expected QuantumCircuitz.Cannot apply instruction with classical bits: )qiskit.circuit.barrierr   r   rq   r2   r.   r   
definitionr   r   r   typer   qubitsclbits	operationr   )r1   r   ro   r   r   r   idxbitqubit_indicesr   tup	new_qargss               r<   r   SuperOp._append_instructionN  s   2++C0 d0BDJ%%
 ~~%!$>sxxj"IJJcnnn==!xxj ;D<P;Q R. .  7@@U@U6VW6V(#SX6VMW"~~22%%%'116679 
 =?J?Q?Q R?Qs!3?QI RIFQFXFX YFXs}'9!:FXI Y(()>)>i(P  3 X !S Ys   !FF%.F*)r.   )NN)r2   zLQuantumCircuit | circuit.instruction.Instruction | BaseOperator | np.ndarrayr3   tuple | Noner4   r   )ra   r   returnr   )NF)ra   r   ro   zlist | Nonerw   boolr   r   rP   )__name__
__module____qualname____firstlineno____doc__r0   r   COPY_ONLY_IF_NEEDEDrB   propertyrH   rM   rU   rX   r[   rb   re   classmethodr_   rq   r   r)   r   r   __static_attributes____classcell__)r;   s   @r<   r   r   )   s    2 $($(	=7Z=7 !=7 "	=7 =7~ #)J)J ; 
 
 V V
)
)
  0l:4x 	 	  0#Q #Qr>   r   )%r   
__future__r   r@   rQ   r&   typingr   numpyr!   qiskitr   qiskit.circuit.instructionr   qiskit.circuit.quantumcircuitr   qiskit.exceptionsr   +qiskit.quantum_info.operators.base_operatorr	   5qiskit.quantum_info.operators.channel.quantum_channelr
   5qiskit.quantum_info.operators.channel.transformationsr   r   $qiskit.quantum_info.operators.mixinsr   &qiskit.quantum_info.operators.op_shaper   &qiskit.quantum_info.operators.operatorr   r   r   r   &qiskit.quantum_info.states.statevectorr   r    r>   r<   <module>r      s`   6 #        2 8 ) D P ` A : ;FBHQn HQX
  r>   