qnet.algebra.toolbox.core module

Summary

Functions:

no_instance_caching Temporarily disable instance caching in create()
symbols The symbols() function from SymPy
temporary_instance_cache Use a temporary cache for instances in create()
temporary_rules Allow temporary modification of rules for create()

__all__: no_instance_caching, symbols, temporary_instance_cache, temporary_rules

Reference

qnet.algebra.toolbox.core.no_instance_caching()[source]

Temporarily disable instance caching in create()

Within the managed context, create() will not use any caching, for any class.

qnet.algebra.toolbox.core.temporary_instance_cache(*classes)[source]

Use a temporary cache for instances in create()

The instance cache used by create() for any of the given classes will be cleared upon entering the managed context, and restored on leaving it. That is, no cached instances from outside of the managed context will be used within the managed context, and vice versa

qnet.algebra.toolbox.core.temporary_rules(*classes, clear=False)[source]

Allow temporary modification of rules for create()

For every one of the given classes, protect the rules (processed by match_replace() or match_replace_binary()) associated with that class from modification beyond the managed context. Implies temporary_instance_cache(). If clear is given as True, all existing rules are temporarily cleared from the given classes on entering the managed context.

Within the managed context, add_rule() may be used for any class in classes to define local rules, or del_rules() to disable specific existing rules (assuming clear is False). Upon leaving the managed context all original rules will be restored, removing any local rules.

The classessimplifications attribute is also protected from permanent modification. Locally modifying simplifications should be done with care, but allows complete control over the creation of expressions.

qnet.algebra.toolbox.core.symbols(names, **args)[source]

The symbols() function from SymPy

This can be used to generate QNET symbols as well:

>>> A, B, C = symbols('A B C', cls=OperatorSymbol, hs=0)
>>> srepr(A)
"OperatorSymbol('A', hs=LocalSpace('0'))"
>>> C1, C2 = symbols('C_1:3', cls=CircuitSymbol, cdim=2)
>>> srepr(C1)
"CircuitSymbol('C_1', cdim=2)"

Basically, the cls keyword argument can be any instantiator, i.e. a class or callable that receives a symbol name as the single positional argument. Any keyword arguments not handled by symbols() directly (see sympy.core.symbol.symbols() documentation) is passed on to the instantiator. Obviously, this is extremely flexible.

Note

symbol() does not pass positional arguments to the instantiator. Two possible workarounds to create symbols with e.g. a scalar argument are:

>>> t = symbols('t', positive=True)
>>> A_t, B_t = symbols(
...     'A B', cls=lambda s: OperatorSymbol(s, t, hs=0))
>>> srepr(A_t, cache={t: 't'})
"OperatorSymbol('A', t, hs=LocalSpace('0'))"
>>> A_t, B_t = (OperatorSymbol(s, t, hs=0) for s in ('A', 'B'))
>>> srepr(B_t, cache={t: 't'})
"OperatorSymbol('B', t, hs=LocalSpace('0'))"