qnet.algebra.toolbox.equation module

Tools for working with equations

Summary

Classes:

Eq Symbolic equation

__all__: Eq

Reference

class qnet.algebra.toolbox.equation.Eq(lhs, rhs, tag=None, _prev_lhs=None, _prev_rhs=None, _prev_tags=None)[source]

Bases: object

Symbolic equation

This class keeps track of the lhs and rhs of an equation across arbitrary manipulations

Parameters:
  • lhs (Expression) – the left-hand-side of the equation
  • rhs (Expression) – the right-hand-side of the equation
  • tag (None or str) – a tag (equation number) to be shown when printing the equation

Example

>>> ω, E0 = sympy.symbols('omega, E_0')
>>> hbar = sympy.symbols('hbar', positive=True)
>>> H_0, H_1 = (OperatorSymbol(s, hs=0) for s in ('H_0', 'H_1'))
>>> H = OperatorSymbol('H', hs=0)
>>> mu = OperatorSymbol('mu', hs=0)
>>> eq0 = Eq(H_0, ω * Create(hs=0) * Destroy(hs=0) + E0, tag='0')
>>> print(unicode(eq0, show_hs_label=False))
Ĥ₀ = E₀ + ω â^† â    (0)
>>> eq1 = Eq(H_1, mu + E0, tag='1')
>>> print(unicode(eq1, show_hs_label=False))
Ĥ₁ = E₀ + μ̂    (1)
>>> eq = (
...     (eq0 + eq1).set_tag('2')
...     .apply_to_rhs(lambda expr: expr - 2*E0, cont=True)
...     .apply(lambda expr: expr * hbar, cont=True)
...     .apply_mtd_to_lhs(
...         'substitute', var_map={H_0 + H_1: H}, cont=True)
...     .apply(lambda expr: expr**2, cont=True)
...     .apply_mtd_to_rhs('substitute', var_map={mu: 0}, cont=True)
...     .apply_mtd_to_rhs('expand', cont=True, tag='⋆')
... )
>>> print(unicode(eq, show_hs_label=False))
    Ĥ₀ + Ĥ₁ = 2 E₀ + μ̂ + ω â^† â                 (2)
            = μ̂ + ω â^† â
h̅ (Ĥ₀ + Ĥ₁) = h̅ (μ̂ + ω â^† â)
        h̅ Ĥ = h̅ (μ̂ + ω â^† â)
     h̅² Ĥ Ĥ = h̅² (μ̂ + ω â^† â) (μ̂ + ω â^† â)
            = h̅² ω² â^† (𝟙 + â^† â) â
            = h̅² ω² â^† â^† â â + h̅² ω² â^† â    (⋆)
>>> (eq
...  .apply_mtd_to_lhs('substitute', eq.as_dict)
...  .verify().is_zero)
True
lhs

The left-hand-side of the equation

rhs

The right-hand-side of the equation

tag

A tag (equation number) to be shown when printing the equation, or None

set_tag(tag)[source]

Return a copy of the equation with a new tag

as_dict

Mapping of the lhs to the rhs

This allows to plug an equation into another expression via substitute().

apply(func, *args, cont=False, tag=None, **kwargs)[source]

Apply func to both sides of the equation

Returns a new equation where the left-hand-side and right-hand side are replaced by the application of func:

lhs=func(lhs, *args, **kwargs)
rhs=func(rhs, *args, **kwargs)

If cont=True, the resulting equation will keep a history of its previous state (resulting in multiple lines of equations when printed, as in the main example above).

The resulting equation with have the given tag.

apply_to_lhs(func, *args, cont=False, tag=None, **kwargs)[source]

Apply func to lhs of equation only

Like apply(), but modifying only the left-hand-side.

apply_to_rhs(func, *args, cont=False, tag=None, **kwargs)[source]

Apply func to rhs of equation only

Like apply(), but modifying only the right-hand-side.

apply_mtd(mtd, *args, cont=False, tag=None, **kwargs)[source]

Call the method mtd on both sides of the equation

That is, the left-hand-side and right-hand-side are replaced by:

lhs=lhs.<mtd>(*args, **kwargs)
rhs=rhs.<mtd>(*args, **kwargs)

The cont and tag parameters are as in apply().

apply_mtd_to_lhs(mtd, *args, cont=False, tag=None, **kwargs)[source]

Call the method mtd on the lhs of the equation only.

Like apply_mtd(), but modifying only the left-hand-side.

apply_mtd_to_rhs(mtd, *args, cont=False, tag=None, **kwargs)[source]

Call the method mtd on the rhs of the equation

Like apply_mtd(), but modifying only the right-hand-side.

substitute(var_map, cont=False, tag=None)[source]

Substitute sub-expressions both on the lhs and rhs

Parameters:var_map (dict) – Dictionary with entries of the form {expr: substitution}
verify(func=None, *args, **kwargs)[source]

Subtract the rhs from the lhs of the equation

Before the substraction, each side is expanded and any scalars are simplified. If given, func with the positional arguments args and keyword-arguments kwargs is applied to the result before returning it.

You may complete the verification by checking the is_zero attribute of the returned expression.

copy()[source]

Return a copy of the equation

free_symbols

Set of free SymPy symbols contained within the equation.

bound_symbols

Set of bound SymPy symbols contained within the equation.

all_symbols

Combination of free_symbols and bound_symbols