o
    GZh71                     @   sZ  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d Z+G dd deZ,dd Z-dd Z.dd Z/e,j01eedd Z2e,j01eedd Z3e,j01eed 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                 C   s   | 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)combined r   R/var/www/auris/lib/python3.10/site-packages/sympy/physics/quantum/tensorproduct.pycombined_tensor_printing)   s   r!   c                   @   s   e Zd ZdZdZedddZedd Zdd	 Z	e
d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd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
    FZTensorProduct_kind_dispatcherT)Zcommutativec                 C   s   dd | j D }| j| S )zBCalculate the kind of a tensor product by looking at its children.c                 s   s    | ]}|j V  qd S N)kind).0ar   r   r    	<genexpr>   s    z%TensorProduct.kind.<locals>.<genexpr>)args_kind_dispatcher)selfZ	arg_kindsr   r   r    r#      s   
zTensorProduct.kindc                 G   sz   t |d ttttfrt| S | t|\}}t| }t	|dkr$|S t	|dkr0||d  S t
j| g|R  }|| S )Nr      )
isinstanceMatrixImmutableMatrixr   r   r   flattenr   r   lenr   __new__)clsr'   c_partnew_argstpr   r   r    r0      s   zTensorProduct.__new__c                 C   sD   g }g }|D ]}|  \}}|t| |t| q||fS r"   )args_cncextendlistappendr   Z
_from_args)r1   r'   r2   Znc_partsargcpZncpr   r   r    r.      s   zTensorProduct.flattenc                 C   s   t dd | jD  S )Nc                 S   s   g | ]}t |qS r   r   )r$   ir   r   r    
<listcomp>   s    z/TensorProduct._eval_adjoint.<locals>.<listcomp>r   r'   )r)   r   r   r    _eval_adjoint      zTensorProduct._eval_adjointc                 K   s   t | jddS )NT)Ztensorproduct)r   expand)r)   ruler'   hintsr   r   r    _eval_rewrite   s   zTensorProduct._eval_rewritec                 G   s   t | j}d}t|D ]4}t| j| tttfr|d }||| j|  }t| j| tttfr5|d }||d kr?|d }q|S )N ()r*   x)r/   r'   ranger+   r   r   r   _print)r)   printerr'   lengthsr;   r   r   r    	_sympystr   s   
zTensorProduct._sympystrc           
      G   s  t rtdd | jD stdd | jD rt| j}|jdg|R  }t|D ]d}|jdg|R  }t| j| j}t|D ]%}|j| j| j| g|R  }	t||	 }||d krdt|d }q?t| j| jdkrxt|jddd	 }t|| }||d krt|d }q(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rt|jddd	 }t|| }||d kr|jrt|d }qt|d }q|S )Nc                 s       | ]}t |tV  qd S r"   r+   r   r$   r9   r   r   r    r&          z(TensorProduct._pretty.<locals>.<genexpr>c                 s   rN   r"   r+   r   rP   r   r   r    r&      rQ   rD   r*   , {})leftrightr   rE   rF   u   ⨂ zx )r   allr'   r/   rI   rH   r
   rW   parensrV   ZlbracketZrbracketr+   r   r   Z_use_unicode)
r)   rJ   r'   rK   Zpformr;   Z
next_pformZlength_ijZ
part_pformr   r   r    _pretty   sT   

zTensorProduct._prettyc                    s   t r8tdd | jD stdd | jD r8dd  d fdd| jD }d	| jd
 j|| jd
 jf S t| j}d}t|D ]:}t| j| t	t
frS|d }|d j| j| gR   d }t| j| t	t
frs|d }||d kr}|d }qC|S )Nc                 s   rN   r"   rO   rP   r   r   r    r&      rQ   z'TensorProduct._latex.<locals>.<genexpr>c                 s   rN   r"   rR   rP   r   r   r    r&      rQ   c                 S   s   |dkr| S d|  S )Nr*   z\left\{%s\right\}r   )labelZnlabelsr   r   r    _label_wrap   r?   z)TensorProduct._latex.<locals>._label_wraprS   c                    s*   g | ]} |j gR  t|jqS r   )Z_print_label_latexr/   r'   rP   r]   r'   rJ   r   r    r<      s
    z(TensorProduct._latex.<locals>.<listcomp>z{%s%s%s}r   rD   z\left(rT   rU   z\right)r*   z\otimes )r   rX   r'   joinZlbracket_latexZrbracket_latexr/   rH   r+   r   r   rI   )r)   rJ   r'   rL   rK   r;   r   r^   r    _latex   s0   

$zTensorProduct._latexc                    s   t  fdd| jD  S )Nc                    s   g | ]
}|j d i  qS )r   )doit)r$   itemrB   r   r    r<      s    z&TensorProduct.doit.<locals>.<listcomp>r=   )r)   rB   r   rc   r    ra      s   zTensorProduct.doitc           	      K   s   | j }g }tt|D ]K}t|| trV|| j D ]:}t|d| |f ||d d   }| \}}t|dkrHt|d trH|d  f}|t	| t	|   q nq|r]t| S | S )z*Distribute TensorProducts across addition.Nr*   r   )
r'   rH   r/   r+   r   r   r5   _eval_expand_tensorproductr8   r   )	r)   rB   r'   Zadd_argsr;   Zaar4   r2   Znc_partr   r   r    rd      s   &z(TensorProduct._eval_expand_tensorproductc                    sT   | dd  | } d u st dkrtdd |jD  S t fddt|jD  S )Nindicesr   c                 S   s   g | ]}t | qS r   r   ra   rP   r   r   r    r<     s    z-TensorProduct._eval_trace.<locals>.<listcomp>c                    s(   g | ]\}}| v rt | n|qS r   rf   )r$   idxvaluere   r   r    r<     s    )getr/   r   r'   	enumerate)r)   kwargsexpr   ri   r    _eval_trace  s   zTensorProduct._eval_traceN)__name__
__module____qualname____doc__Zis_commutativer   r(   propertyr#   r0   classmethodr.   r>   rC   rM   r[   r`   ra   rd   rn   r   r   r   r    r   9   s"    C


,c                 C      t d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Zdeprecated_since_versionZactive_deprecations_targetr   er   r   r    tensor_product_simp_Mul  s   
	r{   c                 C   ru   )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.
        rv   rw   rx   r   ry   r   r   r    tensor_product_simp_Pow4  s   	r|   c                 K   ru   )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.
        rv   rw   rx   r   )rz   rB   r   r   r    r   G  s   	c                 C      t S r"   )r   e1e2r   r   r    find_op_kind_     r   c                 C   r}   r"   )r   r~   r   r   r    find_ket_kindd  r   r   c                 C   r}   r"   )r   r~   r   r   r    find_bra_kindi  r   r   N)5rr   Zsympy.core.addr   Zsympy.core.exprr   Zsympy.core.kindr   Zsympy.core.mulr   Zsympy.core.powerr   Zsympy.core.sympifyr   Zsympy.matrices.denser   r,   Zsympy.matrices.immutabler	   r-   Z sympy.printing.pretty.stringpictr
   Zsympy.utilities.exceptionsr   Zsympy.physics.quantum.daggerr   Zsympy.physics.quantum.kindr   r   r   r   r   r   Z!sympy.physics.quantum.matrixutilsr   r   r   Zsympy.physics.quantum.stater   r   Zsympy.physics.quantum.tracer   __all__r   r!   r   r{   r|   r   r(   registerr   r   r   r   r   r   r    <module>   s@     	 g

