qp.resource.register_resource_extension

register_resource_extension(name, aggregate, *, display_name)[source]

Register aggregation and a report heading for a compiler resource extension.

Warning

The aggregate callback must not modify its input data, including nested values. Inputs are passed without copying, so mutations can affect subsequent aggregations or reports.

Currently, aggregation only supports flat dictionaries of numeric values (int, float, or Expression). Boolean and other metadata types are not supported.

Parameters:
  • name (str) – Key in the compiler’s extended_fields and in extra.

  • aggregate (AggregateCallback) – aggregate(total, incoming, call_count) receives the caller’s data (an empty dictionary if absent), the callee’s data, and its numeric or symbolic call count. Return combined data with the same structure.

  • display_name (str) – Heading shown above the extension’s fields in reports.

Raises:

ValueError – If the name is already registered with different rules.

Register before requesting compiled resource analysis. Unregistered callee extensions retain the parser’s warning and are not aggregated.

Example

Register a count dictionary without writing callbacks:

>>> from pennylane.resource import register_resource_extension
>>> from pennylane.resource.extension import weighted_sum
>>> register_resource_extension(
...     "example_gates",
...     aggregate=weighted_sum,
...     display_name="Example gates",
... )

A payload such as {"Hadamard": 7} appears as a Hadamard row under Example gates in individual and comparison reports.

Symbolic call counts remain expressions that can be substituted later:

>>> from pennylane.resource import Expression
>>> a = Expression({("a",): 1})
>>> combined = weighted_sum({"count": 1}, {"count": 2}, a)
>>> combined["count"].subs(a=0)
1
>>> combined["count"].subs(a=3)
7