Physics Derivation Graph navigation Sign in

quantum bra ket Dirac notation in Sympy

Published 2020-07-29T13:21:00.004Z by Physics Derivation Graph

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

>>> import sympy
>>> sympy.__version__
>>> 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/", 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

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 }'