K i*:dZddlmZddlmZddlmZddlmZddl m Z m Z ddl m Z ddlmZdd lmZmZmZdd lmZmZmZdd lmZmZmZdd lmZe d Z ej?eedZ ejCeefde ejCeefde ejCeefde ej?eedZ"ej?eedZ#ej?eedZ$ej?eedZ%ej?eedZ&ej?eedZ'ej?eedZ(ej?eedZ)ej?eedZ*ej?eedZ+ej?eedZ,ej?eedZ-dZ.d Z/d!Z0e.ge/gd"ejbe<e.ge0gd"ejbe<d#e.giejbe<y$)%a)Transforms that are always applied to quantum expressions. This module uses the kind and _constructor_postprocessor_mapping APIs to transform different combinations of Operators, Bras, and Kets into Inner/Outer/TensorProducts. These transformations are registered with the postprocessing API of core classes like `Mul` and `Pow` and are always applied to any expression involving Bras, Kets, and Operators. This API replaces the custom `__mul__` and `__pow__` methods of the quantum classes, which were found to be inconsistent. THIS IS EXPERIMENTAL. )Basic)Expr)Mul)S) Dispatcher#ambiguity_register_error_ignore_dup)debug InnerProduct)KetKindBraKind OperatorKind) OuterProductIdentityOperatorOperator)BraBaseKetBase StateBase) TensorProduct_transform_state_paircy)z5Default transformer that does nothing for base types.Nabs f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/transforms.py_transform_exprr7s c|fSNrxys rr$@!r) on_ambiguityc|fSr rr!s rr$r$Er%rc"tjSr )rOner!s rr$r$Js rct||fS)z.Transform a bra*ket -> InnerProduct(bra, ket).r rs r_transform_bra_ketr+N A   rct||fS)z.Transform a keT*bra -> OuterProduct(ket, bra).)rrs r_transform_ket_brar.Sr,rctd)zRaise a TypeError if a user tries to multiply two kets. Multiplication based on `*` is not a shorthand for tensor products. zEMultiplication of two kets is not allowed. Use TensorProduct instead. TypeErrorrs r_transform_ket_ketr2X O rctd)zRaise a TypeError if a user tries to multiply two bras. Multiplication based on `*` is not a shorthand for tensor products. zEMultiplication of two bras is not allowed. Use TensorProduct instead.r0rs r_transform_bra_brar5br3rcFt|j||jfSr )r braketrs r_transform_op_ketr9ls  "AEE **rcFt||j|jfSr )r r8r7rs r_transform_bra_opr;ps AEE "AEE **rc@|jtk(r tdy)zRaise a TypeError if a user tries to multiply TensorProduct(*kets)*ket. Multiplication based on `*` is not a shorthand for tensor products. z6Multiplication of TensorProduct(*kets)*ket is invalid.Nkindr r1rs r_transform_tp_ketr?t%  vv D  rc@|jtk(r tdy)zRaise a TypeError if a user tries to multiply ket*TensorProduct(*kets). Multiplication based on `*` is not a shorthand for tensor products. z6Multiplication of ket*TensorProduct(*kets) is invalid.Nr=rs r_transform_ket_tprBr@rc@|jtk(r tdy)zRaise a TypeError if a user tries to multiply TensorProduct(*bras)*bra. Multiplication based on `*` is not a shorthand for tensor products. z6Multiplication of TensorProduct(*bras)*bra is invalid.Nr>r r1rs r_transform_tp_brarEr@rc@|jtk(r tdy)zRaise a TypeError if a user tries to multiply bra*TensorProduct(*bras). Multiplication based on `*` is not a shorthand for tensor products. z6Multiplication of bra*TensorProduct(*bras) is invalid.NrDrs r_transform_bra_tprGr@rc td||t|jt|jk(r|jtk(rY|jt k(rFt t|j|jDcgc]\}}t||c}}Stdt|j|jDfSycc}}w)zECombine a product of tensor products if their number of args matches._transform_tp_tpc3,K|] \}}||zywr r).0ijs r z#_transform_tp_tp..s#JFQAaC#JsN) r lenargsr>r r tuplezipr r)rrrLrMs rrIrIs a# 166{c!&&k! 66W 7!23qvvqvv;NO!Q,q!,OP P!#Jc!&&!&&6I#JKN N "OsC ct|j|jt|j|jfS)z9Extract an inner produt from a product of outer products.)r r7r8rrs r_transform_op_oprTs-  & QUUAEE(B CCrct|j}g}t|dkDr|jd}|d}t ||}||j |n:|jd|j d|d|j|ddt|dkDr|r|j |dtj|dS)aTransform a ``Mul`` of quantum expressions into canonical form. This function is registered ``_constructor_postprocessor_mapping`` as a transformer for ``Mul``. This means that every time a quantum expression is multiplied, this function will be called to transform it into canonical form as defined by the binary functions registered with ``_transform_state_pair``. The algorithm of this function is as follows. It walks the args of the input ``Mul`` from left to right and calls ``_transform_state_pair`` on every overlapping pair of args. Each time ``_transform_state_pair`` is called it can return a tuple of items or None. If None, the pair isn't transformed. If a tuple, then the last element of the tuple goes back into the args to be transformed again and the others are extended onto the result args list. The algorithm can be visualized in the following table: step result args ============================================================================ #0 [] [a, b, c, d, e, f] #1 [] [T(a,b), c, d, e, f] #2 [T(a,b)[:-1]] [T(a,b)[-1], c, d, e, f] #3 [T(a,b)[:-1]] [T(T(a,b)[-1], c), d, e, f] #4 [T(a,b)[:-1], T(T(a,b)[-1], c)[:-1]] [T(T(T(a,b)[-1], c)[-1], d), e, f] #5 ... One limitation of the current implementation is that we assume that only the last item of the transformed tuple goes back into the args to be transformed again. These seems to handle the cases needed for Mul. However, we may need to extend the algorithm to have the entire tuple go back into the args for further transformation. rNF)is_commutative) listrPrOpoprappendinsertextendr _from_args)exprrPresultfirstsecond transformeds r_postprocess_state_mulrdsD  ?D F d)a- a+E6:   MM%  HHQK KK;r? + MM+cr* +! d)a-&  d1g >>& 77rc|j\}}|jtk(s|jtk(r t dy)zHandle bras and kets raised to powers. Under ``*`` multiplication this is invalid. Users should use a TensorProduct instead. z>A bra or ket to a power is invalid, use TensorProduct instead.N) as_base_expr>r r r1)r_baseexps r_postprocess_state_powrisB   "ID# yyGtyyG3 L   4rc(|j\}}td|||jt|trR|j rE|j r8|jtk(r$|jDcgc]}||z }}t |Syyyycc}w)aHandle TensorProduct(*operators)**(positive integer). This handles a tensor product of operators, to an integer power. The power here is interpreted as regular multiplication, not tensor product exponentiation. The form of exponentiation performed here leaves the space and dimension of the object the same. This operation does not make sense for tensor product's of states. z_postprocess_tp_pow: N) rfr rP isinstancer is_integer is_positiver>r)r_rgrhrnew_argss r_postprocess_tp_powros  "ID# !4dii8$ &3>>cooRVR[R[_kRk$(II.qAsF..h''Slo>&.s5 B)rPowrN)2__doc__sympy.core.basicrsympy.core.exprrsympy.core.mulrsympy.core.singletonr!sympy.multipledispatch.dispatcherrrsympy.utilities.miscr "sympy.physics.quantum.innerproductr sympy.physics.quantum.kindr r rsympy.physics.quantum.operatorrrrsympy.physics.quantum.staterrr#sympy.physics.quantum.tensorproductrrregisterraddr+r.r2r5r9r;r?rBrErGrIrTrdriro"_constructor_postprocessor_mappingrrrrs # "';EEDC=##:;&d+, t4  4 '(4 1!2!1!2!1212 g6+7+6+7+ w7 8 7 8  w7 8 7 8  }=O>O l;D<D<8~  (. # # " #7((3 # #  ;((7  " #6((2r