3.2. qupulse.expressions

This subpackage contains qupulse’s expression logic. The submodule expressions.protocol defines the typing.Protocol that expression functionality providers must implement. This allows to substitute the powerful and expressive but slow default implementation with a faster less expressive backend.

The default implementation is in expressions.sympy.

There is are wrapper classes for finding non-protocol uses of expression in expressions.wrapper. Define QUPULSE_EXPRESSION_WRAPPER environment variable when running python to wrap all expression usages.

Exceptions

ExpressionVariableMissingException(variable, ...)

An exception indicating that a variable value was not provided during expression evaluation.

NonNumericEvaluation(expression, ...)

An exception that is raised if the result of evaluate_numeric is not a number.

class qupulse.expressions.Expression(*args, **kwargs)[source]

Bases: qupulse.serialization.AnonymousSerializable

Base class for expressions.

evaluate_in_scope(scope: Mapping) Union[numbers.Number, numpy.ndarray][source]

Evaluate the expression by taking the variables from the given scope (typically of type Scope but it can be any mapping.) :param scope:

Returns:

evaluate_numeric(**kwargs) Union[numbers.Number, numpy.ndarray][source]
evaluate_symbolic(substitutions: Mapping[Any, Any]) qupulse.expressions.sympy.Expression[source]
classmethod make(expression_or_dict, numpy_evaluation=None) Union[qupulse.expressions.sympy.ExpressionScalar, qupulse.expressions.sympy.ExpressionVector, qupulse.expressions.sympy._ExpressionType][source]

Backward compatible expression generation

property underlying_expression: Union[sympy.core.expr.Expr, numpy.ndarray]
property variables: Sequence[str]

Get all free variables in the expression.

Returns

A collection of all free variables occurring in the expression.

class qupulse.expressions.ExpressionScalar(*args, **kwargs)[source]

Bases: qupulse.expressions.sympy.Expression

A scalar mathematical expression instantiated from a string representation. TODO: update doc! TODO: write tests!

Create an Expression object.

Receives the mathematical expression which shall be represented by the object as a string which will be parsed using py_expression_eval. For available operators, functions and constants see SymPy documentation

Parameters

ex (string) – The mathematical expression represented as a string

__init__(ex: Union[str, numbers.Number, sympy.core.expr.Expr]) None[source]

Create an Expression object.

Receives the mathematical expression which shall be represented by the object as a string which will be parsed using py_expression_eval. For available operators, functions and constants see SymPy documentation

Parameters

ex (string) – The mathematical expression represented as a string

evaluate_in_scope(scope: Mapping) Union[numbers.Number, numpy.ndarray][source]

Evaluate the expression by taking the variables from the given scope (typically of type Scope but it can be any mapping.) :param scope:

Returns:

evaluate_with_exact_rationals(scope: Mapping) Union[numbers.Number, numpy.ndarray][source]
get_serialization_data() Union[str, float, int][source]

Return all data relevant for serialization as a JSON compatible type that is accepted as constructor argument

Returns

A JSON compatible type that can be used to construct an equal object.

is_nan() bool[source]
property original_expression: Union[str, numbers.Number]
property sympified_expression: sympy.core.expr.Expr
property underlying_expression: sympy.core.expr.Expr
property variables: Sequence[str]

Get all free variables in the expression.

Returns

A collection of all free variables occurring in the expression.

exception qupulse.expressions.ExpressionVariableMissingException(variable: str, expression: qupulse.expressions.sympy.Expression)[source]

Bases: Exception

An exception indicating that a variable value was not provided during expression evaluation.

See also

qupulse.expressions.Expression

class qupulse.expressions.ExpressionVector(*args, **kwargs)[source]

Bases: qupulse.expressions.sympy.Expression

N-dimensional expression. TODO: write doc! TODO: write tests!

evaluate_in_scope(scope: Mapping) numpy.ndarray[source]

Evaluate the expression by taking the variables from the given scope (typically of type Scope but it can be any mapping.) :param scope:

Returns:

get_serialization_data() Sequence[str][source]

Return all data relevant for serialization as a JSON compatible type that is accepted as constructor argument

Returns

A JSON compatible type that can be used to construct an equal object.

sympify_vector = <numpy.vectorize object>
to_ndarray() numpy.ndarray[source]
property underlying_expression: numpy.ndarray
property variables: Sequence[str]

Get all free variables in the expression.

Returns

A collection of all free variables occurring in the expression.

exception qupulse.expressions.NonNumericEvaluation(expression: qupulse.expressions.sympy.Expression, non_numeric_result, call_arguments)[source]

Bases: Exception

An exception that is raised if the result of evaluate_numeric is not a number.

See also

qupulse.expressions.Expression.evaluate_numeric

Modules

qupulse.expressions.protocol

This module contains the interface / protocol descriptions of Expression, ExpressionScalar and ExpressionVector.

qupulse.expressions.sympy

This module defines the class Expression to represent mathematical expression as well as corresponding exception classes.

qupulse.expressions.wrapper

This module contains the function :py:make_wrappers to define wrapper classes for expression protocol implementations which only implements methods of the protocol.