qp.resource.Resources

class Resources(*, counts, extra=<factory>)[source]

Bases: object

Base class for storing resource information.

Warning

This class is intended to be immutable. Modifying the attributes after creation may lead to unexpected behaviour.

Parameters:
  • counts (dict[str, int]) – A dictionary mapping resources to their counts.

  • extra (dict[int, int]) – A dictionary for storing any extra fields that need to be tracked.

Attributes in this class can be of type Expression, allowing for symbolic manipulation and substitution of variables. When variables of type Expression are present as top-level fields or as values within (possibly nested) dictionaries of arbitrary depth, the vars attribute will contain the set of all symbolic variables used in the resource counts. This includes fields introduced in derived classes. Similarly, the subs() method can be used to substitute symbolic variables with concrete values.

counts

extra

vars

is_symbolic

Returns True if this object contains any symbolic variables, False otherwise.

counts
extra
vars
is_symbolic

Returns True if this object contains any symbolic variables, False otherwise.

subs([substitutions])

Substitute symbolic variables in the object with concrete values.

to_pretty_str([preindent])

Convert a Resources object into a human-readable string representation.

subs(substitutions=None, **kwargs)[source]

Substitute symbolic variables in the object with concrete values.

Automatically iterates over all fields of the dataclass and applies substitutions to any Expression instances, so derived classes do not have to explicitly implement this method unless they want to customize the behaviour.

Note

Every Expression leaf is substituted, including those nested to arbitrary depth within registered pytree containers (dict, list, tuple, …). The original container structure is preserved.

Parameters:

substitutions (dict[str, int | float] | None) – A dictionary mapping variable names to their values. If None, an empty dictionary is used. Additional keyword arguments can also be provided.

Example

>>> from pennylane.resource import SpecsResources, Expression
>>> res = SpecsResources(
...     counts={"CNOT": Expression({("x",): 1})},
...     measurement_processes={"expval(PauliX)": 1},
...     num_wires=2,
...     circuit_depth=1,
... )

Calling this method will return a new instance of the same type which has the same structure but with the symbolic variables substituted with the provided values. A substitution may either use a dictionary or keyword arguments:

>>> res.subs({"x": 3})
SpecsResources(counts={'CNOT': 3}, measurement_processes={'expval(PauliX)': 1}, num_wires=2, circuit_depth=1, total_quantum_operations=3)
>>> res.subs(x=3)
SpecsResources(counts={'CNOT': 3}, measurement_processes={'expval(PauliX)': 1}, num_wires=2, circuit_depth=1, total_quantum_operations=3)
to_pretty_str(preindent=0)[source]

Convert a Resources object into a human-readable string representation.

Automatically iterates over all fields of the dataclass and formats them into a string, including any nested dictionaries.

Note

This method does not have to be implemented by derived classes, but may be overridden if a different formatting is desired.

Parameters:

preindent (int, optional) – The number of spaces to insert before each line. Defaults to 0.

Returns:

A human-readable string representation of this object.

Return type:

str