qml.resource

The resource module provides classes and functionality to estimate the quantum resources (number of qubits, circuit depth, etc.) required to implement advanced quantum algorithms.

Expectation Value Functions

estimate_error(coeffs[, variances, shots])

Estimate the error in computing an expectation value with a given number of measurements.

estimate_shots(coeffs[, variances, error])

Estimate the number of measurements required to compute an expectation value with a target error.

Circuit specifications

specs(qnode[, max_expansion, expansion_strategy])

Resource information about a quantum circuit.

Quantum Phase Estimation Resources

FirstQuantization(n, eta[, omega, error, …])

Estimate the number of non-Clifford gates and logical qubits for a quantum phase estimation algorithm in first quantization using a plane-wave basis.

DoubleFactorization(one_electron, two_electron)

Estimate the number of non-Clifford gates and logical qubits for a quantum phase estimation algorithm in second quantization with a double-factorized Hamiltonian.

Error Tracking

AlgorithmicError(error)

Abstract base class representing an abstract type of error.

SpectralNormError(error)

Class representing the spectral norm error.

ErrorOperation(*params[, wires, id])

Base class that represents quantum operations which carry some form of algorithmic error.

Resource Classes

Resources([num_wires, num_gates, …])

Contains attributes which store key resources such as number of gates, number of wires, shots, depth and gate types.

ResourcesOperation(*params[, wires, id])

Base class that represents quantum gates or channels applied to quantum states and stores the resource requirements of the quantum gate.

Tracking Resources for Custom Operations

We can use the null.qubit device with the qml.Tracker to track the resources used in a quantum circuit with custom operations without execution.

class MyCustomAlgorithm(ResourcesOperation):
    num_wires = 2

    def resources(self):
        return Resources(
            num_wires=self.num_wires,
            num_gates=5,
            gate_types={"Hadamard": 2, "CNOT": 1, "PauliZ": 2},
            gate_sizes={1: 4, 2: 1},
            depth=3,
        )

dev = qml.device("null.qubit", wires=[0, 1, 2], shots=100)

@qml.qnode(dev)
def circuit(theta):
    qml.RZ(theta, wires=0)
    qml.CNOT(wires=[0,1])
    MyCustomAlgorithm(wires=[1, 2])
    return qml.expval(qml.Z(1))

x = np.array(1.23, requires_grad=True)

with qml.Tracker(dev) as tracker:
    circuit(x)

We can examine the resources by accessing the resources key:

>>> resources_lst = tracker.history['resources']
>>> print(resources_lst[0])
wires: 3
gates: 7
depth: 5
shots: Shots(None)
gate_types:
{"RZ": 1, "CNOT": 2, "Hadamard": 2, "PauliZ": 2}
gate_sizes:
{1: 5, 2: 2}