ó
¡¼™\c           @  s|  d  Z  d d l m Z m Z d d l m Z m Z m Z m Z m	 Z	 m
 Z
 m Z d d l m Z d d l m Z m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z d d g Z i e e e f ƒ e 6e e e f ƒ e 6e e e f ƒ e 6e e 6e e e e f ƒ e 6e	 e
 6e e 6Z e d	 „  e j ƒ  Dƒ ƒ Z d
 „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d S(   s   A module for mapping operators to their corresponding eigenstates
and vice versa

It contains a global dictionary with eigenstate-operator pairings.
If a new state-operator pair is created, this dictionary should be
updated as well.

It also contains functions operators_to_state and state_to_operators
for mapping between the two. These can handle both classes and
instances of operators and states. See the individual function
descriptions for details.

TODO List:
- Update the dictionary with a complete list of state-operator pairs
iÿÿÿÿ(   t   print_functiont   division(   t   XOpt   YOpt   ZOpt   XKett   PxOpt   PxKett   PositionKet3D(   t   Operator(   t	   StateBaset   BraBaset   Ket(   t   JxOpt   JyOpt   JzOpt   J2Opt   JxKett   JyKett   JzKett   operators_to_statet   state_to_operatorsc         c  s!   |  ] \ } } | | f Vq d  S(   N(    (   t   .0t   kt   v(    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/operatorset.pys	   <genexpr>.   s    c         K  sá  t  |  t ƒ p* t  |  t ƒ p* t |  t ƒ s< t d ƒ ‚ n  t  |  t ƒ r\x; |  D]3 } t  | t ƒ ps t | t ƒ sR t d ƒ ‚ qR qR Wt |  ƒ } | t k rÿ y< g  | D] } | ƒ  ^ q« } t t | t | ƒ |  } Wn t k
 rú t | } n X| Sg  | D] } t | ƒ ^ q} t | ƒ }	 |	 t k rOt t |	 | |  } n d } | Sn |  t k r­y# |  ƒ  }
 t t |  |
 |  } Wn t k
 r¨t |  } n X| St |  ƒ t k rÙt t t |  ƒ |  |  Sd Sd S(   s.   Returns the eigenstate of the given operator or set of operators

    A global function for mapping operator classes to their associated
    states. It takes either an Operator or a set of operators and
    returns the state associated with these.

    This function can handle both instances of a given operator or
    just the class itself (i.e. both XOp() and XOp)

    There are multiple use cases to consider:

    1) A class or set of classes is passed: First, we try to
    instantiate default instances for these operators. If this fails,
    then the class is simply returned. If we succeed in instantiating
    default instances, then we try to call state._operators_to_state
    on the operator instances. If this fails, the class is returned.
    Otherwise, the instance returned by _operators_to_state is returned.

    2) An instance or set of instances is passed: In this case,
    state._operators_to_state is called on the instances passed. If
    this fails, a state class is returned. If the method returns an
    instance, that instance is returned.

    In both cases, if the operator class or set does not exist in the
    state_mapping dictionary, None is returned.

    Parameters
    ==========

    arg: Operator or set
         The class or instance of the operator or set of operators
         to be mapped to a state

    Examples
    ========

    >>> from sympy.physics.quantum.cartesian import XOp, PxOp
    >>> from sympy.physics.quantum.operatorset import operators_to_state
    >>> from sympy.physics.quantum.operator import Operator
    >>> operators_to_state(XOp)
    |x>
    >>> operators_to_state(XOp())
    |x>
    >>> operators_to_state(PxOp)
    |px>
    >>> operators_to_state(PxOp())
    |px>
    >>> operators_to_state(Operator)
    |psi>
    >>> operators_to_state(Operator())
    |psi>
    s%   Argument is not an Operator or a set!s   Set is not all Operators!N(
   t
   isinstanceR	   t   sett
   issubclasst   NotImplementedErrort	   frozensett
   op_mappingt
   _get_statet   typet   None(   t	   operatorst   optionst   st   opst   opt   op_instancest   rett   ot   tmpt   classest   op_instance(    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/operatorset.pyR   1   s@    6 	c         K  s“  t  |  t ƒ p t |  t ƒ s- t d ƒ ‚ n  |  t k rŒ t |  ƒ } y  t | t t |  ƒ |  } Wq‰t t f k
 rˆ t |  } q‰Xný t	 |  ƒ t k rÃ t |  t t t	 |  ƒ ƒ |  } nÆ t  |  t
 ƒ r|  j ƒ  t k rt |  t t |  j ƒ  ƒ ƒ } nƒ t |  t
 ƒ rƒ|  j ƒ  t k rƒt |  ƒ } y# t | t t |  j ƒ  ƒ ƒ } Wq‰t t f k
 rt |  j ƒ  } q‰Xn d } t | ƒ S(   s`   Returns the operator or set of operators corresponding to the
    given eigenstate

    A global function for mapping state classes to their associated
    operators or sets of operators. It takes either a state class
    or instance.

    This function can handle both instances of a given state or just
    the class itself (i.e. both XKet() and XKet)

    There are multiple use cases to consider:

    1) A state class is passed: In this case, we first try
    instantiating a default instance of the class. If this succeeds,
    then we try to call state._state_to_operators on that instance.
    If the creation of the default instance or if the calling of
    _state_to_operators fails, then either an operator class or set of
    operator classes is returned. Otherwise, the appropriate
    operator instances are returned.

    2) A state instance is returned: Here, state._state_to_operators
    is called for the instance. If this fails, then a class or set of
    operator classes is returned. Otherwise, the instances are returned.

    In either case, if the state's class does not exist in
    state_mapping, None is returned.

    Parameters
    ==========

    arg: StateBase class or instance (or subclasses)
         The class or instance of the state to be mapped to an
         operator or set of operators

    Examples
    ========

    >>> from sympy.physics.quantum.cartesian import XKet, PxKet, XBra, PxBra
    >>> from sympy.physics.quantum.operatorset import state_to_operators
    >>> from sympy.physics.quantum.state import Ket, Bra
    >>> state_to_operators(XKet)
    X
    >>> state_to_operators(XKet())
    X
    >>> state_to_operators(PxKet)
    Px
    >>> state_to_operators(PxKet())
    Px
    >>> state_to_operators(PxBra)
    Px
    >>> state_to_operators(XBra)
    X
    >>> state_to_operators(Ket)
    O
    >>> state_to_operators(Bra)
    O
    s   Argument is not a state!N(   R   R
   R   R   t   state_mappingt   _make_defaultt   _get_opst	   _make_sett	   TypeErrorR    R   t
   dual_classR!   (   t   stateR#   t
   state_instR(   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/operatorset.pyR   –   s0    ;!!c         C  s+   y |  ƒ  } Wn t  k
 r& |  } n X| S(   N(   t	   Exception(   t   exprR(   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/operatorset.pyR.   î   s
    
c         K  s:   y |  j  | |  } Wn t k
 r5 t |  ƒ } n X| S(   N(   t   _operators_to_stateR   R.   (   t   state_classR%   R#   R(   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/operatorset.pyR   ÷   s
    c         K  s”   y |  j  | |  } WnN t k
 rf t | t t t f ƒ rW t d „  | Dƒ ƒ } qg t | ƒ } n Xt | t ƒ r t | ƒ d k r | d S| S(   Nc         s  s   |  ] } t  | ƒ Vq d  S(   N(   R.   (   R   t   x(    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/operatorset.pys	   <genexpr>	  s    i   i    (   t   _state_to_operatorsR   R   R   t   tupleR   R.   t   len(   R4   t
   op_classesR#   R(   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/operatorset.pyR/     s    !c         C  s*   t  |  t t t f ƒ r" t |  ƒ S|  Sd  S(   N(   R   R;   t   listR   R   (   R%   (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/operatorset.pyR0     s    
N(&   t   __doc__t
   __future__R    R   t   sympy.physics.quantum.cartesianR   R   R   R   R   R   R   t   sympy.physics.quantum.operatorR	   t   sympy.physics.quantum.stateR
   R   R   t   sympy.physics.quantum.spinR   R   R   R   R   R   R   t   __all__R   R-   t   dictt   itemsR   R   R   R.   R   R/   R0   (    (    (    s@   lib/python2.7/site-packages/sympy/physics/quantum/operatorset.pyt   <module>   s*   44	

	e	X				