quil-lang / quil

Specification of Quil: A Practical Quantum Instruction Set Architecture
https://quil-lang.github.io/
Apache License 2.0
104 stars 16 forks source link

Consideration for expanding logicalBinaryOp definition #54

Open nilslice opened 2 years ago

nilslice commented 2 years ago

While working on a parser implementation in quil-rust, it struck me that the INT alternative of the right-hand operand in a logical binary operation could possibly be further specified & wanted to get some thoughts. For reference:

logicalBinaryOp     : ( AND | OR | IOR | XOR ) addr ( addr | INT ) ;
// ...
INT                 : [0-9]+ ;
  1. Is it reasonable to introduce a BIT definition which would reduce the range of values to 1 or 0? @kalzoo mentioned we can (maybe already do?) coerce an INT when needed so maybe this isn't necessary.
  2. Is there a case for parsing an expression in this position?
stylewarning commented 2 years ago

The original reason for not allowing INTEGER values there is because different implementations of Quil might represent integers in different ways. For example, different schemes for signed fixed-precision numbers. I didn't want to require any specific width or bit-layout to integers.

There is a way in Quil to allow for this implementation-dependent behavior though; it's to use octet aliasing. Assuming INTEGER values are 16-bit unsigned integers, one could do:

DECLARE xs OCTET[2]
DECLARE ys OCTET[2]
DECLARE x INTEGER SHARING xs
DECLARE y INTEGER SHARING ys

AND xs[0] xs[0] ys[0]
AND xs[1] xs[1] ys[1]

This would be the C equivalent of

uint16_t x, y;
x &= y;