pub enum Expression {
    Address(MemoryReference),
    FunctionCall(FunctionCallExpression),
    Infix(InfixExpression),
    Number(Complex64),
    PiConstant,
    Prefix(PrefixExpression),
    Variable(String),
}

Variants§

§

Address(MemoryReference)

§

FunctionCall(FunctionCallExpression)

§

Infix(InfixExpression)

§

Number(Complex64)

§

PiConstant

§

Prefix(PrefixExpression)

§

Variable(String)

Implementations§

source§

impl Expression

source

pub fn simplify(&mut self)

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

Example
use quil_rs::expression::Expression;
use std::str::FromStr;
use num_complex::Complex64;

let mut expression = Expression::from_str("cos(2 * pi) + 2").unwrap();
expression.simplify();

assert_eq!(expression, Expression::Number(Complex64::from(3.0)));
source

pub fn into_simplified(self) -> Self

Consume the expression, simplifying it as much as possible.

Example
use quil_rs::expression::Expression;
use std::str::FromStr;
use num_complex::Complex64;

let simplified = Expression::from_str("cos(2 * pi) + 2").unwrap().into_simplified();

assert_eq!(simplified, Expression::Number(Complex64::from(3.0)));
source

pub fn evaluate( &self, variables: &HashMap<String, Complex64>, memory_references: &HashMap<&str, Vec<f64>> ) -> Result<Complex64, EvaluationError>

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

Example
use quil_rs::expression::Expression;
use std::str::FromStr;
use std::collections::HashMap;
use num_complex::Complex64;

let expression = Expression::from_str("%beta + theta[0]").unwrap();

let mut variables = HashMap::with_capacity(1);
variables.insert(String::from("beta"), Complex64::from(1.0));

let mut memory_references = HashMap::with_capacity(1);
memory_references.insert("theta", vec![2.0]);

let evaluated = expression.evaluate(&variables, &memory_references).unwrap();

assert_eq!(evaluated, Complex64::from(3.0))
source

pub fn substitute_variables( self, variable_values: &HashMap<String, Expression> ) -> Self

Substitute an expression in the place of each matching variable. Consumes the expression and returns a new one.

Example
use quil_rs::expression::Expression;
use std::str::FromStr;
use std::collections::HashMap;
use num_complex::Complex64;

let expression = Expression::from_str("%x + %y").unwrap();

let mut variables = HashMap::with_capacity(1);
variables.insert(String::from("x"), Expression::Number(Complex64::from(1.0)));

let evaluated = expression.substitute_variables(&variables);

assert_eq!(evaluated, Expression::from_str("1.0 + %y").unwrap())
source

pub fn to_real(&self) -> Result<f64, EvaluationError>

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.

source§

impl Expression

source

pub fn get_memory_references(&self) -> Vec<&MemoryReference>

Return, if any, the memory references contained within this Expression.

Trait Implementations§

source§

impl Add<Expression> for Expression

§

type Output = Expression

The resulting type after applying the + operator.
source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
source§

impl AddAssign<Expression> for Expression

source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
source§

impl BitXor<Expression> for Expression

§

type Output = Expression

The resulting type after applying the ^ operator.
source§

fn bitxor(self, other: Self) -> Self

Performs the ^ operation. Read more
source§

impl BitXorAssign<Expression> for Expression

source§

fn bitxor_assign(&mut self, other: Self)

Performs the ^= operation. Read more
source§

impl Clone for Expression

source§

fn clone(&self) -> Expression

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Expression

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Div<Expression> for Expression

§

type Output = Expression

The resulting type after applying the / operator.
source§

fn div(self, other: Self) -> Self

Performs the / operation. Read more
source§

impl DivAssign<Expression> for Expression

source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
source§

impl FromStr for Expression

§

type Err = ParseProgramError<Expression>

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Expression

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Mul<Expression> for Expression

§

type Output = Expression

The resulting type after applying the * operator.
source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
source§

impl MulAssign<Expression> for Expression

source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
source§

impl PartialEq<Expression> for Expression

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Quil for Expression

source§

fn write( &self, f: &mut impl Write, fall_back_to_debug: bool ) -> Result<(), ToQuilError>

Write the Quil representation of the item to the given writer. If fall_back_to_debug is true, then it must not return an error.
source§

fn to_quil(&self) -> Result<String, ToQuilError>

Return a string in valid Quil syntax or an error if the item cannot be represented with valid Quil.
source§

fn to_quil_or_debug(&self) -> String

Return a string in valid Quil syntax if possible. Any individual component of this object which cannot be represented in Quil will be replaced with a Debug representation of that component.
source§

impl Sub<Expression> for Expression

§

type Output = Expression

The resulting type after applying the - operator.
source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
source§

impl SubAssign<Expression> for Expression

source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
source§

impl Eq for Expression

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CallHasher for Twhere T: Hash + ?Sized,

§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64where H: Hash + ?Sized, B: BuildHasher,

source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.