From 1fc6ecfe9da8b0b11df833546870643d5529a6d1 Mon Sep 17 00:00:00 2001 From: Tyson Jones Date: Tue, 6 May 2025 00:05:25 +0200 Subject: [PATCH] added calcRealAmpSum tests --- tests/unit/calculations.cpp | 41 +++++++++++++++++++++++++++++++++++++ tests/utils/linalg.cpp | 31 ++++++++++++++++++++++++++++ tests/utils/linalg.hpp | 3 +++ 3 files changed, 75 insertions(+) diff --git a/tests/unit/calculations.cpp b/tests/unit/calculations.cpp index 3cbe22c9f..8c9e6c653 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 f4686fc44..716b14b6e 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 87cd116b5..ebca2b4c5 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);