K iddlmZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z ddlmZd Zd Zd ZGd d eZy))Add)Tuple)Expr)Mul)Pow)default_sort_key)sympify)Matrixct|}t|trI|js<|js0|j s$|j s|jr |jryy)z Helper method used in TrTF) r isinstancer is_Integeris_Float is_Rational is_Number is_Symbolis_commutative)es a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/trace.py _is_scalarr sF  A!T LLAJJ MMQ[[ [[Q-- ct|dk(r|St|t}t|Dcgc] \}}||k(s |}}}t |}|j ||j t||dztt|dz Dcgc]}|||||dzg}}|jt|}|||||t|z}|Scc}}wcc}w)a5 Cyclic permutations based on canonical ordering Explanation =========== This method does the sort based ascii values while a better approach would be to used lexicographic sort. TODO: Handle condition such as symbols have subscripts/superscripts in case of lexicographic sort )keyr) lenminr enumeratelistextendappendrangeindex) lmin_itemixindiceslesublistidx ordered_ls r_cycle_permuter+s 1v{1*+H&q\;TQQ(]q;G; aBIIaL NN3q6GAJ&' S\A%&(171:ga!en-.(G( --G %C73< s1v 56I '<(s C%C%C+ct|dk(r|St|dd}|j|ddt|jS)zk this just moves the last arg to first position to enable expansion of args A,B,A ==> A**2,B rNr)rrrrargs)r"r%s r_rearrange_argsr/BsC  1v{ QrsV AHHQqW 7<<rcHeZdZdZdZedZdZedZdZ dZ y) Tra Generic Trace operation than can trace over: a) SymPy matrix b) operators c) outer products Parameters ========== o : operator, matrix, expr i : tuple/list indices (optional) Examples ======== # TODO: Need to handle printing a) Trace(A+B) = Tr(A) + Tr(B) b) Trace(scalar*Operator) = scalar*Trace(Operator) >>> from sympy.physics.quantum.trace import Tr >>> from sympy import symbols, Matrix >>> a, b = symbols('a b', commutative=True) >>> A, B = symbols('A B', commutative=False) >>> Tr(a*A,[2]) a*Tr(A) >>> m = Matrix([[1,2],[1,1]]) >>> Tr(m) 2 c t|dk(r>t|dtttfst|d}n t|d}|d}n)t|dk(rt}|d}n t dt|t r|jSt|dr%t|jr|jSt|tr*t|jDcgc]}t||c}St|tra|j\}}t|dk(rt|Stj |t||}t|dkDr t||zS|St|t"rIt%|jdrt%|jdr|Stj |||St%|r|Stj |||Scc}w)z Construct a Trace object. Parameters ========== args = SymPy expression indices = tuple/list if indices, optional rrz5Arguments to Tr should be of form (expr[, [indices]])trace)rr rrtuple ValueErrorr r4hasattrcallablerr.r1rargs_cncr__new__rr)clsr.r&exprargc_partnc_partobjs rr:z Tr.__new__ns INd1geU';<Q.a/7D$i1ngG7D34 4 dF #::<  T7 #(<::<  c "TYY?cC)?@ @ c ""mmoOFG7|q F|#ll3W w@,/v;?sF|C'CC c "499Q<(tyy|, ||Cw774  <<T73 3)@s*G/cP|jd}|j}|jS)Nr)r.kind element_kind)selfr< expr_kinds rrBzTr.kinds$yy|II %%%rc t|jddr,|jdj|jdS|S)a Perform the trace operation. #TODO: Current version ignores the indices set for partial trace. >>> from sympy.physics.quantum.trace import Tr >>> from sympy.physics.quantum.operator import OuterProduct >>> from sympy.physics.quantum.spin import JzKet, JzBra >>> t = Tr(OuterProduct(JzKet(1,1), JzBra(1,1))) >>> t.doit() 1 r _eval_tracer)r&)r7r.rG)rDhintss rdoitzTr.doits? 499Q< /99Q<++DIIaL+A A rcy)NT)rDs r is_numberz Tr.is_numbersrc`|dkDr&|t|jdjz}n/t|t|jdjz }t|jdj| d|jdjd| z}t t |S)a Permute the arguments cyclically. Parameters ========== pos : integer, if positive, shift-right, else shift-left Examples ======== >>> from sympy.physics.quantum.trace import Tr >>> from sympy import symbols >>> A, B, C, D = symbols('A B C D', commutative=False) >>> t = Tr(A*B*C*D) >>> t.permute(2) Tr(C*D*A*B) >>> t.permute(-2) Tr(C*D*A*B) rN)rr.absrr1r)rDposr.s rpermutez Tr.permutes* 7DIIaL--..CHs499Q<#4#4556CDIIaL%%sde,tyy|/@/@C4/HHI#,rct|jdtr,tt |jdj}n|jdg}t ||jdfzS)Nrr)r r.rr+r/r5)rDr.s r_hashable_contentzTr._hashable_contentsZ diilC (!/$))A,2C2C"DEDIIaL>DT{diil---rN) __name__ __module__ __qualname____doc__r:propertyrBrIrLrPrRrKrrr1r1OsD<34j&& $  <.rr1N)sympy.core.addrsympy.core.containersrsympy.core.exprrsympy.core.mulrsympy.core.powerrsympy.core.sortingrsympy.core.sympifyr sympy.matricesr rr+r/r1rKrrr`s;'  /&! %P W.W.r