qml.labs.resource_estimation.ResourceConfig

class ResourceConfig[source]

Bases: object

A container to track the configuration for precisions and custom decompositions for the resource estimation pipeline.

set_decomp(op_type, decomp_func[, decomp_type])

Sets a custom function to override the default resource decomposition.

set_precision(op_type, precision)

Sets the precision for a given resource operator.

set_single_qubit_rot_precision(precision)

Sets the synthesis precision for all single-qubit rotation gates.

set_decomp(op_type, decomp_func, decomp_type=DecompositionType.BASE)[source]

Sets a custom function to override the default resource decomposition.

Parameters:
  • op_type (type[ResourceOperator]) – the operator class whose decomposition is being overriden.

  • decomp_func (Callable) – the new resource decomposition function to be set as default.

  • decomp_type (None | DecompositionType) – the decomposition type to override. Options are "adj", "pow", "ctrl", and "base". Default is "base".

Raises:

ValueError – If decomp_type is not a valid decomposition type.

Note

The new decomposition function, decomp_func, should have the same signature as the one it replaces. Specifically, the signature should match the resource_keys of the base resource operator class being overriden.

Example

from pennylane.labs import resource_estimation as plre

def custom_res_decomp(**kwargs):
    h = plre.resource_rep(plre.ResourceHadamard)
    s = plre.resource_rep(plre.ResourceS)
    return [plre.GateCount(h, 1), plre.GateCount(s, 2)]
>>> print(plre.estimate(plre.ResourceX(), gate_set={"Hadamard", "Z", "S"}))
--- Resources: ---
Total qubits: 1
Total gates : 4
Qubit breakdown:
  clean qubits: 0, dirty qubits: 0, algorithmic qubits: 1
Gate breakdown:
  {'Hadamard': 2, 'S': 2}
>>> config = plre.ResourceConfig()
>>> config.set_decomp(plre.ResourceX, custom_res_decomp)
>>> print(plre.estimate(plre.ResourceX(), gate_set={"Hadamard", "Z", "S"}, config=config))
--- Resources: ---
Total qubits: 1
Total gates : 3
Qubit breakdown:
  clean qubits: 0, dirty qubits: 0, algorithmic qubits: 1
Gate breakdown:
  {'S': 1, 'Hadamard': 2}
set_precision(op_type, precision)[source]

Sets the precision for a given resource operator.

This method updates the precision value for operators that use a single tolerance parameter (e.g., for synthesis error). It will raise an error if you attempt to set the precision for an operator that is not configurable or uses bit-precisions. A negative precision will also raise an error.

Parameters:
  • op_type (type[ResourceOperator]) – the operator class for which to set the precision

  • precision (float) – The desired synthesis precision tolerance. A smaller value corresponds to a higher precision compilation, which may increase the required gate counts. Must be greater than 0.

Raises:

ValueError – If op_type is not a configurable operator or if setting the precision for it is not supported, or if precision is negative.

Example

from pennylane.labs.resource_estimation import ResourceConfig
from pennylane.labs.resource_estimation.templates import ResourceSelectPauliRot

config = ResourceConfig()

# Check the default precision
default = config.resource_op_precisions[ResourceSelectPauliRot]['precision']
print(f"Default precision for SelectPauliRot: {default}")

# Set a new precision
config.set_precision(ResourceSelectPauliRot, precision=1e-5)
new = config.resource_op_precisions[ResourceSelectPauliRot]['precision']
print(f"New precision for SelectPauliRot: {new}")
Default precision for SelectPauliRot: 1e-09
New precision for SelectPauliRot: 1e-05
set_single_qubit_rot_precision(precision)[source]

Sets the synthesis precision for all single-qubit rotation gates.

This is a convenience method to update the synthesis precision tolerance for all standard single-qubit rotation gates and their controlled versions at once. The synthesis precision dictates the precision for compiling rotation gates into a discrete gate set, which in turn affects the number of gates required.

This method updates the precision value for the following operators: - ResourceRX - ResourceRY - ResourceRZ - ResourceCRX - ResourceCRY - ResourceCRZ

Parameters:

precision (float) – The desired synthesis precision tolerance. A smaller value corresponds to a higher precision compilation, which may increase the required gate counts. Must be greater than 0.

Raises:

ValueError – If precision is a negative value.

Example

from pennylane.labs.resource_estimation import ResourceConfig
from pennylane.labs.resource_estimation.ops.qubit.parametric_ops_single_qubit import ResourceRX

config = ResourceConfig()
print(f"Default RX precision: {config.resource_op_precisions[ResourceRX]['precision']}")

config.set_single_qubit_rot_precision(1e-5)
print(f"Updated RX precision: {config.resource_op_precisions[ResourceRX]['precision']}")
Default RX precision: 1e-09
Updated RX precision: 1e-05