qml.resource.algo_error

algo_error(qnode, level='gradient')[source]

Computes the algorithmic errors in a quantum circuit.

This transform converts a QNode into a callable that returns algorithmic error information after applying the specified amount of transforms/expansions.

Parameters:
  • qnode (.QNode) – the QNode to calculate the algorithmic errors for.

  • level (str | int | slice | iter[int]) – An indication of which transforms to apply before computing the errors. See get_transform_program() for more information about allowable levels.

Returns:

A function that has the same argument signature as qnode. When called, this function returns either:

  • A single dictionary with error type names as keys (e.g., "SpectralNormError") and AlgorithmicError objects as values, when there is only one tape in the batch.

  • A list of such dictionaries, one for each tape in the batch, when there are multiple tapes.

Example

Consider a circuit with operations that introduce algorithmic errors, such as TrotterProduct:

import pennylane as qml

dev = qml.device("null.qubit", wires=2)
Hamiltonian = qml.dot([1.0, 0.5], [qml.X(0), qml.Y(0)])

@qml.qnode(dev)
def circuit(time):
    qml.TrotterProduct(Hamiltonian, time=time, n=4, order=2)
    qml.TrotterProduct(Hamiltonian, time=time, n=4, order=4)
    return qml.state()

We can compute the errors using algo_error:

>>> errors = qml.resource.algo_error(circuit)(time=1.0)
>>> print(errors)
{'SpectralNormError': SpectralNormError(...)}

The error values can be accessed from the returned dictionary:

>>> errors["SpectralNormError"].error
np.float64(0.4299...)

Note

This function is the standard way to retrieve algorithm-specific error metrics from quantum circuits that use ErrorOperation subclasses. Operations like TrotterProduct and QuantumPhaseEstimation implement the error() method and will contribute to the returned error dictionary.