qnet.printing.sympy module

Custom Printers for Sympy expressions

These classes are used by default by the QNET printing systems as sub-printers for SymPy objects (e.g. for symbolic coefficients). They fix some issues with SymPy’s builtin printers:

  • factors like \(\frac{1}{\sqrt{2}}\) occur very commonly in quantum mechanics, and it is standard notation to write them as such. SymPy insists on rationalizing denominators, using \(\frac{\sqrt{2}}{2}\) instead. Our custom printers restore the canonical form. Note that internally, Sympy still uses the rationalized structure; but in any case, Sympy makes no guarantees between the algebraic structure of an expression and how it is printed.
  • Symbols (especially greek letters) are extremely common, and it’s much more readable if the string representation of an expression uses unicode for these. SymPy supports unicode “pretty-printing” (sympy.printing.pretty.pretty.pretty_print()) only in “2D”, where expressions are rendered as multiline unicode strings. While this is fine for interactive display, it does not work so well for a simple str. The SympyUnicodePrinter solves this by producing simple strings with unicode symbols.
  • Some algebraic structures such as factorials, complex-conjugates and indexed symbols have sub-optimal rendering in sympy.printing.str.StrPrinter
  • QNET contains some custom subclasses of SymPy objects (e.g. IdxSym) that the default printers don’t know how to deal with (respectively, render incorrectly!)

Summary

Classes:

SympyLatexPrinter Variation of sympy LatexPrinter that derationalizes denominators
SympyReprPrinter Representation printer with support for IdxSym
SympyStrPrinter Variation of sympy StrPrinter that derationalizes denominators.
SympyUnicodePrinter Printer that represents SymPy expressions as (single-line) unicode strings.

Functions:

derationalize_denom Try to de-rationalize the denominator of the given expression.

Reference

qnet.printing.sympy.derationalize_denom(expr)[source]

Try to de-rationalize the denominator of the given expression.

The purpose is to allow to reconstruct e.g. 1/sqrt(2) from sqrt(2)/2.

Specifically, this matches expr against the following pattern:

Mul(..., Rational(n, d), Pow(d, Rational(1, 2)), ...)

and returns a tuple (numerator, denom_sq, post_factor), where numerator and denom_sq are n and d in the above pattern (of type int), respectively, and post_factor is the product of the remaining factors (... in expr). The result will fulfill the following identity:

(numerator / sqrt(denom_sq)) * post_factor == expr

If expr does not follow the appropriate pattern, a ValueError is raised.

class qnet.printing.sympy.SympyStrPrinter(settings=None)[source]

Bases: sympy.printing.str.StrPrinter

Variation of sympy StrPrinter that derationalizes denominators.

Additionally, it contains the following modifications:

printmethod = '_sympystr'
class qnet.printing.sympy.SympyLatexPrinter(settings=None)[source]

Bases: sympy.printing.latex.LatexPrinter

Variation of sympy LatexPrinter that derationalizes denominators

Additionally, it contains the following modifications:

  • Support for IdxSym
  • A setting conjg_style that allows to specify how complex conjugate are rendered: 'overline' (the default) draws a line over the number, ‘star’ uses an exponentiated asterisk, and ‘func’ renders a a conjugate function
printmethod = '_latex'
class qnet.printing.sympy.SympyUnicodePrinter(settings=None)[source]

Bases: qnet.printing.sympy.SympyStrPrinter

Printer that represents SymPy expressions as (single-line) unicode strings.

This is a mixture of StrPrinter and sympy.printing.pretty.pretty.PrettyPrinter (minus the 2D printing), with the same extensions as SympyStrPrinter

printmethod = '_sympystr'
class qnet.printing.sympy.SympyReprPrinter(settings=None)[source]

Bases: sympy.printing.repr.ReprPrinter

Representation printer with support for IdxSym