quil-lang / qvm

The high-performance and featureful Quil simulator.
Other
411 stars 57 forks source link

Handle finicky corner case of deterministic measurements #278

Closed braised-babbage closed 2 years ago

braised-babbage commented 3 years ago

This addresses #277

The approach used for generating Bernoulli random variables is "inverse transform sampling", which would work fine if (random 1d0) was "truly" uniform. Since we have only 53 bits of precision, even a perfect implementation of (random 1d0) would give a (1/2)^53 chance of producing a 0. On that fateful day, when the planets align and the birds by sheer coincidence begin singing in unison, some poor sob will run a deterministic circuit and get back just the opposite of what was expected. But nobody will have told our unsuspecting QC enthusiast that it was, in fact, Opposite Day.

This is an easy fix at least. With the current method of <= comparison we just handle the zero probability case explicitly. We end up with two conditions met:

  1. preserve the general statistical behavior of measure
  2. enforce deterministic outcomes for events of probability 0 or 1

Note that we're really just hiding the quantization issues elsewhere (which are now felt in the difference between excited-probability being 0 vs the next largest double float), but this provides (2) at no real expense to (1).