12#include <catch2/catch_test_macros.hpp>
13#include <catch2/matchers/catch_matchers_string.hpp>
14#include <catch2/generators/catch_generators_range.hpp>
16#include "tests/utils/macros.hpp"
17#include "tests/utils/cache.hpp"
18#include "tests/utils/convert.hpp"
19#include "tests/utils/compare.hpp"
20#include "tests/utils/random.hpp"
27using Catch::Matchers::ContainsSubstring;
36#define TEST_CATEGORY \
37 LABEL_UNIT_TAG "[debug]"
49TEST_CASE(
"setQuESTInputErrorHandler", TEST_CATEGORY ) {
65TEST_CASE(
"setQuESTMaxNumReportedSigFigs", TEST_CATEGORY ) {
67 SECTION( LABEL_CORRECTNESS ) {
69 qcomp scalar =
getQcomp(0.12345, 0.12345);
71 vector<std::string> refs = {
82 for (
size_t numSigFigs=1; numSigFigs<=refs.size(); numSigFigs++) {
87 std::stringstream buffer;
88 std::streambuf * old = std::cout.rdbuf(buffer.rdbuf());
91 std::string out = buffer.str();
93 std::string ref = refs[numSigFigs-1];
95 CAPTURE( numSigFigs, ref );
96 REQUIRE( out == ref );
100 SECTION( LABEL_VALIDATION ) {
102 SECTION(
"number" ) {
104 int num = GENERATE( -1, 0 );
115TEST_CASE(
"setQuESTNumReportedNewlines", TEST_CATEGORY ) {
117 SECTION( LABEL_CORRECTNESS ) {
119 for (
int numNewlines=0; numNewlines<3; numNewlines++) {
124 std::stringstream buffer;
125 std::streambuf * old = std::cout.rdbuf(buffer.rdbuf());
127 std::cout.rdbuf(old);
128 std::string out = buffer.str();
130 std::string ref =
"x" + std::string(numNewlines,
'\n');
132 CAPTURE( numNewlines, ref );
133 REQUIRE( out == ref );
137 SECTION( LABEL_VALIDATION ) {
139 SECTION(
"number" ) {
144 SECTION(
"multine number" ) {
148 REQUIRE_THROWS_WITH(
reportQuESTEnv(), ContainsSubstring(
"zero") && ContainsSubstring(
"not permitted when calling multi-line") );
157TEST_CASE(
"setQuESTSeeds", TEST_CATEGORY ) {
159 SECTION( LABEL_CORRECTNESS ) {
164 for (
auto& [label, qureg]: getCachedDensmatrs()) {
166 DYNAMIC_SECTION( label ) {
168 SECTION(
"same seed consistency" ) {
170 unsigned seeds[] = {123, 543, 755};
171 const int numSeeds = 3;
172 const int numMixedStates = 10;
173 const int numReps = 5;
180 qmatrix ref = getMatrix(qureg);
183 vector<int> outcomes(qureg.numQubits);
184 for (
int i=0; i<qureg.numQubits; i++)
188 for (
int r=0; r<numReps; r++) {
195 REQUIRE_AGREE( qureg, ref);
198 for (
int i=0; i<qureg.numQubits; i++)
203 SECTION(
"different key inconsistency" ) {
205 unsigned seeds[] = {123, 543, 755};
206 const int numSeeds = 3;
207 const int ampInd = 0;
215 int i = GENERATE_COPY( range(0,numSeeds) );
216 seeds[i] = 987654321;
222 REQUIRE( amp1 != amp2 );
228 SECTION( LABEL_VALIDATION ) {
230 SECTION(
"env not initialised" ) {
236 SECTION(
"number of seeds" ) {
238 unsigned seeds[] = { 0 };
239 int numSeeds = GENERATE( -1, 0 );
241 REQUIRE_THROWS_WITH(
setQuESTSeeds(seeds, numSeeds), ContainsSubstring(
"Invalid number of random seeds") );
244 SECTION(
"invalid pointer" ) {
246 int numSeeds = GENERATE( 1, 2 );
248 REQUIRE_THROWS_WITH(
setQuESTSeeds(
nullptr, numSeeds), ContainsSubstring(
"The given seeds list pointer is NULL") );
259TEST_CASE(
"setQuESTSeedsToDefault", TEST_CATEGORY ) {
261 SECTION( LABEL_CORRECTNESS ) {
266 for (
auto& [label, qureg]: getCachedDensmatrs()) {
268 DYNAMIC_SECTION( label ) {
270 SECTION(
"different key inconsistency" ) {
272 const int ampInd = 0;
285 REQUIRE( amp1 != amp2 );
291 SECTION( LABEL_VALIDATION ) {
293 SECTION(
"env not initialised" ) {
305TEST_CASE(
"getQuESTSeeds", TEST_CATEGORY ) {
307 SECTION( LABEL_CORRECTNESS ) {
309 SECTION(
"can be called immediately" ) {
314 vector<unsigned> out(numSeeds);
319 SECTION(
"correct output" ) {
321 GENERATE( range(0,10) );
325 vector<unsigned> in(numSeeds);
326 for (
int i=0; i<numSeeds; i++)
333 vector<unsigned> out(numSeeds);
335 for (
int i=0; i<numSeeds; i++)
336 REQUIRE( in[i] == out[i] );
340 SECTION( LABEL_VALIDATION ) {
342 SECTION(
"env not initialised" ) {
354TEST_CASE(
"getQuESTNumSeeds", TEST_CATEGORY ) {
356 SECTION( LABEL_CORRECTNESS ) {
358 SECTION(
"can be called immediately" ) {
363 SECTION(
"correct output" ) {
365 GENERATE( range(0,10) );
369 vector<unsigned> in(numSeeds);
370 for (
int i=0; i<numSeeds; i++)
381 SECTION( LABEL_VALIDATION ) {
383 SECTION(
"env not initialised" ) {
395TEST_CASE(
"setQuESTValidationOn", TEST_CATEGORY ) {
397 SECTION( LABEL_CORRECTNESS ) {
400 for (
int i=0; i<3; i++)
407 SECTION( LABEL_VALIDATION ) {
415TEST_CASE(
"setQuESTValidationOff", TEST_CATEGORY ) {
417 SECTION( LABEL_CORRECTNESS ) {
420 for (
int i=0; i<3; i++)
437 SECTION( LABEL_VALIDATION ) {
448TEST_CASE(
"setQuESTValidationEpsilon", TEST_CATEGORY ) {
450 SECTION( LABEL_CORRECTNESS ) {
452 SECTION(
"affects validation" ) {
456 SECTION(
"unitarity" ) {
486 SECTION(
"affects struct fields" ) {
488 SECTION(
"CompMatr" ) {
501 SECTION(
"DiagMatr" ) {
516 SECTION(
"FullStateDiagMatr" ) {
531 SECTION(
"KrausMap" ) {
544 SECTION( LABEL_VALIDATION ) {
546 SECTION(
"negative epsilon" ) {
548 qreal eps = GENERATE( -0.5, -1, -100 );
559TEST_CASE(
"getQuESTValidationEpsilon", TEST_CATEGORY ) {
561 SECTION( LABEL_CORRECTNESS ) {
564 for (
int i=0; i<3; i++)
567 GENERATE( range(0,10) );
576 SECTION( LABEL_VALIDATION ) {
587TEST_CASE(
"setQuESTValidationEpsilonToDefault", TEST_CATEGORY ) {
589 SECTION( LABEL_CORRECTNESS ) {
591 SECTION(
"always safe to call" ) {
593 for (
int i=0; i<3; i++)
597 SECTION(
"affects validation" ) {
617 SECTION(
"affects struct fields" ) {
619 SECTION(
"CompMatr" ) {
632 SECTION(
"DiagMatr" ) {
647 SECTION(
"FullStateDiagMatr" ) {
662 SECTION(
"KrausMap" ) {
675 SECTION( LABEL_VALIDATION ) {
677 SECTION( LABEL_VALIDATION ) {
689TEST_CASE(
"getQuESTGpuCacheSize", TEST_CATEGORY ) {
691 SECTION( LABEL_CORRECTNESS ) {
700 bool usingCuQuantum = std::string(envStr).find(
"cuQuantum=0") == std::string::npos;
703 if (
getQuESTEnv().isGpuAccelerated && !usingCuQuantum) {
708 for (qindex i=0; i<matr.
numRows; i++)
715 int targs[] = {0,1,2,3,4,5};
716 int ctrls[] = {6,7,8,9};
717 qindex cacheSize = 0;
719 for (
int numCtrls=4; numCtrls>=0; numCtrls--) {
728 CAPTURE( cacheSize, newSize );
729 REQUIRE( newSize >= cacheSize );
739 SECTION( LABEL_VALIDATION ) {
KrausMap createKrausMap(int numQubits, int numOperators)
void destroyKrausMap(KrausMap map)
qindex getQuESTGpuCacheSize()
void clearQuESTGpuCache()
void getQuESTEnvironmentString(char str[200])
void setQuESTReportedPauliChars(const char *paulis)
void setQuESTMaxNumReportedItems(qindex numRows, qindex numCols)
void setQuESTMaxNumReportedSigFigs(int numSigFigs)
void setQuESTReportedPauliStrStyle(int style)
void setQuESTNumReportedNewlines(int numNewlines)
void setQuESTSeeds(unsigned *seeds, int numSeeds)
void setQuESTSeedsToDefault()
void getQuESTSeeds(unsigned *seeds)
void setQuESTValidationOff()
void setQuESTValidationEpsilonToDefault()
qreal getQuESTValidationEpsilon()
void setQuESTValidationEpsilon(qreal eps)
void setQuESTValidationOn()
void initRandomPureState(Qureg qureg)
void initRandomMixedState(Qureg qureg, qindex numPureStates)
FullStateDiagMatr createFullStateDiagMatr(int numQubits)
CompMatr createCompMatr(int numQubits)
DiagMatr createDiagMatr(int numQubits)
void destroyDiagMatr(DiagMatr matrix)
void destroyFullStateDiagMatr(FullStateDiagMatr matrix)
void destroyCompMatr(CompMatr matrix)
static CompMatr1 getCompMatr1(qcomp **in)
void syncCompMatr(CompMatr matr)
void applyCompMatr1(Qureg qureg, int target, CompMatr1 matrix)
void applyMultiControlledCompMatr(Qureg qureg, int *controls, int numControls, int *targets, int numTargets, CompMatr matrix)
int applyQubitMeasurement(Qureg qureg, int target)
Qureg createCustomQureg(int numQubits, int isDensMatr, int useDistrib, int useGpuAccel, int useMultithread)
Qureg createQureg(int numQubits)
void destroyQureg(Qureg qureg)
qcomp getDensityQuregAmp(Qureg qureg, qindex row, qindex column)
qreal getRandomReal(qreal min, qreal maxExcl)
int getRandomInt(int min, int maxExcl)
void reportStr(const char *str)
static qcomp getQcomp(qreal re, qreal im)
void reportScalar(const char *label, qcomp num)
TEST_CASE("setQuESTInputErrorHandler", TEST_CATEGORY)