Skip to main content

flatten

flatten(Operator input) -> Operator

Flattens any operator by expanding all expressions into a sum of products (in-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 in-place (recommended: OperatorSum).

Returns

Reference to the modified Operator (allows method chaining)

Example

var my_ops = (2+3i)*(X(0) * (Y(1) + ID()));
// Modifies my_ops to ((2+3i) * X(0) * Y(1) + (2+3i) * X(0) * ID())
flatten(my_ops);

// Alternative syntax:
my_ops.flatten(); // Same result, member function style

// Method chaining example:
var result = flatten(my_ops); // result refers to the same modified my_ops