Resource Estimation#
- count_ops(meas_behavior)[source]#
Decorator to determine resources of large scale quantum computations. This decorator compiles the given Jasp-compatible function into a classical function computing the amount of each gates required. The decorated function will return a dictionary containing the operation counts.
For many algorithms including classical feedback, the result of the measurements can heavily influence the required resources. To reflect this, users can specify the behavior of measurements during the computation of resources. The following strategies are available:
"0"
- computes the resource as if measurements always return 0"1"
- computes the resource as if measurements always return 1callable - allows the user to specify a random number generator (see examples)
For more details on how the callable option can be used, consult the examples section.
Finally it is also possible to call the Qrisp simulator to determine measurement behavior by providing
sim
. This is of course much less scalable but in particular for algorithms involving repeat-until-success components, a necessary evil.Note that the
sim
option might return non-deterministic results, while the other methods do.Warning
It is currently not possible to estimate programs, which include a kernelized function.
- Parameters:
- meas_behaviorstr or callable
A string or callable indicating the behavior of the ressource computation when measurements are performed. Available are
- Returns:
- resource_estimation decorator
A decorator, producing a function to computed the required resources.
Examples
We compute the resources required to perform a large scale integer multiplication.
from qrisp import count_ops, QuantumFloat, measure @count_ops(meas_behavior = "0") def main(i): a = QuantumFloat(i) b = QuantumFloat(i) c = a*b return measure(c) print(main(5)) # {'cx': 506, 'x': 22, 'h': 135, 'measure': 55, '2cx': 2, 's': 45, 't': 90, 't_dg': 90} print(main(5000)) # {'cx': 462552491, 'x': 20002, 'h': 112522500, 'measure': 37517500, '2cx': 2, 's': 37507500, 't': 75015000, 't_dg': 75015000}
Note that even though the second computation contains more than 800 million gates, determining the resources takes less than 200ms, highlighting the scalability features of the Jasp infrastructure.
Modifying the measurement behavior via a random number generator
To specify the behavior, we specify an RNG function (for more details on what that means please check the Jax documentation. This RNG takes as input a “key” and returns a boolean value. In this case, the return value will be uniformly distributed among True and False.
from jax import random import jax.numpy as jnp from qrisp import QuantumFloat, measure, control, count_ops, x # Returns a uniformly distributed boolean def meas_behavior(key): return jnp.bool(random.randint(key, (1,), 0,1)[0]) @count_ops(meas_behavior = meas_behavior) def main(i): qv = QuantumFloat(2) meas_res = measure(qv) with control(meas_res == i): x(qv) return measure(qv)
This script executes two measurements and based on the measurement outcome executes two X gates. We can now execute this resource computation with different values of
i
to see, which measurements returnTrue
with our given random-number generator (recall that this way of specifying the measurement behavior is fully deterministic).print(main(0)) # Yields: {'measure': 4, 'x': 2} print(main(1)) # Yields: {'measure': 4} print(main(2)) # Yields: {'measure': 4} print(main(3)) # Yields: {'measure': 4}
From this we conclude that our RNG returned 0 for both of the initial measurements.
For some algorithms (such as Repeat-Until-Success) sampling the measurement result from a simple distribution won’t cut it because the required ressource can be heavily influenced by measurement outcomes. For this matter it is also possible to perform a full simulation. Note that this simulation is no longer deterministic.
@count_ops(meas_behavior = "sim") def main(i): qv = QuantumFloat(2) meas_res = measure(qv) with control(meas_res == i): x(qv) return measure(qv) print(main(0)) {'measure': 4, 'x': 2} print(main(1)) {'measure': 4}