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[, level])

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.

Resource Functions

add_in_series(r1, r2)

Add two Resources objects assuming the circuits are executed in series.

add_in_parallel(r1, r2)

Add two Resources objects assuming the circuits are executed in parallel.

mul_in_series(resources, scalar)

Multiply the Resources object by a scalar as if the circuit was repeated that many times in series.

mul_in_parallel(resources, scalar)

Multiply the Resources object by a scalar as if the circuit was repeated that many times in parallel.

substitute(initial_resources, gate_info, ...)

Replaces a specified gate in a Resources object with the contents of another Resources object.

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])
num_wires: 3
num_gates: 7
depth: 5
shots: Shots(None)
gate_types:
{"RZ": 1, "CNOT": 2, "Hadamard": 2, "PauliZ": 2}
gate_sizes:
{1: 5, 2: 2}