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
estimatefunction never executes the circuit. It only provides resource estimates.- Parameters:
qnode (
QJIT) – the qjit’d QNode for which to estimate resources.level (str | int | slice) – The portion of the
CompilePipelineto apply before projecting the resources onto thetarget. Seeget_compile_pipeline()for accepted values. Currently, only"top",0, or the label of a marker placed before all transforms are supported. 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_setargument ofdecompose().
- Returns:
A function that has the same argument signature as
qnode. This function returns aCircuitSpecsobject containing the projected resources, including gate and measurement data, wire allocations, device information, shots, and more.
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
estimateon this circuit withlevel="top", thecancel_inverses()pass is ignored. Subsequently, the operations in the original circuit will have their resources projected into thetargetgate 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, bothPauliXgates are counted. TheCZgate is decomposed intoHadamardandCNOTgates.Custom decomposition rules
To reach the
targetgate set,estimateuses thefixed_decompsandexclude_decompsof thedecompose()transforms in the compile pipeline.For example, the following circuit fixes a decomposition rule for
CZthat usesPauliZandRYgates instead ofHadamardgates:@qp.register_resources({qp.Z: 2, qp.RY: 2, qp.CNOT: 1}, name="cz_with_ry") 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.add_decomps(qp.CZ, cz_with_ry) @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
CZgate is decomposed with this rule:>>> qp.estimate(circuit, target={"CNOT", "PauliZ", "RY"})().resources.quantum_operations {'CNOT': 1, 'PauliZ': 2, 'RY': 2}
Symbolic resource counts
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
CZgates in the following circuit, and therefore the number of gates they decompose into, depends on the value ofn:@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
.subsmethod:>>> resources.subs(a=5).quantum_operations {'CNOT': 5, 'Hadamard': 11}
Note
Loops inside the decomposition rules used to reach the
targetgate set are counted symbolically as well, so the result can be symbolic even if the circuit has no dynamic control flow.