qp.track

track(qnode, level='device')[source]

Executes a quantum circuit and tracks the resources it uses.

This transform converts a QNode into a callable that executes the circuit on null.qubit and returns both the result of that execution and the resource information gathered while running it.

Parameters:

qnode (QJIT) – the (qjit’d) QNode to execute and track. functools.partial wrappers around supported callables are also accepted.

Keyword Arguments:

level (str) – The level at which to track resources. Only "device" is currently supported, meaning that resources are counted after all user-specified transforms and device preprocessing transforms have been applied.

Returns:

A function that has the same argument signature as qnode. This function returns a tuple containing the result of executing the circuit and a CircuitSpecs object containing the qnode specifications, including gate and measurement data, total wires, device information, shots, and more.

See also

estimate(), analyze()

Note

Resources are tracked by mock-executing the workflow on null.qubit. For a QNode bound to any other device, the returned execution result therefore carries the shape and dtype of that device’s result, but not its values.

Warning

null.qubit does not perform a true state-vector simulation, so mid-circuit measurement outcomes are not grounded in real measurement statistics. If the circuit contains a conditional whose branch depends on such an outcome, the reported circuit depth and any branch-dependent gate counts correspond to the branch taken during the mock execution, and should be treated as an estimate rather than exact counts.

Example

n_wires = 2
dev = qp.device("lightning.qubit", wires=n_wires)

@qp.qjit
@qp.qnode(dev)
def circuit(theta):
    qp.RX(theta, wires=0)
    qp.CNOT(wires=(0,1))
    return qp.probs(wires=(0,1))
>>> result, circuit_specs = qp.track(circuit)(1.23)
>>> circuit_specs.resources.quantum_operations
{'CNOT': 1, 'RX': 1}

Since the circuit is executed on null.qubit, the result has the shape and dtype of the lightning.qubit result, but not its values:

>>> result
Array([1., 0., 0., 0.], dtype=float64)
>>> circuit(1.23)
Array([0.66711886, 0.        , 0.        , 0.33288114], dtype=float64)

The specifications are the same as the ones returned by specs() at the device level:

>>> circuit_specs == qp.specs(circuit, level="device")(1.23)
True

Because the circuit is executed, control flow is unrolled with the given arguments and the resources are concrete numbers:

@qp.qjit(autograph=True)
@qp.qnode(dev)
def circuit(n):
    for i in range(n):
        qp.Hadamard(wires=i % n_wires)
    qp.CNOT(wires=(0, 1))
    return qp.probs(wires=(0, 1))
>>> _, circuit_specs = qp.track(circuit)(3)
>>> circuit_specs.resources.quantum_operations
{'CNOT': 1, 'Hadamard': 3}

In contrast, analyze() does not execute the circuit, so it reports the number of loop iterations symbolically:

>>> qp.analyze(circuit, level=0)(3).resources.quantum_operations
{'CNOT': 1, 'Hadamard': Expression({('a',): 1})}