qp.analyze¶
- analyze(qnode, level='user')[source]¶
Provides a compile-time resource estimate of a quantum circuit, obtained by analyzing its intermediate representation at the specified compilation level.
This transform converts a QNode into a callable that compiles the circuit with the portion(s) of its
CompilePipelineselected bylevel, and inspects the resulting representation, without executing the circuit or unrolling its control flow. The resource information is therefore subject to compile-time constraints.- Parameters:
qnode (
QJIT) – the (qjit’d) QNode for which to estimate resources.functools.partialwrappers around supported callables are also accepted.level (str | int | slice | Sequence[str | int]) – The portion of the
CompilePipelineto apply before analyzing resources. Seeget_compile_pipeline()for accepted values. Iflevelis aSequence,"all-user", or"all", resources are analyzed independently for each provided case. Defaults to"user".
- Returns:
A function that has the same argument signature as
qnode. This function returns aCircuitSpecsobject containing theqnodespecifications, including gate and measurement data, wire allocations, device information, shots, and more.
See also
Example
dev = qp.device("null.qubit", wires=2) @qp.qjit(capture=True) @qp.transforms.merge_rotations @qp.transforms.cancel_inverses @qp.qnode(dev) def circuit(x): qp.RX(x, wires=0) qp.RX(x, wires=0) qp.X(0) qp.X(0) qp.CNOT([0, 1]) return qp.probs()
>>> print(qp.analyze(circuit, level="user")(1.23)) Device: null.qubit Device wires: 2 Shots: Shots(total=None) Level: After merge-rotations Quantum operations: - Total: 2 - CNOT: 1 - RX: 1 Measurement processes: - probs(all wires): 1 Total wires: 2 Circuit Depth: Not computed
Using
level="all"shows how the resources change after each transform:>>> all_specs = qp.analyze(circuit, level="all")(1.23) >>> print(all_specs) Device: null.qubit Device wires: 2 Shots: Shots(total=None) Levels: - 0: Before MLIR Passes - 1: After cancel-inverses - 2: After merge-rotations - 3: After Device Preprocessing ↓Metric Level→ | 0 | 1 | 2 | 3 ------------------------------------------ Quantum operations: | - Total | 5 | 3 | 2 | 2 - CNOT | 1 | 1 | 1 | 1 - PauliX | 2 | 0 | 0 | 0 - RX | 2 | 2 | 1 | 1 Measurement processes: | - probs(all wires) | 1 | 1 | 1 | 1 Total wires | 2 | 2 | 2 | 2
The resources at a given level can be accessed with the name of the corresponding transform or marker:
>>> all_specs.resources["cancel-inverses"].quantum_operations {'CNOT': 1, 'RX': 2}
A
sliceonly applies that portion of the compile pipeline. For example, the resources after applyingmerge_rotations, but notcancel_inverses, are given by:>>> qp.analyze(circuit, level=slice(1, 2))(1.23).resources.quantum_operations {'CNOT': 1, 'PauliX': 2, 'RX': 1}
Compile-time constraints of resource analysis
Since the circuit is not executed, some resources cannot be counted exactly:
Resources inside a
forloop whose number of iterations depends on runtime values are counted symbolically, usingExpressioninstances instead of integers.Resources inside a
whileloop are counted as if only one iteration occurred.For conditional branches from
iforswitchstatements, each gate is counted with its maximum count across all branches, providing an upper bound.
For example, the number of
PauliXgates in the following circuit depends on the value ofn:@qp.qjit(autograph=True) @qp.qnode(qp.device("null.qubit", wires=1)) def circuit(n): qp.Hadamard(0) for _ in range(n): qp.PauliX(0) return qp.expval(qp.Z(0))
>>> resources = qp.analyze(circuit, level=0)(5).resources >>> print(resources) Symbolic Variables: a Quantum operations: - Total: a + 1 - Hadamard: 1 - PauliX: a Measurement processes: - expval(PauliZ): 1 Total wires: 1 Circuit Depth: Not computed
Concrete values can be estimated by substituting each symbolic variable with an integer, using the
.subsmethod:>>> resources.subs(a=5).quantum_operations {'Hadamard': 1, 'PauliX': 5}
To get concrete counts for given arguments instead, use
track(), which executes the circuit and therefore unrolls its control flow.