ó
¡¼™\c           @  sû   d  Z  d d l m Z m Z d d l m Z m Z m Z 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 m Z m Z m Z d d l m Z d g Z d „  Z d „  Z  d S(   s}   Logic for applying operators to states.

Todo:
* Sometimes the final result needs to be expanded, we should do this by hand.
iÿÿÿÿ(   t   print_functiont   division(   t   Addt   Mult   Powt   sympifyt   S(   t   range(   t   AntiCommutator(   t
   Commutator(   t   Dagger(   t   InnerProduct(   t   OuterProductt   Operator(   t   Statet   KetBaset   BraBaset   Wavefunction(   t   TensorProductt   qapplyc         K  sù  d d l  m } | j d t ƒ } |  d k r5 t j S|  j d t d t ƒ }  t |  t	 ƒ r` |  St |  t
 ƒ r¦ d } x$ |  j D] } | t | |  7} q W| j ƒ  St |  | ƒ rð g  |  j D]! \ } } t | |  | f ^ q¿ } | | Œ  St |  t ƒ r(t g  |  j D] }	 t |	 |  ^ qŒ  St |  t ƒ rNt |  j |  |  j St |  t ƒ rñ|  j ƒ  \ }
 } t |
 Œ  } t | Œ  } t | t ƒ r¬| t | |  } n | t | |  } | |  k rê| rêt t t |  ƒ |  ƒ S| Sn |  Sd S(   s_  Apply operators to states in a quantum expression.

    Parameters
    ==========

    e : Expr
        The expression containing operators and states. This expression tree
        will be walked to find operators acting on states symbolically.
    options : dict
        A dict of key/value pairs that determine how the operator actions
        are carried out.

        The following options are valid:

        * ``dagger``: try to apply Dagger operators to the left
          (default: False).
        * ``ip_doit``: call ``.doit()`` in inner products when they are
          encountered (default: True).

    Returns
    =======

    e : Expr
        The original expression, but with the operators applied to states.

    Examples
    ========

        >>> from sympy.physics.quantum import qapply, Ket, Bra
        >>> b = Bra('b')
        >>> k = Ket('k')
        >>> A = k * b
        >>> A
        |k><b|
        >>> qapply(A * b.dual / (b * b.dual))
        |k>
        >>> qapply(k.dual * A / (k.dual * k), dagger=True)
        <b|
        >>> qapply(k.dual * A / (k.dual * k))
        <k|*|k><b|/<k|k>
    iÿÿÿÿ(   t   Densityt   daggeri    t
   commutatort   tensorproductN(   t   sympy.physics.quantum.densityR   t   gett   FalseR   t   Zerot   expandt   Truet
   isinstanceR   R   t   argsR   R   R   t   baset   expR   t   args_cnct
   qapply_MulR
   (   t   et   optionsR   R   t   resultt   argt   statet   probt   new_argst   tt   c_partt   nc_partt   c_mult   nc_mul(    (    s;   lib/python2.7/site-packages/sympy/physics/quantum/qapply.pyR      s>    *
.
)c   
   	   K  s   | j  d t ƒ } t |  j ƒ } t | ƒ d k sC t |  t ƒ rG |  S| j ƒ  } | j ƒ  } t | ƒ j	 r~ t | t
 ƒ s t | ƒ j	 r¡ t | t
 ƒ r¡ |  St | t ƒ rã | j j rã | j | j | j d ƒ | j } n  t | t ƒ r| j | j ƒ | j } n  t | t t f ƒ r¤| j ƒ  } t | t ƒ rƒt |  j | | j d | g Œ  |  j | | j d | g Œ  |  St |  j | Œ  | | |  Sn  t | t ƒ rÕt g  | j D]- } t | t t t t f ƒ pê| d k ^ qÀƒ rÕt | t ƒ rÕt g  | j D]- } t | t t t t f ƒ p<| d k ^ qƒ rÕt | j ƒ t | j ƒ k rÕt g  t t | j ƒ ƒ D]' } t | j | | j | |  ^ qŒ  j d t ƒ }	 t |  j | Œ  |  |	 Sy | j  | |  }	 Wn’ t! t" f k
 ry | j  | |  }	 Wq€t! t" f k
 r{t | t# ƒ rrt | t$ ƒ rrt% | | ƒ }	 | rx|	 j ƒ  }	 qxq|d  }	 q€Xn X|	 d k r“t' j( S|	 d  k rÙt | ƒ d k rµ|  St |  j | | g Œ  |  | SnC t |	 t% ƒ r|	 t |  j | Œ  |  St |  j | Œ  |	 |  Sd  S(   Nt   ip_doiti   i    R   ()   R   R   t   listR   t   lenR   R   t   popR   t   is_commutativeR   R   R!   t
   is_Integert   appendR    R   t   kett   braR	   R   t   doitR   R   t   funcR   t   allR   R   R   R   R#   t   _apply_operatort   NotImplementedErrort   AttributeErrorR   R   R   t   NoneR   R   (
   R$   R%   R0   R   t   rhst   lhst   commR'   t   nR&   (    (    s;   lib/python2.7/site-packages/sympy/physics/quantum/qapply.pyR#   „   s`    "!RRU$N(!   t   __doc__t
   __future__R    R   t   sympyR   R   R   R   R   t   sympy.core.compatibilityR   t$   sympy.physics.quantum.anticommutatorR   t    sympy.physics.quantum.commutatorR	   t   sympy.physics.quantum.daggerR
   t"   sympy.physics.quantum.innerproductR   t   sympy.physics.quantum.operatorR   R   t   sympy.physics.quantum.stateR   R   R   R   t#   sympy.physics.quantum.tensorproductR   t   __all__R   R#   (    (    (    s;   lib/python2.7/site-packages/sympy/physics/quantum/qapply.pyt   <module>   s   ("		g