forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathTests.qs
127 lines (105 loc) · 5.09 KB
/
MathTests.qs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Canon {
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Extensions.Math;
function NativeFnsAreCallableTest() : () {
let arg = PI() / 2.0;
AssertAlmostEqual(Sin(arg), 1.0);
AssertAlmostEqual(Cos(arg), 0.0);
let arcArg = 1.0;
AssertAlmostEqual(ArcCos(arcArg), 0.0);
AssertAlmostEqual(ArcSin(arcArg), arg);
}
function RealModTest() : () {
AssertAlmostEqual(RealMod(5.5 * PI(), 2.0 * PI(), 0.0), 1.5 * PI());
AssertAlmostEqual(RealMod(0.5 * PI(), 2.0 * PI(), -PI() / 2.0), 0.5 * PI());
}
function ArcHyperbolicFnsTest() : () {
// These tests were generated using NumPy's implementations
// of the inverse hyperbolic functions.
AssertAlmostEqual(ArcTanh(0.3), 0.30951960420311175);
AssertAlmostEqual(ArcCosh(1.3), 0.75643291085695963);
AssertAlmostEqual(ArcSinh(-0.7), -0.65266656608235574);
}
function ExtendedGCDTestHelper( a : Int , b : Int, gcd : Int ) : () {
Message($"Testing {a}, {b}, {gcd} ");
let (u,v) = ExtendedGCD(a,b);
let expected = AbsI(gcd);
let actual = AbsI(u*a+v*b);
AssertIntEqual( expected, actual,
$"Expected absolute value of gcd to be {expected}, got {actual}");
}
function ExtendedGCDTest() : ()
{
let testTuples = [ (1,1,1); (1,-1,1); (-1,1,1); (-1,-1,1); (5,7,1); (-5,7,1); (3,15,3) ];
Ignore(Map(ExtendedGCDTestHelper, testTuples));
}
function BitSizeTest() : () {
AssertIntEqual(BitSize(3),2,"BitSize(3) must be 2");
AssertIntEqual(BitSize(7),3,"BitSize(7) must be 2");
}
function ExpModTest() : () {
// this test is generated using Mathematica PowerMod function
let result = ExpMod(5,4611686018427387903,7);
AssertIntEqual(result,6, $"The result must be 6, got {result}");
}
function ContinuedFractionConvergentTestHelper( numerator : Int, denominator : Int ) : () {
let bitSize = 2 * BitSize(denominator);
let numeratorDyadic = numerator * 2 ^ bitSize / denominator;
let (u,v) = ContinuedFractionConvergent( Fraction(numeratorDyadic, 2^bitSize), denominator );
AssertBoolEqual(
(AbsI(u) == numerator ) && (AbsI(v) == denominator ) , true,
$"The result must be ±{numerator}/±{denominator} got {u}/{v}");
}
function ContinuedFractionConvergentEdgeCaseTestHelper( numerator : Int, denominator : Int, bound : Int ) : () {
let (num,denom) = ContinuedFractionConvergent( Fraction(numerator,denominator), bound );
AssertBoolEqual(
( AbsI(num) == numerator ) && ( AbsI(denom) == denominator ) , true,
$"The result must be ±{numerator}/±{denominator} got {num}/{denom}");
}
function ContinuedFractionConvergentTest() : () {
let testTuples = [ (29,47); (17,37); (15,67) ];
Ignore(Map(ContinuedFractionConvergentTestHelper, testTuples));
let edgeCaseTestTuples = [ (1,4,512); (3,4,512) ];
Ignore(Map(ContinuedFractionConvergentEdgeCaseTestHelper, edgeCaseTestTuples));
}
function ComplexMathTest() : () {
mutable complexCases = [(0.123,0.321);(0.123, -0.321);(-0.123, 0.321);(-0.123, -0.321)];
for(idxCases in 0..Length(complexCases)-1){
let (complexRe, complexIm) = complexCases[idxCases];
let complexAbs = Sqrt(complexRe * complexRe + complexIm * complexIm);
let complexArg = ArcTan2(complexIm, complexRe);
let complex = Complex(complexRe, complexIm);
let complexPolar = ComplexPolar(complexAbs , complexArg);
AssertAlmostEqual(AbsSquaredComplex(complex), complexAbs * complexAbs);
AssertAlmostEqual(AbsComplex(complex), complexAbs);
AssertAlmostEqual(ArgComplex(complex), complexArg);
AssertAlmostEqual(AbsSquaredComplexPolar(complexPolar), complexAbs * complexAbs);
AssertAlmostEqual(AbsComplexPolar(complexPolar), complexAbs);
AssertAlmostEqual(ArgComplexPolar(complexPolar), complexArg);
let (x,y) = ComplexPolarToComplex(complexPolar);
AssertAlmostEqual(x, complexRe);
AssertAlmostEqual(y, complexIm);
let (r,t) = ComplexToComplexPolar(complex);
AssertAlmostEqual(r, complexAbs);
AssertAlmostEqual(t, complexArg);
}
}
function PNormTest() : (){
mutable testCases = [(1.0,[-0.1;0.2;0.3],0.6);
(1.5,[0.1;-0.2;0.3],0.43346228721136096815);
(2.0,[0.1;0.2;-0.3],0.37416573867739413856);
(3.0,[0.0;0.0;-0.0],0.0)];
for(idxTest in 0..Length(testCases)-1){
let (p, array, pNormExpected) = testCases[idxTest];
AssertAlmostEqual(PNorm(p,array),pNormExpected);
// if PNorm fails, PNormalize will fail.
let arrayNormalized = PNormalize(p, array);
for(idxCoeff in 0..Length(array)-1){
AssertAlmostEqual(array[idxCoeff] / pNormExpected, arrayNormalized[idxCoeff]);
}
}
}
}