kwant.continuum.sympify

Contents

kwant.continuum.sympify#

kwant.continuum.sympify(expr, locals=None)[source]#

Sympify object using special rules for Hamiltonians.

If ‘expr` is already a type that SymPy understands, it will do nothing but return that value. Note that locals will not be used in this situation.

Otherwise, it is sympified by sympy.sympify with a modified namespace such that

  • the position operators “x”, “y” or “z” and momentum operators “k_x”, “k_y”, and “k_z” do not commute,

  • all single-letter identifiers and names of Greek letters (e.g. “pi” or “gamma”) are treated as symbols,

  • “kron” corresponds to sympy.physics.quantum.TensorProduct, and “identity” to sympy.eye,

  • “sigma_0”, “sigma_x”, “sigma_y”, “sigma_z” are the Pauli matrices.

In addition, Python list literals are interpreted as SymPy matrices.

Warning

This function uses eval (because it calls sympy.sympify), and thus should not be used on unsanitized input.

Parameters:
  • expr (str or SymPy expression) – Expression to be converted to a SymPy object.

  • locals (dict or None (default)) –

    Additional entries for the namespace under which expr is sympified. The keys must be valid Python variable names. The values may be strings, since they are all are sent through continuum.sympify themselves before use. (Note that this is a difference to how sympy.sympify behaves.)

    Note

    When a value of locals is already a SymPy object, it is used as-is, and the caller is responsible to set the commutativity of its symbols appropriately. This possible source of errors is demonstrated in the last example below.

Returns:

result

Return type:

SymPy object

Examples

>>> sympify('k_x * A(x) * k_x + V(x)')
k_x*A(x)*k_x + V(x)     # as valid sympy object
>>> sympify('k_x**2 + V', locals={'V': 'V_0 + V(x)'})
k_x**2 + V(x) + V_0
>>> ns = {'sigma_plus': [[0, 2], [0, 0]]}
>>> sympify('k_x**2 * sigma_plus', ns)
Matrix([
[0, 2*k_x**2],
[0,        0]])
>>> sympify('k_x * A(c) * k_x', locals={'c': 'x'})
k_x*A(x)*k_x
>>> sympify('k_x * A(c) * k_x', locals={'c': sympy.Symbol('x')})
A(x)*k_x**2