CQCL / guppylang

Pythonic quantum-classical programming language
https://pypi.org/project/guppylang
Apache License 2.0
25 stars 2 forks source link

Use tket2 angle type for quantum gate parameters #240

Closed ss2165 closed 3 weeks ago

cqc-alec commented 1 month ago

I think there are three motivations for having an angle type distinct from float:

  1. the desire to express commonly used angles exactly;
  2. the desire to compare angles (and hence operations including them) for equality;
  3. being able to express mod-2pi equivalence as value equality.

Actually floating point is good enough to meet (1) for dyadic rationals (i.e. angles of the form (a/2^n) pi where a and n are integers). I think that the dyadic rationals are the most commonly used angles in quantum circuits, as they arise naturally in the Clifford hierarchy; however, occasionally other rationals are wanted: an example (the only one I can find) is the FSim gate, which uses an angle of 1/6. So there is an argument for being to able to express arbitrary rational multiples of pi exactly (up to some maximum size of denominator).

However, one advantage of working with dyadics rather than general rationals is that converting from float is easy: we can specify a maximum denominator and take the nearest approximation, or simply convert exactly (since the exact value of every finite float is a dyadic rational). With general rationals that is not so easy: finding the nearest rational with denominator <= b is non-trivial, so we'd actually have to specify the exact denominator, which may not be known. In the other direction, dyadic rationals would be represented correctly for denominators up to about 2^52, which is good enough for practical purposes.

I think the simplicity of the angle--float round trip is a good enough reason to restrict to dyadics, given that they account for most of the exact angles used in quantum circuits.

We can then define operations of

all of which reduce their result modulo 1. Dividing by int is the most problematic case. It is convenient to be able to write e.g. alpha/2 in guppy code when alpha is an angle; but when dividing by an int that is not a power of 2 the result may not be a dyadic rational and we may be forced to approximate with a large denominator. I think this case would be rare, however.