Skip to content

Commit 1fc6ecf

Browse files
committed
added calcRealAmpSum tests
1 parent 44909e2 commit 1fc6ecf

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

tests/unit/calculations.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,47 @@ void TEST_ON_CACHED_QUREG_AND_MATRIX(quregCache quregs, matrixCache matrices, au
156156

157157

158158

159+
TEST_CASE( "calcRealAmpSum", TEST_CATEGORY ) {
160+
161+
SECTION( LABEL_CORRECTNESS ) {
162+
163+
// The boilerplate for testing a function differs
164+
// greatly depending on what the function does;
165+
// this function is trivial so has a simple test,
166+
// re-using the existing TEST_ALL_QUREGS() macro.
167+
// This macro invokes the below RHS expressions
168+
// passing substitutions of the LHS expressions
169+
// with Quregs (statevector or density matrix)
170+
// and reference objects (qvector or qmatrix), for
171+
// every possible deployment (i.e. multithreading,
172+
// GPU-acceleration, distribution, hybrids, etc).
173+
174+
TEST_ALL_QUREGS(
175+
qureg, calcRealAmpSum(qureg),
176+
refer, std::real(getTotal(refer))
177+
);
178+
}
179+
180+
SECTION( LABEL_VALIDATION ) {
181+
182+
SECTION( "qureg uninitialised" ) {
183+
184+
// prepare an un-initialised qureg
185+
Qureg qureg;
186+
187+
// manually mangle the fields for validation
188+
// to detect, since the default values are
189+
// undefined behaviour and might not trigger
190+
// (e.g. compiler could re-use a valid Qureg)
191+
qureg.numQubits = -123;
192+
193+
REQUIRE_THROWS_WITH( calcRealAmpSum(qureg), ContainsSubstring("invalid Qureg") );
194+
}
195+
}
196+
}
197+
198+
199+
159200
TEST_CASE( "calcExpecPauliStr", TEST_CATEGORY ) {
160201

161202
SECTION( LABEL_CORRECTNESS ) {

tests/utils/linalg.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,37 @@ int getNumPermutations(int n, int k) {
107107

108108

109109

110+
/*
111+
* NONSENSE PR OPERATIONS
112+
*/
113+
114+
115+
qcomp getTotal(qvector in) {
116+
117+
qcomp out = 0;
118+
119+
// no compensated summation
120+
for (auto& elem : in)
121+
out += elem;
122+
123+
return out;
124+
}
125+
126+
127+
qcomp getTotal(qmatrix in) {
128+
129+
qcomp out = 0;
130+
131+
// no compensated summation
132+
for (auto& row : in)
133+
for (auto& elem : row)
134+
out += elem;
135+
136+
return out;
137+
}
138+
139+
140+
110141
/*
111142
* VECTOR OPERATIONS
112143
*/

tests/utils/linalg.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ qindex setBitAt(qindex num, int ind, int bit);
2929
qindex setBitsAt(qindex num, vector<int> inds, qindex bits);
3030
qindex getPow2(int);
3131

32+
qcomp getTotal(qvector);
33+
qcomp getTotal(qmatrix);
34+
3235
qreal getSum(vector<qreal> vec);
3336
qcomp getSum(qvector);
3437
qvector getNormalised(qvector);

0 commit comments

Comments
 (0)