K i%dZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZmZdd lmZdd lmZdd lmZdd lmZdd lmZmZddlmZmZmZmZddl m!Z!dgZ"dZ#dZ$dZ%dZ&y)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) OuterProductOperator)StateKetBaseBraBase Wavefunction) TensorProductqapplyc0|jtdS)zETransform the inner products in an expression by calling ``.doit()``.c.t|jSN)rdoitargss b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/qapply.pyzip_doit_func..#st1D1I1I1K)replaceres r ip_doit_funcr#!s 99\#K LLrc0|jtdS)z;Transform the sums in an expression by calling ``.doit()``.c.t|jSr)rrrs rrzsum_doit_func..(sT (9r)r rr!s r sum_doit_funcr&&s 99S9 ::rc ddlm}|jdd}|jdd}|jdd}t|}|jt k(r|r t |S|S|jdd}t|tr|St|tr2d}|jD]}|t|fi|z }|jSt||r/|jD cgc]\}} t|fi|| f} }} || St|tr*t|jD cgc]} t| fi|c} St|tr=tt|jfi|g|j }|r t#|}|S|}|St|t$r#t|j&fi||j(zSt|t*r|j-\} } t+| }t+| }| s|}n/t|t*r|t/|fi|z}n|t|fi|z}||k(r |rt1t/t1|fi|}|r t |n|}|r t#|}|S|}|S|Scc} }wcc} w) aApply 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>>> qapply(A * b.dual / (b * b.dual)) |k> >>> qapply(k.dual * A / (k.dual * k)) QXX>*2v& 9?  As aff((!%%// As **,V gF  $:f888F6&4G44F Q;6Jvayzqapply_Mul..s2-{knjxPSUX>Y.Z.f^aef^f.f-{13c3jK|]+}t|ttttfxs|dk(-ywrGrHrIs rrKzqapply_Mul..s83Aps:cHeUXZ]C^3_3kcfjkck3k3ArLT)r-z4Duplicated dummy indices in separate sums in qapply._apply_operator_apply_from_right_to).listrrOnelenr2rpoprr is_commutativerr6 is_Integerappendr5rketbrar r rrrfuncrallranger1r8rset variables intersection ValueErrorr4r3getattrNotImplementedErrorrrrintcomplexfloatr ) r"r9rextrar:rhslhscommnr4_apply _apply_rights rr8r8s- g> >CLL 0g> >#sVCLL,88F3::F&!&&$-.:'::S+T 2F  C+7+F ~s$:DA  # %c5W5~ c7 # 3(@!#s+F&3/0  t9>Hfafftse|5AA#EeK K FL )j:'::5@@faffdmF*6g6u<r}s] & "0?7/;AMM=  M ; tne=r