
    sh71                        S 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  SSKJrJrJrJrJrJr  SSK J!r!J"r"J#r#  SSK$J%r%J&r&  SSK'J(r(  SS/r)Sq*S r+ " S S\5      r,S r-S r.S r/\,R`                  Rc                  \\5      S 5       r2\,R`                  Rc                  \\5      S 5       r3\,R`                  Rc                  \\5      S 5       r4g)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                     U q g)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    w/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/sympy/physics/quantum/tensorproduct.pycombined_tensor_printingr!   )   s
     "    c                       \ rS rSrSrSr\" SSS9r\S 5       r	S r
\S	 5       rS
 rS rS rS rS rS rS rS rSrg)r   9   a3  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    S U R                    5       nU R                  " U6 $ )zBCalculate the kind of a tensor product by looking at its children.c              3   8   #    U  H  oR                   v   M     g 7fN)kind).0as     r    	<genexpr>%TensorProduct.kind.<locals>.<genexpr>   s     /YVVYs   )args_kind_dispatcher)self	arg_kindss     r    r*   TensorProduct.kind   s#     0TYY/	$$i00r"   c                 .   [        US   [        [        [        [        45      (       a  [        U6 $ U R                  [        U5      5      u  p#[        U6 n[        U5      S:X  a  U$ [        U5      S:X  a  X#S   -  $ [        R                  " U /UQ76 nX$-  $ )Nr      )
isinstanceMatrixImmutableMatrixr   r   r   flattenr   r   lenr   __new__)clsr/   c_partnew_argstps        r    r;   TensorProduct.__new__   s    d1g4G I J J($//;;wt}5fx=AM]aQK''c-H-B;r"   c                     / n/ nU HT  nUR                  5       u  pVUR                  [        U5      5        UR                  [        R
                  " U5      5        MV     X#4$ r)   )args_cncextendlistappendr   
_from_args)r<   r/   r=   nc_partsargcpncps          r    r9   TensorProduct.flatten   sU     CllnGBMM$r(#OOCNN3/0  r"   c                 d    [        U R                   Vs/ s H  n[        U5      PM     sn6 $ s  snf r)   )r   r/   r   )r1   is     r    _eval_adjointTensorProduct._eval_adjoint   s'    $))<)Qvay)<==<s   -c                 ,    [        U6 R                  SS9$ )NT)tensorproduct)r   expand)r1   ruler/   hintss       r    _eval_rewriteTensorProduct._eval_rewrite   s    d#***>>r"   c                 ~   [        U R                  5      nSn[        U5       H  n[        U R                  U   [        [
        [        45      (       a  US-   nXAR                  U R                  U   5      -   n[        U R                  U   [        [
        [        45      (       a  US-   nXSS-
  :w  d  M  US-   nM     U$ )N ()r5   x)r:   r/   ranger6   r   r   r   _print)r1   printerr/   lengthsrM   s         r    	_sympystrTensorProduct._sympystr   s    TYYvA$))A,c388GNN499Q<00A$))A,c388GQJG  r"   c                    [         (       Ga  [        S U R                   5       5      (       d"  [        S U R                   5       5      (       Ga  [        U R                  5      nUR                  " S/UQ76 n[        U5       GH%  nUR                  " S/UQ76 n[        U R                  U   R                  5      n[        U5       Hh  nUR                  " U R                  U   R                  U   /UQ76 n	[        UR                  U	5      6 nXS-
  :w  d  MQ  [        UR                  S5      6 nMj     [        U R                  U   R                  5      S:  a  [        UR                  SSS96 n[        UR                  U5      6 nXSS-
  :w  d  GM  [        UR                  S5      6 nGM(     [        UR                  U R                  S	   R                  5      6 n[        UR                  U R                  S	   R                  5      6 nU$ [        U R                  5      nUR                  " S/UQ76 n[        U5       H  nUR                  " U R                  U   /UQ76 n[        U R                  U   [        [        45      (       a  [        UR                  S
SS96 n[        UR                  U5      6 nXSS-
  :w  d  M  UR                  (       a  [        UR                  S5      6 nM  [        UR                  S5      6 nM     U$ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr)   r6   r   r+   rH   s     r    r-   (TensorProduct._pretty.<locals>.<genexpr>        ?YcZS))Y   c              3   B   #    U  H  n[        U[        5      v   M     g 7fr)   r6   r   rf   s     r    r-   rg      rh   ri   rX   r5   , {})leftrightr   rY   rZ   u   ⨂ zx )r   allr/   r:   r]   r\   r   rp   parensro   lbracketrbracketr6   r   r   _use_unicode)
r1   r^   r/   r_   pformrM   
next_pformlength_ij
part_pforms
             r    _prettyTensorProduct._pretty   sz   ?TYY????TYY???^FNN2--E6]$^^B66
tyy|001xA!(		!0A0A!0D!Lt!LJ!+Z-=-=j-I!JJqL(%/1A1A$1G%H
	 ) 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3'&&Cs&;
 J 78EQJ''&4\(]^E&I(>?E  r"   c                 J   [         (       a  [        S U R                   5       5      (       d!  [        S U R                   5       5      (       a  S nSR                  U R                   Vs/ s H0  nU" UR                  " U/UQ76 [        UR                  5      5      PM2     sn5      nSU R                  S   R                  < U< U R                  S   R                  < S3$ [        U R                  5      nSn[        U5       H  n[        U R                  U   [        [        45      (       a  US	-   nUS-   UR                  " U R                  U   /UQ76 -   S-   n[        U R                  U   [        [        45      (       a  US
-   nXvS-
  :w  d  M  US-   nM     U$ s  snf )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr)   re   rf   s     r    r-   'TensorProduct._latex.<locals>.<genexpr>   rh   ri   c              3   B   #    U  H  n[        U[        5      v   M     g 7fr)   rk   rf   s     r    r-   r      rh   ri   c                     US:X  a  U $ SU -  $ )Nr5   z\left\{%s\right\} )labelnlabelss     r    _label_wrap)TensorProduct._latex.<locals>._label_wrap   s     '1uN2F2NNr"   rl   rm   r   rn   rX   z\left(z\right)r5   z\otimes )r   rq   r/   join_print_label_latexr:   lbracket_latexrbracket_latexr\   r6   r   r   r]   )r1   r^   r/   r   rH   r`   r_   rM   s           r    _latexTensorProduct._latex   sl   ?TYY????TYY???O 

BF))MBK3 ((>(>w(N(N(+CHH7BKM NA #'))A,"="=q"&))A,"="=? ? TYYvA$))A,c
33	M C'..1===CA$))A,c
33
NQJO  %Ms   *7F c           
      p    [        U R                   Vs/ s H  o"R                  " S0 UD6PM     sn6 $ s  snf )Nr   )r   r/   doit)r1   rT   items      r    r   TensorProduct.doit   s-    diiHidyy151iHIIHs   3c                    U R                   n/ n[        [        U5      5       H  n[        X$   [        5      (       d  M  X$   R                    H  n[        USU U4-   X$S-   S -   6 nUR                  5       u  px[        U5      S:X  a,  [        US   [
        5      (       a  US   R                  5       4nUR                  [        U6 [        U6 -  5        M       O   U(       a  [	        U6 $ U $ )z*Distribute TensorProducts across addition.Nr5   r   )
r/   r\   r:   r6   r   r   rB   _eval_expand_tensorproductrE   r   )	r1   rT   r/   add_argsrM   aar?   r=   nc_parts	            r    r   (TensorProduct._eval_expand_tensorproduct   s    yys4y!A$'3''',,B&RaB5(84A<(GHB&(kkmOF 7|q(Z
M-R-R#*1:#H#H#J"MOOCLg$>? '  " >!Kr"   c           	      t   UR                  SS 5      nU nUb  [        U5      S:X  a:  [        UR                   Vs/ s H  n[	        U5      R                  5       PM     sn6 $ [        [        UR                  5       VVs/ s H%  u  pVXR;   a  [	        U5      R                  5       OUPM'     snn6 $ s  snf s  snnf )Nindicesr   )getr:   r   r/   r   r   	enumerate)r1   kwargsr   exprH   idxvalues          r    _eval_traceTensorProduct._eval_trace  s    **Y-?c'la/388<8CC8<==+4SXX+>@+>ZS .1^E)F+>@ A A =@s   #B/>,B4
r   N)__name__
__module____qualname____firstlineno____doc__is_commutativer   r0   propertyr*   r;   classmethodr9   rN   rU   ra   r{   r   r   r   r   __static_attributes__r   r"   r    r   r   9   su    BF N%&ESWX1 1
    >?*X:J*Ar"   c                     [        SSSS9  U $ )a-  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                     [        SSSS9  U $ )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                     [        SSSS9  U $ )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   rT   s     r    r   r   G  s      	
 "(#B Hr"   c                     [         $ r)   )r   e1e2s     r    find_op_kindr   _  s    r"   c                     [         $ r)   )r   r   s     r    find_ket_kindr   d      Nr"   c                     [         $ 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	   r7   sympy.matrices.immutabler
   r8    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   r0   registerr   r   r   r   r"   r    <module>r      s        *    & 6 L 7 @ / 
 
 1 *   " cAD cAL*&0 ((F G ((8< = ((8< =r"   