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 up to 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 | Iterable[int | str]) –

    The level of compilation at which to estimate resources. Defaults to "user".

    level accepts the following types:

    • int: The number of transforms to apply before analyzing resources.

    • str: One of the presets listed below, or the label of a marker().

    • iterable: A list, tuple, or similar containing int or str values. These should be sorted in ascending transform order with no duplicates. Resources will be analyzed for each provided level in the iterable.

    The following str presets are provided for ease of use:

    • "top": Equivalent to 0; to analyze resources before any transforms have been applied.

    • "user": To analyze resources after the user-specified transforms have been applied.

    • "device": To analyze resources after the user-specified transforms and device preprocessing transforms have been applied.

    • "all-user": To analyze resources at each stage of compilation with respect to user-specified transforms.

    • "all": To analyze resources at each stage of compilation with respect to user-specified and device preprocessing transforms.

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: 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: cancel-inverses
- 2: merge-rotations
- 3: 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}

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.