<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> from sympy import Add , Expr , I , integrate , Mul , Pow <EOL> from sympy . physics . quantum . dagger import Dagger <EOL> from sympy . physics . quantum . commutator import Commutator <EOL> from sympy . physics . quantum . anticommutator import AntiCommutator <EOL> from sympy . physics . quantum . innerproduct import InnerProduct <EOL> from sympy . physics . quantum . qexpr import QExpr <EOL> from sympy . physics . quantum . tensorproduct import TensorProduct <EOL> from sympy . physics . quantum . matrixutils import flatten_scalar <EOL> from sympy . physics . quantum . state import KetBase , BraBase , StateBase <EOL> from sympy . physics . quantum . operator import Operator , OuterProduct <EOL> from sympy . physics . quantum . qapply import qapply <EOL> from sympy . physics . quantum . operatorset import operators_to_state , state_to_operators <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def _sympy_to_scalar ( e ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( e , Expr ) : <EOL> if e . is_Integer : <EOL> return int ( e ) <EOL> elif e . is_Float : <EOL> return float ( e ) <EOL> elif e . is_Rational : <EOL> return float ( e ) <EOL> elif e . is_Number or e . is_NumberSymbol or e == I : <EOL> return complex ( e ) <EOL> raise TypeError ( '<STR_LIT>' % e ) <EOL> def represent ( expr , ** options ) : <EOL> """<STR_LIT>""" <EOL> format = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if isinstance ( expr , QExpr ) and not isinstance ( expr , OuterProduct ) : <EOL> options [ '<STR_LIT>' ] = False <EOL> temp_basis = get_basis ( expr , ** options ) <EOL> if temp_basis is not None : <EOL> options [ '<STR_LIT>' ] = temp_basis <EOL> try : <EOL> return expr . _represent ( ** options ) <EOL> except NotImplementedError as strerr : <EOL> options [ '<STR_LIT>' ] = True <EOL> if isinstance ( expr , ( KetBase , BraBase ) ) : <EOL> try : <EOL> return rep_innerproduct ( expr , ** options ) <EOL> except NotImplementedError : <EOL> raise NotImplementedError ( strerr ) <EOL> elif isinstance ( expr , Operator ) : <EOL> try : <EOL> return rep_expectation ( expr , ** options ) <EOL> except NotImplementedError : <EOL> raise NotImplementedError ( strerr ) <EOL> else : <EOL> raise NotImplementedError ( strerr ) <EOL> elif isinstance ( expr , Add ) : <EOL> result = represent ( expr . args [ <NUM_LIT:0> ] , ** options ) <EOL> for args in expr . args [ <NUM_LIT:1> : ] : <EOL> result = result + represent ( args , ** options ) <EOL> return result <EOL> elif isinstance ( expr , Pow ) : <EOL> base , exp = expr . as_base_exp ( ) <EOL> if format == '<STR_LIT>' or format == '<STR_LIT>' : <EOL> exp = _sympy_to_scalar ( exp ) <EOL> return represent ( base , ** options ) ** exp <EOL> elif isinstance ( expr , TensorProduct ) : <EOL> new_args = [ represent ( arg , ** options ) for arg in expr . args ] <EOL> return TensorProduct ( * new_args ) <EOL> elif isinstance ( expr , Dagger ) : <EOL> return Dagger ( represent ( expr . args [ <NUM_LIT:0> ] , ** options ) ) <EOL> elif isinstance ( expr , Commutator ) : <EOL> A = represent ( expr . args [ <NUM_LIT:0> ] , ** options ) <EOL> B = represent ( expr . args [ <NUM_LIT:1> ] , ** options ) <EOL> return A * B - B * A <EOL> elif isinstance ( expr , AntiCommutator ) : <EOL> A = represent ( expr . args [ <NUM_LIT:0> ] , ** options ) <EOL> B = represent ( expr . args [ <NUM_LIT:1> ] , ** options ) <EOL> return A * B + B * A <EOL> elif isinstance ( expr , InnerProduct ) : <EOL> return represent ( Mul ( expr . bra , expr . ket ) , ** options ) <EOL> elif not ( isinstance ( expr , Mul ) or isinstance ( expr , OuterProduct ) ) : <EOL> if format == '<STR_LIT>' or format == '<STR_LIT>' : <EOL> return _sympy_to_scalar ( expr ) <EOL> return expr <EOL> if not ( isinstance ( expr , Mul ) or isinstance ( expr , OuterProduct ) ) : <EOL> raise TypeError ( '<STR_LIT>' % expr ) <EOL> if "<STR_LIT:index>" in options : <EOL> options [ "<STR_LIT:index>" ] += <NUM_LIT:1> <EOL> else : <EOL> options [ "<STR_LIT:index>" ] = <NUM_LIT:1> <EOL> if not "<STR_LIT>" in options : <EOL> options [ "<STR_LIT>" ] = [ ] <EOL> result = represent ( expr . args [ - <NUM_LIT:1> ] , ** options ) <EOL> last_arg = expr . args [ - <NUM_LIT:1> ] <EOL> for arg in reversed ( expr . args [ : - <NUM_LIT:1> ] ) : <EOL> if isinstance ( last_arg , Operator ) : <EOL> options [ "<STR_LIT:index>" ] += <NUM_LIT:1> <EOL> options [ "<STR_LIT>" ] . append ( options [ "<STR_LIT:index>" ] ) <EOL> elif isinstance ( last_arg , BraBase ) and isinstance ( arg , KetBase ) : <EOL> options [ "<STR_LIT:index>" ] += <NUM_LIT:1> <EOL> elif isinstance ( last_arg , KetBase ) and isinstance ( arg , Operator ) : <EOL> options [ "<STR_LIT>" ] . append ( options [ "<STR_LIT:index>" ] ) <EOL> elif isinstance ( last_arg , KetBase ) and isinstance ( arg , BraBase ) : <EOL> options [ "<STR_LIT>" ] . append ( options [ "<STR_LIT:index>" ] ) <EOL> result = represent ( arg , ** options ) * result <EOL> last_arg = arg <EOL> result = flatten_scalar ( result ) <EOL> result = integrate_result ( expr , result , ** options ) <EOL> return result <EOL> def rep_innerproduct ( expr , ** options ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( expr , ( KetBase , BraBase ) ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> basis = get_basis ( expr , ** options ) <EOL> if not isinstance ( basis , StateBase ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if not "<STR_LIT:index>" in options : <EOL> options [ "<STR_LIT:index>" ] = <NUM_LIT:1> <EOL> basis_kets = enumerate_states ( basis , options [ "<STR_LIT:index>" ] , <NUM_LIT:2> ) <EOL> if isinstance ( expr , BraBase ) : <EOL> bra = expr <EOL> ket = ( basis_kets [ <NUM_LIT:1> ] if basis_kets [ <NUM_LIT:0> ] . dual == expr else basis_kets [ <NUM_LIT:0> ] ) <EOL> else : <EOL> bra = ( basis_kets [ <NUM_LIT:1> ] . dual if basis_kets [ <NUM_LIT:0> ] <EOL> == expr else basis_kets [ <NUM_LIT:0> ] . dual ) <EOL> ket = expr <EOL> prod = InnerProduct ( bra , ket ) <EOL> result = prod . doit ( ) <EOL> format = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return expr . _format_represent ( result , format ) <EOL> def rep_expectation ( expr , ** options ) : <EOL> """<STR_LIT>""" <EOL> if not "<STR_LIT:index>" in options : <EOL> options [ "<STR_LIT:index>" ] = <NUM_LIT:1> <EOL> if not isinstance ( expr , Operator ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> basis_state = get_basis ( expr , ** options ) <EOL> if basis_state is None or not isinstance ( basis_state , StateBase ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> basis_kets = enumerate_states ( basis_state , options [ "<STR_LIT:index>" ] , <NUM_LIT:2> ) <EOL> bra = basis_kets [ <NUM_LIT:1> ] . dual <EOL> ket = basis_kets [ <NUM_LIT:0> ] <EOL> return qapply ( bra * expr * ket ) <EOL> def integrate_result ( orig_expr , result , ** options ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( result , Expr ) : <EOL> return result <EOL> options [ '<STR_LIT>' ] = True <EOL> if not "<STR_LIT>" in options : <EOL> arg = orig_expr . args [ - <NUM_LIT:1> ] <EOL> options [ "<STR_LIT>" ] = get_basis ( arg , ** options ) <EOL> elif not isinstance ( options [ "<STR_LIT>" ] , StateBase ) : <EOL> options [ "<STR_LIT>" ] = get_basis ( orig_expr , ** options ) <EOL> basis = options . pop ( "<STR_LIT>" , None ) <EOL> if basis is None : <EOL> return result <EOL> unities = options . pop ( "<STR_LIT>" , [ ] ) <EOL> if len ( unities ) == <NUM_LIT:0> : <EOL> return result <EOL> kets = enumerate_states ( basis , unities ) <EOL> coords = [ k . label [ <NUM_LIT:0> ] for k in kets ] <EOL> for coord in coords : <EOL> if coord in result . free_symbols : <EOL> basis_op = state_to_operators ( basis ) <EOL> start = basis_op . hilbert_space . interval . start <EOL> end = basis_op . hilbert_space . interval . end <EOL> result = integrate ( result , ( coord , start , end ) ) <EOL> return result <EOL> def get_basis ( expr , ** options ) : <EOL> """<STR_LIT>""" <EOL> basis = options . pop ( "<STR_LIT>" , None ) <EOL> replace_none = options . pop ( "<STR_LIT>" , True ) <EOL> if basis is None and not replace_none : <EOL> return None <EOL> if basis is None : <EOL> if isinstance ( expr , KetBase ) : <EOL> return _make_default ( expr . __class__ ) <EOL> elif isinstance ( expr , BraBase ) : <EOL> return _make_default ( ( expr . dual_class ( ) ) ) <EOL> elif isinstance ( expr , Operator ) : <EOL> state_inst = operators_to_state ( expr ) <EOL> return ( state_inst if state_inst is not None else None ) <EOL> else : <EOL> return None <EOL> elif ( isinstance ( basis , Operator ) or <EOL> ( not isinstance ( basis , StateBase ) and issubclass ( basis , Operator ) ) ) : <EOL> state = operators_to_state ( basis ) <EOL> if state is None : <EOL> return None <EOL> elif isinstance ( state , StateBase ) : <EOL> return state <EOL> else : <EOL> return _make_default ( state ) <EOL> elif isinstance ( basis , StateBase ) : <EOL> return basis <EOL> elif issubclass ( basis , StateBase ) : <EOL> return _make_default ( basis ) <EOL> else : <EOL> return None <EOL> def _make_default ( expr ) : <EOL> try : <EOL> expr = expr ( ) <EOL> except Exception : <EOL> return expr <EOL> return expr <EOL> def enumerate_states ( * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> state = args [ <NUM_LIT:0> ] <EOL> if not ( len ( args ) == <NUM_LIT:2> or len ( args ) == <NUM_LIT:3> ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if not isinstance ( state , StateBase ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if len ( args ) == <NUM_LIT:3> : <EOL> num_states = args [ <NUM_LIT:2> ] <EOL> options [ '<STR_LIT>' ] = args [ <NUM_LIT:1> ] <EOL> else : <EOL> num_states = len ( args [ <NUM_LIT:1> ] ) <EOL> options [ '<STR_LIT>' ] = args [ <NUM_LIT:1> ] <EOL> try : <EOL> ret = state . _enumerate_state ( num_states , ** options ) <EOL> except NotImplementedError : <EOL> ret = [ ] <EOL> return ret </s>
