quil.expression

class EvaluationError(quil.QuilError):

Errors that may occur while evaluation an Expression.

class ParseExpressionError(quil.QuilError):

Errors that may occur while parsing an Expression.

class Expression:

The type of Quil expressions.

Quil expressions take advantage of structural sharing; if a Quil expression contains the same subexpression twice, such as x + y in (x + y) * (x + y), the two children of the * node will be the same pointer. This is implemented through interning, also known as hash-consing; the recursive references to child nodes are done via [ArcIntern<Expression>]s. Creating an [ArcIntern] from an Expression will always return the same pointer for the same expression.

The structural sharing means that equality, cloning, and hashing on Quil expressions are all very cheap, as they do not need to be recursive: equality of [ArcIntern]s is a single-pointer comparison, cloning of [ArcIntern]s is a pointer copy and an atomic increment, and hashing of [ArcIntern]s hashes a single pointer. It is also very cheap to key [HashMap]s by an [ArcIntern<Expression>], which can allow for cheap memoization of operations on Expressions.

The structural sharing also means that Quil expressions are fundamentally immutable; it is impossible to get an owned or &mut reference to the child Expressions of any Expression, as the use of interning means there may be multiple references to that Expression at any time.

Note that when comparing Quil expressions, any embedded NaNs are treated as equal to other NaNs, not unequal, in contravention of the IEEE 754 spec.

def to_real(self, /):

If this is a number with imaginary part "equal to" zero (of _small_ absolute value), return that number. Otherwise, error with an evaluation error of a descriptive type.

def into_simplified(self, /):

Return an expression derived from this one, simplified as much as possible.

def evaluate(self, /, variables, memory_references):

Evaluate an expression, expecting that it may be fully reduced to a single complex number.

If it cannot be reduced to a complex number, this raises an error.

def substitute_variables(self, /, variable_values):

Substitute an expression in the place of each matching variable.

def parse(input):

Parse an Expression from a string.

Raises a ParseExpressionError error if the string isn't a valid Quil expression.

class Expression.Address(Expression):
class Expression.FunctionCall(Expression):
class Expression.Infix(Expression):
class Expression.Number(Expression):
class Expression.Pi(Expression):
class Expression.Prefix(Expression):
class Expression.Variable(Expression):
class ExpressionFunction:

A function defined within Quil syntax.

CIS = Cis
COSINE = Cosine
EXPONENT = Exponent
SINE = Sine
SQUARE_ROOT = SquareRoot
class FunctionCallExpression:

The type of function call Quil expressions, e.g. sin(e).

Quil expressions take advantage of structural sharing, which is why the expression here is wrapped in an [ArcIntern]; for more details, see the documentation for [Expression].

Note that when comparing Quil expressions, any embedded NaNs are treated as equal to other NaNs, not unequal, in contravention of the IEEE 754 spec.

expression
function
class InfixExpression:

The type of infix Quil expressions, e.g. e1 + e2.

Quil expressions take advantage of structural sharing, which is why the left and right expressions here are wrapped in [ArcIntern]s; for more details, see the documentation for [Expression].

Note that when comparing Quil expressions, any embedded NaNs are treated as equal to other NaNs, not unequal, in contravention of the IEEE 754 spec.

operator
left
right
class InfixOperator:
CARET = Caret
PLUS = Plus
MINUS = Minus
SLASH = Slash
STAR = Star
class PrefixExpression:

The type of prefix Quil expressions, e.g. -e.

Quil expressions take advantage of structural sharing, which is why the expression here is wrapped in an [ArcIntern]; for more details, see the documentation for [Expression].

Note that when comparing Quil expressions, any embedded NaNs are treated as equal to other NaNs, not unequal, in contravention of the IEEE 754 spec.

operator
expression
class PrefixOperator:
PLUS = Plus
MINUS = Minus