Skip to content

added calcRealAmpSum tests #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions tests/unit/calculations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
31 changes: 31 additions & 0 deletions tests/utils/linalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/utils/linalg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ qindex setBitAt(qindex num, int ind, int bit);
qindex setBitsAt(qindex num, vector<int> inds, qindex bits);
qindex getPow2(int);

qcomp getTotal(qvector);
qcomp getTotal(qmatrix);

qreal getSum(vector<qreal> vec);
qcomp getSum(qvector);
qvector getNormalised(qvector);
Expand Down