diff --git a/tests/unit/calculations.cpp b/tests/unit/calculations.cpp index 3cbe22c9..8c9e6c65 100644 --- a/tests/unit/calculations.cpp +++ b/tests/unit/calculations.cpp @@ -156,6 +156,47 @@ void TEST_ON_CACHED_QUREG_AND_MATRIX(quregCache quregs, matrixCache matrices, au +TEST_CASE( "calcRealAmpSum", TEST_CATEGORY ) { + + SECTION( LABEL_CORRECTNESS ) { + + // The boilerplate for testing a function differs + // greatly depending on what the function does; + // this function is trivial so has a simple test, + // re-using the existing TEST_ALL_QUREGS() macro. + // This macro invokes the below RHS expressions + // passing substitutions of the LHS expressions + // with Quregs (statevector or density matrix) + // and reference objects (qvector or qmatrix), for + // every possible deployment (i.e. multithreading, + // GPU-acceleration, distribution, hybrids, etc). + + TEST_ALL_QUREGS( + qureg, calcRealAmpSum(qureg), + refer, std::real(getTotal(refer)) + ); + } + + SECTION( LABEL_VALIDATION ) { + + SECTION( "qureg uninitialised" ) { + + // prepare an un-initialised qureg + Qureg qureg; + + // manually mangle the fields for validation + // to detect, since the default values are + // undefined behaviour and might not trigger + // (e.g. compiler could re-use a valid Qureg) + qureg.numQubits = -123; + + REQUIRE_THROWS_WITH( calcRealAmpSum(qureg), ContainsSubstring("invalid Qureg") ); + } + } +} + + + TEST_CASE( "calcExpecPauliStr", TEST_CATEGORY ) { SECTION( LABEL_CORRECTNESS ) { diff --git a/tests/utils/linalg.cpp b/tests/utils/linalg.cpp index f4686fc4..716b14b6 100644 --- a/tests/utils/linalg.cpp +++ b/tests/utils/linalg.cpp @@ -107,6 +107,37 @@ int getNumPermutations(int n, int k) { +/* + * NONSENSE PR OPERATIONS + */ + + +qcomp getTotal(qvector in) { + + qcomp out = 0; + + // no compensated summation + for (auto& elem : in) + out += elem; + + return out; +} + + +qcomp getTotal(qmatrix in) { + + qcomp out = 0; + + // no compensated summation + for (auto& row : in) + for (auto& elem : row) + out += elem; + + return out; +} + + + /* * VECTOR OPERATIONS */ diff --git a/tests/utils/linalg.hpp b/tests/utils/linalg.hpp index 87cd116b..ebca2b4c 100644 --- a/tests/utils/linalg.hpp +++ b/tests/utils/linalg.hpp @@ -29,6 +29,9 @@ qindex setBitAt(qindex num, int ind, int bit); qindex setBitsAt(qindex num, vector inds, qindex bits); qindex getPow2(int); +qcomp getTotal(qvector); +qcomp getTotal(qmatrix); + qreal getSum(vector vec); qcomp getSum(qvector); qvector getNormalised(qvector);