# qnet.algebra.ordering module¶

The ordering package implements the default canonical ordering for sums and products of operators, states, and superoperators.

To the extent that commutativity rules allow this, the ordering defined here groups objects of the same Hilbert space together, and orders these groups in the same order that the Hilbert spaces occur in a ProductSpace (lexicographically/by order_index/by complexity). Objects within the same Hilbert space (again, assuming they commute) are ordered by the KeyTuple value that expr_order_key returns for each object. Note that expr_order_key defers to the object’s _order_key property, if available. This property should be defined for all QNET Expressions, generally ordering objects according to their type, then their label (if any), then their pre-factor then any other properties.

We assume that quantum operations have either full commutativity (sums, or products of states), or commutativity of objects only in different Hilbert spaces (e.g. products of operators). The former is handled by FullCommutativeHSOrder, the latter by DisjunctCommutativeHSOrder. Theses classes serve as the order_key for sums and products (e.g. OperatorPlus and similar classes)

A user may implement a custom ordering by subclassing (or replacing) FullCommutativeHSOrder and/or DisjunctCommutativeHSOrder, and assigning their replacements to all the desired algebraic classes.

## Summary¶

Classes:

 DisjunctCommutativeHSOrder Auxiliary class that generates the correct pseudo-order relation for operator products. FullCommutativeHSOrder Auxiliary class that generates the correct pseudo-order relation for operator sums. KeyTuple A tuple that allows for ordering, facilitating the default ordering of Operations.

Functions:

 expr_order_key A default order key for arbitrary expressions

## Reference¶

class qnet.algebra.ordering.KeyTuple[source]

Bases: tuple

A tuple that allows for ordering, facilitating the default ordering of Operations. It differs from a normal tuple in that it falls back to string comparison if any elements are not directly comparable

qnet.algebra.ordering.expr_order_key(expr)[source]

A default order key for arbitrary expressions

class qnet.algebra.ordering.DisjunctCommutativeHSOrder(op, space_order=None, op_order=None)[source]

Bases: object

Auxiliary class that generates the correct pseudo-order relation for operator products. Only operators acting on disjoint Hilbert spaces are commuted to reflect the order the local factors have in the total Hilbert space. I.e., sorted(factors, key=DisjunctCommutativeHSOrder) achieves this ordering.

class qnet.algebra.ordering.FullCommutativeHSOrder(op, space_order=None, op_order=None)[source]

Bases: object

Auxiliary class that generates the correct pseudo-order relation for operator sums. Operators are first ordered by their Hilbert space, then by their order-key; sorted(factors, key=FullCommutativeHSOrder) achieves this ordering.