K iL8dZddlmZddlmZddlmZddlmZm Z ddl m Z ddl m Z ddlmZdd lmZdd lmZdd lmZdd lmZmZdd lmZddlmZgdZGddeZGddeZGddeZ GddeZ!GddeZ"GddeZ#y)aQuantum mechanical operators. TODO: * Fix early 0 in apply_operators. * Debug and test apply_operators. * Get cse working with classes in this file. * Doctests and documentation of special methods for InnerProduct, Commutator, AntiCommutator, represent, apply_operators. )Optional)Add)Expr) Derivativeexpand)MulooS prettyForm)Dagger) OperatorKind)QExprdispatch_method)eye)sympy_deprecation_warning)OperatorHermitianOperatorUnitaryOperatorIdentityOperator OuterProductDifferentialOperatorceZdZUdZdZeeed<dZeeed<e dZ e Z dZ dZeZdZd Zd Zd Zd Zd ZdZdZdZdZeZdZy)ra Base class for non-commuting quantum operators. An operator maps between quantum states [1]_. In quantum mechanics, observables (including, but not limited to, measured physical values) are represented as Hermitian operators [2]_. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the operator. For time-dependent operators, this will include the time. Examples ======== Create an operator and examine its attributes:: >>> from sympy.physics.quantum import Operator >>> from sympy import I >>> A = Operator('A') >>> A A >>> A.hilbert_space H >>> A.label (A,) >>> A.is_commutative False Create another operator and do some arithmetic operations:: >>> B = Operator('B') >>> C = 2*A*A + I*B >>> C 2*A**2 + I*B Operators do not commute:: >>> A.is_commutative False >>> B.is_commutative False >>> A*B == B*A False Polymonials of operators respect the commutation properties:: >>> e = (A+B)**3 >>> e.expand() A*B*A + A*B**2 + A**2*B + A**3 + B*A*B + B*A**2 + B**2*A + B**3 Operator inverses are handle symbolically:: >>> A.inv() A**(-1) >>> A*A.inv() 1 References ========== .. [1] https://en.wikipedia.org/wiki/Operator_%28physics%29 .. [2] https://en.wikipedia.org/wiki/Observable N is_hermitian is_unitarycy)N)Oselfs d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/operator.py default_argszOperator.default_argsns,c.|jjSN) __class____name__r"printerargss r#_print_operator_namezOperator._print_operator_namezs~~&&&r%c@t|jjSr()rr)r*r+s r#_print_operator_name_prettyz$Operator._print_operator_name_prettys$..1122r%ct|jdk(r|j|g|S|j|g|d|j|g|dS)N())lenlabel _print_labelr.r+s r#_print_contentszOperator._print_contentss` tzz?a $4$$W4t4 4*))'9D9!!!'1D1 r%ct|jdk(r|j|g|S|j|g|}|j|g|}t |j dd}t |j |}|S)Nr2r3r4leftright)r5r6_print_label_prettyr0rparensr<r"r,r-pform label_pforms r#_print_contents_prettyzOperator._print_contents_prettys tzz?a +4++G;d; ;4D44WDtDE2$227BTBK$##C#8K K 89ELr%ct|jdk(r|j|g|S|j|g|d|j|g|dS)Nr2z\left(z\right))r5r6_print_label_latex_print_operator_name_latexr+s r#_print_contents_latexzOperator._print_contents_latexs` tzz?a *4**7:T: :0//?$?'''7$7 r%c t|d|fi|S)z:Evaluate [self, other] if known, return None if not known._eval_commutatorrr"otheroptionss r#rHzOperator._eval_commutatorst%7J'JJr%c t|d|fi|S)z Evaluate [self, other] if known._eval_anticommutatorrIrJs r#rNzOperator._eval_anticommutatorst%;UNgNNr%c t|d|fi|S)N_apply_operatorrIr"ketrLs r#rPzOperator._apply_operatorst%6GwGGr%c yr(r r"brarLs r#_apply_from_right_tozOperator._apply_from_right_tosr%ctd)Nzmatrix_elements is not defined)NotImplementedError)r"r-s r#matrix_elementzOperator.matrix_elements!"BCCr%c"|jSr( _eval_inverser!s r#inversezOperator.inverse!!##r%c |dzSNr r!s r#r\zOperator._eval_inverses bzr%)r* __module__ __qualname____doc__rrbool__annotations__r classmethodr$rkind_label_separatorr.rEr0r8rBrFrHrNrPrVrYr]invr\r r%r#rr*s@B$(L(4.'!%J% D '"63 KOHD$ Cr%rc eZdZdZdZdZdZy)raA Hermitian operator that satisfies H == Dagger(H). Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the operator. For time-dependent operators, this will include the time. Examples ======== >>> from sympy.physics.quantum import Dagger, HermitianOperator >>> H = HermitianOperator('H') >>> Dagger(H) H TcPt|tr|Stj|Sr() isinstancerrr\r!s r#r\zHermitianOperator._eval_inverses" dO ,K))$/ /r%ct|tr,|jrddlm}|j S|j r|Stj||S)Nrr ) rmris_evensympy.core.singletonr Oneis_oddr _eval_power)r"expr s r#rszHermitianOperator._eval_powers> dO ,{{2uu  ##D#..r%N)r*rbrcrdrr\rsr r%r#rrs$L0 /r%rceZdZdZdZdZy)raA unitary operator that satisfies U*Dagger(U) == 1. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the operator. For time-dependent operators, this will include the time. Examples ======== >>> from sympy.physics.quantum import Dagger, UnitaryOperator >>> U = UnitaryOperator('U') >>> U*Dagger(U) 1 Tc"|jSr(r[r!s r# _eval_adjointzUnitaryOperator._eval_adjointr^r%N)r*rbrcrdrrwr r%r#rrs"J$r%rceZdZdZdZdZedZedZ dZ dZ dZ dZ d Zd Zd Zd Zd ZdZdZdZy)ra,An identity operator I that satisfies op * I == I * op == op for any operator op. .. deprecated:: 1.14. Use the scalar S.One instead as the multiplicative identity for operators and states. Parameters ========== N : Integer Optional parameter that specifies the dimension of the Hilbert space of operator. This is used when generating a matrix representation. Examples ======== >>> from sympy.physics.quantum import IdentityOperator >>> IdentityOperator() # doctest: +SKIP I Tc|jSr()Nr!s r# dimensionzIdentityOperator.dimensions vv r%ctfSr(r r!s r#r$zIdentityOperator.default_argss u r%ctdddt|dvrtd|zt|dk(r|dr |d|_yt|_y) Nz IdentityOperator has been deprecated. In the future, please use S.One as the identity for quantum operators and states. z1.14zdeprecated-operator-identity)deprecated_since_versionactive_deprecations_target)rr2z"0 or 1 parameters expected, got %sr2r)rr5 ValueErrorr rz)r"r-hintss r#__init__zIdentityOperator.__init__ sW! &,'E  4yF"ADHI I Y!^Qabr%c "tjSr()r Zeror"rKrs r#rHz!IdentityOperator._eval_commutator.s vv r%c d|zS)Nr rs r#rNz%IdentityOperator._eval_anticommutator1s 5yr%c|Sr(r r!s r#r\zIdentityOperator._eval_inverse4 r%c|Sr(r r!s r#rwzIdentityOperator._eval_adjoint7rr%c |Sr(r rQs r#rPz IdentityOperator._apply_operator: r%c |Sr(r rTs r#rVz%IdentityOperator._apply_from_right_to=rr%c|Sr(r )r"rts r#rszIdentityOperator._eval_power@rr%cyNIr r+s r#r8z IdentityOperator._print_contentsCsr%ctdSrr r+s r#rBz'IdentityOperator._print_contents_prettyFs #r%cy)Nz {\mathcal{I}}r r+s r#rFz&IdentityOperator._print_contents_latexIsr%c |jr|jtk(r td|jdd}|dk7rtdd|zzt |jS)NzCCannot represent infinite dimensional identity operator as a matrixformatsympyzRepresentation in format z%s not implemented.)rzr rXgetr)r"rLrs r#_represent_default_basisz)IdentityOperator._represent_default_basisLsrvv2%'GH HXw/ W %&A&;f&D'EF F466{r%N)r*rbrcrdrrpropertyr{rgr$rrHrNr\rwrPrVrsr8rBrFrr r%r#rrsv*LJ  A  r%rcdeZdZdZdZdZedZedZdZ dZ dZ d Z d Z d Zd Zy )raAn unevaluated outer product between a ket and bra. This constructs an outer product between any subclass of ``KetBase`` and ``BraBase`` as ``|a>>> from sympy.physics.quantum import Ket, Bra, OuterProduct, Dagger >>> k = Ket('k') >>> b = Bra('b') >>> op = OuterProduct(k, b) >>> op |k>>> op.hilbert_space H >>> op.ket |k> >>> op.bra >> Dagger(op) |b>>> k*b |k>>> b*k*b *!@A AH~# &$MM A Xx!@/>!@A AH~ 8% r%c |jdS)z5Return the ket on the left side of the outer product.rr-r!s r#rRzOuterProduct.ketyy|r%c |jdS)z6Return the bra on the right side of the outer product.r2rr!s r#rUzOuterProduct.brarr%cftt|jt|jSr()rrrUrRr!s r#rwzOuterProduct._eval_adjoints!F488,fTXX.>??r%cp|j|j|j|jzSr(_printrRrUr+s r# _sympystrzOuterProduct._sympystrs'~~dhh''..*BBBr%c|jjd|j|jg|d|j|jg|dS)Nr3r&r4)r)r*rrRrUr+s r# _sympyreprzOuterProduct._sympyreprsH"nn55 GNN488 +d +^W^^DHH-Lt-LN Nr%c|jj|g|}t|j|jj|g|Sr()rR_prettyrr<rU)r"r,r-r@s r#rzOuterProduct._prettysH   0405;;'7txx'7'7'G$'GHIIr%c|j|jg|}|j|jg|}||zSr(r)r"r,r-kbs r#_latexzOuterProduct._latexs= GNN488 +d + GNN488 +d +1u r%c ||jjdi|}|jjdi|}||zS)Nr )rR _representrU)r"rLrrs r#rzOuterProduct._represents= DHH   *' * DHH   *' *s r%c P|jj|jfi|Sr()rR _eval_tracerU)r"kwargss r#rzOuterProduct._eval_traces$$txx##DHH777r%N)r*rbrcrdis_commutativerrrRrUrwrrrrrrr r%r#rrYsd3hN6p@CNJ  8r%rcheZdZdZedZedZedZedZdZ dZ dZ d Z y ) ra+An operator for representing the differential operator, i.e. d/dx It is initialized by passing two arguments. The first is an arbitrary expression that involves a function, such as ``Derivative(f(x), x)``. The second is the function (e.g. ``f(x)``) which we are to replace with the ``Wavefunction`` that this ``DifferentialOperator`` is applied to. Parameters ========== expr : Expr The arbitrary expression which the appropriate Wavefunction is to be substituted into func : Expr A function (e.g. f(x)) which is to be replaced with the appropriate Wavefunction when this DifferentialOperator is applied Examples ======== You can define a completely arbitrary expression and specify where the Wavefunction is to be substituted >>> from sympy import Derivative, Function, Symbol >>> from sympy.physics.quantum.operator import DifferentialOperator >>> from sympy.physics.quantum.state import Wavefunction >>> from sympy.physics.quantum.qapply import qapply >>> f = Function('f') >>> x = Symbol('x') >>> d = DifferentialOperator(1/x*Derivative(f(x), x), f(x)) >>> w = Wavefunction(x**2, x) >>> d.function f(x) >>> d.variables (x,) >>> qapply(d*w) Wavefunction(2, x) c4|jdjS)a Returns the variables with which the function in the specified arbitrary expression is evaluated Examples ======== >>> from sympy.physics.quantum.operator import DifferentialOperator >>> from sympy import Symbol, Function, Derivative >>> x = Symbol('x') >>> f = Function('f') >>> d = DifferentialOperator(1/x*Derivative(f(x), x), f(x)) >>> d.variables (x,) >>> y = Symbol('y') >>> d = DifferentialOperator(Derivative(f(x, y), x) + ... Derivative(f(x, y), y), f(x, y)) >>> d.variables (x, y) rarr!s r# variableszDifferentialOperator.variabless.yy}!!!r%c |jdS)ad Returns the function which is to be replaced with the Wavefunction Examples ======== >>> from sympy.physics.quantum.operator import DifferentialOperator >>> from sympy import Function, Symbol, Derivative >>> x = Symbol('x') >>> f = Function('f') >>> d = DifferentialOperator(Derivative(f(x), x), f(x)) >>> d.function f(x) >>> y = Symbol('y') >>> d = DifferentialOperator(Derivative(f(x, y), x) + ... Derivative(f(x, y), y), f(x, y)) >>> d.function f(x, y) rarr!s r#functionzDifferentialOperator.function4s,yy}r%c |jdS)a Returns the arbitrary expression which is to have the Wavefunction substituted into it Examples ======== >>> from sympy.physics.quantum.operator import DifferentialOperator >>> from sympy import Function, Symbol, Derivative >>> x = Symbol('x') >>> f = Function('f') >>> d = DifferentialOperator(Derivative(f(x), x), f(x)) >>> d.expr Derivative(f(x), x) >>> y = Symbol('y') >>> d = DifferentialOperator(Derivative(f(x, y), x) + ... Derivative(f(x, y), y), f(x, y)) >>> d.expr Derivative(f(x, y), x) + Derivative(f(x, y), y) rrr!s r#exprzDifferentialOperator.exprLs.yy|r%c.|jjS)z< Return the free symbols of the expression. )r free_symbolsr!s r#rz!DifferentialOperator.free_symbolses yy%%%r%c ddlm}|j}|jdd}|j}|j j |||}|j}||g|S)Nr) Wavefunctionr2)rrrr-rrsubsdoit)r"funcrLrvarwf_varsfnew_exprs r#_apply_operator_Wavefunctionz1DifferentialOperator._apply_operator_WavefunctionmsY<nn))AB- MM99>>!T3Z0==?H/w//r%c`t|j|}t||jdSr`)rrrr-)r"symbolrs r#_eval_derivativez%DifferentialOperator._eval_derivativexs'dii0#Hdiim<r<r?s r# _print_prettyz"DifferentialOperator._print_prettysg000@4@.d..w>>    S  4 EKK 45 r%N) r*rbrcrdrrrrrrrrrr r%r#rrsl'R""0.0&& 0= r%rN)$rdtypingrsympy.core.addrsympy.core.exprrsympy.core.functionrrsympy.core.mulrsympy.core.numbersr rpr sympy.printing.pretty.stringpictrsympy.physics.quantum.daggerrsympy.physics.quantum.kindrsympy.physics.quantum.qexprrrsympy.matricesrsympy.utilities.exceptionsr__all__rrrrrrr r%r#rs  4!"7/3>@ UuUp$/$/N$h$.VxVrU88U8p\8\r%