
    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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
Choi-matrix representation of a Quantum Channel.
    )annotationsN)TYPE_CHECKING)_numpy_compat)QuantumCircuit)Instruction)QiskitError)QuantumChannel)OpShape)SuperOp)_to_choi)_bipartite_tensor)generate_apidocs)BaseOperator)circuitc                     ^  \ 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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U =r$ )Choi)   aY  Choi-matrix representation of a Quantum Channel.

The Choi-matrix representation of a quantum channel :math:`\mathcal{E}`
is a matrix

.. math::

    \Lambda = \sum_{i,j} |i\rangle\!\langle j|\otimes
                \mathcal{E}\left(|i\rangle\!\langle j|\right)

Evolution of a :class:`~qiskit.quantum_info.DensityMatrix`
:math:`\rho` with respect to the Choi-matrix is given by

.. math::

    \mathcal{E}(\rho) = \mbox{Tr}_{1}\left[\Lambda
                        (\rho^T \otimes \mathbb{I})\right]

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

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  pVXV:w  a  [        S5      eU(       a  [        R                  " U5      nU(       a  [        R                  " U5      nUc'  Uc$  [        [        R                  " U5      5      nXX-  nOUc  UW-  nOUc  UW-  nWW-  U: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[.        TU ]a  XIS9  g)	a  Initialize a quantum channel Choi matrix 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
                 Choi matrix.

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.
)dtypezInvalid Choi-matrix input.Nz$Invalid shape for input Choi-matrix.)dims_ldims_rshape_channel_repOperator)op_shape)
isinstancelistnpndarrayasarraycomplexr   r   prodintmathsqrtr
   autor   r   r   _init_instruction_init_transformer	_op_shapegetattrr   _datasuper__init__)selfdata
input_dimsoutput_dimschoi_matdim_ldim_r	input_dim
output_dimr   rep	__class__s              d/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/quantum_info/operators/channel/choi.pyr-   Choi.__init__F   sY   4 dT2::.//zz$g6H#>>LE~!">??GGJ/	WW[1
"z'9 5!12
!/	#!Z/	$"i/
:%.!"HII||"j=TH $ =>> 006 --d3~~H$,NN!J$
;CZZGH5    c                v    Uc  U R                   R                  OUn[        R                  " U R                   XS9$ )N)r   copy)r/   r   r   array)r.   r   r=   s      r9   	__array__Choi.__array__   s*    #(=		exx		::r;   c                ^    U R                   U R                  U R                   U R                  4$ )z%Return the shape for bipartite matrix)
_input_dim_output_dim)r.   s    r9   _bipartite_shapeChoi._bipartite_shape   s'     !1!14??DDTDTUUr;   c                6    [        U 5      R                  X5      $ N)r   _evolve)r.   stateqargss      r9   rH   Choi._evolve   s    t}$$U22r;   c                |    [         R                  " U 5      n[        R                  " U R                  5      Ul        U$ rG   )_copyr=   r   conjr+   )r.   rets     r9   	conjugateChoi.conjugate   s)    jjGGDJJ'	
r;   c                D   [         R                  " U 5      nU R                  R                  5       Ul        U R                  u  p#[
        R                  " U R                  X#X#45      n[
        R                  " US5      n[
        R                  " XBU-  X#-  45      Ul        U$ )N)   r         )rM   r=   r)   	transposedimr   reshaper+   )r.   rO   d_ind_outr/   s        r9   rV   Choi.transpose   sx    jj002hhzz$**tD&@A||D,/JJtUlDL%AB	
r;   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                  u  pVU(       aW  [        R                  " UR                  UR                  5      n[        R                  " U R                  U R                  5      nOV[        R                  " U R                  U R                  5      n[        R                  " UR                  UR                  5      n[        R                  " [        R                  " SXx5      Xe-  Xe-  45      n	[        U	5      n
XJl        U
$ )NrJ   )rJ   frontziAjB,AkBl->ikjl)r*   r   r   composer   r)   r   r   rX   r+   rD   einsum)r.   otherrJ   r]   	new_shaper6   r5   firstsecondr/   rO   s              r9   r^   Choi.compose   s#   =E7D1E--e-NOO%&&KENN**5??EI	 )
JJu{{E,B,BCEZZ

D,A,ABFJJtzz4+@+@AEZZU-C-CDF zzII'7#Y%;<
 4j!
r;   c                d    [        U[        5      (       d  [        U5      nU R                  X5      $ rG   r   r   _tensorr.   r`   s     r9   tensorChoi.tensor   s'    %&&KE||D((r;   c                d    [        U[        5      (       d  [        U5      nU R                  X5      $ rG   rf   rh   s     r9   expandChoi.expand   s'    %&&KE||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)rM   r=   r)   ri   r   r+   r/   rD   )clsabrO   s       r9   rg   Choi._tensor   sY    jjm**1;;7%GGQVVA$6$6q?Q?Q
	 
r;    )NN)r/   zLQuantumCircuit | circuit.instruction.Instruction | BaseOperator | np.ndarrayr0   int | tuple | Noner1   rv   rG   )NF)r`   r   rJ   zlist | Noner]   boolreturnr   )r`   r   rx   r   )__name__
__module____qualname____firstlineno____doc__r-   r   COPY_ONLY_IF_NEEDEDr?   propertyrD   rH   rP   rV   r^   ri   rl   classmethodrg   __static_attributes____classcell__)r8   s   @r9   r   r   )   s    > *.*.	B6ZB6 'B6 (	B6 B6H #)J)J ; V V3
	6)
)
  r;   r   )!r}   
__future__r   r=   rM   r$   typingr   numpyr   qiskitr   qiskit.circuit.quantumcircuitr   qiskit.circuit.instructionr   qiskit.exceptionsr   5qiskit.quantum_info.operators.channel.quantum_channelr	   &qiskit.quantum_info.operators.op_shaper
   -qiskit.quantum_info.operators.channel.superopr   5qiskit.quantum_info.operators.channel.transformationsr   r   $qiskit.quantum_info.operators.mixinsr   +qiskit.quantum_info.operators.base_operatorr   r   r   ru   r;   r9   <module>r      s[    #        8 2 ) P : A J S A Dm> mb  r;   