microsoft / Quantum

Microsoft Quantum Development Kit Samples
https://docs.microsoft.com/quantum
MIT License
3.87k stars 920 forks source link

Unable to use shift operator #75

Closed nthalpy closed 6 years ago

nthalpy commented 6 years ago

According to https://docs.microsoft.com/en-us/quantum/quantum-qr-expressions?view=qsharp-preview, Q# provides arithmetic shift operator <<< and >>>.

And since Q# uses Int64 type to express integer, following code

for (x in 0 .. (2 <<< bitCount))
{
    // do some stuffs...
}

translates to

foreach (var x in new Range(0L, (2L << bitCount)))
{
    // do some translated stuffs...
}

and since both left hand and right hand is Int64, C# compiler throws following error:

error CS0019: Operator `<<' cannot be applied to operands of type `long' and `long'

I think Q# compiler should translate arithmetic shift to something else, such as function does arithmetic shift with 2 Int64 values.

RolfHuisman commented 6 years ago

Was reading this item and my first reaction was; "but if they reduce it to a int, would that semantically be incorrect. Since its only 32 bits where one expects 64 bits in the specification of the language." But; int.MaxValue = 2,147,483,647 bits = 256 MB datatype... So if we really want to shift based on a Int64, our datatypes need to be at least 256 MB to have any value where the current Int64 is just 64 bits. Think to generate a cast to int would be good enough here.

foreach (var x in new Range(0L, (2L << (int) bitCount))) { // do some translated stuffs... }

alan-geller commented 6 years ago

This is indeed a code generation bug; I've entered it into our internal tracking system. It will be fixed in a coming release. Sorry about that!!

cpalmer2020 commented 6 years ago

This is now fixed with the most recent update (0.2.1809.701).