
    bi                     x    d Z ddlmZmZ ddlZddlmZ ddl	m
Z
 d
dedee   dee   fdZd
dee   dee   fd	Zy)a6  
Copyright 2022, adapted from Convex.jl.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    )OptionalTupleN)Atomjdimsaxisc                    t        j                  dgdgdgff      }t        j                  dgdgdgff      }t        |      D ]  \  }}||k(  rWt        j                  dg|gdgff|df      }t        j                  ||j                        }t        j                  ||      }bt        j
                  |      }	t        j                  ||	      }t        j                  ||	      } || z  |z  S )a  Helper function for partial trace.

    Parameters
    ----------
    expr : :class:`~cvxpy.expressions.expression.Expression`
        The 2D expression to take the partial trace of.
    j : int
        Term in the partial trace sum.
    dims : tuple of ints.
        A tuple of integers encoding the dimensions of each subsystem.
    axis : int
        The index of the subsystem to be traced out
        from the tensor product that defines expr.
    g      ?r      )shape)sp
coo_matrix	enumeratekronT	eye_array)
exprr   r   r   abi_axisdimveye_mats
             [/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/partial_trace.py_termr      s    & 	usQCj)*A
usQCj)*A"4 $T>saS1#J/Qx@A133A1All3'G7#A7#A$ t8a<    c                    t        j                  |       } | j                  dk  s| j                  d   | j                  d   k7  rt	        d      |dk  s|t        |      k\  rt	        dt        |       d| d      | j                  d   t        j                  |      k7  rt	        d      t        t        ||         D cg c]  }t        | |||       c}      S c c}w )	a8  
    Assumes :math:`\texttt{expr} = X_1 \otimes \cdots \otimes X_n` is a 2D Kronecker
    product composed of :math:`n = \texttt{len(dims)}` implicit subsystems.
    Letting :math:`k = \texttt{axis}`, the returned expression represents
    the *partial trace* of :math:`\texttt{expr}` along its :math:`k^{\text{th}}` implicit subsystem:

    .. math::

        \text{tr}(X_k) (X_1 \otimes \cdots \otimes X_{k-1} \otimes X_{k+1} \otimes \cdots \otimes X_n).

    Parameters
    ----------
    expr : :class:`~cvxpy.expressions.expression.Expression`
        The 2D expression to take the partial trace of.
    dims : tuple of ints.
        A tuple of integers encoding the dimensions of each subsystem.
    axis : int
        The index of the subsystem to be traced out
        from the tensor product that defines expr.
       r   r
   z.partial_trace only supports 2-d square arrays.z/Invalid axis argument, should be between 0 and z, got .zBDimension of system doesn't correspond to dimension of subsystems.)r   cast_to_constndimr   
ValueErrorlennpprodsumranger   )r   r   r   r   s       r   partial_tracer'   :   s    * d#Dyy1}

1A6IJJax43t9$=c$i[tfTUV
 	
 zz!}%]^^E$t*4EFqdAtT*FGGFs   C)r   )__doc__typingr   r   numpyr#   scipy.sparsesparser   cvxpy.atoms.atomr   intr   r'    r   r   <module>r0      sY    #   !3 eCj  DHeCj H Hr   