Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/QirRuntime/lib/QIR/callables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ int QirTupleHeader::Release()
}

assert(this->refCount > 0); // doesn't guarantee we catch double releases but better than nothing
--this->refCount;
int retVal = --this->refCount;
if (this->refCount == 0)
{
char* buffer = reinterpret_cast<char*>(this);
delete[] buffer;
}
return this->refCount;
return retVal;
}

QirTupleHeader* QirTupleHeader::Create(int size)
Expand Down
46 changes: 38 additions & 8 deletions src/QirRuntime/lib/QSharpFoundation/intrinsicsMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
namespace // Visible in this translation unit only.
{
extern thread_local bool randomizeSeed;
extern int64_t lastGeneratedRndNum;
extern int64_t lastGeneratedRndI64;
extern double lastGeneratedRndDouble;
}

// Implementation:
Expand Down Expand Up @@ -74,7 +75,7 @@ int64_t quantum__qis__drawrandomint__body(int64_t minimum, int64_t maximum)
{
if(minimum > maximum)
{
quantum__rt__fail(quantum__rt__string_create(Quantum::Qis::Internal::excStrDrawRandomInt));
quantum__rt__fail(quantum__rt__string_create(Quantum::Qis::Internal::excStrDrawRandomVal));
}

// https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
Expand All @@ -83,16 +84,39 @@ int64_t quantum__qis__drawrandomint__body(int64_t minimum, int64_t maximum)
? std::random_device()() : // Default
0); // For test purposes only.

lastGeneratedRndNum = std::uniform_int_distribution<int64_t>(minimum, maximum)(gen);
return lastGeneratedRndNum;
lastGeneratedRndI64 = std::uniform_int_distribution<int64_t>(minimum, maximum)(gen);
return lastGeneratedRndI64;
}

double quantum__qis__drawrandomdouble__body(double minimum, double maximum)
{
if(minimum > maximum)
{
quantum__rt__fail(quantum__rt__string_create(Quantum::Qis::Internal::excStrDrawRandomVal));
}

// For testing purposes we need separate generators for Int and Double:
// https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
// https://en.cppreference.com/w/cpp/numeric/random
thread_local static std::mt19937_64 gen(randomizeSeed
? std::random_device()() : // Default
0); // For test purposes only.

// https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution
lastGeneratedRndDouble = std::uniform_real_distribution<double>(
minimum,
std::nextafter(maximum, std::numeric_limits<double>::max())) // "Notes" section.
(gen);
return lastGeneratedRndDouble;
}

} // extern "C"

namespace // Visible in this translation unit only.
{
thread_local bool randomizeSeed = true;
int64_t lastGeneratedRndNum = 0;
int64_t lastGeneratedRndI64 = 0;
double lastGeneratedRndDouble = 0.0;
}

// For test purposes only:
Expand All @@ -102,17 +126,23 @@ namespace Qis
{
namespace Internal
{
char const excStrDrawRandomInt[] = "Invalid Argument: minimum > maximum for DrawRandomInt()";
char const excStrDrawRandomVal[] = "Invalid Argument: minimum > maximum";

void RandomizeSeed(bool randomize)
{
randomizeSeed = randomize;
}

int64_t GetLastGeneratedRandomNumber()
int64_t GetLastGeneratedRandomI64()
{
return lastGeneratedRndNum;
return lastGeneratedRndI64;
}

double GetLastGeneratedRandomDouble()
{
return lastGeneratedRndDouble;
}

} // namespace Internal
} // namespace Qis
} // namespace Quantum
12 changes: 11 additions & 1 deletion src/QirRuntime/lib/QSharpFoundation/qsharp-foundation-qis.ll
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
%struct.QirString = type opaque
%PauliId = type i32

declare dllimport void @quantum__rt__message(%"struct.QirString"* %str)
declare void @quantum__rt__message(%"struct.QirString"* %str)

;===============================================================================
;
Expand Down Expand Up @@ -64,6 +64,7 @@ declare double @quantum__qis__arccos__body(double %theta)
declare double @quantum__qis__arctan__body(double %theta)
declare double @quantum__qis__ieeeremainder__body(double %y, double %x)
declare i64 @quantum__qis__drawrandomint__body(i64 %min, i64 %max)
declare double @quantum__qis__drawrandomdouble__body(double %min, double %max)

; API for the user code:
define dllexport double @__quantum__qis__nan__body() { ; Q#: function NAN() : Double http://www.cplusplus.com/reference/cmath/nan-function/
Expand Down Expand Up @@ -191,6 +192,15 @@ define dllexport i64 @__quantum__qis__drawrandomint__body(i64 %min, i64 %max) {
ret i64 %result
}

; operation DrawRandomDouble (min : Double, max : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.random.drawrandomdouble
define double @__quantum__qis__drawrandomdouble__body(double %min, double %max) {
%result = call double @quantum__qis__drawrandomdouble__body(double %min, double %max)
ret double %result
}



;===============================================================================
; quantum.qis conditional functions
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern "C"

QIR_SHARED_API double quantum__qis__ieeeremainder__body(double x, double y); // NOLINT
QIR_SHARED_API int64_t quantum__qis__drawrandomint__body(int64_t minimum, int64_t maximum); // NOLINT
QIR_SHARED_API double quantum__qis__drawrandomdouble__body(double minimum, double maximum); // NOLINT

// Q# ApplyIf:
QIR_SHARED_API void quantum__qis__applyifelseintrinsic__body(RESULT*, QirCallable*, QirCallable*); // NOLINT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ namespace Qis
{
namespace Internal
{
extern char const excStrDrawRandomInt[];
extern char const excStrDrawRandomVal[];

void RandomizeSeed(bool randomize);
int64_t GetLastGeneratedRandomNumber();
int64_t GetLastGeneratedRandomI64();
double GetLastGeneratedRandomDouble();
} // namespace Internal
} // namespace Qis
} // namespace Quantum
2 changes: 1 addition & 1 deletion src/QirRuntime/public/QirTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct QIR_SHARED_API QirString
using PTuple = char*;
struct QIR_SHARED_API QirTupleHeader
{
int32_t refCount = 0;
int refCount = 0;
int32_t aliasCount = 0; // used to enable copy elision, see the QIR specifications for details
int32_t tupleSize = 0; // when creating the tuple, must be set to the size of the tuple's data buffer (in bytes)

Expand Down
33 changes: 31 additions & 2 deletions src/QirRuntime/test/QIR-static/qir-test-math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__CoshTest__body();
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__TanhTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__IeeeRemainderTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomInt__body(int64_t min, int64_t max); // NOLINT
extern "C" double Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomDouble__body(double min, double max); // NOLINT

TEST_CASE("QIR: Math.Sqrt", "[qir.math][qir.Math.Sqrt]")
{
Expand Down Expand Up @@ -97,7 +98,7 @@ TEST_CASE("QIR: Math.DrawRandomInt", "[qir.math][qir.Math.DrawRandomInt]")
const uint64_t qsRndNum =
Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomInt__body(std::numeric_limits<int64_t>::min(),
std::numeric_limits<int64_t>::max());
const uint64_t cppRndNum = Quantum::Qis::Internal::GetLastGeneratedRandomNumber(); // This call must be done
const uint64_t cppRndNum = Quantum::Qis::Internal::GetLastGeneratedRandomI64(); // This call must be done
// _after_ the Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomInt__body().
REQUIRE(qsRndNum == cppRndNum);
}
Expand All @@ -112,7 +113,7 @@ TEST_CASE("QIR: Math.DrawRandomInt", "[qir.math][qir.Math.DrawRandomInt]")
}
catch (std::runtime_error const& exc)
{
REQUIRE(0 == strcmp(exc.what(), Quantum::Qis::Internal::excStrDrawRandomInt));
REQUIRE(0 == strcmp(exc.what(), Quantum::Qis::Internal::excStrDrawRandomVal));
}

// There is a strong difference in the opinions about how the random number generator must be tested.
Expand Down Expand Up @@ -201,3 +202,31 @@ TEST_CASE("QIR: Math.DrawRandomInt", "[qir.math][qir.Math.DrawRandomInt]")
// }

} // TEST_CASE("QIR: Math.DrawRandomInt", "[qir.math][qir.Math.DrawRandomInt]")

TEST_CASE("QIR: Math.DrawRandomDouble", "[qir.math][qir.Math.DrawRandomDouble]")
{
// Test that the Q# random number generator is a wrapper around the C++ generator:
size_t times = 1000;
while(--times)
{
const double qsRndNum =
Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomDouble__body(std::numeric_limits<double>::min(),
std::numeric_limits<double>::max());
const double cppRndNum = Quantum::Qis::Internal::GetLastGeneratedRandomDouble(); // This call must be done
// _after_ the Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomDouble__body().
REQUIRE(qsRndNum == cppRndNum);
}

// Make sure the correct exception is thrown if min > max:
REQUIRE_THROWS_AS(Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomDouble__body(10.0, 5.0), std::runtime_error);

// Check the exception string:
try
{
(void)Microsoft__Quantum__Testing__QIR__Math__TestDrawRandomDouble__body(10.0, 5.0);
}
catch (std::runtime_error const& exc)
{
REQUIRE(0 == strcmp(exc.what(), Quantum::Qis::Internal::excStrDrawRandomVal));
}
} // TEST_CASE("QIR: Math.DrawRandomDouble", "[qir.math][qir.Math.DrawRandomDouble]")
1 change: 1 addition & 0 deletions src/QirRuntime/test/QIR-static/qsharp/qir-test-arrays.qs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace Microsoft.Quantum.Testing.QIR {
let res19 = ParityTest();
let res20 = PauliArrayAsIntTest();
let res21 = PauliArrayAsIntFailTest();
let res22 = TestDrawRandomDouble(0.0, 1.0);
MessageTest("Test");

// Conditionals:
Expand Down
4 changes: 4 additions & 0 deletions src/QirRuntime/test/QIR-static/qsharp/qir-test-math.qs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ namespace Microsoft.Quantum.Testing.QIR.Math {
return DrawRandomInt(min, max);
}

operation TestDrawRandomDouble(min : Double, max : Double) : Double {
return DrawRandomDouble(min, max);
}

function Close(expected : Double, actual : Double) : Bool {
let neighbourhood = 0.0000001; // On x86-64 + Win the error is in 16th digit after the decimal point.
// E.g. enstead of 0.0 there can be 0.00000000000000012.
Expand Down