The expression
module
The expression
module defines an Expression
type as a symbolic representation for mathematical expressions together with arithmetic rules. It is syntactically constructed using S-Expressions in prefix notation where the ordered parameters are:
- A tag as a Python
Enum
: a token identifier for variables, functions or operations. - Arguments: a set of arguments to define or be passed to the expression.
- Attributes: keyword arguments for symbolic evaluation and compilation directives.
The Expression
type defines constructors for four identifiers and operations variants:
VALUE
: A value type to hold numericalcomplex
,float
orint
primitive types.SYMBOL
: A symbol type for names definition.FN
: A variadic function type defined as a symbolic name and arguments.QUANTUM_OP
: A quantum operator type defined as an expression, a support of qubit resources and a collection of property attributes.ADD
: A variadic addition type as the sum of arguments.MUL
: A variadic multiplication type as the product of arguments.KRON
: A variadic multiplication type as the product of arguments with commutative rules.POW
: A power type as the exponentiation of a base and power arguments.
Expression(head, *args, **attributes)
A symbolic representation of mathematical expressions.
Besides arithmetic operations, the expression can contain classical functions such as sine, cosine, and logarithm, and abstract representations of quantum operators.
Multiplication between quantum operators is stored as Kronecker products. Operators are ordered by qubit index, preserving the order when the qubit indices of two operators overlap. This ensures that operators acting on the same subspace are kept together, enhancing optimisation.
Source code in qadence2_expressions/core/expression.py
dag: Expression
property
Returns the conjugated/dagger version of and expression.
max_index: int
cached
property
subspace: Support | None
cached
property
Returns the total subspace coverage of an expression with quantum operators. If there are no quantum operators, the subspace is None. If controlled operators are present, it returns a controlled support if there is no overlap between targets and controls; otherwise, all indices are treated as targets.
Example:
Tag
Bases: Enum
This auxiliar class allows the Expression
to be represented as a tagged union.
__getitem__(index)
Makes the arguments of the expression directly accessible through expression[i]
.
__pow__(other)
Power involving quantum operators always promote expression to quantum operators.
Source code in qadence2_expressions/core/expression.py
add(*args)
classmethod
In Expressions, addition is a variadic operation representing the sum of its arguments.
Expression.add(a, b, c) == a + b + c
Source code in qadence2_expressions/core/expression.py
as_quantum_operator()
Promotes and expression to a quantum operator.
When a function takes a quantum operator as input, the function itself must be transformed into a quantum operator to preserve commutation properties. For instance,
Example:
Source code in qadence2_expressions/core/expression.py
function(name, *args)
classmethod
Symbolic representation of a function. The name
indicates the function identifier, the
remaining arguments are used as the function arguments.
Expression.function("sin", 1.57) => sin(1.57)
PARAMETER | DESCRIPTION |
---|---|
name
|
The function name.
TYPE:
|
args
|
The arguments to be passed to the function.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Expression
|
A |
Source code in qadence2_expressions/core/expression.py
get(attribute, default=None)
Retrieve the value of the chosen attribute
if it exists, or return the default
value
if it doesn't.
kron(*args)
classmethod
In Expressions, the Kronecker product is a variadic operation representing the multiplication of its arguments applying commutative rules. When the qubit indices of two operators overlap, the order is preserved. Otherwise, the operators are ordered by index, with operators acting on the same qubits placed next to each other.
Expression.kron(X(1), X(2), Y(1)) == X(1)Y(1) ⊗ X(2)
Source code in qadence2_expressions/core/expression.py
mul(*args)
classmethod
In Expressions, multiplication is a variadic operation representing the product of its arguments.
Expression.mul(a, b, c) == a * b * c
Source code in qadence2_expressions/core/expression.py
one()
classmethod
Used to represent both, the numerical value 1
and the identity operator.
RETURNS | DESCRIPTION |
---|---|
Expression
|
An |
pow(base, power)
classmethod
Define a power expression.
Expression.power(a, b) == a**b
quantum_operator(expr, support, **attributes)
classmethod
To turn an expression into a quantum operator, specify the support it acts on. Attributes
like is_projector
[bool], is_hermitian
[bool], is_unitary
[bool], is_dagger
[bool],
and join
[callable] indicate how the operator behaves during evaluation.
A parametric quantum operator is a function wrapped in a quantum operator. The join
attribute is used to combine the arguments of two parametric operators.
PARAMETER | DESCRIPTION |
---|---|
expr
|
An expression that describes the operator. If
TYPE:
|
support
|
The qubit indices to what the operator is applied.
TYPE:
|
Kwargs
Keyword arguments are used primarily to symbolic evaluation. Examples of keywords are:
- is_projector
[bool]
- is_hermitian
[bool]
- is_unitary
[bool]
- is_dagger
[bool]
- join
[callable]
The keyword, join
is used with parametric operators. Whe two parametric operator of
the same kind action on the same subspace are muliplied, the join
is used to combine
their arguments.
RETURNS | DESCRIPTION |
---|---|
Expression
|
An expression of type |
Source code in qadence2_expressions/core/expression.py
symbol(identifier, **attributes)
classmethod
Create a symbol from the identifier.
PARAMETER | DESCRIPTION |
---|---|
identifier
|
A string used as the symbol name.
TYPE:
|
Kwargs
Keyword arguments are used as flags for compilation steps. The valid flags are defined in Qadence2-IR.
RETURNS | DESCRIPTION |
---|---|
Expression
|
A |
Source code in qadence2_expressions/core/expression.py
value(x)
classmethod
Promote a numerical value (complex, float, int) to an expression.
PARAMETER | DESCRIPTION |
---|---|
x
|
A numerical value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Expression
|
A |
Source code in qadence2_expressions/core/expression.py
zero()
classmethod
Used to represent both, the numerical value 0
and the null operator.
RETURNS | DESCRIPTION |
---|---|
Expression
|
An |
evaluate_kron(expr)
Evaluate Kronecker product expressions.
Source code in qadence2_expressions/core/expression.py
evaluate_kronjoin(lhs, rhs)
Evaluate the Kronecker product between a LHS=quantum operators and a RHS=Kronecker product.
Source code in qadence2_expressions/core/expression.py
evaluate_kronleft(lhs, rhs)
Left associativity of the Kronecker product.
Evaluate the Kronecker product between a LHS=quantum operators and a RHS=Kronecker product.
Source code in qadence2_expressions/core/expression.py
evaluate_kronop(lhs, rhs)
Evaluate the Kronecker product between two quantum operators.
Source code in qadence2_expressions/core/expression.py
evaluate_kronright(lhs, rhs)
Right associativity of the Kronecker product.
Evaluate the Kronecker product between a LHS=quantum operators and a RHS=Kronecker product.
Source code in qadence2_expressions/core/expression.py
visualize_expression(expr)
Stringfy expressions.
Source code in qadence2_expressions/core/expression.py
visualize_sequence(expr, operator, with_brackets=True)
Stringfy the arguments of an expression expr
with the designed operator
.
The with_brackets
option wrap any argument that is either a multiplication or a sum.
Source code in qadence2_expressions/core/expression.py
visualize_with_brackets(expr)
Stringfy addition and multiplication expression surrounded by brackets.