qp.estimate

estimate(qnode, level='top', *, target)[source]

Provides a resource estimate of a quantum circuit with respect to the provided compilation target, via rapid analysis of decomposition pathways.

Note

Only QNodes which have been decorated with @qp.qjit(capture=True) are supported.

The estimate function never executes the circuit. It only provides resource estimates.

Parameters:
  • qnode (QJIT) – the qjit’d QNode for which to estimate resources.

  • level (str | int) – The level to compile to before projecting the resources onto the target. Only "top", 0, or the name of a marker placed before all passes, are currently supported, indicating the original circuit before any compilation passes have been applied. Defaults to "top".

  • target (str | Iterable[type | str] | dict[type | str, float]) – The gate set to project the resources onto, in any form accepted by the gate_set argument of decompose().

Returns:

A function that has the same argument signature as qnode. This function returns a CircuitSpecs object containing the projected resources, including gate and measurement data, wire allocations, device information, shots, and more.

See also

analyze(), track()

Example

Consider the following circuit.

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

@qp.qjit(capture=True)
@qp.transforms.cancel_inverses
@qp.qnode(dev)
def circuit(x):
    qp.X(0)
    qp.X(0)
    qp.CZ([0, 1])
    qp.RX(x, wires=1)
    return qp.probs()

By calling estimate on this circuit with level="top", the cancel_inverses() pass is ignored. Subsequently, the operations in the original circuit will have their resources projected into the target gate set by analyzing decomposition pathways.

>>> print(qp.estimate(circuit, target={"PauliX", "Hadamard", "CNOT", "RX"})(1.23))
Device: null.qubit
Device wires: 2
Shots: Shots(total=None)
Level: top

Quantum operations:
- Total: 6
  - CNOT: 1
  - Hadamard: 2
  - PauliX: 2
  - RX: 1
Measurement processes:
- probs(all wires): 1
Total wires: 2
Circuit Depth: Not computed

Since cancel_inverses() is ignored, both PauliX gates are counted. The CZ gate is decomposed into Hadamard and CNOT gates.

To reach the target gate set, estimate uses the fixed_decomps and alt_decomps of the decompose() transforms in the compile pipeline.

For example, the following circuit fixes a decomposition rule for CZ that uses PauliZ and RY gates instead of Hadamard gates:

@qp.register_resources({qp.Z: 2, qp.RY: 2, qp.CNOT: 1})
def cz_with_ry(wires, **_):
    qp.Z(wires[1])
    qp.RY(np.pi / 2, wires[1])
    qp.CNOT(wires)
    qp.Z(wires[1])
    qp.RY(np.pi / 2, wires[1])

@qp.qjit(capture=True)
@qp.decompose(gate_set={"CNOT", "PauliZ", "RY"}, fixed_decomps={qp.CZ: cz_with_ry})
@qp.qnode(qp.device("null.qubit", wires=2))
def circuit():
    qp.CZ([0, 1])
    return qp.probs()

The CZ gate is decomposed with this rule:

>>> qp.estimate(circuit, target={"CNOT", "PauliZ", "RY"})().resources.quantum_operations
{'CNOT': 1, 'PauliZ': 2, 'RY': 2}

Since the circuit is not executed, the projected resources are subject to the same compile-time constraints as analyze(). In particular, operations inside a loop whose number of iterations depends on runtime values are counted symbolically.

For example, the number of CZ gates in the following circuit, and therefore the number of gates they decompose into, depends on the value of n:

@qp.qjit(capture=True)
@qp.qnode(qp.device("null.qubit", wires=2))
def circuit(n):
    qp.Hadamard(0)

    @qp.for_loop(n)
    def loop(i):
        qp.CZ([0, 1])

    loop()
    return qp.expval(qp.Z(0))
>>> resources = qp.estimate(circuit, target={"Hadamard", "CNOT"})(5).resources
>>> print(resources)
Symbolic Variables: a
Quantum operations:
- Total: 3*a + 1
  - CNOT: a
  - Hadamard: 2*a + 1
Measurement processes:
- expval(PauliZ): 1
Total wires: 2
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
{'CNOT': 5, 'Hadamard': 11}

Note

Loops inside the decomposition rules used to reach the target gate set are counted symbolically as well, so the result can be symbolic even if the circuit has no dynamic control flow.