qml.estimator.resource_config.ResourceConfig

class ResourceConfig[source]

Bases: object

Sets the values of precisions and custom decompositions when estimating resources for a quantum workflow.

The precisions and custom decompositions of resource operators can be modified using the set_precision() and set_decomp() functions of the ResourceConfig class.

Example

This example shows how to set a custom precision value for every instance of the RX gate.

>>> import pennylane.estimator as qre
>>> my_config = qre.ResourceConfig()
>>> my_config.set_precision(qre.RX, precision=1e-5)
>>> res = qre.estimate(
...     qre.RX(),
...     gate_set={"RZ", "T", "Hadamard"},
...     config=my_config,
... )
>>> print(res)
--- Resources: ---
 Total wires: 1
   algorithmic wires: 1
   allocated wires: 0
     zero state: 0
     any state: 0
 Total gates : 28
   'T': 28

The ResourceConfig can also be used to set custom decompositions. The following example shows how to define a custom decomposition for the RX gate.

>>> def custom_RX_decomp(precision):  # RX = H @ RZ @ H
...     h = qre.Hadamard.resource_rep()
...     rz = qre.RZ.resource_rep(precision)
...     return [qre.GateCount(h, 2), qre.GateCount(rz, 1)]
>>>
>>> my_config = qre.ResourceConfig()
>>> my_config.set_decomp(qre.RX, custom_RX_decomp)
>>> res = qre.estimate(
...     qre.RX(precision=None),
...     gate_set={"RZ", "T", "Hadamard"},
...     config=my_config,
... )
>>> print(res)
--- Resources: ---
 Total wires: 1
   algorithmic wires: 1
   allocated wires: 0
     zero state: 0
     any state: 0
 Total gates : 3
   'RZ': 1,
   'Hadamard': 2

adj_custom_decomps

Returns the dictionary of custom adjoint decompositions.

ctrl_custom_decomps

Returns the dictionary of custom controlled decompositions.

custom_decomps

Returns the dictionary of custom base decompositions.

pow_custom_decomps

Returns the dictionary of custom power decompositions.

adj_custom_decomps

Returns the dictionary of custom adjoint decompositions.

ctrl_custom_decomps

Returns the dictionary of custom controlled decompositions.

custom_decomps

Returns the dictionary of custom base decompositions.

pow_custom_decomps

Returns the dictionary of custom power decompositions.

set_decomp(op_type, decomp_func[, decomp_type])

Sets a custom function to override the default resource decomposition.

set_precision(op_type, precision[, resource_key])

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

import pennylane.estimator as qre

def custom_res_decomp(**kwargs):
    h = qre.resource_rep(qre.Hadamard)
    s = qre.resource_rep(qre.S)
    return [qre.GateCount(h, 1), qre.GateCount(s, 2)]
>>> print(qre.estimate(qre.X(), gate_set={"Hadamard", "Z", "S"}))
--- Resources: ---
 Total wires: 1
    algorithmic wires: 1
    allocated wires: 0
     zero state: 0
     any state: 0
 Total gates : 4
  'S': 2,
  'Hadamard': 2
>>> config = qre.ResourceConfig()
>>> config.set_decomp(qre.X, custom_res_decomp)
>>> print(qre.estimate(qre.X(), gate_set={"Hadamard", "Z", "S"}, config=config))
--- Resources: ---
 Total wires: 1
    algorithmic wires: 1
    allocated wires: 0
     zero state: 0
     any state: 0
 Total gates : 3
  'S': 2,
  'Hadamard': 1
set_precision(op_type, precision, resource_key='precision')[source]

Sets the precision for a given resource operator.

This method updates the parameter value for operators that use tolerance parameters (e.g., for synthesis error). By default the parameter name is assumed to be precision. It will raise an error if users attempt to set the precision for an operator that is not configurable. A negative precision will also raise an error.

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

  • precision (float | int) – The desired precision tolerance. Must be greater than 0.

  • resource_key (str) – the name of the specific precision parameter to be updated

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

import pennylane.estimator as qre

config = qre.ResourceConfig()

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

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

Some resource operators have multiple parameters which tune the precision of the operator’s decomposition. For example, the TrotterVibronic operator has parameters phase_grad_precision and coeff_precision. A dictionary of all such parameters of an operator can be accessed through ResourceConfig.resource_op_precisions:

>>> import pennylane.estimator as qre
>>> my_config = qre.ResourceConfig()
>>> my_config.resource_op_precisions[qre.TrotterVibronic]
{'phase_grad_precision': 1e-06, 'coeff_precision': 0.001}

We can modify the default value of the coeff_precision:

>>> my_config.set_precision(qre.TrotterVibronic, 1e-9, resource_key="coeff_precision")
>>> my_config.resource_op_precisions[qre.TrotterVibronic]
{'phase_grad_precision': 1e-06, 'coeff_precision': 1e-09}
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: RX, RY, RZ, CRX, CRY, CRZ.

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

import pennylane.estimator as qre

config = qre.ResourceConfig()
rot_ops = [qre.RX, qre.RY, qre.RZ, qre.CRX, qre.CRY, qre.CRZ]
print([config.resource_op_precisions[op]['precision'] for op in rot_ops])

config.set_single_qubit_rot_precision(1e-5)
print([config.resource_op_precisions[op]['precision'] for op in rot_ops])
[1e-09, 1e-09, 1e-09, 1e-09, 1e-09, 1e-09]
[1e-05, 1e-05, 1e-05, 1e-05, 1e-05, 1e-05]