Terminal Sampling#
- terminal_sampling(func=None, shots=0)[source]#
The
terminal_samplingdecorator runs a hybrid simulation and samples from the resulting quantum state.The idea behind this function is that it is very cheap for a classical simulator to sample from a given quantum state without simulating the whole state from scratch. For quantum simulators that simulate pure quantum computations (i.e. no classical steps) this is very established and usually achieved through a “shots” keyword. For hybrid simulators (like Jasp) it is not so straightforward because mid-circuit measurements can alter the classical computation.
In general, generating N samples from a hybrid program requires N executions of said program. If it is however known that the quantum state is the same regardless of mid-circuit measurement outcomes, we can use the terminal sampling function. If this condition is not met, the
terminal_samplingfunction will not return a valid distribution. A demonstration for this is given in the examples section.Note
terminal_samplingonly supports sampling kernels that return QuantumVariables. Kernels that return classical values (frommid-circuit measurements) are not supported — usesample()with@jaspify(terminal_sampling=False)(the default) for those.Additionally, terminal sampling currently cannot be combined with the
stimsimulator backend (i.e. calling the lower-levelsimulate_jasprwith bothsimulator="stim"andterminal_sampling=Trueraises an exception).To use the terminal sampling decorator, a Jasp-compatible sampling kernel returning some QuantumVariables has to be given as a parameter.
- Parameters:
- funccallable
A Jasp-compatible sampling kernel returning QuantumVariables.
- shotsint, optional
An integer specifying the amount of shots. The default is
0, which results in the exact probabilities being returned instead of shot-sampled counts.
- Returns:
- callable
A function that returns a dictionary of measurement results similar to
get_measurement.
Examples
We sample from a QuantumFloat that has been brought in a superposition.
from qrisp import QuantumFloat, QuantumBool, h, cx from qrisp.jasp import terminal_sampling @terminal_sampling(shots = 1000) def main(i): qf = QuantumFloat(8) qbl = QuantumBool() h(qf[i]) cx(qf[i], qbl[0]) return qf, qbl sampling_function = terminal_sampling(main, shots = 1000) print(main(0)) print(main(1)) print(main(2)) # Yields: {(1.0, True): 526, (0.0, False): 474} {(2.0, True): 503, (0.0, False): 497} {(4.0, True): 502, (0.0, False): 498}
Example of invalid use
In this example we demonstrate a hybrid program that can not be properly sampled via
terminal_sampling. The key ingredient here is a realtime component.from qrisp import QuantumBool, QuantumFloat, h, measure, control @terminal_sampling def main(): qbl = QuantumBool() qf = QuantumFloat(4) # Bring qbl into superposition h(qbl) # Perform a measure cl_bl = measure(qbl) # Perform a conditional operation based on the measurement outcome with control(cl_bl): qf[:] = 1 h(qf[2]) return qf print(main()) # Yields either {0.0: 1.0} or {1.0: 0.5, 5.0: 0.5} (with a 50/50 probability)
The problem here is the fact that the distribution of the returned QuantumFloat is depending on the measurement outcome of the QuantumBool. The
terminal_samplingfunction performs this simulation (including the measurement) only once and simply samples from the final distribution.