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
82 changes: 82 additions & 0 deletions src/QirRuntime/lib/QIR/bridge-qis.ll
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,20 @@ define void @__quantum__qis__message__body(%String* %.str) {
; LLVM intrinsics (https://llvm.org/docs/LangRef.html):
declare double @llvm.sqrt.f64(double %.val)
declare double @llvm.log.f64(double %Val)
declare double @llvm.sin.f64(double %Val)
declare double @llvm.cos.f64(double %Val)

; Native implementations:
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 double @quantum__qis__sinh__body(double %theta)
declare double @quantum__qis__cosh__body(double %theta)
declare double @quantum__qis__arcsin__body(double %theta)
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)

; API for the user code:
Expand Down Expand Up @@ -348,6 +356,80 @@ define double @__quantum__qis__arctan2__body(double %y, double %x) { ; Q#: func
ret double %result
}

; function Sin (theta : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.sin
define double @__quantum__qis__sin__body(double %theta) { ; https://en.cppreference.com/w/cpp/numeric/math/sin
%result = call double @llvm.sin.f64(double %theta) ; https://llvm.org/docs/LangRef.html#llvm-sin-intrinsic
ret double %result
}

; function Cos (theta : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.cos
define double @__quantum__qis__cos__body(double %theta) { ; https://en.cppreference.com/w/cpp/numeric/math/cos
%result = call double @llvm.cos.f64(double %theta) ; https://llvm.org/docs/LangRef.html#llvm-cos-intrinsic
ret double %result
}

; function Tan (theta : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.tan
define double @__quantum__qis__tan__body(double %theta) { ; https://en.cppreference.com/w/cpp/numeric/math/tan
%sin = call double @llvm.sin.f64(double %theta)
%cos = call double @llvm.cos.f64(double %theta)
%result = fdiv double %sin, %cos ; tg(x) = sin(x) / cos(x)
ret double %result
}

; function Sinh (theta : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.sinh
define double @__quantum__qis__sinh__body(double %theta) { ; https://en.cppreference.com/w/cpp/numeric/math/sinh
%result = call double @quantum__qis__sinh__body(double %theta)
ret double %result
}

; function Cosh (theta : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.cosh
define double @__quantum__qis__cosh__body(double %theta) { ; https://en.cppreference.com/w/cpp/numeric/math/cosh
%result = call double @quantum__qis__cosh__body(double %theta)
ret double %result
}

; function Tanh (theta : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.tanh
define double @__quantum__qis__tanh__body(double %theta) { ; https://en.cppreference.com/w/cpp/numeric/math/tanh
%sin = call double @__quantum__qis__sinh__body(double %theta)
%cos = call double @__quantum__qis__cosh__body(double %theta)
%result = fdiv double %sin, %cos ; tanh(x) = sinh(x) / cosh(x)
ret double %result
}

; function ArcSin (theta : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.arcsin
define double @__quantum__qis__arcsin__body(double %theta) { ; https://en.cppreference.com/w/cpp/numeric/math/asin
%result = call double @quantum__qis__arcsin__body(double %theta)
ret double %result
}

; function ArcCos (theta : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.arccos
define double @__quantum__qis__arccos__body(double %theta) { ; https://en.cppreference.com/w/cpp/numeric/math/acos
%result = call double @quantum__qis__arccos__body(double %theta)
ret double %result
}

; function ArcTan (theta : Double) : Double
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.math.arctan
define double @__quantum__qis__arctan__body(double %theta) { ; https://en.cppreference.com/w/cpp/numeric/math/atan
%result = call double @quantum__qis__arctan__body(double %theta)
ret double %result
}


; function IEEERemainder(x : Double, y : Double) : Double
define double @__quantum__qis__ieeeremainder__body(double %x, double %y) {
%result = call double @quantum__qis__ieeeremainder__body(double %x, double %y)
ret double %result
}


; operation DrawRandomInt (min : Int, max : Int) : Int
; https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.random.drawrandomint
Expand Down
30 changes: 30 additions & 0 deletions src/QirRuntime/lib/QIR/intrinsicsMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ double quantum__qis__arctan2__body(double y, double x)
return std::atan2(y, x); // https://en.cppreference.com/w/cpp/numeric/math/atan2
}

double quantum__qis__sinh__body(double theta)
{
return std::sinh(theta);
}

double quantum__qis__cosh__body(double theta)
{
return std::cosh(theta);
}

double quantum__qis__arcsin__body(double theta)
{
return std::asin(theta); // https://en.cppreference.com/w/cpp/numeric/math/asin
}

double quantum__qis__arccos__body(double theta)
{
return std::acos(theta); // https://en.cppreference.com/w/cpp/numeric/math/acos
}

double quantum__qis__arctan__body(double theta)
{
return std::atan(theta); // https://en.cppreference.com/w/cpp/numeric/math/atan
}

double quantum__qis__ieeeremainder__body(double x, double y)
{
return std::remainder(x, y); // https://en.cppreference.com/w/cpp/numeric/math/remainder
}

int64_t quantum__qis__drawrandomint__body(int64_t minimum, int64_t maximum)
{
if(minimum > maximum)
Expand Down
7 changes: 7 additions & 0 deletions src/QirRuntime/lib/QIR/quantum__qis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ extern "C"
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 double quantum__qis__sinh__body(double theta); // NOLINT
QIR_SHARED_API double quantum__qis__cosh__body(double theta); // NOLINT
QIR_SHARED_API double quantum__qis__arcsin__body(double theta); // NOLINT
QIR_SHARED_API double quantum__qis__arccos__body(double theta); // NOLINT
QIR_SHARED_API double quantum__qis__arctan__body(double theta); // NOLINT

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

}
60 changes: 60 additions & 0 deletions src/QirRuntime/test/QIR-static/qir-test-math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__SqrtTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__LogTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__ArcTan2Test__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__SinTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__CosTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__TanTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__ArcSinTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__ArcCosTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__ArcTanTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__SinhTest__body(); // NOLINT
extern "C" uint64_t Microsoft__Quantum__Testing__QIR__Math__CoshTest__body(); // NOLINT
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

TEST_CASE("QIR: Math.Sqrt", "[qir.math][qir.Math.Sqrt]")
Expand All @@ -28,6 +38,56 @@ TEST_CASE("QIR: Math.ArcTan2", "[qir.math][qir.Math.ArcTan2]")
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__ArcTan2Test__body());
}

TEST_CASE("QIR: Math.Sin", "[qir.math][qir.Math.Sin]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__SinTest__body());
}

TEST_CASE("QIR: Math.Cos", "[qir.math][qir.Math.Cos]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__CosTest__body());
}

TEST_CASE("QIR: Math.Tan", "[qir.math][qir.Math.Tan]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__TanTest__body());
}

TEST_CASE("QIR: Math.ArcSin", "[qir.math][qir.Math.ArcSin]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__ArcSinTest__body());
}

TEST_CASE("QIR: Math.ArcCos", "[qir.math][qir.Math.ArcCos]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__ArcCosTest__body());
}

TEST_CASE("QIR: Math.ArcTan", "[qir.math][qir.Math.ArcTan]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__ArcTanTest__body());
}

TEST_CASE("QIR: Math.Sinh", "[qir.math][qir.Math.Sinh]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__SinhTest__body());
}

TEST_CASE("QIR: Math.Cosh", "[qir.math][qir.Math.Cosh]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__CoshTest__body());
}

TEST_CASE("QIR: Math.Tanh", "[qir.math][qir.Math.Tanh]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__TanhTest__body());
}

TEST_CASE("QIR: Math.IeeeRemainder", "[qir.math][qir.Math.IeeeRemainder]")
{
REQUIRE(0 == Microsoft__Quantum__Testing__QIR__Math__IeeeRemainderTest__body());
}

TEST_CASE("QIR: Math.DrawRandomInt", "[qir.math][qir.Math.DrawRandomInt]")
{
// Test that the Q# random number generator is a wrapper around the C++ generator:
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/test/QIR-static/qsharp/qir-gen.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.15.2102128704-alpha">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.2102129370-alpha">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
10 changes: 10 additions & 0 deletions src/QirRuntime/test/QIR-static/qsharp/qir-test-arrays.qs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ namespace Microsoft.Quantum.Testing.QIR {
let res6 = ArcTan2Test();
let res7 = PauliToStringTest();
let res8 = TestDrawRandomInt(0, 1);
let res9 = SinTest();
let res10 = CosTest();
let res11 = TanTest();
let res12 = SinhTest();
let res13 = CoshTest();
let res14 = TanhTest();
let res15 = IeeeRemainderTest();
let res16 = ArcSinTest();
let res17 = ArcCosTest();
let res18 = ArcTanTest();
MessageTest("Test");
}
return sum;
Expand Down
Loading