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 CompilePipeline selected by level, 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.partial wrappers around supported callables are also accepted.

  • level (str | int | slice | Sequence[str | int]) – The portion of the CompilePipeline to apply before analyzing resources. See get_compile_pipeline() for accepted values. If level is a Sequence, "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 a CircuitSpecs object containing the qnode specifications, including gate and measurement data, wire allocations, device information, shots, and more.

See also

estimate(), track()

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 slice only applies that portion of the compile pipeline. For example, the resources after applying merge_rotations, but not cancel_inverses, are given by:

>>> qp.analyze(circuit, level=slice(1, 2))(1.23).resources.quantum_operations
{'CNOT': 1, 'PauliX': 2, 'RX': 1}

Since the circuit is not executed, some resources cannot be counted exactly:

  • Resources inside a for loop whose number of iterations depends on runtime values are counted symbolically, using Expression instances instead of integers.

  • Resources inside a while loop are counted as if only one iteration occurred.

  • For conditional branches from if or switch statements, each gate is counted with its maximum count across all branches, providing an upper bound.

For example, the number of PauliX gates in the following circuit depends on the value of n:

@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 .subs method:

>>> 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.