o
    ÏeoK  ã                   @   sð   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„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d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                   @   sˆ   e Zd ZdZedd„ ƒZdZdd„ ZeZdd„ Z	d	d
„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZeZdd„ Zd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                 C   ó   dS )N)ÚO© ©Úselfr   r   úND:\Projects\ConvertPro\env\Lib\site-packages\sympy/physics/quantum/operator.pyÚdefault_argsh   s   zOperator.default_argsú,c                 G   ó   | j jS ©N)Ú	__class__Ú__name__©r   ÚprinterÚargsr   r   r   Ú_print_operator_namer   ó   zOperator._print_operator_namec                 G   s   t | jjƒS r    )r   r!   r"   r#   r   r   r   Ú_print_operator_name_prettyw   s   z$Operator._print_operator_name_prettyc                 G   óH   t | jƒdkr| j|g|¢R Ž S d| j|g|¢R Ž | j|g|¢R Ž f S )Né   ú%s(%s))ÚlenÚlabelÚ_print_labelr&   r#   r   r   r   Ú_print_contentsz   ó   þzOperator._print_contentsc                 G   sh   t | jƒdkr| j|g|¢R Ž S | j|g|¢R Ž }| j|g|¢R Ž }t|jddd�Ž }t| |¡Ž }|S )Nr*   ú(ú)©ÚleftÚright)r,   r-   Ú_print_label_prettyr(   r   Úparensr5   ©r   r$   r%   ÚpformZlabel_pformr   r   r   Ú_print_contents_prettyƒ   s   ÿzOperator._print_contents_prettyc                 G   r)   )Nr*   z%s\left(%s\right))r,   r-   Z_print_label_latexÚ_print_operator_name_latexr#   r   r   r   Ú_print_contents_latex�   r0   zOperator._print_contents_latexc                 K   ó   t | d|fi |¤ŽS )z:Evaluate [self, other] if known, return None if not known.Ú_eval_commutator©r   ©r   ÚotherÚoptionsr   r   r   r>   œ   ó   zOperator._eval_commutatorc                 K   r=   )z Evaluate [self, other] if known.Ú_eval_anticommutatorr?   r@   r   r   r   rD       rC   zOperator._eval_anticommutatorc                 K   r=   )NÚ_apply_operatorr?   ©r   ÚketrB   r   r   r   rE   ¨   s   zOperator._apply_operatorc                 G   s   t dƒ‚)Nzmatrix_elements is not defined)ÚNotImplementedError)r   r%   r   r   r   Úmatrix_element«   r'   zOperator.matrix_elementc                 C   ó   |   ¡ S r    ©Ú_eval_inverser   r   r   r   Úinverse®   r'   zOperator.inversec                 C   s   | d S ©Néÿÿÿÿr   r   r   r   r   rL   ³   r'   zOperator._eval_inversec                 C   s   t |tƒr| S t| |ƒS r    )Ú
isinstancer   r   ©r   rA   r   r   r   Ú__mul__¶   s   

zOperator.__mul__N)r"   Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   Z_label_separatorr&   r;   r(   r/   r:   r<   r>   rD   rE   rI   rM   ÚinvrL   rR   r   r   r   r   r   %   s&    B
	r   c                   @   s$   e Zd ZdZdZdd„ Zd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                 C   s   t | tƒr| S t | ¡S r    )rP   r   r   rL   r   r   r   r   rL   Ó   s   

zHermitianOperator._eval_inversec                 C   s8   t | tƒr|jrddlm} |jS |jr| S t | |¡S )Nr   r	   )	rP   r   Zis_evenÚsympy.core.singletonr
   ZOneZis_oddr   Ú_eval_power)r   Úexpr
   r   r   r   rY   Ù   s   
zHermitianOperator._eval_powerN)r"   rS   rT   rU   Zis_hermitianrL   rY   r   r   r   r   r   ¾   s
    r   c                   @   s   e Zd ZdZd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                 C   rJ   r    rK   r   r   r   r   Ú_eval_adjointø   r'   zUnitaryOperator._eval_adjointN)r"   rS   rT   rU   r[   r   r   r   r   r   å   s    r   c                   @   s�   e Zd ZdZedd„ ƒZedd„ ƒZdd„ Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd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                 C   s   | j S r    )ÚNr   r   r   r   Ú	dimension  ó   zIdentityOperator.dimensionc                 C   s   t fS r    r   r   r   r   r   r     r^   zIdentityOperator.default_argsc                 O   sD   t |ƒdvrtd| ƒ‚t |ƒdkr|d r|d | _d S t| _d S )N)r   r*   z"0 or 1 parameters expected, got %sr*   r   )r,   Ú
ValueErrorr   r\   )r   r%   Úhintsr   r   r   Ú__init__  s   ,zIdentityOperator.__init__c                 K   s   t jS r    )r
   ZZero©r   rA   r`   r   r   r   r>     s   z!IdentityOperator._eval_commutatorc                 K   s   d| S )Né   r   rb   r   r   r   rD     r'   z%IdentityOperator._eval_anticommutatorc                 C   ó   | S r    r   r   r   r   r   rL   "  ó   zIdentityOperator._eval_inversec                 C   rd   r    r   r   r   r   r   r[   %  re   zIdentityOperator._eval_adjointc                 K   ó   |S r    r   rF   r   r   r   rE   (  re   z IdentityOperator._apply_operatorc                 K   rf   r    r   )r   ÚbrarB   r   r   r   Ú_apply_from_right_to+  re   z%IdentityOperator._apply_from_right_toc                 C   rd   r    r   )r   rZ   r   r   r   rY   .  re   zIdentityOperator._eval_powerc                 G   r   ©NÚIr   r#   r   r   r   r/   1  re   z IdentityOperator._print_contentsc                 G   s   t dƒS ri   r   r#   r   r   r   r:   4  r'   z'IdentityOperator._print_contents_prettyc                 G   r   )Nz{\mathcal{I}}r   r#   r   r   r   r<   7  re   z&IdentityOperator._print_contents_latexc                 C   s   t |ttfƒr	|S t| |ƒS r    )rP   r   r   r   rQ   r   r   r   rR   :  s   
zIdentityOperator.__mul__c                 K   sF   | j r| j tkrtdƒ‚| dd¡}|dkrtdd|  ƒ‚t| j ƒS )NzCCannot represent infinite dimensional identity operator as a matrixÚformatZsympyzRepresentation in format z%s not implemented.)r\   r   rH   Úgetr   )r   rB   rk   r   r   r   Ú_represent_default_basisA  s   ÿ
z)IdentityOperator._represent_default_basisN)r"   rS   rT   rU   Úpropertyr]   rV   r   ra   r>   rD   rL   r[   rE   rh   rY   r/   r:   r<   rR   rm   r   r   r   r   r   ü   s&    

r   c                   @   sl   e Zd ZdZdZdd„ Zedd„ ƒZedd„ ƒZd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zd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                 O   s
  ddl m}m} t|ƒdkrtdt|ƒ ƒ‚t|d ƒ}t|d ƒ}t||tfƒrŸt||tfƒrŸ| ¡ \}}| ¡ \}	}
t|ƒdksIt|d |ƒsQt	dt|Ž  ƒ‚t|
ƒdks^t|
d |ƒsft	dt|
Ž  ƒ‚|d  
¡ |
d jkst	d|d j|
d jf ƒ‚tj| g|d |
d f¢R i |¤Ž}|d j|_t||	 Ž | S g }t|tƒrÇt|tƒrÇ|jD ]}|jD ]}| t||fi |¤Ž¡ q³q®t|Ž S t|tƒrâ|jD ]}| t||fi |¤Ž¡ qÏt|Ž S t|tƒrý|jD ]}| t||fi |¤Ž¡ qêt|Ž S t	d	||f ƒ‚)
Nr   )ÚKetBaseÚBraBaserc   z2 parameters expected, got %dr*   z"KetBase subclass expected, got: %rz"BraBase subclass expected, got: %rz(ket and bra are not dual classes: %r, %rz,Expected ket and bra expression, got: %r, %r)Úsympy.physics.quantum.statero   rp   r,   r_   r   rP   r   Zargs_cncÚ	TypeErrorZ
dual_classr!   r   Ú__new__Zhilbert_spacer   r%   Úappendr   )Úclsr%   Zold_assumptionsro   rp   Zket_exprZbra_exprZket_cZketsZbra_cZbrasÚobjZop_termsZket_termZbra_termr   r   r   rs   �  sj   ÿÿÿÿÿ$


ÿÿ
ò

ÿ
ö

ÿûÿÿzOuterProduct.__new__c                 C   ó
   | j d S )z5Return the ket on the left side of the outer product.r   ©r%   r   r   r   r   rG   Å  ó   
zOuterProduct.ketc                 C   rw   )z6Return the bra on the right side of the outer product.r*   rx   r   r   r   r   rg   Ê  ry   zOuterProduct.brac                 C   s   t t| jƒt| jƒƒS r    )r   r   rg   rG   r   r   r   r   r[   Ï  s   zOuterProduct._eval_adjointc                 G   s   |  | j¡|  | j¡ S r    ©Ú_printrG   rg   r#   r   r   r   Ú	_sympystrÒ  s   zOuterProduct._sympystrc                 G   s2   d| j j|j| jg|¢R Ž |j| jg|¢R Ž f S )Nz	%s(%s,%s))r!   r"   r{   rG   rg   r#   r   r   r   Ú
_sympyreprÕ  s   $ÿzOuterProduct._sympyreprc                 G   s2   | j j|g|¢R Ž }t| | jj|g|¢R Ž ¡Ž S r    )rG   Ú_prettyr   r5   rg   )r   r$   r%   r9   r   r   r   r~   Ù  s   zOuterProduct._prettyc                 G   s0   |j | jg|¢R Ž }|j | jg|¢R Ž }|| S r    rz   )r   r$   r%   ÚkÚbr   r   r   Ú_latexÝ  s   zOuterProduct._latexc                 K   s,   | j jdi |¤Ž}| jjdi |¤Ž}|| S )Nr   )rG   Ú
_representrg   )r   rB   r   r€   r   r   r   r‚   â  s   zOuterProduct._representc                 K   s   | j j| jfi |¤ŽS r    )rG   Ú_eval_tracerg   )r   Úkwargsr   r   r   rƒ   ç  s   zOuterProduct._eval_traceN)r"   rS   rT   rU   Zis_commutativers   rn   rG   rg   r[   r|   r}   r~   r�   r‚   rƒ   r   r   r   r   r   N  s    <8

r   c                   @   s`   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZedd	„ ƒZd
d„ Z	dd„ Z
dd„ Zd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                 C   s   | 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)
        rO   rx   r   r   r   r   Ú	variables  s   zDifferentialOperator.variablesc                 C   rw   )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)
        rO   rx   r   r   r   r   Úfunction1  s   
zDifferentialOperator.functionc                 C   rw   )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   rx   r   r   r   r   ÚexprI  s   
zDifferentialOperator.exprc                 C   r   )z<
        Return the free symbols of the expression.
        )r‡   Úfree_symbolsr   r   r   r   rˆ   b  s   z!DifferentialOperator.free_symbolsc                 K   sP   ddl m} | j}|jdd … }| j}| j |||Ž ¡}| ¡ }||g|¢R Ž S )Nr   )ÚWavefunctionr*   )rq   r‰   r…   r%   r†   r‡   ÚsubsZdoit)r   ÚfuncrB   r‰   ÚvarZwf_varsÚfÚnew_exprr   r   r   Ú_apply_operator_Wavefunctionj  s   z1DifferentialOperator._apply_operator_Wavefunctionc                 C   s   t | j|ƒ}t|| jd ƒS rN   )r   r‡   r   r%   )r   ÚsymbolrŽ   r   r   r   Ú_eval_derivativeu  s   z%DifferentialOperator._eval_derivativec                 G   s(   d| j |g|¢R Ž | j|g|¢R Ž f S )Nr+   )r&   r.   r#   r   r   r   r{   }  s   þzDifferentialOperator._printc                 G   sH   | j |g|¢R Ž }| j|g|¢R Ž }t|jddd�Ž }t| |¡Ž }|S )Nr1   r2   r3   )r(   r6   r   r7   r5   r8   r   r   r   Ú_print_prettyƒ  s   ÿz"DifferentialOperator._print_prettyN)r"   rS   rT   rU   rn   r…   r†   r‡   rˆ   r�   r‘   r{   r’   r   r   r   r   r   î  s    )



r   N)rU   Zsympy.core.addr   Zsympy.core.exprr   Zsympy.core.functionr   r   Zsympy.core.mulr   Zsympy.core.numbersr   rX   r
   Z sympy.printing.pretty.stringpictr   Zsympy.physics.quantum.daggerr   Zsympy.physics.quantum.qexprr   r   Zsympy.matricesr   Ú__all__r   r   r   r   r   r   r   r   r   r   Ú<module>   s(     'R !