
    XhL                     ^   d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZ ddlmZ g dZ G d de          Z G d de          Z G d de          Z  G d de          Z! G d de          Z" G d de          Z#dS )a  Quantum 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)
Derivativeexpand)MulooS
prettyForm)Dagger)OperatorKind)QExprdispatch_method)eye)sympy_deprecation_warning)OperatorHermitianOperatorUnitaryOperatorIdentityOperatorOuterProductDifferentialOperatorc                       e Zd ZU dZdZee         ed<   dZee         ed<   e	d             Z
eZdZd ZeZd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd ZeZd ZdS )r   a
  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_unitaryc                     dS )N)O selfs    p/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/sympy/physics/quantum/operator.pydefault_argszOperator.default_argsn   s    v    ,c                     | j         j        S N)	__class____name__r"   printerargss      r#   _print_operator_namezOperator._print_operator_namez   s    ~&&r%   c                 4    t          | j        j                  S r(   )r   r)   r*   r+   s      r#   _print_operator_name_prettyz$Operator._print_operator_name_pretty   s    $.1222r%   c                     t          | j                  dk    r | j        |g|R  S  | j        |g|R  d | j        |g|R  dS )N   ())lenlabel_print_labelr.   r+   s      r#   _print_contentszOperator._print_contents   sx    tz??a$4$W4t4444 *)'9D99999!!'1D11111 r%   c                     t          | j                  dk    r | j        |g|R  S  | j        |g|R  } | j        |g|R  }t	          |                    dd           }t	          |                    |           }|S )Nr2   r3   r4   leftright)r5   r6   _print_label_prettyr0   r   parensr<   r"   r,   r-   pformlabel_pforms        r#   _print_contents_prettyzOperator._print_contents_pretty   s    tz??a+4+G;d;;;;4D4WDtDDDE2$27BTBBBK$##C#88K K 8 89ELr%   c                     t          | j                  dk    r | j        |g|R  S  | j        |g|R  d | j        |g|R  dS )Nr2   z\left(z\right))r5   r6   _print_label_latex_print_operator_name_latexr+   s      r#   _print_contents_latexzOperator._print_contents_latex   sx    tz??a*4*7:T:::: 0/?$?????''7$77777 r%   c                      t          | d|fi |S )z:Evaluate [self, other] if known, return None if not known._eval_commutatorr   r"   otheroptionss      r#   rH   zOperator._eval_commutator   s    t%7JJ'JJJr%   c                      t          | d|fi |S )z Evaluate [self, other] if known._eval_anticommutatorrI   rJ   s      r#   rN   zOperator._eval_anticommutator   s    t%;UNNgNNNr%   c                      t          | d|fi |S )N_apply_operatorrI   r"   ketrL   s      r#   rP   zOperator._apply_operator   s    t%6GGwGGGr%   c                     d S r(   r    r"   brarL   s      r#   _apply_from_right_tozOperator._apply_from_right_to   s    tr%   c                      t          d          )Nzmatrix_elements is not defined)NotImplementedError)r"   r-   s     r#   matrix_elementzOperator.matrix_element   s    !"BCCCr%   c                 *    |                                  S r(   _eval_inverser!   s    r#   inversezOperator.inverse       !!###r%   c                     | dz  S Nr    r!   s    r#   r\   zOperator._eval_inverse   s    bzr%   )r*   
__module____qualname____doc__r   r   bool__annotations__r   classmethodr$   r   kind_label_separatorr.   rE   r0   r8   rB   rF   rH   rN   rP   rV   rY   r]   invr\   r    r%   r#   r   r   *   sD        @ @B $(L(4.'''!%J%%%  [ D ' ' ' "63 3 3  
 
 
  K K KO O OH H H  D D D$ $ $ C    r%   r   c                   "    e Zd ZdZdZd Zd ZdS )r   a  A 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
    Tc                 d    t          | t                    r| S t                              |           S r(   )
isinstancer   r   r\   r!   s    r#   r\   zHermitianOperator._eval_inverse   s-    dO,, 	0K))$///r%   c                     t          | t                    r|j        rddlm} |j        S |j        r| S t                              | |          S )Nr   r   )	rm   r   is_evensympy.core.singletonr   Oneis_oddr   _eval_power)r"   expr   s      r#   rs   zHermitianOperator._eval_power   s\    dO,, 	{ 222222u ##D#...r%   N)r*   rb   rc   rd   r   r\   rs   r    r%   r#   r   r      sC         $ L0 0 0	/ 	/ 	/ 	/ 	/r%   r   c                       e Zd ZdZdZd ZdS )r   a  A 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                 *    |                                  S r(   r[   r!   s    r#   _eval_adjointzUnitaryOperator._eval_adjoint   r^   r%   N)r*   rb   rc   rd   r   rw   r    r%   r#   r   r      s4         " J$ $ $ $ $r%   r   c                       e Zd ZdZdZdZed             Zed             Z	d Z
d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )r   a,  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                     | j         S r(   )Nr!   s    r#   	dimensionzIdentityOperator.dimension  s	    vr%   c                     t           fS r(   r	   r!   s    r#   r$   zIdentityOperator.default_args  s	    ur%   c                     t          ddd           t          |          dvrt          d|z            t          |          dk    r|d         r|d         nt          | _        d S )	Nz
            IdentityOperator has been deprecated. In the future, please use
            S.One as the identity for quantum operators and states.
            z1.14zdeprecated-operator-identity)deprecated_since_versionactive_deprecations_target)r   r2   z"0 or 1 parameters expected, got %sr2   r   )r   r5   
ValueErrorr
   rz   )r"   r-   hintss      r#   __init__zIdentityOperator.__init__   sv    ! &,'E	
 	
 	
 	
 4yyF""ADHIII YY!^^Q^abr%   c                     t           j        S r(   )r   Zeror"   rK   r   s      r#   rH   z!IdentityOperator._eval_commutator.  s	    vr%   c                     d|z  S )N   r    r   s      r#   rN   z%IdentityOperator._eval_anticommutator1  s    5yr%   c                     | S r(   r    r!   s    r#   r\   zIdentityOperator._eval_inverse4      r%   c                     | S r(   r    r!   s    r#   rw   zIdentityOperator._eval_adjoint7  r   r%   c                     |S r(   r    rQ   s      r#   rP   z IdentityOperator._apply_operator:      
r%   c                     |S r(   r    rT   s      r#   rV   z%IdentityOperator._apply_from_right_to=  r   r%   c                     | S r(   r    )r"   rt   s     r#   rs   zIdentityOperator._eval_power@  r   r%   c                     dS NIr    r+   s      r#   r8   z IdentityOperator._print_contentsC  s    sr%   c                      t          d          S r   r   r+   s      r#   rB   z'IdentityOperator._print_contents_prettyF  s    #r%   c                     dS )Nz{\mathcal{I}}r    r+   s      r#   rF   z&IdentityOperator._print_contents_latexI  s    r%   c                     | j         r| j         t          k    rt          d          |                    dd          }|dk    rt          dd|z  z             t	          | j                   S )NzCCannot represent infinite dimensional identity operator as a matrixformatsympyzRepresentation in format z%s not implemented.)rz   r
   rX   getr   )r"   rL   r   s      r#   _represent_default_basisz)IdentityOperator._represent_default_basisL  s    v 	H2% 'G H H H Xw//W%&A&;f&D'E F F F 46{{r%   N)r*   rb   rc   rd   r   r   propertyr{   rg   r$   r   rH   rN   r\   rw   rP   rV   rs   r8   rB   rF   r   r    r%   r#   r   r      s        * LJ  X   [A A A                       
 
 
 
 
r%   r   c                   r    e Zd ZdZdZd Zed             Zed             Zd Z	d Z
d Zd	 Zd
 Zd Zd ZdS )r   a  An unevaluated outer product between a ket and bra.

    This constructs an outer product between any subclass of ``KetBase`` and
    ``BraBase`` as ``|a><b|``. An ``OuterProduct`` inherits from Operator as they act as
    operators in quantum expressions.  For reference see [1]_.

    Parameters
    ==========

    ket : KetBase
        The ket on the left side of the outer product.
    bar : BraBase
        The bra on the right side of the outer product.

    Examples
    ========

    Create a simple outer product by hand and take its dagger::

        >>> from sympy.physics.quantum import Ket, Bra, OuterProduct, Dagger

        >>> k = Ket('k')
        >>> b = Bra('b')
        >>> op = OuterProduct(k, b)
        >>> op
        |k><b|
        >>> op.hilbert_space
        H
        >>> op.ket
        |k>
        >>> op.bra
        <b|
        >>> Dagger(op)
        |b><k|

    In quantum expressions, outer products will be automatically
    identified and created::

        >>> k*b
        |k><b|

    However, the creation of inner products always has higher priority than that of
    outer products:

        >>> b*k*b
        <b|k>*<b|

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Outer_product
    Fc           	         ddl m}m} t          |          dk    rt	          dt          |          z            t          |d                   }t          |d                   }t          ||t          f          rbt          ||t          f          rJ|                                \  }}|                                \  }	}
t          |          dk    st          |d         |          st          dt          | z            t          |
          dk    st          |
d         |          st          dt          |
 z            |d         
                                |
d         j        k    s+t          d|d         j        d	|
d         j                  t          j        | g|d         |
d         fR i |}|d         j        |_        t          ||	z    |z  S g }t          |t                    rKt          |t                    r6|j        D ]-}|j        D ]#}|                    t%          ||fi |           $.nt          |t                    r,|j        D ]#}|                    t%          ||fi |           $nVt          |t                    r,|j        D ]#}|                    t%          ||fi |           $nt          d
|d	|          t          | S )Nr   )KetBaseBraBaser   z2 parameters expected, got %dr2   z"KetBase subclass expected, got: %rz"BraBase subclass expected, got: %rz"ket and bra are not dual classes: z, z&Expected ket and bra expression, got: )sympy.physics.quantum.stater   r   r5   r   r   rm   r   args_cnc	TypeError
dual_classr)   r   __new__hilbert_spacer   r-   appendr   )clsr-   old_assumptionsr   r   ket_exprbra_exprket_cketsbra_cbrasobjop_termsket_termbra_terms                  r#   r   zOuterProduct.__new__  s   @@@@@@@@t99>><s4yyHIII$q'??$q'??x'300 	/8gs^44	/"++--KE4"++--KE44yyA~~ZQ%A%A~ !,.14j!9 : : : 4yyA~~ZQ%A%A~ !,.14j!9 : : : 7%%''47+<<<i!W&&&Q(9(9;   ,sKd1gtAw%7KKK?KKC $Q 5C(3..h$$ 	Hc)B)B 	$M E E ( E EHOOL8 %D %D3B%D %D E E E EEE #&& 	$M A AXx !@ !@/>!@ !@ A A A AA #&& 	$M A AXx !@ !@/>!@ !@ A A A AA )88%  
 H~r%   c                     | j         d         S )z5Return the ket on the left side of the outer product.r   r-   r!   s    r#   rR   zOuterProduct.ket       y|r%   c                     | j         d         S )z6Return the bra on the right side of the outer product.r2   r   r!   s    r#   rU   zOuterProduct.bra  r   r%   c                 j    t          t          | j                  t          | j                            S r(   )r   r   rU   rR   r!   s    r#   rw   zOuterProduct._eval_adjoint  s&    F48,,fTX.>.>???r%   c                 l    |                     | j                  |                     | j                  z   S r(   _printrR   rU   r+   s      r#   	_sympystrzOuterProduct._sympystr  s)    ~~dh'''..*B*BBBr%   c                 p    | j         j        d |j        | j        g|R  d |j        | j        g|R  dS )Nr3   r&   r4   )r)   r*   r   rR   rU   r+   s      r#   
_sympyreprzOuterProduct._sympyrepr  sY    "n555GN48+d+++++^W^DH-Lt-L-L-L-L-LN 	Nr%   c                      | j         j        |g|R  }t          |                     | j        j        |g|R             S r(   )rR   _prettyr   r<   rU   )r"   r,   r-   r@   s       r#   r   zOuterProduct._pretty  sM      040005;;'7tx'7'G$'G'G'GHHIIr%   c                 X     |j         | j        g|R  } |j         | j        g|R  }||z   S r(   r   )r"   r,   r-   kbs        r#   _latexzOuterProduct._latex  sA    GN48+d+++GN48+d+++1ur%   c                 T     | j         j        di |} | j        j        di |}||z  S )Nr    )rR   
_representrU   )r"   rL   r   r   s       r#   r   zOuterProduct._represent  s?    DH**'**DH**'**s
r%   c                 2     | j         j        | j        fi |S r(   )rR   _eval_tracerU   )r"   kwargss     r#   r   zOuterProduct._eval_trace  s#     $tx#DH77777r%   N)r*   rb   rc   rd   is_commutativer   r   rR   rU   rw   r   r   r   r   r   r   r    r%   r#   r   r   Y  s        3 3h N6 6 6p   X   X@ @ @C C CN N NJ J J  
  
8 8 8 8 8r%   r   c                       e Zd ZdZed             Zed             Zed             Zed             Zd Z	d Z
d Zd	 Zd
S )r   a+  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)

    c                 &    | j         d         j         S )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)
        ra   r   r!   s    r#   	variableszDifferentialOperator.variables  s    . y}!!r%   c                     | j         d         S )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)
        ra   r   r!   s    r#   functionzDifferentialOperator.function4  s    , y}r%   c                     | j         d         S )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)
        r   r   r!   s    r#   exprzDifferentialOperator.exprL  s    . y|r%   c                     | j         j        S )z<
        Return the free symbols of the expression.
        )r   free_symbolsr!   s    r#   r   z!DifferentialOperator.free_symbolse  s     y%%r%   c                     ddl m} | j        }|j        dd          }| j        }| j                            | ||           }|                                } ||g|R  S )Nr   )Wavefunctionr2   )r   r   r   r-   r   r   subsdoit)r"   funcrL   r   varwf_varsfnew_exprs           r#   _apply_operator_Wavefunctionz1DifferentialOperator._apply_operator_Wavefunctionm  sq    <<<<<<n)ABB-M9>>!TT3Z00==??|H/w////r%   c                 b    t          | j        |          }t          || j        d                   S r`   )r   r   r   r-   )r"   symbolr   s      r#   _eval_derivativez%DifferentialOperator._eval_derivativex  s)    di00#Hdim<<<r%   c                 B     | j         |g|R  d | j        |g|R  dS )Nr3   r4   )r.   r7   r+   s      r#   r   zDifferentialOperator._print  sH    %D%g555555Dg------
 	
r%   c                      | j         |g|R  } | j        |g|R  }t          |                    dd           }t          |                    |           }|S )Nr3   r4   r:   )r0   r=   r   r>   r<   r?   s        r#   _print_prettyz"DifferentialOperator._print_pretty  ss    00@4@@@.d.w>>>> S44
 EKK445r%   N)r*   rb   rc   rd   r   r   r   r   r   r   r   r   r   r    r%   r#   r   r     s        ' 'R " " X"0   X.   X0 & & X&	0 	0 	0= = =
 
 
    r%   r   N)$rd   typingr   sympy.core.addr   sympy.core.exprr   sympy.core.functionr   r   sympy.core.mulr   sympy.core.numbersr
   rp   r    sympy.printing.pretty.stringpictr   sympy.physics.quantum.daggerr   sympy.physics.quantum.kindr   sympy.physics.quantum.qexprr   r   sympy.matricesr   sympy.utilities.exceptionsr   __all__r   r   r   r   r   r   r    r%   r#   <module>r      sD  	 	                         4 4 4 4 4 4 4 4       ! ! ! ! ! ! " " " " " " 7 7 7 7 7 7 / / / / / / 3 3 3 3 3 3 > > > > > > > >       @ @ @ @ @ @  U U U U Uu U U Up$/ $/ $/ $/ $/ $/ $/ $/N$ $ $ $ $h $ $ $.V V V V Vx V V VrU8 U8 U8 U8 U88 U8 U8 U8p\ \ \ \ \8 \ \ \ \ \r%   