qat.core.variables.ArithExpression

class qat.core.variables.ArithExpression(symbol, *children)

Class to represent arbitrary arithmetic expressions.

This class is not designed to be instantiated by hand. Expressions are constructed by calling a Symbol object on one or several variables.

Additionally, expressions (and variables) have their numeric operator overloaded (i.e +, -, etc).

from qat.core.variables import cos, Variable

a = Variable("a")
print(type(cos))
expr = cos(a)
print(type(expr))
<class 'qat.core.variables.Symbol'>
<class 'qat.core.variables.ArithExpression'>
Parameters
  • symbol (Symbol) – the symbol labeling the node

  • children (list) – the list of children of the node. These could be variables, expressions, or constants

differentiate(other_var)

Differentiate an expression w.r.t. to a variable.

from qat.core.variables import cos, Variable

a = Variable("a")
expr = a * cos(a)
print(expr.differentiate("a"))
((a * -(sin(a))) + cos(a))
Parameters

other_var (str) – a variable name

Returns

an arithmetic expression

Return type

ArithExpression

static from_string(thrift_string, var_dic=None)

Builds an arithmetic expression out of a RPN string.

Effectively the inverse of the to_thrift method.

from qat.core.variables import ArithExpression

expr = ArithExpression.from_string("+ a b")
print(expr)
(a + b)
Parameters
  • thrift_string (str) – a RPN string

  • var_dic (optional, dict) – a dictionary of variables as found in a Circuit

get_variables()

Returns the list of variables appearing inside the expression.

Returns

a set of variable indexes

Return type

list<int>

pretty_print(depth=0)

Pretty prints the expression for debugging (mostly)

to_thrift()

Generates a thrift RPN version of the expression.

See Variable method with the same name.

Returns

a string representation

Return type

str