
    biX                     r    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lmZ ddlmZ ddlmZ  G d	 d
e      Zy)a0  
Copyright 2023, the CVXPY authors

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.
    )ListTupleN)linalg)entropy)settings)Atom)
Constraintc                        e Zd ZdZ eej                  d      Zddee	e	f   ddf fdZ
d ZddZdeeef   fd	Zdefd
Zdee	df   fdZdefdZdefdZdefdZd Zd Zdee   fdZ xZS )quantum_rel_entraw  
    An approximation of the quantum relative entropy between systems with (possibly un-normalized) 
    density matrices :math:`X` and :math`Y:`
     
    .. math::
        \operatorname{tr}\left( X ( \log X - \log Y ) \right).
      
    The approximation uses a quadrature scheme described in https://arxiv.org/abs/1705.00812.

    Parameters
    ----------
    X : Expression or numeric
        A PSD matrix
    Y : Expression or numeric
        A PSD matrix
    quad_approx : Tuple[int, int]
        quad_approx[0] is the number of quadrature nodes and quad_approx[1] is the number of scaling
        points in the quadrature scheme from https://arxiv.org/abs/1705.00812.

    Notes
    -----
    This function does not assume :math:`\operatorname{tr}(X)=\operatorname{tr}(Y)=1,` which
    would be required for most uses of this function in the context of quantum information.
    gư>quad_approxreturnNc                 <    || _         t        t        |   ||       y N)r   superr   __init__)selfXYr   	__class__s       W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/quantum_rel_entr.pyr   zquantum_rel_entr.__init__7   s    &.q!4    c                    |\  }}t        |d      r$t        |d      r|j                  }|j                  }||j                         j                  z   dz  }||j                         j                  z   dz  }t	        j
                  |      \  }}t	        j
                  |      \  }}|j                  t        j                  |j                         j                  |z        dz  z  }t        j                  || j                   k        s#t        j                  || j                   k        rt        j                  S d||dk  <   d||dk  <   t        |       }	|t        j                  |      z  }
|	|
z
  S )Nvalue   r   )hasattrr   conjTLAeighnpabsanyEVAL_TOLinfr   log)r   valuesr   r   w1Vw2Wur1r2s              r   numericzquantum_rel_entr.numeric;   s"   11g71g#6AA^q ^q 
A
ADD266!&&(**q.)Q..66"&'266"~2E+F66MBrAvJBrAvJbk\^Rr   c                     | j                   d   j                         r| j                   d   j                         st        d      y )Nr      z9The arguments to quantum_rel_entr must both be hermitian.)argsis_hermitian
ValueErrorr   s    r   validate_argumentsz#quantum_rel_entr.validate_argumentsO   s?    		!))+		!0I0I0KK  1Lr   c                      y)zCReturns sign (is positive, is negative) of the expression.
        )FF r4   s    r   sign_from_argszquantum_rel_entr.sign_from_argsU   s     r   c                      y)zIs the atom convex?
        Tr7   r4   s    r   is_atom_convexzquantum_rel_entr.is_atom_convexZ   s     r   .c                     t               S )z-Returns the shape of the expression.
        )tupler4   s    r   shape_from_argsz quantum_rel_entr.shape_from_args_   s     wr   c                      y)zIs the atom concave?
        Fr7   r4   s    r   is_atom_concavez quantum_rel_entr.is_atom_concaved        r   c                      y)z;Is the composition non-decreasing in argument idx?
        Fr7   r   idxs     r   is_incrzquantum_rel_entr.is_incri   r@   r   c                      y)z;Is the composition non-increasing in argument idx?
        Fr7   rB   s     r   is_decrzquantum_rel_entr.is_decrn   r@   r   c                     | j                   gS r   )r   r4   s    r   get_datazquantum_rel_entr.get_datas   s      !!r   c                     t               )a+  Gives the (sub/super)gradient of the atom w.r.t. each argument.

        Matrix expressions are vectorized, so the gradient is a matrix.

        Args:
            values: A list of numeric values for the arguments.

        Returns:
            A list of SciPy CSC sparse matrices or None.
        )NotImplementedError)r   r&   s     r   _gradzquantum_rel_entr._gradv   s     "##r   c                 J    | j                   d   dz	  | j                   d   dz	  gS )z?Returns constraints describing the domain of the node.
        r   r0   )r1   r4   s    r   _domainzquantum_rel_entr._domain   s)     		!!499Q<1#455r   ))   rN   )r   N)__name__
__module____qualname____doc__minr   ATOM_EVAL_TOLr#   r   intr   r.   r5   boolr8   r:   r=   r?   rD   rF   rH   rK   r   r	   rM   __classcell__)r   s   @r   r   r      s    2 8))40H5%S/ 5t 5(dDj 1 
 
sCx 
 
d 
d 
"$6j) 6r   r   )rR   typingr   r   numpyr    scipyr   r   scipy.statsr   cvxpyr   cvxpy.atoms.atomr   cvxpy.constraints.constraintr	   r   r7   r   r   <module>r_      s/         ! 3k6t k6r   