
    \h%                         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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  SSKJrJrJrJr  SSK J!r!  S/r"S r#S r$S r%S r&g)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                 0    U R                  [        S 5      $ )zETransform the inner products in an expression by calling ``.doit()``.c                  .    [        U 6 R                  5       $ N)r   doitargss    T/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/quantum/qapply.py<lambda>ip_doit_func.<locals>.<lambda>#   s    t1D1I1I1K    )replacer   es    r   ip_doit_funcr$   !   s    99\#KLLr    c                 0    U R                  [        S 5      $ )z;Transform the sums in an expression by calling ``.doit()``.c                  .    [        U 6 R                  5       $ r   )r   r   r   s    r   r   sum_doit_func.<locals>.<lambda>(   s    T
(9r    )r!   r   r"   s    r   sum_doit_funcr(   &   s    99S9::r    c           
         SSK Jn  UR                  SS5      nUR                  SS5      nUR                  SS5      n[        U 5      n U R                  [
        :X  a  U(       a  [        U 5      $ U $ U R                  SSS9n [        U [        5      (       a  U $ [        U [        5      (       a4  SnU R                   H  nU[        U40 UD6-  nM     UR                  5       $ [        X5      (       a2  U R                   VV	s/ s H  u  nn	[        U40 UD6U	4PM     n
nn	U" U
6 $ [        U [        5      (       a-  [        U R                   Vs/ s H  n[        U40 UD6PM     sn6 $ [        U [        5      (       aB  [        [        U R                  40 UD6/U R                   Q76 nU(       a  [#        U5      nU$ UnU$ [        U [$        5      (       a#  [        U R&                  40 UD6U R(                  -  $ [        U [*        5      (       a  U R-                  5       u  p[+        U6 n[+        U6 nU(       d  UnO4[        U[*        5      (       a  U[/        U40 UD6-  nOU[        U40 UD6-  nX`:X  a%  U(       a  [1        [/        [1        U 5      40 UD65      nU(       a  [        U5      OUnU(       a  [#        U5      nU$ UnU$ U $ s  sn	nf s  snf )	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)
commutatortensorproduct)sympy.physics.quantum.densityr*   getr
   kindr   r$   expand
isinstancer   r   r   r   r   r   functionlimitsr(   r   baseexpr   args_cnc
qapply_Mulr   )r#   optionsr*   r+   r,   r-   resultargstateprobnew_argstc_partnc_partc_mulnc_muls                   r   r   r   +   s   V 6[[5)F{{:u-Hkk)T*GA 	vv")|A0q0 	
D5A !W 
As		66CfS,G,,F }} 
A		ff&$:% E-W-t4$ 	 &!! 
A}	%	%QVVDVva373VDEE 
As		VAJJ2'2>QXX>*2v& 9? 
As		aff((!%%// 
As		**,VgF$$:f888F6&4G44F;6Jvay<G<=F)0f%f*2v& 9?
 M& Es   ?K 
Kc           
      t   [        U R                  5      n[        R                  nS n[	        U5      S::  d  [        U [        5      (       d  U $ UR                  5       nUR                  5       n[        U[        5      (       d  [        U5      R                  (       d/  [        U[        5      (       d  [        U5      R                  (       a  U $ [        U[        5      (       aR  UR                  R                  (       a7  UR                  UR                  UR                  S-
  -  5        UR                  n[        U[         5      (       a'  UR                  UR"                  5        UR$                  n[        U[         5      (       a  UR$                  nUR"                  n[        U[&        [(        45      (       a  UR+                  5       n[        U[,        5      (       aN  [/        U R0                  " X'R                  S   U/-   6 U R0                  " X'R                  S   U/-   6 -   40 UD6U-  $ [/        U R0                  " U6 U-  U-  40 UD6U-  $ [        U[2        5      (       Ga  [5        S UR                   5       5      (       a  [        U[2        5      (       a  [5        S UR                   5       5      (       a  [	        UR                  5      [	        UR                  5      :X  a  [3        [7        [	        UR                  5      5       Vs/ s H,  n[/        UR                  U   UR                  U   -  40 UD6PM.     sn6 R9                  SS9n[;        U R0                  " U6 40 UD6U-  U-  $ [        U[<        5      (       Ga  [        U[<        5      (       a  [?        UR@                  5      RC                  [?        UR@                  5      5      (       a  [E        S5      eURF                  URF                  -   n	[=        [/        URH                  URH                  -  40 UD6/U	Q76 n[;        U R0                  " U6 U-  40 UD6$ [=        [/        XeRH                  -  40 UD6/URF                  Q76 n[;        U R0                  " U6 U-  40 UD6$ [        U[<        5      (       aI  [=        [/        URH                  U-  40 UD6/URF                  Q76 n[;        U R0                  " U6 U-  40 UD6$ [K        USS 5      n
U
b   U
" U40 UD6nOS nUc  [K        US	S 5      nUb
   U" U40 UD6nUc5  [        U[N        5      (       a   [        U[P        5      (       a  [S        Xe5      n[        U[T        [V        [X        45      (       a  [[        U5      $ Uc3  [	        U5      S:X  a  U $ [;        U R0                  " X&/-   6 40 UD6U-  U-  $ [        U[R        5      (       a  U[;        U R0                  " U6 40 UD6-  U-  $ [/        U R0                  " U6 U-  40 UD6U-  $ s  snf ! [L         a    S n GNf = f! [L         a    S n GNf = f)
N   r   c              3   z   #    U  H1  n[        U[        [        [        [        45      =(       d    US :H  v   M3     g7frG   Nr4   r   r   r   r   .0r=   s     r   	<genexpr>qapply_Mul.<locals>.<genexpr>   s4     -{rzknjxPSUX>Y.Z.f^aef^f.frz   9;c              3   z   #    U  H1  n[        U[        [        [        [        45      =(       d    US :H  v   M3     g7frI   rJ   rK   s     r   rM   rN      s7       3Awps:cHeUXZ]C^3_3kcfjkck3kwrO   T)r/   z4Duplicated dummy indices in separate sums in qapply._apply_operator_apply_from_right_to).listr   r   Onelenr4   r   popr   r	   is_commutativer   r8   
is_Integerappendr7   r   ketbrar   r   r   r   r   funcr   allranger3   r:   r   set	variablesintersection
ValueErrorr6   r5   getattrNotImplementedErrorr   r   r   intcomplexfloatr
   )r#   r;   r   extrar<   rhslhscommnr6   _apply_apply_rights               r   r:   r:      sT   <DEEEF 4yA~Z3//
((*C
((*C sL))gcl.I.IC..73<3N3N #s 2 2CHHsww{+,hh #|$$CGGgg#|$$gg #
N344xxzdC  1s 3351s 3356  	  !&&$-,S0<G<UBB #}%%#-{rurzrz-{*{*{sM**s  3Awzww  3A  0A  0AMS]*UZ[^_b_g_g[hUi jUiPQCHHQK(?!K7!KUi jkrr  BFr  G!&&$-373F:5@@ #sc33==!..s3==/ABB !WXXZZ#**,FS\\ 9EWEOOFaffdmF2>g>>LL 0<G<JszzJFaffdmF2>g>>#sVCLL,88F3::F!&&$-.:':: S+T2F	C+7+F ~s$:DA#%c5W5 ~c7##
3(@(@!#+F &3/00	t9>Haffte|5AA#EeKK	FL	)	)j:'::5@@affdmF*6g6u<<k !k0 # 	F	 ' s*   3X:	X 	X' X$#X$'X76X7N)'__doc__sympy.concreter   sympy.core.addr   sympy.core.kindr   sympy.core.mulr   sympy.core.powerr   sympy.core.singletonr   sympy.core.sympifyr	   r
   $sympy.physics.quantum.anticommutatorr    sympy.physics.quantum.commutatorr   sympy.physics.quantum.daggerr   "sympy.physics.quantum.innerproductr   sympy.physics.quantum.operatorr   r   sympy.physics.quantum.stater   r   r   r   #sympy.physics.quantum.tensorproductr   __all__r$   r(   r   r:    r    r   <module>r      s]      &    " 0 ? 7 / ; A M M = M
;
tne=r    