qnet.printing package¶
Printing system for QNET Expressions and related objects
Submodules:
Summary¶
__all__
Functions:
ascii |
Return an ASCII representation of the given object / expression |
configure_printing |
Context manager for temporarily changing the printing system. |
dotprint |
Return the `DOT`_ (graph) description of an Expression tree as a string |
init_printing |
Initialize the printing system. |
latex |
Return a LaTeX representation of the given object / expression |
print_tree |
Print a tree representation of the structure of expr |
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() |
tree |
Give the output of tree as a multiline string, using line drawings to visualize the hierarchy of expressions (similar to the tree unix command line program for showing directory trees) |
unicode |
Return a unicode representation of the given object / expression |
Reference¶
-
qnet.printing.
init_printing
(*, reset=False, init_sympy=True, **kwargs)[source]¶ Initialize the printing system.
This determines the behavior of the
ascii()
,unicode()
, andlatex()
functions, as well as the__str__
and__repr__
of anyExpression
.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 anExpression
. 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()
forstr_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 withcaching=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 to ‘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 passreset=True
. In a Jupyter notebook, expressions are rendered graphically via LaTeX, using the settings as they affect thelatex()
printer.The
sympy.init_printing()
routine is called automatically, unless init_sympy is given asFalse
.See also
configure_printing()
allows to temporarily change the printing system from what was configured ininit_printing()
.- str_format (str) – Format for
-
qnet.printing.
configure_printing
(**kwargs)[source]¶ Context manager for temporarily changing the printing system.
This takes the same parameters as
init_printing()
Example
>>> A = OperatorSymbol('A', hs=1); B = OperatorSymbol('B', hs=1) >>> with configure_printing(show_hs_label=False): ... print(ascii(A + B)) A + B >>> print(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 withshow_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
>>> 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()
orconfigure_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 withshow_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
>>> 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()
orconfigure_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 withshow_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.
- tex_frac_for_spin_labels (bool) – Whether to use ‘frac’ when printing basis state labels for spin Hilbert spaces
Examples
>>> 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)}'
>>> 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)}'
>>> 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)}'
>>> spin = SpinSpace('s', spin=(1, 2)) >>> up = SpinBasisKet(1, 2, hs=spin) >>> latex(up) '\\left\\lvert +1/2 \\right\\rangle^{(s)}' >>> latex(up, tex_frac_for_spin_labels=True) '\\left\\lvert +\\frac{1}{2} \\right\\rangle^{(s)}'
Note that the accepted parameters and their default values may be changed through
init_printing()
orconfigure_printing()
-
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
>>> 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()
, respectivelyqnet.printing.tree.tree()
, produces an output similar to the indentedsrepr()
, for interactive use. Their result cannot be evaluated and the exact output depends oninit_printing()
.dotprint()
provides a way to graphically explore the tree structure of an expression.