
    Qi71                        d 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
 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 ddlmZ ddlmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddgZ)da*d Z+ G d de          Z,d Z-d Z.d Z/e,j0        1                    ee          d             Z2e,j0        1                    ee          d             Z3e,j0        1                    ee          d             Z4dS )zAbstract tensor product.    )Add)Expr)KindDispatcher)Mul)Pow)sympify)DenseMatrix)ImmutableDenseMatrix)
prettyFormsympy_deprecation_warningDagger)KetKind_KetKindBraKind_BraKindOperatorKind_OperatorKind)numpy_ndarrayscipy_sparse_matrixmatrix_tensor_product)KetBra)TrTensorProducttensor_product_simpFc                 
    | a dS )a  Set flag controlling whether tensor products of states should be
    printed as a combined bra/ket or as an explicit tensor product of different
    bra/kets. This is a global setting for all TensorProduct class instances.

    Parameters
    ----------
    combine : bool
        When true, tensor product states are combined into one ket/bra, and
        when false explicit tensor product notation is used between each
        ket/bra.
    N)_combined_printing)combineds    {/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/sympy/physics/quantum/tensorproduct.pycombined_tensor_printingr"   )   s     "    c                       e Zd ZdZdZ edd          Zed             Zd Z	e
d             Zd	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )r   a  The tensor product of two or more arguments.

    For matrices, this uses ``matrix_tensor_product`` to compute the Kronecker
    or tensor product matrix. For other objects a symbolic ``TensorProduct``
    instance is returned. The tensor product is a non-commutative
    multiplication that is used primarily with operators and states in quantum
    mechanics.

    Currently, the tensor product distinguishes between commutative and
    non-commutative arguments.  Commutative arguments are assumed to be scalars
    and are pulled out in front of the ``TensorProduct``. Non-commutative
    arguments remain in the resulting ``TensorProduct``.

    Parameters
    ==========

    args : tuple
        A sequence of the objects to take the tensor product of.

    Examples
    ========

    Start with a simple tensor product of SymPy matrices::

        >>> from sympy import Matrix
        >>> from sympy.physics.quantum import TensorProduct

        >>> m1 = Matrix([[1,2],[3,4]])
        >>> m2 = Matrix([[1,0],[0,1]])
        >>> TensorProduct(m1, m2)
        Matrix([
        [1, 0, 2, 0],
        [0, 1, 0, 2],
        [3, 0, 4, 0],
        [0, 3, 0, 4]])
        >>> TensorProduct(m2, m1)
        Matrix([
        [1, 2, 0, 0],
        [3, 4, 0, 0],
        [0, 0, 1, 2],
        [0, 0, 3, 4]])

    We can also construct tensor products of non-commutative symbols:

        >>> from sympy import Symbol
        >>> A = Symbol('A',commutative=False)
        >>> B = Symbol('B',commutative=False)
        >>> tp = TensorProduct(A, B)
        >>> tp
        AxB

    We can take the dagger of a tensor product (note the order does NOT reverse
    like the dagger of a normal product):

        >>> from sympy.physics.quantum import Dagger
        >>> Dagger(tp)
        Dagger(A)xDagger(B)

    Expand can be used to distribute a tensor product across addition:

        >>> C = Symbol('C',commutative=False)
        >>> tp = TensorProduct(A+B,C)
        >>> tp
        (A + B)xC
        >>> tp.expand(tensorproduct=True)
        AxC + BxC
    FTensorProduct_kind_dispatcherT)commutativec                 8    d | j         D             } | j        | S )zBCalculate the kind of a tensor product by looking at its children.c              3   $   K   | ]}|j         V  d S N)kind).0as     r!   	<genexpr>z%TensorProduct.kind.<locals>.<genexpr>   s$      //QV//////r#   )args_kind_dispatcher)self	arg_kindss     r!   r*   zTensorProduct.kind   s)     0/TY///	$t$i00r#   c                 `   t          |d         t          t          t          t          f          r	t          | S |                     t          |                    \  }}t          | }t          |          dk    r|S t          |          dk    r||d         z  S t          j        | g|R  }||z  S )Nr      )
isinstanceMatrixImmutableMatrixr   r   r   flattenr   r   lenr   __new__)clsr.   c_partnew_argstps        r!   r9   zTensorProduct.__new__   s    d1g4G I J J 	0($//;;wt}}55fx==AM]]aHQK''c-H---BB;r#   c                     g }g }|D ]b}|                                 \  }}|                    t          |                     |                    t	          j        |                     c||fS r)   )args_cncextendlistappendr   
_from_args)r:   r.   r;   nc_partsargcpncps          r!   r7   zTensorProduct.flatten   sp      	1 	1CllnnGBMM$r((###OOCN3//0000xr#   c                 2    t          d | j        D              S )Nc                 ,    g | ]}t          |          S  r   )r+   is     r!   
<listcomp>z/TensorProduct._eval_adjoint.<locals>.<listcomp>   s    <<<Qvayy<<<r#   r   r.   )r0   s    r!   _eval_adjointzTensorProduct._eval_adjoint   s    <<$)<<<==r#   c                 <    t          |                     d          S )NT)tensorproduct)r   expand)r0   ruler.   hintss       r!   _eval_rewritezTensorProduct._eval_rewrite   s    d#***>>>r#   c                    t          | j                  }d}t          |          D ]}t          | j        |         t          t
          t          f          r|dz   }||                    | j        |                   z   }t          | j        |         t          t
          t          f          r|dz   }||dz
  k    r|dz   }|S )N ()r3   x)r8   r.   ranger4   r   r   r   _print)r0   printerr.   lengthsrK   s         r!   	_sympystrzTensorProduct._sympystr   s    TYv 	 	A$)A,c388 GGNN49Q<000A$)A,c388 GFQJGr#   c                    t           rt          d | j        D                       st          d | j        D                       rt          | j                  } |j        dg|R  }t          |          D ])} |j        dg|R  }t          | j        |         j                  }t          |          D ]g} |j        | j        |         j        |         g|R  }	t          |                    |	           }||dz
  k    rt          |                    d           }ht          | j        |         j                  dk    rt          |                    dd           }t          |                    |           }||dz
  k    rt          |                    d           }+t          |	                    | j        d	         j
                   }t          |                    | j        d	         j                   }|S t          | j                  } |j        dg|R  }t          |          D ]} |j        | j        |         g|R  }t          | j        |         t          t          f          rt          |                    d
d           }t          |                    |           }||dz
  k    r@|j        rt          |                    d           }t          |                    d           }|S )Nc              3   @   K   | ]}t          |t                    V  d S r)   r4   r   r+   rE   s     r!   r-   z(TensorProduct._pretty.<locals>.<genexpr>   ,      ??cZS))??????r#   c              3   @   K   | ]}t          |t                    V  d S r)   r4   r   rc   s     r!   r-   z(TensorProduct._pretty.<locals>.<genexpr>   rd   r#   rV   r3   , {})leftrightr   rW   rX   u   ⨂ zx )r   allr.   r8   r[   rZ   r   rk   parensrj   lbracketrbracketr4   r   r   _use_unicode)
r0   r\   r.   r]   pformrK   
next_pformlength_ij
part_pforms
             r!   _prettyzTensorProduct._pretty   s    	??TY?????	??TY?????	 ^^F"GN2----E6]] @ @+W^B6666
ty|011x I IA!/	!0A!0D!Lt!L!L!LJ!+Z-=-=j-I-I!JJHqL((%/1A1A$1G1G%H
ty|())A--!+#**3*??"AJ"EKK
$;$;<
??&I(>(>?E

49Q<+@ A ABEDIaL,A B BCELTYr)D)))v 	@ 	@A'	!<t<<<J$)A,c
33 '&&Cs&;;
 J 7 78EFQJ' @&4\(](]^EE&I(>(>?Er#   c                    t           rt          d | j        D                       st          d | j        D                       rUd d                    fd| j        D                       }d| j        d         j        || j        d         j        dS t          | j                  }d	}t          |          D ]}t          | j        |         t          t          f          r|d
z   }|dz    j        | j        |         gR  z   dz   }t          | j        |         t          t          f          r|dz   }||dz
  k    r|dz   }|S )Nc              3   @   K   | ]}t          |t                    V  d S r)   rb   rc   s     r!   r-   z'TensorProduct._latex.<locals>.<genexpr>   rd   r#   c              3   @   K   | ]}t          |t                    V  d S r)   rf   rc   s     r!   r-   z'TensorProduct._latex.<locals>.<genexpr>   rd   r#   c                     |dk    r| nd| z  S )Nr3   z\left\{%s\right\}rJ   )labelnlabelss     r!   _label_wrapz)TensorProduct._latex.<locals>._label_wrap   s     '1uu2F2NNr#   rg   c                 d    g | ],}  |j         gR  t          |j                            -S rJ   )_print_label_latexr8   r.   )r+   rE   r}   r.   r\   s     r!   rL   z(TensorProduct._latex.<locals>.<listcomp>   s^     M M M;> (K(>(>w(N(N(N(N(+CH7 7 M M Mr#   rh   r   ri   rV   z\left(z\right)r3   z\otimes )r   rl   r.   joinlbracket_latexrbracket_latexr8   rZ   r4   r   r   r[   )r0   r\   r.   r^   r]   rK   r}   s    ``   @r!   _latexzTensorProduct._latex   s    	???TY?????	???TY?????	?O O O 

 M M M M M MBF)M M M N NAA #')A,"="=qq"&)A,"="="=? ? TYv 		$ 		$A$)A,c
33 "	M C.'.1=====CA$)A,c
33 #
NFQJOr#   c                 8    t          fd| j        D              S )Nc                 *    g | ]} |j         d i S )rJ   )doit)r+   itemrS   s     r!   rL   z&TensorProduct.doit.<locals>.<listcomp>   s+    HHHdyty11511HHHr#   rM   )r0   rS   s    `r!   r   zTensorProduct.doit   s%    HHHHdiHHHIIr#   c                    | j         }g }t          t          |                    D ]}t          ||         t                    r||         j         D ]}t          |d|         |fz   ||dz   d         z    }|                                \  }}t          |          dk    r6t          |d         t
                    r|d                                         f}|                    t          | t          | z              n|r	t	          | S | S )z*Distribute TensorProducts across addition.Nr3   r   )
r.   rZ   r8   r4   r   r   r?   _eval_expand_tensorproductrB   r   )	r0   rS   r.   add_argsrK   aar=   r;   nc_parts	            r!   r   z(TensorProduct._eval_expand_tensorproduct   s   ys4yy!! 
	 
	A$q'3'' 	q', @ @B&RaRB5(84A<(GHB&(kkmmOFG 7||q((Z
M-R-R(#*1:#H#H#J#J"MOOCLg$>????	  	>!Kr#   c                     |                     dd           | }t                    dk    rt          d |j        D              S t          fdt	          |j                  D              S )Nindicesr   c                 P    g | ]#}t          |                                          $S rJ   r   r   rc   s     r!   rL   z-TensorProduct._eval_trace.<locals>.<listcomp>  s&    <<<CC<<<r#   c                 d    g | ],\  }}|v r!t          |                                          n|-S rJ   r   )r+   idxvaluer   s      r!   rL   z-TensorProduct._eval_trace.<locals>.<listcomp>  sP     @ @ @'S% .1G^^E))) @ @ @r#   )getr8   r   r.   	enumerate)r0   kwargsexpr   s      @r!   _eval_tracezTensorProduct._eval_trace  s    **Y--?c'lla//<<38<<<== @ @ @ @+4SX+>+>@ @ @ A Ar#   N)__name__
__module____qualname____doc__is_commutativer   r/   propertyr*   r9   classmethodr7   rN   rT   r_   rv   r   r   r   r   rJ   r#   r!   r   r   9   s       B BF N%~&ESWXXX1 1 X1
       [ > > >? ? ?  * * *X  :J J J  *A A A A Ar#   c                 *    t          ddd           | S )aE  Simplify a Mul with tensor products.

    .. deprecated:: 1.14.
        The transformations applied by this function are not done automatically
        when tensor products are combined.

    Originally, the main use of this function is to simplify a ``Mul`` of
    ``TensorProduct``s to a ``TensorProduct`` of ``Muls``.
    z
        tensor_product_simp_Mul has been deprecated. The transformations
        performed by this function are now done automatically when
        tensor products are multiplied.
        1.14deprecated-tensorproduct-simpdeprecated_since_versionactive_deprecations_targetr   es    r!   tensor_product_simp_Mulr     s/     	
 "(#B    Hr#   c                 *    t          ddd           | S )zEvaluates ``Pow`` expressions whose base is ``TensorProduct``

    .. deprecated:: 1.14.
        The transformations applied by this function are not done automatically
        when tensor products are combined.
    z
        tensor_product_simp_Pow has been deprecated. The transformations
        performed by this function are now done automatically when
        tensor products are exponentiated.
        r   r   r   r   r   s    r!   tensor_product_simp_Powr   4  s/     	
 "(#B    Hr#   c                 *    t          ddd           | S )a  Try to simplify and combine tensor products.

    .. deprecated:: 1.14.
        The transformations applied by this function are not done automatically
        when tensor products are combined.

    Originally, this function tried to pull expressions inside of ``TensorProducts``.
    It only worked for relatively simple cases where the products have
    only scalars, raw ``TensorProducts``, not ``Add``, ``Pow``, ``Commutators``
    of ``TensorProducts``.
    z
        tensor_product_simp has been deprecated. The transformations
        performed by this function are now done automatically when
        tensor products are combined.
        r   r   r   r   )r   rS   s     r!   r   r   G  s/     	
 "(#B    Hr#   c                     t           S r)   )r   e1e2s     r!   find_op_kindr   _  s    r#   c                     t           S r)   )r   r   s     r!   find_ket_kindr   d      Nr#   c                     t           S r)   )r   r   s     r!   find_bra_kindr   i  r   r#   N)5r   sympy.core.addr   sympy.core.exprr   sympy.core.kindr   sympy.core.mulr   sympy.core.powerr   sympy.core.sympifyr   sympy.matrices.denser	   r5   sympy.matrices.immutabler
   r6    sympy.printing.pretty.stringpictr   sympy.utilities.exceptionsr   sympy.physics.quantum.daggerr   sympy.physics.quantum.kindr   r   r   r   r   r   !sympy.physics.quantum.matrixutilsr   r   r   sympy.physics.quantum.stater   r   sympy.physics.quantum.tracer   __all__r   r"   r   r   r   r   r/   registerr   r   r   rJ   r#   r!   <module>r      s                       * * * * * *                   & & & & & & 6 6 6 6 6 6 L L L L L L 7 7 7 7 7 7 @ @ @ @ @ @ / / / / / /               
         
 1 0 0 0 0 0 0 0 * * * * * *   " " " cA cA cA cA cAD cA cA cAL  *  &  0 ((FF  GF ((8<<  =< ((8<<  =<  r#   