Skip to main content

flattened

flattened(const Operator input) -> Operator

Flattens any operator by expanding all expressions into a sum of products (out-of-place).

This function distributes multiplication over addition, expanding nested sums and products into a flat sum of products. For example, expressions like A*(B + C) become A*B + A*C. We recommend using OperatorSum as input, which is safe. While any operator type is allowed, flattening does not make sense for other operator types.

Parameters

  • input: The input Operator to be flattened (recommended: OperatorSum).

Returns

A fully flattened Operator iciOpProdi\sum_i c_i \cdot \text{OpProd}_i where each OpProdi_i contains only products (no nested sum) with scalar coefficient cic_i.

Example

var my_ops = (2+3i)*X(0) * (Y(1) + ID());
var my_flat = flattened(my_ops);
// Returns ((2+3i) * X(0) * Y(1) + (2+3i) * X(0) * ID())