qnet.printing package

Printing system for QNET Expressions and related objects

Submodules:

Summary

Functions:

ascii Return an ASCII representation of the given object / expression
configure_printing Context manager for temporarily changing the printing system.
init_printing Initialize the printing system.
latex Return a LaTeX representation of the given object / expression
srepr Render the given expression into a string that can be evaluated in an appropriate context to re-instantiate an identical expression.
tex Alias for latex()
unicode Return a unicode representation of the given object / expression

__all__: ascii, configure_printing, dotprint, init_printing, latex, print_tree, srepr, tex, tree, unicode

Reference

qnet.printing.init_printing(*, reset=False, **kwargs)[source]

Initialize the printing system.

This determines the behavior of the ascii(), unicode(), and latex() functions, as well as the __str__ and __repr__ of any Expression.

The routine may be called in one of two forms. First,

init_printing(
    str_format=<str_fmt>, repr_format=<repr_fmt>,
    caching=<use_caching>, **settings)

provides a simplified, “manual” setup with the following parameters.

Parameters:
  • str_format (str) – Format for __str__ representation of an Expression. One of ‘ascii’, ‘unicode’, ‘latex’, ‘srepr’, ‘indsrepr’ (“indented srepr”), or ‘tree’. The string representation will be affected by the settings for the corresponding print routine, e.g. unicode() for str_format='unicode'
  • repr_format (str) – Like str_format, but for __repr__. This is what gets displayed in an interactive (I)Python session.
  • caching (bool) – By default, the printing functions (ascii(), unicode(), latex()) cache their result for any expression and sub-expression. This is both for efficiency and to give the ability to to supply custom strings for subexpression by passing a cache parameter to the printing functions. Initializing the printing system with caching=False disables this possibility.
  • settings – Any setting understood by any of the printing routines.

Second,

init_printing(inifile=<path_to_file>)

allows for more detailed settings through a config file, see the notes on using an INI file.

If str_format or repr_format are not given, they will be set of ‘unicode’ if the current terminal is known to support an UTF8 (accordig to sys.stdout.encoding), and ‘ascii’ otherwise.

Generally, init_printing() should be called only once at the beginning of a script or notebook. If it is called multiple times, any settings accumulate. To avoid this and to reset the printing system to the defaults, you may pass reset=True. In a Jupyter notebook, expressions are rendered graphically via LaTeX, using the settings as they affect the latex() printer.

See also

configure_printing() allows to temporarily change the printing system from what was configured in init_printing().

qnet.printing.configure_printing(**kwargs)[source]

Context manager for temporarily changing the printing system.

This takes the same parameters as init_printing()

Example

>>> from qnet.algebra import OperatorSymbol
>>> A = OperatorSymbol('A', hs=1); B = OperatorSymbol('B', hs=1)
>>> with configure_printing(show_hs_label=False):
...     ascii(A + B)
'A + B'
>>> ascii(A + B)
'A^(1) + B^(1)'
qnet.printing.ascii(expr, cache=None, **settings)[source]

Return an ASCII representation of the given object / expression

Parameters:
  • expr – Expression to print
  • cache (dict or None) – dictionary to use for caching
  • show_hs_label (bool or str) – Whether to a label for the Hilbert space of expr. By default (show_hs_label=True), the label is shown as a superscript. It can be shown as a subscript with show_hs_label='subscript' or suppressed entirely (show_hs_label=False)
  • sig_as_ketbra (bool) – Whether to render instances of LocalSigma as a ket-bra (default), or as an operator symbol

Examples

>>> from qnet.algebra import OperatorSymbol, LocalSigma
>>> A = OperatorSymbol('A', hs=1); B = OperatorSymbol('B', hs=1)
>>> ascii(A + B)
'A^(1) + B^(1)'
>>> ascii(A + B, cache={A: 'A', B: 'B'})
'A + B'
>>> ascii(A + B, show_hs_label='subscript')
'A_(1) + B_(1)'
>>> ascii(A + B, show_hs_label=False)
'A + B'
>>> ascii(LocalSigma(0, 1, hs=1))
'|0><1|^(1)'
>>> ascii(LocalSigma(0, 1, hs=1), sig_as_ketbra=False)
'sigma_0,1^(1)'

Note that the accepted parameters and their default values may be changed through init_printing() or configure_printing()

qnet.printing.unicode(expr, cache=None, **settings)[source]

Return a unicode representation of the given object / expression

Parameters:
  • expr – Expression to print
  • cache (dict or None) – dictionary to use for caching
  • show_hs_label (bool or str) – Whether to a label for the Hilbert space of expr. By default (show_hs_label=True), the label is shown as a superscript. It can be shown as a subscript with show_hs_label='subscript' or suppressed entirely (show_hs_label=False)
  • sig_as_ketbra (bool) – Whether to render instances of LocalSigma as a ket-bra (default), or as an operator symbol
  • unicode_sub_super (bool) – Whether to try to use unicode symbols for sub- or superscripts if possible
  • unicode_op_hats (bool) – Whether to draw unicode hats on single-letter operator symbols

Examples

>>> from qnet.algebra import OperatorSymbol, LocalSigma
>>> A = OperatorSymbol('A', hs=1); B = OperatorSymbol('B', hs=1)
>>> unicode(A + B)
'Â⁽¹⁾ + B̂⁽¹⁾'
>>> unicode(A + B, cache={A: 'A', B: 'B'})
'A + B'
>>> unicode(A + B, show_hs_label='subscript')
'Â₍₁₎ + B̂₍₁₎'
>>> unicode(A + B, show_hs_label=False)
'Â + B̂'
>>> unicode(LocalSigma(0, 1, hs=1))
'|0⟩⟨1|⁽¹⁾'
>>> unicode(LocalSigma(0, 1, hs=1), sig_as_ketbra=False)
'σ̂_0,1^(1)'
>>> unicode(A + B, unicode_sub_super=False)
'Â^(1) + B̂^(1)'
>>> unicode(A + B, unicode_op_hats=False)
'A⁽¹⁾ + B⁽¹⁾'

Note that the accepted parameters and their default values may be changed through init_printing() or configure_printing()

qnet.printing.latex(expr, cache=None, **settings)[source]

Return a LaTeX representation of the given object / expression

Parameters:
  • expr – Expression to print
  • cache (dict or None) – dictionary to use for caching
  • show_hs_label (bool or str) – Whether to a label for the Hilbert space of expr. By default (show_hs_label=True), the label is shown as a superscript. It can be shown as a subscript with show_hs_label='subscript' or suppressed entirely (show_hs_label=False)
  • tex_op_macro (str) – macro to use for formatting operator symbols. Must accept ‘name’ as a format key.
  • tex_textop_macro (str) – macro to use for formatting multi-letter operator names.
  • tex_sop_macro (str) – macro to use for formattign super-operator symbols
  • tex_textsop_macro (str) – macro to use for formatting multi-letter super-operator names
  • tex_identity_sym (str) – macro for the identity symbol
  • tex_use_braket (bool) – If True, use macros from the braket package. Note that this will not automatically render in IPython Notebooks, but it is recommended when generating latex for a document.

Examples

>>> from qnet.algebra import OperatorSymbol, LocalSigma
>>> A = OperatorSymbol('A', hs=1); B = OperatorSymbol('B', hs=1)
>>> latex(A + B)
'\\hat{A}^{(1)} + \\hat{B}^{(1)}'
>>> latex(A + B, cache={A: 'A', B: 'B'})
'A + B'
>>> latex(A + B, show_hs_label='subscript')
'\\hat{A}_{(1)} + \\hat{B}_{(1)}'
>>> latex(A + B, show_hs_label=False)
'\\hat{A} + \\hat{B}'
>>> latex(LocalSigma(0, 1, hs=1))
'\\left\\lvert 0 \\middle\\rangle\\!\\middle\\langle 1 \\right\\rvert^{(1)}'
>>> latex(LocalSigma(0, 1, hs=1), sig_as_ketbra=False)
'\\hat{\\sigma}_{0,1}^{(1)}'
>>> latex(A + B, tex_op_macro=r'\Op{{{name}}}')
'\\Op{A}^{(1)} + \\Op{B}^{(1)}'
>>> CNOT = OperatorSymbol('CNOT', hs=1)
>>> latex(CNOT)
'\\text{CNOT}^{(1)}'
>>> latex(CNOT, tex_textop_macro=r'\Op{{{name}}}')
'\\Op{CNOT}^{(1)}'
>>> from qnet.algebra import SuperOperatorSymbol
>>> A = SuperOperatorSymbol('A', hs=1)
>>> latex(A)
'\\mathrm{A}^{(1)}'
>>> latex(A, tex_sop_macro=r'\SOp{{{name}}}')
'\\SOp{A}^{(1)}'
>>> Lindbladian = SuperOperatorSymbol('Lindbladian', hs=1)
>>> latex(Lindbladian)
'\\mathrm{Lindbladian}^{(1)}'
>>> latex(Lindbladian, tex_textsop_macro=r'\SOp{{{name}}}')
'\\SOp{Lindbladian}^{(1)}'
>>> from qnet.algebra import IdentityOperator
>>> latex(IdentityOperator)
'\\mathbb{1}'
>>> latex(IdentityOperator, tex_identity_sym=r'\identity')
'\\identity'
>>> latex(LocalSigma(0, 1, hs=1), tex_use_braket=True)
'\\Ket{0}\\!\\Bra{1}^{(1)}'

Note that the accepted parameters and their default values may be changed through init_printing() or configure_printing()

qnet.printing.tex(expr, cache=None, **settings)[source]

Alias for latex()

qnet.printing.srepr(expr, indented=False, cache=None)[source]

Render the given expression into a string that can be evaluated in an appropriate context to re-instantiate an identical expression. If indented is False (default), the resulting string is a single line. Otherwise, the result is a multiline string, and each positional and keyword argument of each Expression is on a separate line, recursively indented to produce a tree-like output. The cache may be used to generate more readable expressions.

Example

>>> from qnet.algebra import OperatorSymbol, OperatorPlus, LocalSpace
>>> hs = LocalSpace('1')
>>> A = OperatorSymbol('A', hs=hs); B = OperatorSymbol('B', hs=hs)
>>> expr = A + B
>>> srepr(expr)
"OperatorPlus(OperatorSymbol('A', hs=LocalSpace('1')), OperatorSymbol('B', hs=LocalSpace('1')))"
>>> eval(srepr(expr)) == expr
True
>>> srepr(expr, cache={hs:'hs'})
"OperatorPlus(OperatorSymbol('A', hs=hs), OperatorSymbol('B', hs=hs))"
>>> eval(srepr(expr, cache={hs:'hs'})) == expr
True
>>> print(srepr(expr, indented=True))
OperatorPlus(
    OperatorSymbol(
        'A',
        hs=LocalSpace(
            '1')),
    OperatorSymbol(
        'B',
        hs=LocalSpace(
            '1')))
>>> eval(srepr(expr, indented=True)) == expr
True

See also

print_tree(), respectively qnet.printing.tree.tree(), produces an output similar to the indented srepr(), for interactive use. Their result cannot be evaluated and the exact output depends on init_printing().

dotprint() provides a way to graphically explore the tree structure of an expression.