
    RieoK                     :   d 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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.
    )Add)Expr)
Derivativeexpand)MulooS
prettyForm)Dagger)QExprdispatch_method)eye)OperatorHermitianOperatorUnitaryOperatorIdentityOperatorOuterProductDifferentialOperatorc                   |    e Zd ZdZed             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eZd 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
    c                     dS )N)O selfs    >lib/python3.11/site-packages/sympy/physics/quantum/operator.pydefault_argszOperator.default_argsh   s    v    ,c                     | j         j        S N)	__class____name__r   printerargss      r   _print_operator_namezOperator._print_operator_namer   s    ~&&r    c                 4    t          | j        j                  S r#   )r   r$   r%   r&   s      r   _print_operator_name_prettyz$Operator._print_operator_name_prettyw   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_contentsz   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 )Nr-   r.   r/   leftright)r0   r1   _print_label_prettyr+   r   parensr7   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 )Nr-   z\left(z\right))r0   r1   _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   rC   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_anticommutatorrD   rE   s      r   rI   zOperator._eval_anticommutator   s    t%;UNNgNNNr    c                      t          | d|fi |S )N_apply_operatorrD   r   ketrG   s      r   rK   zOperator._apply_operator   s    t%6GGwGGGr    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   rS   zOperator._eval_inverse   s    bzr    c                 P    t          |t                    r| S t          | |          S r#   )
isinstancer   r   r   rF   s     r   __mul__zOperator.__mul__   s*    e-.. 	K4r    N)r%   
__module____qualname____doc__classmethodr   _label_separatorr)   r@   r+   r3   r=   rA   rC   rI   rK   rP   rT   invrS   r\   r   r    r   r   r   %   s       @ @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#   )rZ   r   r   rS   r   s    r   rS   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
   )	rZ   r   is_evensympy.core.singletonr   Oneis_oddr   _eval_power)r   expr   s      r   rj   zHermitianOperator._eval_power   s\    dO,, 	{ 222222u ##D#...r    N)r%   r]   r^   r_   is_hermitianrS   rj   r   r    r   r   r      sC         $ L0 0 0	/ 	/ 	/ 	/ 	/r    r   c                       e 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
    c                 *    |                                  S r#   rR   r   s    r   _eval_adjointzUnitaryOperator._eval_adjoint   rU   r    N)r%   r]   r^   r_   ro   r   r    r   r   r      s-         $$ $ $ $ $r    r   c                       e 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 ZdS )r   a  An identity operator I that satisfies op * I == I * op == op for any
    operator op.

    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()
    I
    c                     | 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vrt          d|z            t          |          dk    r|d         r|d         nt          | _        d S )N)r   r-   z"0 or 1 parameters expected, got %sr-   r   )r0   
ValueErrorr	   rr   )r   r(   hintss      r   __init__zIdentityOperator.__init__  sP    4yyF""ADHIII YY!^^Q^abr    c                     t           j        S r#   )r   Zeror   rF   rw   s      r   rC   z!IdentityOperator._eval_commutator  s	    vr    c                     d|z  S )N   r   r{   s      r   rI   z%IdentityOperator._eval_anticommutator  s    5yr    c                     | S r#   r   r   s    r   rS   zIdentityOperator._eval_inverse"      r    c                     | S r#   r   r   s    r   ro   zIdentityOperator._eval_adjoint%  r   r    c                     |S r#   r   rL   s      r   rK   z IdentityOperator._apply_operator(      
r    c                     |S r#   r   )r   brarG   s      r   _apply_from_right_toz%IdentityOperator._apply_from_right_to+  r   r    c                     | S r#   r   )r   rk   s     r   rj   zIdentityOperator._eval_power.  r   r    c                     dS NIr   r&   s      r   r3   z IdentityOperator._print_contents1  s    sr    c                      t          d          S r   r   r&   s      r   r=   z'IdentityOperator._print_contents_pretty4  s    #r    c                     dS )Nz{\mathcal{I}}r   r&   s      r   rA   z&IdentityOperator._print_contents_latex7  s    r    c                 ^    t          |t          t          f          r|S t          | |          S r#   )rZ   r   r   r   r[   s     r   r\   zIdentityOperator.__mul__:  s.    eh/00 	L4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.)rr   r	   rO   getr   )r   rG   r   s      r   _represent_default_basisz)IdentityOperator._represent_default_basisA  s    v 	H2% 'G H H H Xw//W%&A&;f&D'E F F F 46{{r    N)r%   r]   r^   r_   propertyrs   r`   r   rx   rC   rI   rS   ro   rK   r   rj   r3   r=   rA   r\   r   r   r    r   r   r      s        "   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
        >>> from sympy.physics.quantum import Operator

        >>> 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 simple products of kets and bras outer products will be automatically
    identified and created::

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

    But in more complex expressions, outer products are not automatically
    created::

        >>> A = Operator('A')
        >>> A*k*b
        A*|k>*<b|

    A user can force the creation of an outer product in a complex expression
    by using parentheses to group the ket and bra::

        >>> A*(k*b)
        A*|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 %dr-   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   r0   rv   r   rZ   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   rM   zOuterProduct.ket       y|r    c                     | j         d         S )z6Return the bra on the right side of the outer product.r-   r   r   s    r   r   zOuterProduct.bra  r   r    c                 j    t          t          | j                  t          | j                            S r#   )r   r   r   rM   r   s    r   ro   zOuterProduct._eval_adjoint  s&    F48,,fTX.>.>???r    c                 l    |                     | j                  |                     | j                  z   S r#   _printrM   r   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 )Nr.   r!   r/   )r$   r%   r   rM   r   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#   )rM   _prettyr   r7   r   )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   )rM   
_representr   )r   rG   r   r   s       r   r   zOuterProduct._represent  s?    DH**'**DH**'**s
r    c                 2     | j         j        | j        fi |S r#   )rM   _eval_tracer   )r   kwargss     r   r   zOuterProduct._eval_trace  s#     $tx#DH77777r    N)r%   r]   r^   r_   is_commutativer   r   rM   r   ro   r   r   r   r   r   r   r   r    r   r   r   N  s        ; ;x 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)
        rX   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)
        rX   r   r   s    r   functionzDifferentialOperator.function1  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.exprI  s    . y|r    c                     | j         j        S )z<
        Return the free symbols of the expression.
        )r   free_symbolsr   s    r   r   z!DifferentialOperator.free_symbolsb  s     y%%r    c                     ddl m} | j        }|j        dd          }| j        }| j                            | ||           }|                                } ||g|R  S )Nr   )Wavefunctionr-   )r   r   r   r(   r   r   subsdoit)r   funcrG   r   varwf_varsfnew_exprs           r   _apply_operator_Wavefunctionz1DifferentialOperator._apply_operator_Wavefunctionj  sq    <<<<<<n)ABB-M9>>!TT3Z00==??|H/w////r    c                 b    t          | j        |          }t          || j        d                   S rW   )r   r   r   r(   )r   symbolr   s      r   _eval_derivativez%DifferentialOperator._eval_derivativeu  s)    di00#Hdim<<<r    c                 B     | j         |g|R  d | j        |g|R  dS )Nr.   r/   )r)   r2   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 )Nr.   r/   r5   )r+   r8   r   r9   r7   r:   s        r   _print_prettyz"DifferentialOperator._print_pretty  ss    00@4@@@.d.w>>>> S44
 EKK445r    N)r%   r]   r^   r_   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)r_   sympy.core.addr   sympy.core.exprr   sympy.core.functionr   r   sympy.core.mulr   sympy.core.numbersr	   rg   r    sympy.printing.pretty.stringpictr   sympy.physics.quantum.daggerr   sympy.physics.quantum.qexprr   r   sympy.matricesr   __all__r   r   r   r   r   r   r   r    r   <module>r      s  	 	                   4 4 4 4 4 4 4 4       ! ! ! ! ! ! " " " " " " 7 7 7 7 7 7 / / / / / / > > > > > > > >        V  V  V  V  V u V  V  V r$/ $/ $/ $/ $/ $/ $/ $/N$ $ $ $ $h $ $ $.O O O O Ox O O Od]8 ]8 ]8 ]8 ]88 ]8 ]8 ]8@\ \ \ \ \8 \ \ \ \ \r    