
    MZdoK                        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y)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                   t    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y)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                      y)N)O selfs    @/usr/lib/python3/dist-packages/sympy/physics/quantum/operator.pydefault_argszOperator.default_argsh   s        ,c                 .    | j                   j                  S N)	__class____name__r   printerargss      r   _print_operator_namezOperator._print_operator_namer   s    ~~&&&r    c                 @    t        | j                  j                        S r#   )r   r$   r%   r&   s      r   _print_operator_name_prettyz$Operator._print_operator_name_prettyw   s    $..1122r    c                     t        | j                        dk(  r | j                  |g| S  | j                  |g| d | j                  |g| dS )N   ())lenlabel_print_labelr)   r&   s      r   _print_contentszOperator._print_contentsz   s`    tzz?a$4$$W4t44 *))'9D9!!!'1D1 r    c                    t        | j                        dk(  r | j                  |g| S  | j                  |g| } | j                  |g| }t	        |j                  dd       }t	        |j                  |       }|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    tzz?a+4++G;d;;4D44WDtDE2$227BTBK$##C#8K K 89ELr    c                     t        | j                        dk(  r | j                  |g| S  | j                  |g| d | j                  |g| 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   s`    tzz?a*4**7:T:: 0//?$?'''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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NgNNr    c                     t        | d|fi |S )N_apply_operatorrD   r   ketrG   s      r   rK   zOperator._apply_operator   s    t%6GwGGr    c                     t        d      )Nzmatrix_elements is not defined)NotImplementedError)r   r(   s     r   matrix_elementzOperator.matrix_element   s    !"BCCr    c                 "    | j                         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                 >    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   %   st    @D   ' "63
KOHD$ C r    r   c                        e Zd ZdZdZd Zd Zy)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                 P    t        | t              r| S t        j                  |       S r#   )rZ   r   r   rS   r   s    r   rS   zHermitianOperator._eval_inverse   s"    dO,K))$//r    c                     t        | t              r,|j                  rddlm} |j
                  S |j                  r| S t        j                  | |      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uu##D#..r    N)r%   r]   r^   r_   is_hermitianrS   rj   r   r    r   r   r      s    $ L0	/r    r   c                       e Zd ZdZd Zy)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                 "    | j                         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y)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    vv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   | _        y t        | _        y )N)r   r-   z"0 or 1 parameters expected, got %sr-   r   )r0   
ValueErrorr	   rr   )r   r(   hintss      r   __init__zIdentityOperator.__init__  s@    4yF"ADHII Y!^Qabr    c                 "    t         j                  S r#   )r   Zeror   rF   rw   s      r   rC   z!IdentityOperator._eval_commutator  s    vv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                      yNIr   r&   s      r   r3   z IdentityOperator._print_contents1  s    r    c                     t        d      S r   r   r&   s      r   r=   z'IdentityOperator._print_contents_pretty4  s    #r    c                      y)Nz{\mathcal{I}}r   r&   s      r   rA   z&IdentityOperator._print_contents_latex7  s    r    c                 J    t        |t        t        f      r|S t        | |      S r#   )rZ   r   r   r   r[   s     r   r\   zIdentityOperator.__mul__:  s#    eh/0L4r    c                     | j                   r| j                   t        k(  rt        d      |j                  dd      }|dk7  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  sr    vv2% 'G H H Xw/W%&A&;f&D'E F F 466{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      sq    "    A  
r    r   c                   d    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y)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k7  rt	        dt        |      z        t        |d         }t        |d         }t        ||t        f      r,t        ||t        f      r|j                         \  }}|j                         \  }	}
t        |      dk7  st        |d   |      st        dt        | z        t        |
      dk7  st        |
d   |      st        dt        |
 z        |d   j                         |
d   j                  k(  s+t        d|d   j                  d	|
d   j                        t        j                  | g|d   |
d   fi |}|d   j                  |_        t        ||	z    |z  S g }t        |t              rVt        |t              rF|j                   D ]/  }|j                   D ]  }|j#                  t%        ||fi |         1 t        | S t        |t              r5|j                   D ]  }|j#                  t%        ||fi |         t        | S t        |t              r5|j                   D ]  }|j#                  t%        ||fi |         t        | S t        d
|d	|      )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   @t9><s4yHII$q'?$q'?x'308gs^4"++-KE4"++-KE44yA~ZQ%A !,.14j!9 : : 4yA~ZQ%A !,.14j!9 : : 7%%'47+<+<<!W&&Q(9(9;  ,,sKd1gtAw%7K?KC $Q 5 5C(3..h$Hc)B$MM E ( EHOOL8 %D3B%D EEE$ H~ #&$MM AXx !@/>!@ AA H~ #&$MM AXx !@/>!@ AA H~ 8% 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       yy|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                 f    t        t        | j                        t        | j                              S r#   )r   r   r   rM   r   s    r   ro   zOuterProduct._eval_adjoint  s!    F488,fTXX.>??r    c                 p    |j                  | j                        |j                  | j                        z   S r#   _printrM   r   r&   s      r   	_sympystrzOuterProduct._sympystr  s'    ~~dhh''..*BBBr    c                     | j                   j                  d |j                  | j                  g| d |j                  | j                  g| dS )Nr.   r!   r/   )r$   r%   r   rM   r   r&   s      r   
_sympyreprzOuterProduct._sympyrepr  sH    "nn55GNN488+d+^W^^DHH-Lt-LN 	Nr    c                      | j                   j                  |g| }t        |j                   | j                  j                  |g|        S r#   )rM   _prettyr   r7   r   )r   r'   r(   r;   s       r   r   zOuterProduct._pretty  sH       0405;;'7txx'7'7'G$'GHIIr    c                      |j                   | j                  g| } |j                   | j                  g| }||z   S r#   r   )r   r'   r(   kbs        r   _latexzOuterProduct._latex  s=    GNN488+d+GNN488+d+1ur    c                 |     | j                   j                  di |} | j                  j                  di |}||z  S )Nr   )rM   
_representr   )r   rG   r   r   s       r   r   zOuterProduct._represent  s=    DHH*'*DHH*'*s
r    c                 P     | j                   j                  | j                  fi |S r#   )rM   _eval_tracer   )r   kwargss     r   r   zOuterProduct._eval_trace  s$     $txx##DHH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  sd    ;x N6p    @CNJ

8r    r   c                   h    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y
)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                 4    | 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    . yy}!!!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    , yy}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    . yy|r    c                 .    | j                   j                  S )z<
        Return the free symbols of the expression.
        )r   free_symbolsr   s    r   r   z!DifferentialOperator.free_symbolsb  s     yy%%%r    c                     ddl m} | j                  }|j                  dd  }| j                  }| j
                  j                  | ||       }|j                         } ||g| 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  sY    <nn))AB-MM99>>!T3Z0==?H/w//r    c                 `    t        | j                  |      }t        || j                  d         S rW   )r   r   r   r(   )r   symbolr   s      r   _eval_derivativez%DifferentialOperator._eval_derivativeu  s'    dii0#Hdiim<<r    c                 V     | j                   |g| d | j                  |g| dS )Nr.   r/   )r)   r2   r&   s      r   r   zDifferentialOperator._print}  s6    %D%%g55Dg--
 	
r    c                      | j                   |g| } | j                  |g| }t        |j                  dd       }t        |j	                  |       }|S )Nr.   r/   r5   )r+   r8   r   r9   r7   r:   s        r   _print_prettyz"DifferentialOperator._print_pretty  sg    000@4@.d..w>> S4
 EKK45r    N)r%   r]   r^   r_   r   r   r   r   r   r   r   r   r   r   r    r   r   r     sl    'R " "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  ! " 7 / > V u V r$/ $/N$h $.Ox Od]88 ]8@\8 \r    