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
1 change: 1 addition & 0 deletions src/QirRuntime/lib/QIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ compile_from_qir(bridge-qis ${bridge_qis_target})
set(qis_sup_source_files
"intrinsics.cpp"
"intrinsicsMath.cpp"
intrinsicsOut.cpp
)

add_library(qir-qis-support ${qis_sup_source_files})
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/allocationsTracker.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include "allocationsTracker.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/allocationsTracker.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/arrays.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <algorithm>
Expand Down
21 changes: 20 additions & 1 deletion src/QirRuntime/lib/QIR/bridge-qis.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; Copyright (c) Microsoft Corporation. All rights reserved.
; Copyright (c) Microsoft Corporation.
; Licensed under the MIT License.

; The __quantum__qis__* definitions should be automatically generated by QIR, depending on the specific target.
Expand Down Expand Up @@ -60,6 +60,8 @@ declare void @quantum__qis__y__ctl(%struct.QirArray*, %class.QUBIT*)
declare void @quantum__qis__z__body(%class.QUBIT*)
declare void @quantum__qis__z__ctl(%struct.QirArray*, %class.QUBIT*)

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

;===============================================================================
; quantum.qis namespace implementations
;
Expand Down Expand Up @@ -279,6 +281,14 @@ define void @__quantum__qis__z__ctl(%Array* %.ctls, %Qubit* %.q) {
}


;===============================================================================
;
define void @__quantum__qis__message__body(%String* %.str) {
%str = bitcast %String* %.str to %"struct.QirString"*
call void @quantum__qis__message__body(%"struct.QirString"* %str)
ret void
}

;===============================================================================
; quantum.qis math functions
;
Expand All @@ -292,6 +302,7 @@ declare i1 @quantum__qis__isnan__body(double %d)
declare double @quantum__qis__infinity__body()
declare i1 @quantum__qis__isinf__body(double %d)
declare double @quantum__qis__arctan2__body(double %y, double %x)
declare i64 @quantum__qis__drawrandomint__body(i64 %min, i64 %max)

; API for the user code:
define double @__quantum__qis__nan__body() { ; Q#: function NAN() : Double http://www.cplusplus.com/reference/cmath/nan-function/
Expand Down Expand Up @@ -336,3 +347,11 @@ define double @__quantum__qis__arctan2__body(double %y, double %x) { ; Q#: func
%result = call double @quantum__qis__arctan2__body(double %y, double %x)
ret double %result
}


; operation DrawRandomInt (min : Int, max : Int) : Int
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.random.drawrandomint
define i64 @__quantum__qis__drawrandomint__body(i64 %min, i64 %max) {
%result = call i64 @quantum__qis__drawrandomint__body(i64 %min, i64 %max)
ret i64 %result
}
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/bridge-rt.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; Copyright (c) Microsoft Corporation. All rights reserved.
; Copyright (c) Microsoft Corporation.
; Licensed under the MIT License.

;=======================================================================================================================
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/callables.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <assert.h>
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/context.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <assert.h>
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/context.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/delegated.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/*=============================================================================
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/intrinsics.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/*=============================================================================
Expand Down
59 changes: 58 additions & 1 deletion src/QirRuntime/lib/QIR/intrinsicsMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
// Licensed under the MIT License.

#include <cmath>
#include <random>
#include <stdexcept>
#include "quantum__qis.hpp"
#include "quantum__qis_internal.hpp"
#include "quantum__rt.hpp"

// Forward declarations:
namespace // Visible in this translation unit only.
{
extern thread_local bool randomizeSeed;
extern int64_t lastGeneratedRndNum;
}

// Implementation:
extern "C"
{

Expand All @@ -28,4 +40,49 @@ double quantum__qis__arctan2__body(double y, double x)
return std::atan2(y, x); // https://en.cppreference.com/w/cpp/numeric/math/atan2
}

} // extern "C"
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));
}

// 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.

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

} // extern "C"

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

// For test purposes only:
namespace Quantum
{
namespace Qis
{
namespace Internal
{
char const excStrDrawRandomInt[] = "Invalid Argument: minimum > maximum for DrawRandomInt()";

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

int64_t GetLastGeneratedRandomNumber()
{
return lastGeneratedRndNum;
}
} // namespace Internal
} // namespace Qis
} // namespace Quantum
47 changes: 47 additions & 0 deletions src/QirRuntime/lib/QIR/intrinsicsOut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <iostream>

#include "qirTypes.hpp"
#include "quantum__qis.hpp"

// Forward declarations:
static std::ostream& GetOutputStream();

// Public API:
extern "C"
{
void quantum__qis__message__body(QirString* qstr) // NOLINT
{
GetOutputStream() << qstr->str << std::endl;
}
} // extern "C"


// Internal API:
static std::ostream* currentOutputStream = &std::cout; // Log to std::cout by default.

static std::ostream& GetOutputStream()
{
return *currentOutputStream;
}


// For test purposes only:
namespace Quantum // Replace with `namespace Quantum::Qis::Internal` after migration to C++17.
{
namespace Qis
{
namespace Internal
{
std::ostream& SetOutputStream(std::ostream & newOStream)
{
std::ostream& oldOStream = *currentOutputStream;
currentOutputStream = &newOStream;
return oldOStream;
}
} // namespace Internal
} // namespace Qis
} // namespace Quantum

2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/qirTypes.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once
Expand Down
6 changes: 5 additions & 1 deletion src/QirRuntime/lib/QIR/quantum__qis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "CoreTypes.hpp"

// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#ifdef _WIN32
Expand Down Expand Up @@ -60,9 +60,13 @@ extern "C"
QIR_SHARED_API void quantum__qis__z__body(QUBIT*); // NOLINT
QIR_SHARED_API void quantum__qis__z__ctl(QirArray*, QUBIT*); // NOLINT

QIR_SHARED_API void quantum__qis__message__body(QirString* qstr); // NOLINT

// Q# Math:
QIR_SHARED_API bool quantum__qis__isnan__body(double d); // NOLINT
QIR_SHARED_API double quantum__qis__infinity__body(); // NOLINT
QIR_SHARED_API bool quantum__qis__isinf__body(double d); // NOLINT
QIR_SHARED_API double quantum__qis__arctan2__body(double y, double x); // NOLINT
QIR_SHARED_API int64_t quantum__qis__drawrandomint__body(int64_t minimum, int64_t maximum); // NOLINT

}
29 changes: 29 additions & 0 deletions src/QirRuntime/lib/QIR/quantum__qis_internal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// To be included by the QIS implementation and QIS tests only.
// Not to be included by parties outside QIS.

#ifdef _WIN32
#define QIR_SHARED_API __declspec(dllexport)
#else
#define QIR_SHARED_API
#endif

// For test purposes only:
namespace Quantum // Replace with `namespace Quantum::Qis::Internal` after migration to C++17.
{
namespace Qis
{
namespace Internal
{
extern char const excStrDrawRandomInt[];

extern std::ostream& SetOutputStream(std::ostream& newOStream);
void RandomizeSeed(bool randomize);
int64_t GetLastGeneratedRandomNumber();
} // namespace Internal
} // namespace Qis
} // namespace Quantum
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/quantum__rt.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/strings.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <assert.h>
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/lib/QIR/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <assert.h>
Expand Down
1 change: 1 addition & 0 deletions src/QirRuntime/test/QIR-static/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_executable(qir-static-tests
qir-driver.cpp
qir-test-math.cpp
qir-test-strings.cpp
qir-test-ouput.cpp
)

target_link_libraries(qir-static-tests PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/test/QIR-static/qir-driver.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <assert.h>
Expand Down
Loading