microsoft / qsharp-runtime

Runtime components for Q#
https://docs.microsoft.com/quantum
MIT License
285 stars 93 forks source link

Random - Should this be zero-indexed? #256

Closed christopherkang closed 4 years ago

christopherkang commented 4 years ago

Describe the bug The 'Random' function is not zero indexed.

To Reproduce Use Message($"{Random[1.0]}");

Expected behavior If our arrays are zero-indexed, the returned index should (intuitively) be zero-indexed.

Screenshots

System information

Additional context

christopherkang commented 4 years ago

Hold up - this is more complicated than I expected. Run this method using the resource simulator:

Message($"{Random([1.0, 2.0])}");

I'm getting 0, 2 as my values?!

christopherkang commented 4 years ago

With the following code:

Message($"{Random([1.0, 2.0, 2.0])}");

I'm getting 0, 1, 3 - I'm guessing that the final element is erroneously increased by 1? Additionally, this bug only happens on the ResourceEstimator - it's fine using the QuantumSimulator.

cgranade commented 4 years ago

Hm, this is very odd indeed. Thanks for reporting, we'll go on and investigate.

cgranade commented 4 years ago

I think I may have found the issue; it looks like the resources estimator uses a different implementation of Random than other simulators. That said, investigating it looks like this it is a qsharp-runtime issue so I'll transfer there. Thanks for the report!

cgranade commented 4 years ago

This should now be fixed, thanks again for the report @christopherkang!