Wednesday, July 29, 2020

quantum bra ket Dirac notation in Sympy

Quantum mechanics notation is supported in SymPy, but not for parsing Latex input.

>>> import sympy
>>> sympy.__version__
'1.5.1'
>>> from sympy.parsing.latex import parse_latex
>>> sympy.srepr(parse_latex("\\langle a |"))
"Mul(Symbol('a'), Symbol('langle'))"

That isn't what I intended.

>>> from sympy.physics.quantum.state import Ket, Bra
>>> sympy.latex(Bra('a'))
'{\\left\\langle a\\right|}'

SymPy can produce Latex; can it then parse that?

>>> sympy.srepr(parse_latex("{\\left\\langle a\\right|}"))
  ...
  File "/usr/local/lib/python3.6/dist-packages/sympy/parsing/latex/_parse_latex_antlr.py", line 57, in syntaxError
    raise LaTeXParsingError(err)
sympy.parsing.latex.errors.LaTeXParsingError: I don't understand this
{\left\langle a\right|}
~~~~~~~~~~~~~~~~~~~~~^

I've opened a ticket 
https://github.com/sympy/sympy/issues/19854


A good amount of Dirac notation is supported in Sympy:
>>> from sympy.physics.quantum import Operator, Dagger
>>> sympy.latex(Bra('a')*Dagger(Operator('A'))*Ket('b'))
{\left\langle a\right|} A^{\dagger} {\left|b\right\rangle }
>>> sympy.latex(sympy.Symbol('alpha')*Bra('pdg1')*Ket('pdg2')) 
'\\alpha {\\left\\langle pdg_{1}\\right|} {\\left|pdg_{2}\\right\\rangle }'

No comments:

Post a Comment