
    oVh71                        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,j`                  jc                  ee      d        Z2e,j`                  jc                  ee      d        Z3e,j`                  jc                  ee      d        Z4y)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 y)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    S/home/dcms/DCMS/lib/python3.12/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y)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                 F    d | j                   D        } | j                  | S )zBCalculate the kind of a tensor product by looking at its children.c              3   4   K   | ]  }|j                     y wN)kind).0as     r    	<genexpr>z%TensorProduct.kind.<locals>.<genexpr>   s     /QVV/s   )args_kind_dispatcher)self	arg_kindss     r    r)   zTensorProduct.kind   s%     0TYY/	$t$$i00r"   c                 $   t        |d   t        t        t        t        f      rt        | S | j                  t        |            \  }}t        | }t        |      dk(  r|S t        |      dk(  r||d   z  S t        j                  | g| }||z  S )Nr      )
isinstanceMatrixImmutableMatrixr   r   r   flattenr   r   lenr   __new__)clsr-   c_partnew_argstps        r    r8   zTensorProduct.__new__   s    d1g4G I J($//;;wt}5fx=AM]aHQK''c-H-BB;r"   c                     g }g }|D ]S  }|j                         \  }}|j                  t        |             |j                  t	        j
                  |             U ||fS r(   )args_cncextendlistappendr   
_from_args)r9   r-   r:   nc_partsargcpncps          r    r6   zTensorProduct.flatten   s^      	1CllnGBMM$r(#OOCNN3/0	1 xr"   c                 ^    t        | j                  D cg c]  }t        |       c} S c c}w r(   )r   r-   r   )r/   is     r    _eval_adjointzTensorProduct._eval_adjoint   s#    $))<Qvay<==<s   *c                 2    t        | j                  d      S )NT)tensorproduct)r   expand)r/   ruler-   hintss       r    _eval_rewritezTensorProduct._eval_rewrite   s    d#***>>r"   c                 f   t        | j                        }d}t        |      D ]  }t        | j                  |   t        t
        t        f      r|dz   }||j                  | j                  |         z   }t        | j                  |   t        t
        t        f      r|dz   }||dz
  k7  s|dz   } |S )N ()r2   x)r7   r-   ranger3   r   r   r   _print)r/   printerr-   lengthsrH   s         r    	_sympystrzTensorProduct._sympystr   s    TYYv 	A$))A,c38GGNN499Q<00A$))A,c38GFQJG	 r"   c                    t         rt        d | j                  D              st        d | j                  D              rt        | j                        } |j                  dg| }t        |      D ]"  } |j                  dg| }t        | j                  |   j                        }t        |      D ]f  } |j                  | j                  |   j                  |   g| }	t        |j                  |	       }||dz
  k7  sPt        |j                  d       }h t        | j                  |   j                        dkD  rt        |j                  dd       }t        |j                  |       }||dz
  k7  st        |j                  d       }% t        |j                  | j                  d	   j                         }t        |j                  | j                  d	   j                         }|S t        | j                        } |j                  dg| }t        |      D ]  } |j                  | j                  |   g| }t        | j                  |   t        t        f      rt        |j                  d
d       }t        |j                  |       }||dz
  k7  s|j                  rt        |j                  d       }t        |j                  d       } |S )Nc              3   <   K   | ]  }t        |t                y wr(   r3   r   r*   rD   s     r    r,   z(TensorProduct._pretty.<locals>.<genexpr>        ?cZS)?   c              3   <   K   | ]  }t        |t                y wr(   r3   r   r^   s     r    r,   z(TensorProduct._pretty.<locals>.<genexpr>   r_   r`   rQ   r2   , {})leftrightr   rR   rS   u   ⨂ zx )r   allr-   r7   rV   rU   r   rg   parensrf   lbracketrbracketr3   r   r   _use_unicode)
r/   rW   r-   rX   pformrH   
next_pformlength_ij
part_pforms
             r    _prettyzTensorProduct._pretty   s   ?TYY???TYY??^F"GNN2--E6] @+W^^B66
tyy|001x IA!/		!0A0A!0D!Lt!LJ!+Z-=-=j-I!JJHqL(%/1A1A$1G%H
	I tyy|(()A-!+#**3*?"AJ"EKK
$;<
?&I(>?E@  

499Q<+@+@ ABEDIIaL,A,A BCELTYYr)D)v 	@A'		!<t<J$))A,c
3'&&Cs&;
 J 78EFQJ''&4\(]^E&I(>?E	@ r"   c                    t         rt        d | j                  D              st        d | j                  D              rd }dj                  | j                  D cg c]/  } | |j                  |g| t        |j                              1 c}      }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| z   dz   }t        | j                  |   t        t        f      r|d
z   }||dz
  k7  s|dz   } |S c c}w )Nc              3   <   K   | ]  }t        |t                y wr(   r]   r^   s     r    r,   z'TensorProduct._latex.<locals>.<genexpr>   r_   r`   c              3   <   K   | ]  }t        |t                y wr(   rb   r^   s     r    r,   z'TensorProduct._latex.<locals>.<genexpr>   r_   r`   c                     |dk(  r| S d| z  S )Nr2   z\left\{%s\right\} )labelnlabelss     r    _label_wrapz)TensorProduct._latex.<locals>._label_wrap   s     '1uN2F2NNr"   rc   rd   r   re   rQ   z\left(z\right)r2   z\otimes )r   rh   r-   join_print_label_latexr7   lbracket_latexrbracket_latexrU   r3   r   r   rV   )r/   rW   r-   rz   rD   rY   rX   rH   s           r    _latexzTensorProduct._latex   sd   ?TYY???TYY??O 

BF))M;> ((>(>(>w(N(N(+CHH7 M NA #'))A,"="=q"&))A,"="=? ? TYYv 		$A$))A,c
3	M C.'..1===CA$))A,c
3
NFQJO		$ %Ms   4E>c           
      l    t        | j                  D cg c]  } |j                  di | c} S c c}w )Nrw   )r   r-   doit)r/   rN   items      r    r   zTensorProduct.doit   s-    diiHdytyy151HIIHs   1c                    | j                   }g }t        t        |            D ]  }t        ||   t              s||   j                   D ]  }t        |d| |fz   ||dz   d z    }|j                         \  }}t        |      dk(  r't        |d   t
              r|d   j                         f}|j                  t        | t        | z           n |rt	        | S | S )z*Distribute TensorProducts across addition.Nr2   r   )
r-   rU   r7   r3   r   r   r>   _eval_expand_tensorproductrA   r   )	r/   rN   r-   add_argsrH   aar<   r:   nc_parts	            r    r   z(TensorProduct._eval_expand_tensorproduct   s    yys4y! 
	A$q'3'q',, @B&RaB5(84A<(GHB&(kkmOFG 7|q(Z
M-R#*1:#H#H#J"MOOCLg$>?@ 
	 >!Kr"   c           	      d   |j                  dd       }| }|t        |      dk(  r7t        |j                  D cg c]  }t	        |      j                          c} S t        t        |j                        D cg c]$  \  }}||v rt	        |      j                         n|& c}} S c c}w c c}}w )Nindicesr   )getr7   r   r-   r   r   	enumerate)r/   kwargsr   exprD   idxvalues          r    _eval_tracezTensorProduct._eval_trace  s    **Y-?c'la/388<CC<==+4SXX+>@'S% .1G^E)F @ A A =@s    B'9)B,
N)__name__
__module____qualname____doc__is_commutativer   r.   propertyr)   r8   classmethodr6   rI   rO   rZ   rr   r   r   r   r   rw   r"   r    r   r   9   su    BF N%&ESWX1 1
    >?*X:J*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   rN   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	   r4   sympy.matrices.immutabler
   r5    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   rw   r"   r    <module>r      s        *    & 6 L 7 @ / 
 
 1 *   " cAD cAL*&0 ((F G ((8< = ((8< =r"   