quil.expression

The expression module contains classes for representing Quil expressions.

class Expression:

A Quil expression.

Variants:

As seen above, some variants contain inner data that fully specify the expression. For example, the number variant contains an int. This is in contrast to variants like pi that have no inner data because they require none to fully specify the expression. This difference is important for determining which methods are available for each variant.

Methods (for every variant):

  • is_*: Returns True if the expression is that variant, False otherwise.

If the variant has inner data:

  • as_*: returns the inner data if it is the given variant, None otherwise.
  • to_*: returns the inner data if it is the given variant, raises ValueError otherwise.
  • from_*: Creates a new Expression of the given variant from an instance of the inner type.

If the variant doesn't have inner data (e.g. pi)

def from_address(inner: MemoryReference) -> Expression:
def from_function_call( inner: quil.expression.FunctionCallExpression) -> Expression:
def from_infix(inner: quil.expression.InfixExpression) -> Expression:
def from_number(inner: complex) -> Expression:
def new_pi() -> Expression:
def from_prefix(inner: quil.expression.PrefixExpression) -> Expression:
def from_variable(inner: str) -> Expression:

Returns the inner value of the variant. Raises a RuntimeError if inner data doesn't exist.

def is_address(self) -> bool:
def as_address(self) -> Optional[MemoryReference]:
def to_address(self) -> MemoryReference:
def is_function_call(self) -> bool:
def as_function_call(self) -> Optional[quil.expression.FunctionCallExpression]:
def to_function_call(self) -> quil.expression.FunctionCallExpression:
def is_infix(self) -> bool:
def as_infix(self) -> Optional[quil.expression.InfixExpression]:
def to_infix(self) -> quil.expression.InfixExpression:
def is_number(self) -> bool:
def as_number(self) -> Optional[complex]:
def to_number(self) -> complex:
def is_pi(self) -> bool:
def is_prefix(self) -> bool:
def as_prefix(self) -> Optional[quil.expression.PrefixExpression]:
def to_prefix(self) -> quil.expression.PrefixExpression:
def is_variable(self) -> bool:
def as_variable(self) -> Optional[str]:
def to_variable(self) -> str:
def to_quil(self) -> str:

Attempt to convert the instruction to a valid Quil string. Raises an exception if the instruction can't be converted to valid Quil.

def to_quil_or_debug(self) -> str:

Convert the instruction to a Quil string.

If any part of the instruction can't be converted to valid Quil, it will be printed in a human-readable debug format that isn't valid Quil.

def parse(input: str) -> Expression:

Parses an Expression from a string.

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

def simplify(self):

Simplify the expression as much as possible, in-place.

def into_simplified(self) -> Expression:

Return a simplified copy of the expression.

def evaluate( self, variables: Dict[str, complex], memory_references: Dict[str, Sequence[float]]) -> complex:

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

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

def substitute_variables( self, variable_values: Dict[str, Expression]) -> Expression:

Returns a copy of the expression where every matching variable in variable_values is replaced by the corresponding expression.

def to_real(self) -> float:

If this is a number with imaginary part "equal to" zero (of small absolute value), return that number. Otherwise, raises an EvaluationError.

class FunctionCallExpression:

A Quil function call.

expression: Expression
class InfixExpression:

A Quil infix expression.

right: Expression
left: Expression
class PrefixExpression:

A Quil prefix expression.

expression: Expression
class ExpressionFunction:

An enum representing a Quil function that can be applied to an expression.

Cis = Cis
Cosine = Cosine
Exponent = Exponent
Sine = Sine
SquareRoot = SquareRoot
class PrefixOperator:

An enum that represents the operators supported on a prefix expression.

Plus = Plus
Minus = Minus
class InfixOperator:

An enum that represents the operators supported on an infix expression.

Caret = Caret
Plus = Plus
Minus = Minus
Slash = Slash
Star = Star
class EvaluationError(builtins.ValueError):

Error that may occur while evaluation an Expression.

Inherited Members
builtins.ValueError
ValueError
builtins.BaseException
with_traceback
add_note
args
class ParseExpressionError(builtins.ValueError):

Error that may occur while parsing an Expression.

Inherited Members
builtins.ValueError
ValueError
builtins.BaseException
with_traceback
add_note
args