Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 1939470

Browse files
Chris Granademsoeken
Chris Granade
authored andcommitted
Functions for arithmetic, logical, and bitwise functions. (#152)
* Slightly improved complex in Microsoft.Quantum.Math. * First set of operators as functions. * Added operatorfns to Microsoft.Quantum.Logical. * Fixed layout of Bitwise.qs. * Added tests for logical functions. * Refactored equality facts to use common base fact, add ℂ. * Polishing up tests and hunting down bugs. * One last test fix. * Fixed truncation error in bigint left-shift test. * Addressing feedback. * Addressing feedback in Bitwise. * Addressing more feedback.
1 parent 4490ba8 commit 1939470

20 files changed

+1727
-87
lines changed

Chemistry/tests/ChemistryTests/UnitaryCoupledClusterTests.qs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ namespace Microsoft.Quantum.Chemistry.Tests {
6969
let s = sortedIndices[3];
7070

7171
Fact(p<q and q<r and r<s, "Expected p<q<r<s");
72-
NearEqualityFact(globalSign, expectedGlobalSign);
72+
NearEqualityFactD(globalSign, expectedGlobalSign);
7373
for (signIdx in 0..Length(signs)-1) {
74-
NearEqualityFact(signs[signIdx], expectedSigns[signIdx]);
74+
NearEqualityFactD(signs[signIdx], expectedSigns[signIdx]);
7575
}
7676
}
7777
}

Standard/src/Bitwise/Bitwise.qs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.Quantum.Bitwise {
5+
6+
/// # Summary
7+
/// Shifts the bitwise representation of a number left by a given number of
8+
/// bits.
9+
///
10+
/// # Input
11+
/// ## value
12+
/// The number whose bitwise representation is to be shifted to the left
13+
/// (more significant).
14+
/// ## amount
15+
/// The number of bits by which `value` is to be shifted to the left.
16+
///
17+
/// # Output
18+
/// The value of `value`, shifted left by `amount` bits.
19+
///
20+
/// # Remarks
21+
/// The following are equivalent:
22+
/// ```Q#
23+
/// let c = a <<< b;
24+
/// let c = LeftShiftedI(a, b);
25+
/// ```
26+
function LeftShiftedI(value : Int, amount : Int) : Int {
27+
return value <<< amount;
28+
}
29+
30+
/// # Summary
31+
/// Shifts the bitwise representation of a number left by a given number of
32+
/// bits.
33+
///
34+
/// # Input
35+
/// ## value
36+
/// The number whose bitwise representation is to be shifted to the left
37+
/// (more significant).
38+
/// ## amount
39+
/// The number of bits by which `value` is to be shifted to the left.
40+
///
41+
/// # Output
42+
/// The value of `value`, shifted left by `amount` bits.
43+
///
44+
/// # Remarks
45+
/// The following are equivalent:
46+
/// ```Q#
47+
/// let c = a <<< b;
48+
/// let c = LeftShiftedL(a, b);
49+
/// ```
50+
function LeftShiftedL(value : BigInt, amount : Int) : BigInt {
51+
return value <<< amount;
52+
}
53+
54+
/// # Summary
55+
/// Shifts the bitwise representation of a number right by a given number of
56+
/// bits.
57+
///
58+
/// # Input
59+
/// ## value
60+
/// The number whose bitwise representation is to be shifted to the right
61+
/// (less significant).
62+
/// ## amount
63+
/// The number of bits by which `value` is to be shifted to the right.
64+
///
65+
/// # Output
66+
/// The value of `value`, shifted right by `amount` bits.
67+
///
68+
/// # Remarks
69+
/// The following are equivalent:
70+
/// ```Q#
71+
/// let c = a >>> b;
72+
/// let c = RightShiftedI(a, b);
73+
/// ```
74+
function RightShiftedI(value : Int, amount : Int) : Int {
75+
return value >>> amount;
76+
}
77+
78+
/// # Summary
79+
/// Shifts the bitwise representation of a number right by a given number of
80+
/// bits.
81+
///
82+
/// # Input
83+
/// ## value
84+
/// The number whose bitwise representation is to be shifted to the right
85+
/// (less significant).
86+
/// ## amount
87+
/// The number of bits by which `value` is to be shifted to the right.
88+
///
89+
/// # Output
90+
/// The value of `value`, shifted right by `amount` bits.
91+
///
92+
/// # Remarks
93+
/// The following are equivalent:
94+
/// ```Q#
95+
/// let c = a >>> b;
96+
/// let c = RightShiftedL(a, b);
97+
/// ```
98+
function RightShiftedL(value : BigInt, amount : Int) : BigInt {
99+
return value >>> amount;
100+
}
101+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
/// # Summary
5+
/// This namespace contains functions for acting on the bitwise representation of
6+
/// classical data types.
7+
namespace Microsoft.Quantum.Bitwise { }

Standard/src/Diagnostics/Deprecated.qs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ namespace Microsoft.Quantum.Canon {
1414
}
1515

1616
/// # Deprecated
17-
/// Please use @"Microsoft.Quantum.Diagnostics.NearEqualityFact" instead.
17+
/// Please use @"Microsoft.Quantum.Diagnostics.NearEqualityFactD" instead.
1818
function AssertAlmostEqual(actual : Double, expected : Double) : Unit {
19-
_Renamed("Microsoft.Quantum.Canon.AssertAlmostEqual", "Microsoft.Quantum.Diagnostics.NearEqualityFact");
20-
NearEqualityFact(actual, expected);
19+
_Renamed("Microsoft.Quantum.Canon.AssertAlmostEqual", "Microsoft.Quantum.Diagnostics.NearEqualityFactD");
20+
NearEqualityFactD(actual, expected);
21+
}
22+
23+
/// # Deprecated
24+
/// Please use @"Microsoft.Quantum.Diagnostics.NearEqualityFactD" instead.
25+
function NearEqualityFact(actual : Double, expected : Double) : Unit {
26+
_Renamed("Microsoft.Quantum.Diagnostics.NearEqualityFact", "Microsoft.Quantum.Diagnostics.NearEqualityFactD");
27+
NearEqualityFactD(actual, expected);
2128
}
2229

2330
/// # Deprecated

Standard/src/Diagnostics/Facts.qs

Lines changed: 85 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33

44
namespace Microsoft.Quantum.Diagnostics {
55
open Microsoft.Quantum.Canon;
6+
open Microsoft.Quantum.Math;
67
open Microsoft.Quantum.Arrays;
8+
open Microsoft.Quantum.Logical;
9+
10+
/// # Summary
11+
/// Private function used to generate meaningful error messages.
12+
function _FormattedExpectation<'T>(actual : 'T, expected : 'T) : String {
13+
return $"Expected: '{expected}'. Actual: '{actual}'";
14+
}
715

816
/// # Summary
917
/// Declares that a classical condition is true.
@@ -33,12 +41,12 @@ namespace Microsoft.Quantum.Diagnostics {
3341
function EqualityWithinToleranceFact(actual : Double, expected : Double, tolerance : Double) : Unit {
3442
let delta = actual - expected;
3543
if (delta > tolerance or delta < -tolerance) {
36-
fail $"Fact was false. Expected: '{expected}'. Actual: '{actual}'";
44+
fail _FormattedExpectation(actual, expected);
3745
}
3846
}
3947

4048
/// # Summary
41-
/// Asserts that a classical floating point variable has the expected value up to a
49+
/// Asserts that a classical floating point value has the expected value up to a
4250
/// small tolerance of 1e-10.
4351
///
4452
/// # Input
@@ -50,8 +58,42 @@ namespace Microsoft.Quantum.Diagnostics {
5058
/// # Remarks
5159
/// This is equivalent to <xref:microsoft.quantum.diagnostics.equalitywithintolerancefact> with
5260
/// hardcoded tolerance of $10^{-10}$.
53-
function NearEqualityFact(actual : Double, expected : Double) : Unit {
54-
EqualityWithinToleranceFact(actual, expected, 1E-10);
61+
function NearEqualityFactD(actual : Double, expected : Double) : Unit {
62+
EqualityWithinToleranceFact(actual, expected, 1e-10);
63+
}
64+
65+
/// # Summary
66+
/// Asserts that a classical complex number has the expected value up to a
67+
/// small tolerance of 1e-10.
68+
///
69+
/// # Input
70+
/// ## actual
71+
/// The number to be checked.
72+
/// ## expected
73+
/// The expected value.
74+
function NearEqualityFactC(actual : Complex, expected : Complex) : Unit {
75+
// Don't reduce to the base case of Fact, since we need to check two
76+
// conditions.
77+
let ((reA, imA), (reE, imE)) = (actual!, expected!);
78+
if (AbsD(reA - reE) >= 1e-10 or AbsD(imA - imE) >= 1e-10) {
79+
fail _FormattedExpectation(actual, expected);
80+
}
81+
}
82+
83+
/// # Summary
84+
/// Asserts that a classical complex number has the expected value up to a
85+
/// small tolerance of 1e-10.
86+
///
87+
/// # Input
88+
/// ## actual
89+
/// The number to be checked.
90+
/// ## expected
91+
/// The expected value.
92+
function NearEqualityFactCP(actual : ComplexPolar, expected : ComplexPolar) : Unit {
93+
return NearEqualityFactC(
94+
ComplexPolarAsComplex(actual),
95+
ComplexPolarAsComplex(expected)
96+
);
5597
}
5698

5799
/// # Summary
@@ -65,12 +107,8 @@ namespace Microsoft.Quantum.Diagnostics {
65107
///
66108
/// ## message
67109
/// Failure message string to be used when the assertion is triggered.
68-
function EqualityFactI(actual : Int, expected : Int, message : String) : Unit
69-
{
70-
if (actual != expected)
71-
{
72-
fail message;
73-
}
110+
function EqualityFactI(actual : Int, expected : Int, message : String) : Unit {
111+
Fact(actual == expected, $"{actual} ≠ {expected}: {message}");
74112
}
75113

76114
/// # Summary
@@ -84,12 +122,8 @@ namespace Microsoft.Quantum.Diagnostics {
84122
///
85123
/// ## message
86124
/// Failure message string to be used when the assertion is triggered.
87-
function EqualityFactL(actual : BigInt, expected : BigInt, message : String) : Unit
88-
{
89-
if (actual != expected)
90-
{
91-
fail message;
92-
}
125+
function EqualityFactL(actual : BigInt, expected : BigInt, message : String) : Unit {
126+
Fact(actual == expected, $"{actual} ≠ {expected}: {message}");
93127
}
94128

95129
/// # Summary
@@ -104,12 +138,8 @@ namespace Microsoft.Quantum.Diagnostics {
104138
///
105139
/// ## message
106140
/// Failure message string to be used when the assertion is triggered.
107-
function EqualityFactB(actual : Bool, expected : Bool, message : String) : Unit
108-
{
109-
if (actual != expected)
110-
{
111-
fail message;
112-
}
141+
function EqualityFactB(actual : Bool, expected : Bool, message : String) : Unit {
142+
Fact(actual == expected, $"{actual} ≠ {expected}: {message}");
113143
}
114144

115145
/// # Summary
@@ -125,10 +155,39 @@ namespace Microsoft.Quantum.Diagnostics {
125155
/// ## message
126156
/// Failure message string to be used when the assertion is triggered.
127157
function EqualityFactR (actual : Result, expected : Result, message : String) : Unit {
128-
if (actual != expected)
129-
{
130-
fail message;
131-
}
158+
Fact(actual == expected, $"{actual} ≠ {expected}: {message}");
159+
}
160+
161+
/// # Summary
162+
/// Asserts that a complex number has the expected value.
163+
///
164+
/// # Input
165+
/// ## actual
166+
/// The value to be checked.
167+
///
168+
/// ## expected
169+
/// The expected value.
170+
///
171+
/// ## message
172+
/// Failure message string to be used when the assertion is triggered.
173+
function EqualityFactC(actual : Complex, expected : Complex, message : String) : Unit {
174+
Fact(EqualC(actual, expected), $"{actual} ≠ {expected}: {message}");
175+
}
176+
177+
/// # Summary
178+
/// Asserts that a complex number has the expected value.
179+
///
180+
/// # Input
181+
/// ## actual
182+
/// The value to be checked.
183+
///
184+
/// ## expected
185+
/// The expected value.
186+
///
187+
/// ## message
188+
/// Failure message string to be used when the assertion is triggered.
189+
function EqualityFactCP(actual : ComplexPolar, expected : ComplexPolar, message : String) : Unit {
190+
Fact(EqualCP(actual, expected), $"{actual} ≠ {expected}: {message}");
132191
}
133192

134193
/// # Summary

0 commit comments

Comments
 (0)