a
    kh%                     @   s   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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 ddlmZmZmZmZ ddl m!Z! dgZ"dd Z#dd Z$dd Z%dd Z&dS )z}Logic for applying operators to states.

Todo:
* Sometimes the final result needs to be expanded, we should do this by hand.
    )Sum)Add)
NumberKind)Mul)Pow)S)sympify_sympify)AntiCommutator)
Commutator)Dagger)InnerProduct)OuterProductOperator)StateKetBaseBraBaseWavefunction)TensorProductqapplyc                 C   s   |  tdd S )zETransform the inner products in an expression by calling ``.doit()``.c                  W   s   t |   S N)r   doitargs r   J/var/www/auris/lib/python3.9/site-packages/sympy/physics/quantum/qapply.py<lambda>#       zip_doit_func.<locals>.<lambda>)replacer   er   r   r   ip_doit_func!   s    r!   c                 C   s   |  tdd S )z;Transform the sums in an expression by calling ``.doit()``.c                  W   s   t |   S r   )r   r   r   r   r   r   r   (   r   zsum_doit_func.<locals>.<lambda>)r   r   r   r   r   r   sum_doit_func&   s    r"   c                    s  ddl m}  dd} dd} dd}t| } | jtkrR|rNt| S | S | jddd} t| t	rn| S t| t
rd}| jD ]}|t|fi  7 }q| S t| |rʇ fd	d
| jD }|| S t| trt fdd
| jD  S t| tr.tt| jfi  g| jR  }|r&t|n|}|S t| trRt| jfi  | j S t| tr|  \}	}
t|	 }t|
 }|
s|}n6t|tr|t|fi   }n|t|fi   }|| kr|rttt| fi  }|rt|n|}|rt|n|}|S | S dS )a  Apply operators to states in a quantum expression.

    Parameters
    ==========

    e : Expr
        The expression containing operators and states. This expression tree
        will be walked to find operators acting on states symbolically.
    options : dict
        A dict of key/value pairs that determine how the operator actions
        are carried out.

        The following options are valid:

        * ``dagger``: try to apply Dagger operators to the left
          (default: False).
        * ``ip_doit``: call ``.doit()`` in inner products when they are
          encountered (default: True).
        * ``sum_doit``: call ``.doit()`` on sums when they are encountered
          (default: False). This is helpful for collapsing sums over Kronecker
          delta's that are created when calling ``qapply``.

    Returns
    =======

    e : Expr
        The original expression, but with the operators applied to states.

    Examples
    ========

        >>> from sympy.physics.quantum import qapply, Ket, Bra
        >>> b = Bra('b')
        >>> k = Ket('k')
        >>> A = k * b
        >>> A
        |k><b|
        >>> qapply(A * b.dual / (b * b.dual))
        |k>
        >>> qapply(k.dual * A / (k.dual * k))
        <b|
    r   )DensitydaggerFsum_doitip_doitT)Z
commutatortensorproductc                    s$   g | ]\}}t |fi  |fqS r   r   ).0stateZproboptionsr   r   
<listcomp>y   s   
zqapply.<locals>.<listcomp>c                    s   g | ]}t |fi  qS r   r(   )r)   tr+   r   r   r-      r   N)Zsympy.physics.quantum.densityr#   getr	   kindr   r!   expand
isinstancer   r   r   r   r   r   functionlimitsr"   r   baseexpr   Zargs_cnc
qapply_Mulr   )r    r,   r#   r$   r%   r&   resultargnew_argsZc_partZnc_partZc_mulZnc_mulr   r+   r   r   +   sV    +






 c           	         sj  t | j}tj}d }t|dks*t| ts.| S | |  ttsRt	j
sft tsjt	 j
rj| S t tr jjr| j jd    j t tr| j  j ttrʈj}jt ttfrP  }t|tr.t| j||jd g  | j||jd g   fi | S t| j| |  fi | S t trtdd  jD rttrtdd jD rt jtjkrt fddtt jD  jdd	}t| j| fi | | S ttrt trvt j !tj r,t"d
 j#j# }tt j$j$ fi g|R  }t| j| | fi S tt j$ fi gj#R  }t| j| | fi S t trtt j$ fi g j#R  }t| j| | fi S t% dd }|d urDz|fi }W n t&y@   d }Y n0 nd }|d u rt%dd }|d urz| fi }W n t&y   d }Y n0 |d u rt t'rtt(rt) }t|t*t+t,frt-|S |d u rt|dkr| S t| j| g  fi  | S nHt|t)rH|t| j| fi  | S t| j| | fi | S d S )N   r   c                 s   s(   | ] }t |ttttfp|d kV  qdS r;   Nr2   r   r   r   r   r)   r9   r   r   r   	<genexpr>   r   zqapply_Mul.<locals>.<genexpr>c                 s   s(   | ] }t |ttttfp|d kV  qdS r<   r=   r>   r   r   r   r?      r   c                    s,   g | ]$}t  j| j|  fi qS r   )r   r   )r)   nlhsr,   rhsr   r   r-      r   zqapply_Mul.<locals>.<listcomp>T)r'   z4Duplicated dummy indices in separate sums in qapply.Z_apply_operatorZ_apply_from_right_to).listr   r   ZOnelenr2   r   popr   r   Zis_commutativer   r6   Z
is_Integerappendr5   r   ZketZbrar   r
   r   r   r   funcr   allranger1   r7   r   set	variablesintersection
ValueErrorr4   r3   getattrNotImplementedErrorr   r   r   intcomplexfloatr	   )	r    r,   r   extrar8   Zcommr4   _applyZ_apply_rightr   rA   r   r7      s    


"",$$$






&r7   N)'__doc__Zsympy.concreter   Zsympy.core.addr   Zsympy.core.kindr   Zsympy.core.mulr   Zsympy.core.powerr   Zsympy.core.singletonr   Zsympy.core.sympifyr   r	   Z$sympy.physics.quantum.anticommutatorr
   Z sympy.physics.quantum.commutatorr   Zsympy.physics.quantum.daggerr   Z"sympy.physics.quantum.innerproductr   Zsympy.physics.quantum.operatorr   r   Zsympy.physics.quantum.stater   r   r   r   Z#sympy.physics.quantum.tensorproductr   __all__r!   r"   r   r7   r   r   r   r   <module>   s(   
w