3
3
4
4
namespace Microsoft .Quantum .Diagnostics {
5
5
open Microsoft .Quantum .Canon ;
6
+ open Microsoft .Quantum .Math ;
6
7
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
+ }
7
15
8
16
/// # Summary
9
17
/// Declares that a classical condition is true.
@@ -33,12 +41,12 @@ namespace Microsoft.Quantum.Diagnostics {
33
41
function EqualityWithinToleranceFact (actual : Double , expected : Double , tolerance : Double ) : Unit {
34
42
let delta = actual - expected ;
35
43
if (delta > tolerance or delta < - tolerance ) {
36
- fail $"Fact was false. Expected: '{ expected}'. Actual: '{actual}'" ;
44
+ fail _FormattedExpectation ( actual , expected ) ;
37
45
}
38
46
}
39
47
40
48
/// # 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
42
50
/// small tolerance of 1e-10.
43
51
///
44
52
/// # Input
@@ -50,8 +58,42 @@ namespace Microsoft.Quantum.Diagnostics {
50
58
/// # Remarks
51
59
/// This is equivalent to <xref:microsoft.quantum.diagnostics.equalitywithintolerancefact> with
52
60
/// 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
+ );
55
97
}
56
98
57
99
/// # Summary
@@ -65,12 +107,8 @@ namespace Microsoft.Quantum.Diagnostics {
65
107
///
66
108
/// ## message
67
109
/// 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}" );
74
112
}
75
113
76
114
/// # Summary
@@ -84,12 +122,8 @@ namespace Microsoft.Quantum.Diagnostics {
84
122
///
85
123
/// ## message
86
124
/// 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}" );
93
127
}
94
128
95
129
/// # Summary
@@ -104,12 +138,8 @@ namespace Microsoft.Quantum.Diagnostics {
104
138
///
105
139
/// ## message
106
140
/// 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}" );
113
143
}
114
144
115
145
/// # Summary
@@ -125,10 +155,39 @@ namespace Microsoft.Quantum.Diagnostics {
125
155
/// ## message
126
156
/// Failure message string to be used when the assertion is triggered.
127
157
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}" );
132
191
}
133
192
134
193
/// # Summary
0 commit comments