cache_execute(fn, cache, pass_kwargs=False, return_tuple=True, expand_fn=None)[source]

Decorator that adds caching to a function that executes multiple tapes on a device.

This decorator makes use of QuantumTape.hash to identify unique tapes.

  • If a tape does not match a hash in the cache, then the tape has not been previously executed. It is executed, and the result added to the cache.

  • If a tape matches a hash in the cache, then the tape has been previously executed. The corresponding cached result is extracted, and the tape is not passed to the execution function.

  • Finally, there might be the case where one or more tapes in the current set of tapes to be executed are identical and thus share a hash. If this is the case, duplicates are removed, to avoid redundant evaluations.

  • fn (callable) – The execution function to add caching to. This function should have the signature fn(tapes, **kwargs), and it should return list[tensor_like], with the same length as the input tapes.

  • cache (None or dict or Cache or bool) – The cache to use. If None, caching will not occur.

  • pass_kwargs (bool) – If True, keyword arguments passed to the wrapped function will be passed directly to fn. If False, they will be ignored.

  • return_tuple (bool) – If True, the output of fn is returned as a tuple (fn_ouput, []), to match the output of execution functions that also return gradients.


a wrapped version of the execution function fn with caching support

Return type