10#include "quest/include/quest.h"
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/qvector.hpp"
17#include "tests/utils/qmatrix.hpp"
18#include "tests/utils/cache.hpp"
19#include "tests/utils/compare.hpp"
20#include "tests/utils/convert.hpp"
21#include "tests/utils/evolve.hpp"
22#include "tests/utils/linalg.hpp"
23#include "tests/utils/lists.hpp"
24#include "tests/utils/macros.hpp"
25#include "tests/utils/random.hpp"
30using Catch::Matchers::ContainsSubstring;
38#define TEST_CATEGORY \
39 LABEL_UNIT_TAG "[matrices]"
53 SECTION( LABEL_CORRECTNESS ) {
55 constexpr int dim = 2;
56 qmatrix ref = getRandomMatrix(dim);
58 SECTION( LABEL_C_INTERFACE ) {
61 qcomp arr[dim][dim] = {{ref[0][0], ref[0][1]}, {ref[1][0], ref[1][1]}};
65 qcomp** ptrs = (qcomp**) malloc(dim *
sizeof *ptrs);
66 for (
int i=0; i<dim; i++) {
67 ptrs[i] = (qcomp*) malloc(dim *
sizeof **ptrs);
68 for (
int j=0; j<dim; j++)
69 ptrs[i][j] = ref[i][j];
75 for (
int i=0; i<dim; i++)
80 for (
int i=0; i<dim; i++)
85 SECTION( LABEL_CPP_INTERFACE ) {
91 REQUIRE_AGREE(
getCompMatr1({{ref[0][0],ref[0][1]},{ref[1][0],ref[1][1]}}), ref );
95 SECTION( LABEL_VALIDATION ) {
97 SECTION(
"null pointer" ) {
99 qcomp** ptr =
nullptr;
100 REQUIRE_THROWS_WITH(
getCompMatr1(ptr), ContainsSubstring(
"was a null pointer") );
102 qcomp* arr[1] = {
nullptr};
103 REQUIRE_THROWS_WITH(
getCompMatr1(arr), ContainsSubstring(
"contained a null pointer") );
106 SECTION(
"invalid dimensions" ) {
110 REQUIRE_THROWS_WITH(
getCompMatr1({{0,0}}), ContainsSubstring(
"Incompatible number of rows") );
111 REQUIRE_THROWS_WITH(
getCompMatr1({{0,0},{0,0},{0,0}}), ContainsSubstring(
"Incompatible number of rows") );
113 REQUIRE_THROWS_WITH(
getCompMatr1({{0,0},{0}}), ContainsSubstring(
"incompatible number of elements") );
114 REQUIRE_THROWS_WITH(
getCompMatr1({{0,0},{0,0,0}}), ContainsSubstring(
"incompatible number of elements") );
120TEST_CASE(
"getCompMatr2", TEST_CATEGORY ) {
122 SECTION( LABEL_CORRECTNESS ) {
124 constexpr int dim = 4;
125 qmatrix ref = getRandomMatrix(dim);
127 SECTION( LABEL_C_INTERFACE ) {
131 for (
int i=0; i<dim; i++)
132 for (
int j=0; j<dim; j++)
133 arr[i][j] = ref[i][j];
137 qcomp** ptrs = (qcomp**) malloc(dim *
sizeof *ptrs);
138 for (
int i=0; i<dim; i++) {
139 ptrs[i] = (qcomp*) malloc(dim *
sizeof **ptrs);
140 for (
int j=0; j<dim; j++)
141 ptrs[i][j] = ref[i][j];
147 for (
int i=0; i<dim; i++)
152 for (
int i=0; i<dim; i++)
157 SECTION( LABEL_CPP_INTERFACE ) {
165 {ref[0][0],ref[0][1],ref[0][2],ref[0][3]},
166 {ref[1][0],ref[1][1],ref[1][2],ref[1][3]},
167 {ref[2][0],ref[2][1],ref[2][2],ref[2][3]},
168 {ref[3][0],ref[3][1],ref[3][2],ref[3][3]}
175 SECTION( LABEL_VALIDATION ) {
177 SECTION(
"null pointer" ) {
179 qcomp** ptr =
nullptr;
180 REQUIRE_THROWS_WITH(
getCompMatr2(ptr), ContainsSubstring(
"was a null pointer") );
182 qcomp* arr[1] = {
nullptr};
183 REQUIRE_THROWS_WITH(
getCompMatr2(arr), ContainsSubstring(
"contained a null pointer") );
186 SECTION(
"invalid dimensions" ) {
190 qmatrix bad1 = {{0,0,0,0}};
191 qmatrix bad2 = {{0,0,0,0},{0,0,0,0},{0,0,0,0}};
193 REQUIRE_THROWS_WITH(
getCompMatr2(bad1), ContainsSubstring(
"Incompatible number of rows") );
194 REQUIRE_THROWS_WITH(
getCompMatr2(bad2), ContainsSubstring(
"Incompatible number of rows") );
196 REQUIRE_THROWS_WITH(
getCompMatr2({{0,0,0,0},{0},{0},{0}}), ContainsSubstring(
"incompatible number of elements") );
197 REQUIRE_THROWS_WITH(
getCompMatr2({{0,0,0,0,0},{0},{0},{0}}), ContainsSubstring(
"incompatible number of elements") );
203TEST_CASE(
"getDiagMatr1", TEST_CATEGORY ) {
205 SECTION( LABEL_CORRECTNESS ) {
207 constexpr int dim = 2;
208 qmatrix ref = getRandomDiagonalMatrix(dim);
210 SECTION( LABEL_C_INTERFACE ) {
213 qcomp arr[dim] = {ref[0][0], ref[1][1] };
217 qvector diags = getDiagonals(ref);
221 SECTION( LABEL_CPP_INTERFACE ) {
224 REQUIRE_AGREE(
getDiagMatr1({ref[0][0],ref[1][1]}), ref );
228 SECTION( LABEL_VALIDATION ) {
230 SECTION(
"null pointer" ) {
232 qcomp* ptr =
nullptr;
233 REQUIRE_THROWS_WITH(
getDiagMatr1(ptr), ContainsSubstring(
"was a null pointer") );
236 SECTION(
"invalid dimensions" ) {
240 qvector bad2 = {0,0,0};
242 REQUIRE_THROWS_WITH(
getDiagMatr1(bad1), ContainsSubstring(
"Incompatible number of elements") );
243 REQUIRE_THROWS_WITH(
getDiagMatr1(bad2), ContainsSubstring(
"Incompatible number of elements") );
249TEST_CASE(
"getDiagMatr2", TEST_CATEGORY ) {
251 SECTION( LABEL_CORRECTNESS ) {
253 constexpr int dim = 4;
254 qmatrix ref = getRandomDiagonalMatrix(dim);
256 SECTION( LABEL_C_INTERFACE ) {
259 qcomp arr[dim] = {ref[0][0], ref[1][1], ref[2][2], ref[3][3] };
263 qvector diags = getDiagonals(ref);
267 SECTION( LABEL_CPP_INTERFACE ) {
270 REQUIRE_AGREE(
getDiagMatr2({ref[0][0], ref[1][1], ref[2][2], ref[3][3] }), ref );
274 SECTION( LABEL_VALIDATION ) {
276 SECTION(
"null pointer" ) {
278 qcomp* ptr =
nullptr;
279 REQUIRE_THROWS_WITH(
getDiagMatr2(ptr), ContainsSubstring(
"was a null pointer") );
282 SECTION(
"invalid dimensions" ) {
285 qvector bad1 = {0,0,0};
286 qvector bad2 = {0,0,0,0,0};
288 REQUIRE_THROWS_WITH(
getDiagMatr2(bad1), ContainsSubstring(
"Incompatible number of elements") );
289 REQUIRE_THROWS_WITH(
getDiagMatr2(bad2), ContainsSubstring(
"Incompatible number of elements") );
295TEST_CASE(
"getInlineCompMatr1", TEST_CATEGORY ) {
297 SECTION( LABEL_CORRECTNESS ) {
299 qmatrix ref = {{1,2},{3_i,4_i}};
304 SECTION( LABEL_VALIDATION ) {
306 SECTION(
"invalid dimensions" ) {
310 REQUIRE_THROWS_WITH(
getInlineCompMatr1({{0,0}}), ContainsSubstring(
"Incompatible number of rows") );
311 REQUIRE_THROWS_WITH(
getInlineCompMatr1({{0,0},{0,0},{0,0}}), ContainsSubstring(
"Incompatible number of rows") );
313 REQUIRE_THROWS_WITH(
getInlineCompMatr1({{0,0},{0}}), ContainsSubstring(
"incompatible number of elements") );
314 REQUIRE_THROWS_WITH(
getInlineCompMatr1({{0,0},{0,0,0}}), ContainsSubstring(
"incompatible number of elements") );
320TEST_CASE(
"getInlineCompMatr2", TEST_CATEGORY ) {
322 SECTION( LABEL_CORRECTNESS ) {
328 {-1_i,-2_i,-3_i,-4_i}};
335 {-1_i,-2_i,-3_i,-4_i}
339 SECTION( LABEL_VALIDATION ) {
341 SECTION(
"invalid dimensions" ) {
345 REQUIRE_THROWS_WITH(
getInlineCompMatr2({{0,0,0,0}}), ContainsSubstring(
"Incompatible number of rows") );
346 REQUIRE_THROWS_WITH(
getInlineCompMatr2({{0,0,0,0},{0,0,0,0},{0,0,0,0}}), ContainsSubstring(
"Incompatible number of rows") );
348 REQUIRE_THROWS_WITH(
getInlineCompMatr2({{0,0,0,0},{0},{0},{0}}), ContainsSubstring(
"incompatible number of elements") );
349 REQUIRE_THROWS_WITH(
getInlineCompMatr2({{0,0,0,0,0},{0},{0},{0}}), ContainsSubstring(
"incompatible number of elements") );
355TEST_CASE(
"getInlineDiagMatr1", TEST_CATEGORY ) {
357 SECTION( LABEL_CORRECTNESS ) {
359 qmatrix ref = getDiagonalMatrix({1,2_i});
364 SECTION( LABEL_VALIDATION ) {
366 SECTION(
"invalid dimensions" ) {
368 REQUIRE_THROWS_WITH(
getInlineDiagMatr1({0,0,0}), ContainsSubstring(
"Incompatible number of elements") );
374TEST_CASE(
"getInlineDiagMatr2", TEST_CATEGORY ) {
376 SECTION( LABEL_CORRECTNESS ) {
378 qmatrix ref = getDiagonalMatrix({1,2_i,-3,-4_i});
383 SECTION( LABEL_VALIDATION ) {
385 SECTION(
"invalid dimensions" ) {
387 REQUIRE_THROWS_WITH(
getInlineDiagMatr2({0,0,0}), ContainsSubstring(
"Incompatible number of elements") );
388 REQUIRE_THROWS_WITH(
getInlineDiagMatr2({0,0,0,0,0}), ContainsSubstring(
"Incompatible number of elements") );
394TEST_CASE(
"createCompMatr", TEST_CATEGORY ) {
396 SECTION( LABEL_CORRECTNESS ) {
400 int maxNumQubits = getNumCachedQubits();
401 int numQubits = GENERATE_COPY( range(1, maxNumQubits+1) );
402 CAPTURE( numQubits );
408 REQUIRE_AGREE( matr, blank );
411 REQUIRE( matr.numQubits == numQubits );
412 REQUIRE( matr.numRows == getPow2(numQubits) );
415 REQUIRE( *(matr.isApproxUnitary) == -1 );
416 REQUIRE( *(matr.isApproxHermitian) == -1 );
420 REQUIRE( matr.cpuElems !=
nullptr );
421 REQUIRE( matr.cpuElemsFlat !=
nullptr );
424 REQUIRE( matr.gpuElemsFlat !=
nullptr );
426 REQUIRE( matr.gpuElemsFlat ==
nullptr );
431 SECTION( LABEL_VALIDATION ) {
433 SECTION(
"env not initialised" ) {
439 SECTION(
"too few qubits" ) {
441 int numQubits = GENERATE( -1, 0 );
443 REQUIRE_THROWS_WITH(
createCompMatr(numQubits), ContainsSubstring(
"must target one or more qubits") );
446 SECTION(
"too many qubits" ) {
449 REQUIRE_THROWS_WITH(
createCompMatr(50), ContainsSubstring(
"maximum which can be addressed by the qindex type") );
452 REQUIRE_THROWS_WITH(
createCompMatr(31), ContainsSubstring(
"necessary memory would overflow size_t") );
458 #ifndef SANITIZER_IS_ACTIVE
459 REQUIRE_THROWS_WITH(
createCompMatr(25), ContainsSubstring(
"failed") || ContainsSubstring(
"insufficient available memory") || ContainsSubstring(
"available GPU memory") );
466TEST_CASE(
"createDiagMatr", TEST_CATEGORY ) {
468 SECTION( LABEL_CORRECTNESS ) {
472 int maxNumQubits = getNumCachedQubits();
473 int numQubits = GENERATE_COPY( range(1, maxNumQubits+1) );
474 CAPTURE( numQubits );
480 REQUIRE_AGREE( matr, blank );
483 REQUIRE( matr.numQubits == numQubits );
484 REQUIRE( matr.numElems == getPow2(numQubits) );
487 REQUIRE( *(matr.isApproxUnitary) == -1 );
488 REQUIRE( *(matr.isApproxHermitian) == -1 );
494 REQUIRE( matr.cpuElems !=
nullptr );
497 REQUIRE( matr.gpuElems !=
nullptr );
499 REQUIRE( matr.gpuElems ==
nullptr );
504 SECTION( LABEL_VALIDATION ) {
506 SECTION(
"env not initialised" ) {
512 SECTION(
"too few qubits" ) {
514 int numQubits = GENERATE( -1, 0 );
516 REQUIRE_THROWS_WITH(
createDiagMatr(numQubits), ContainsSubstring(
"must target one or more qubits") );
519 SECTION(
"too many qubits" ) {
522 REQUIRE_THROWS_WITH(
createDiagMatr(100), ContainsSubstring(
"maximum which can be addressed by the qindex type") );
525 REQUIRE_THROWS_WITH(
createDiagMatr(62), ContainsSubstring(
"necessary memory would overflow size_t") );
531 #ifndef SANITIZER_IS_ACTIVE
532 REQUIRE_THROWS_WITH(
createDiagMatr(50), ContainsSubstring(
"failed") || ContainsSubstring(
"insufficient available memory") || ContainsSubstring(
"available GPU memory") );
539TEST_CASE(
"createFullStateDiagMatr", TEST_CATEGORY ) {
541 SECTION( LABEL_CORRECTNESS ) {
543 int numQubits = GENERATE_COPY( range(1, 20) );
544 CAPTURE( numQubits );
551 for (qindex i=0; i<matr.numElemsPerNode && isBlank; i++)
552 isBlank = (matr.cpuElems[i] == qcomp(0,0));
556 REQUIRE( matr.numQubits == numQubits );
557 REQUIRE( matr.numElems == getPow2(numQubits) );
558 REQUIRE( matr.numElemsPerNode == matr.numElems / (matr.isDistributed?
getQuESTEnv().numNodes : 1) );
561 REQUIRE( *(matr.isApproxUnitary) == -1 );
562 REQUIRE( *(matr.isApproxHermitian) == -1 );
563 REQUIRE( *(matr.isApproxNonZero) == -1 );
564 REQUIRE( *(matr.isStrictlyNonNegative) == -1 );
565 REQUIRE( *(matr.wasGpuSynced) == 0 );
568 REQUIRE( matr.cpuElems !=
nullptr );
570 int gpu = matr.isGpuAccelerated;
571 if (gpu) REQUIRE( matr.gpuElems !=
nullptr );
572 else REQUIRE( matr.gpuElems ==
nullptr );
577 SECTION( LABEL_VALIDATION ) {
579 SECTION(
"env not initialised" ) {
585 SECTION(
"too few qubits" ) {
587 int numQubits = GENERATE( -1, 0 );
589 REQUIRE_THROWS_WITH(
createFullStateDiagMatr(numQubits), ContainsSubstring(
"must target one or more qubits") );
592 SECTION(
"too many qubits" ) {
595 REQUIRE_THROWS_WITH(
createFullStateDiagMatr(100), ContainsSubstring(
"maximum which can be addressed by the qindex type") );
604 #ifndef SANITIZER_IS_ACTIVE
605 REQUIRE_THROWS_WITH(
createFullStateDiagMatr(50), ContainsSubstring(
"failed") || ContainsSubstring(
"insufficient available memory") || ContainsSubstring(
"available GPU memory") );
615TEST_CASE(
"createCustomFullStateDiagMatr", TEST_CATEGORY ) {
617 SECTION( LABEL_CORRECTNESS ) {
619 for (
auto [label, mpi, gpu, omp] : getSupportedDeployments()) {
621 DYNAMIC_SECTION( label ) {
623 int minNumQubits = std::max({1, getLog2(
getQuESTEnv().numNodes)});
624 int maxNumQubits = std::min({20, minNumQubits + 1});
625 int numQubits = GENERATE_COPY( range(minNumQubits, maxNumQubits+1) );
626 CAPTURE( numQubits );
632 for (qindex i=0; i<matr.numElemsPerNode && isBlank; i++)
633 isBlank = (matr.cpuElems[i] == qcomp(0,0));
637 REQUIRE( matr.numQubits == numQubits );
638 REQUIRE( matr.numElems == getPow2(numQubits) );
639 REQUIRE( matr.numElemsPerNode == matr.numElems / (mpi?
getQuESTEnv().numNodes : 1) );
642 REQUIRE( matr.isDistributed == (
int) (mpi &&
getQuESTEnv().numNodes > 1) );
643 REQUIRE( matr.isGpuAccelerated == (
int) gpu );
644 REQUIRE( matr.isMultithreaded == (
int) omp );
647 REQUIRE( *(matr.isApproxUnitary) == -1 );
648 REQUIRE( *(matr.isApproxHermitian) == -1 );
649 REQUIRE( *(matr.isApproxNonZero) == -1 );
650 REQUIRE( *(matr.isStrictlyNonNegative) == -1 );
651 REQUIRE( *(matr.wasGpuSynced) == 0 );
654 REQUIRE( matr.cpuElems !=
nullptr );
656 if (gpu) REQUIRE( matr.gpuElems !=
nullptr );
657 else REQUIRE( matr.gpuElems ==
nullptr );
665 SECTION( LABEL_VALIDATION ) {
667 SECTION(
"env not initialised" ) {
673 SECTION(
"too few qubits" ) {
675 int numQubits = GENERATE( -1, 0 );
677 for (
auto [label, mpi, gpu, omp] : getSupportedDeployments())
681 int minNumQubits = getLog2(
getQuESTEnv().numNodes);
686 SECTION(
"too many qubits" ) {
688 for (
auto [label, mpi, gpu, omp] : getSupportedDeployments()) {
691 REQUIRE_THROWS_WITH(
createCustomFullStateDiagMatr(100, mpi,gpu,omp), ContainsSubstring(
"maximum which can be addressed by the qindex type") );
700 #ifndef SANITIZER_IS_ACTIVE
701 REQUIRE_THROWS_WITH(
createCustomFullStateDiagMatr(50, mpi,gpu,omp), ContainsSubstring(
"failed") || ContainsSubstring(
"insufficient available memory") || ContainsSubstring(
"available GPU memory") );
706 SECTION(
"unsupported deployments" ) {
721TEST_CASE(
"destroyCompMatr", TEST_CATEGORY ) {
723 SECTION( LABEL_CORRECTNESS ) {
729 SECTION( LABEL_VALIDATION ) {
732 #ifndef SANITIZER_IS_ACTIVE
733 SECTION(
"not created" ) {
736 REQUIRE_THROWS_WITH(
destroyCompMatr(m), ContainsSubstring(
"Invalid CompMatr") && ContainsSubstring(
"not created") );
743TEST_CASE(
"destroyDiagMatr", TEST_CATEGORY ) {
745 SECTION( LABEL_CORRECTNESS ) {
751 SECTION( LABEL_VALIDATION ) {
754 #ifndef SANITIZER_IS_ACTIVE
755 SECTION(
"not created" ) {
758 REQUIRE_THROWS_WITH(
destroyDiagMatr(m), ContainsSubstring(
"Invalid DiagMatr") && ContainsSubstring(
"not created") );
765TEST_CASE(
"destroyFullStateDiagMatr", TEST_CATEGORY ) {
767 SECTION( LABEL_CORRECTNESS ) {
773 SECTION( LABEL_VALIDATION ) {
776 #ifndef SANITIZER_IS_ACTIVE
777 SECTION(
"not created" ) {
787TEST_CASE(
"syncCompMatr", TEST_CATEGORY ) {
789 SECTION( LABEL_CORRECTNESS ) {
795 SECTION(
"overwrites GPU elements" ) {
805 SECTION(
"sets was-synced flag" ) {
813 SECTION(
"clears numerical flags" ) {
815 *(matr).isApproxHermitian = 1;
816 *(matr).isApproxUnitary = 0;
819 REQUIRE( *(matr.isApproxHermitian) == -1 );
820 REQUIRE( *(matr.isApproxUnitary) == -1 );
826 SECTION( LABEL_VALIDATION ) {
829 #ifndef SANITIZER_IS_ACTIVE
830 SECTION(
"not created" ) {
833 REQUIRE_THROWS_WITH(
syncCompMatr(m), ContainsSubstring(
"Invalid CompMatr") && ContainsSubstring(
"not created") );
840TEST_CASE(
"syncDiagMatr", TEST_CATEGORY ) {
842 SECTION( LABEL_CORRECTNESS ) {
847 SECTION(
"overwrites GPU elements" ) {
857 SECTION(
"sets was-synced flag" ) {
865 SECTION(
"clears numerical flags" ) {
867 *(matr).isApproxHermitian = 1;
868 *(matr).isApproxUnitary = 0;
869 *(matr).isApproxNonZero = 1;
870 *(matr).isStrictlyNonNegative = 0;
873 REQUIRE( *(matr.isApproxHermitian) == -1 );
874 REQUIRE( *(matr.isApproxUnitary) == -1 );
882 SECTION( LABEL_VALIDATION ) {
885 #ifndef SANITIZER_IS_ACTIVE
886 SECTION(
"not created" ) {
889 REQUIRE_THROWS_WITH(
syncDiagMatr(m), ContainsSubstring(
"Invalid DiagMatr") && ContainsSubstring(
"not created") );
896TEST_CASE(
"syncFullStateDiagMatr", TEST_CATEGORY ) {
898 SECTION( LABEL_CORRECTNESS ) {
900 for (
auto& [label, matrix]: getCachedFullStateDiagMatrs()) {
902 DYNAMIC_SECTION( label ) {
904 *(matrix.wasGpuSynced) = 0;
905 *(matrix).isApproxHermitian = 1;
906 *(matrix).isApproxUnitary = 0;
907 *(matrix).isApproxNonZero = 1;
908 *(matrix).isStrictlyNonNegative = 0;
911 REQUIRE( *(matrix.wasGpuSynced) == 1 );
913 REQUIRE( *(matrix.isApproxHermitian) == -1 );
914 REQUIRE( *(matrix.isApproxUnitary) == -1 );
915 REQUIRE( *(matrix.isApproxNonZero) == -1 );
916 REQUIRE( *(matrix.isStrictlyNonNegative) == -1 );
926 SECTION( LABEL_VALIDATION ) {
929 #ifndef SANITIZER_IS_ACTIVE
930 SECTION(
"not created" ) {
942 SECTION( LABEL_CORRECTNESS ) {
944 int numQubits = GENERATE( range(1,6) );
945 CAPTURE( numQubits );
950 int dim = getPow2(numQubits);
951 qmatrix ref = getRandomMatrix(dim);
953 SECTION( LABEL_C_INTERFACE ) {
956 qcomp** ptrs = (qcomp**) malloc(dim *
sizeof *ptrs);
957 for (
int i=0; i<dim; i++) {
958 ptrs[i] = (qcomp*) malloc(dim *
sizeof **ptrs);
959 for (
int j=0; j<dim; j++)
960 ptrs[i][j] = ref[i][j];
963 REQUIRE_AGREE( matr, ref );
969 for (
int i=0; i<dim; i++)
974 SECTION( LABEL_CPP_INTERFACE ) {
979 REQUIRE_AGREE( matr, ref );
986 SECTION( LABEL_VALIDATION ) {
993 #ifndef SANITIZER_IS_ACTIVE
994 SECTION(
"not created" ) {
998 REQUIRE_THROWS_WITH(
setCompMatr(bad, dummy), ContainsSubstring(
"Invalid CompMatr") && ContainsSubstring(
"not created") );
1003 SECTION(
"null pointer" ) {
1005 qcomp** ptr =
nullptr;
1006 REQUIRE_THROWS_WITH(
setCompMatr(matr, ptr), ContainsSubstring(
"was a null pointer") );
1008 qcomp* arr[1] = {
nullptr};
1009 REQUIRE_THROWS_WITH(
setCompMatr(matr, arr), ContainsSubstring(
"contained a null pointer") );
1012 SECTION(
"invalid dimensions" ) {
1016 REQUIRE_NOTHROW(
setCompMatr(matr, {{1,2},{3,4}}) );
1018 REQUIRE_THROWS_WITH(
setCompMatr(matr,{{1,2}}), ContainsSubstring(
"Incompatible number of rows") );
1019 REQUIRE_THROWS_WITH(
setCompMatr(matr, {{1,2},{3,4},{5,6}}), ContainsSubstring(
"Incompatible number of rows") );
1021 REQUIRE_THROWS_WITH(
setCompMatr(matr, {{0,0},{0}}), ContainsSubstring(
"incompatible number of elements") );
1022 REQUIRE_THROWS_WITH(
setCompMatr(matr, {{0,0},{0,0,0}}), ContainsSubstring(
"incompatible number of elements") );
1030TEST_CASE(
"setDiagMatr", TEST_CATEGORY ) {
1032 SECTION( LABEL_CORRECTNESS ) {
1034 int numQubits = GENERATE( range(1,6) );
1035 CAPTURE( numQubits );
1040 int dim = getPow2(numQubits);
1041 qmatrix ref = getRandomDiagonalMatrix(dim);
1043 SECTION( LABEL_C_INTERFACE ) {
1046 qvector diags = getDiagonals(ref);
1048 REQUIRE_AGREE( matr, ref );
1052 SECTION( LABEL_CPP_INTERFACE ) {
1057 REQUIRE_AGREE( matr, ref );
1064 SECTION( LABEL_VALIDATION ) {
1069 #ifndef SANITIZER_IS_ACTIVE
1070 SECTION(
"not created" ) {
1074 REQUIRE_THROWS_WITH(
setDiagMatr(bad, dummy), ContainsSubstring(
"Invalid DiagMatr") && ContainsSubstring(
"not created") );
1078 SECTION(
"null pointer" ) {
1080 qcomp* ptr =
nullptr;
1081 REQUIRE_THROWS_WITH(
setDiagMatr(matr, ptr), ContainsSubstring(
"was a null pointer") );
1084 SECTION(
"invalid dimensions" ) {
1090 REQUIRE_THROWS_WITH(
setDiagMatr(matr, {1}), ContainsSubstring(
"Incompatible number of elements") );
1091 REQUIRE_THROWS_WITH(
setDiagMatr(matr, {1,2,3}), ContainsSubstring(
"Incompatible number of elements") );
1101 SECTION( LABEL_CORRECTNESS ) {
1107 REQUIRE_AGREE( matr, {{1,2},{3,4}} );
1113 SECTION( LABEL_VALIDATION ) {
1120 #ifndef SANITIZER_IS_ACTIVE
1121 SECTION(
"not created" ) {
1124 REQUIRE_THROWS_WITH(
setInlineCompMatr(bad, 1, {{1,2},{3,4}}), ContainsSubstring(
"Invalid CompMatr") && ContainsSubstring(
"not created") );
1129 SECTION(
"mismatching dimension" ) {
1131 REQUIRE_THROWS_WITH(
setInlineCompMatr(matr, 2, {{1,2},{3,4}}), ContainsSubstring(
"declared number of qubits") && ContainsSubstring(
"differs") );
1134 SECTION(
"invalid dimensions" ) {
1140 REQUIRE_THROWS_WITH(
setInlineCompMatr(matr, 1, {{1,2}}), ContainsSubstring(
"Incompatible number of rows") );
1141 REQUIRE_THROWS_WITH(
setInlineCompMatr(matr, 1, {{1,2},{3,4},{5,6}}), ContainsSubstring(
"Incompatible number of rows") );
1143 REQUIRE_THROWS_WITH(
setInlineCompMatr(matr, 1, {{1},{2}}), ContainsSubstring(
"One or more rows contained an incompatible number of elements") );
1144 REQUIRE_THROWS_WITH(
setInlineCompMatr(matr, 1, {{1,2,3},{4,5,6}}), ContainsSubstring(
"One or more rows contained an incompatible number of elements") );
1154 SECTION( LABEL_CORRECTNESS ) {
1160 REQUIRE_AGREE( matr, {{1,0},{0,2_i}} );
1166 SECTION( LABEL_VALIDATION ) {
1173 #ifndef SANITIZER_IS_ACTIVE
1174 SECTION(
"not created" ) {
1177 REQUIRE_THROWS_WITH(
setInlineDiagMatr(bad, 1, {1,2}), ContainsSubstring(
"Invalid DiagMatr") && ContainsSubstring(
"not created") );
1182 SECTION(
"mismatching dimension" ) {
1184 REQUIRE_THROWS_WITH(
setInlineDiagMatr(matr, 2, {1,2}), ContainsSubstring(
"declared number of qubits") && ContainsSubstring(
"differs") );
1187 SECTION(
"invalid dimensions" ) {
1193 REQUIRE_THROWS_WITH(
setInlineDiagMatr(matr, 1, {1}), ContainsSubstring(
"Incompatible number of elements") );
1194 REQUIRE_THROWS_WITH(
setInlineDiagMatr(matr, 1, {1,2,3}), ContainsSubstring(
"Incompatible number of elements") );
1202TEST_CASE(
"createInlineCompMatr", TEST_CATEGORY ) {
1204 SECTION( LABEL_CORRECTNESS ) {
1208 REQUIRE_AGREE( matr, {{1,2},{3,4}} );
1214 SECTION( LABEL_VALIDATION ) {
1216 SECTION(
"env not initialised" ) {
1222 SECTION(
"too few qubits" ) {
1224 int numQubits = GENERATE( -1, 0 );
1226 REQUIRE_THROWS_WITH(
createInlineCompMatr(numQubits, {{1}}), ContainsSubstring(
"must target one or more qubits") );
1229 SECTION(
"mismatching dimension" ) {
1231 REQUIRE_THROWS_WITH(
createInlineCompMatr(2, {{1,2},{3,4}}), ContainsSubstring(
"Incompatible number of rows") );
1237TEST_CASE(
"createInlineDiagMatr", TEST_CATEGORY ) {
1239 SECTION( LABEL_CORRECTNESS ) {
1243 REQUIRE_AGREE( matr, {{1,0},{0,2_i}} );
1249 SECTION( LABEL_VALIDATION ) {
1251 SECTION(
"env not initialised" ) {
1257 SECTION(
"too few qubits" ) {
1259 int numQubits = GENERATE( -1, 0 );
1261 REQUIRE_THROWS_WITH(
createInlineDiagMatr(numQubits, {1}), ContainsSubstring(
"must target one or more qubits") );
1264 SECTION(
"mismatching dimension" ) {
1266 REQUIRE_THROWS_WITH(
createInlineDiagMatr(2, {1,2}), ContainsSubstring(
"Incompatible number of elements") );
FullStateDiagMatr createCustomFullStateDiagMatr(int numQubits, int useDistrib, int useGpuAccel, int useMultithread)
DiagMatr createInlineDiagMatr(int numQb, std::vector< qcomp > elems)
FullStateDiagMatr createFullStateDiagMatr(int numQubits)
CompMatr createInlineCompMatr(int numQb, std::vector< std::vector< qcomp > > elems)
CompMatr createCompMatr(int numQubits)
DiagMatr createDiagMatr(int numQubits)
FullStateDiagMatr createFullStateDiagMatrFromPauliStrSum(PauliStrSum in)
void destroyDiagMatr(DiagMatr matrix)
void destroyFullStateDiagMatr(FullStateDiagMatr matrix)
void destroyCompMatr(CompMatr matrix)
static CompMatr2 getCompMatr2(qcomp **in)
static CompMatr1 getCompMatr1(qcomp **in)
static DiagMatr2 getDiagMatr2(qcomp *in)
CompMatr1 getInlineCompMatr1({{ matrix }})
CompMatr2 getInlineCompMatr2({{ matrix }})
DiagMatr2 getInlineDiagMatr2({ list })
static DiagMatr1 getDiagMatr1(qcomp *in)
DiagMatr1 getInlineDiagMatr1({ list })
void reportCompMatr2(CompMatr2 matrix)
void reportDiagMatr1(DiagMatr1 matrix)
void reportCompMatr(CompMatr matrix)
void reportDiagMatr2(DiagMatr2 matrix)
void reportCompMatr1(CompMatr1 matrix)
void reportDiagMatr(DiagMatr matrix)
void reportFullStateDiagMatr(FullStateDiagMatr matr)
void setInlineDiagMatr(DiagMatr matr, int numQb, std::vector< qcomp > in)
void setFullStateDiagMatrFromMultiDimLists(FullStateDiagMatr out, void *lists, int *numQubitsPerDim, int numDims)
void setDiagMatrFromMultiDimLists(DiagMatr out, void *lists, int *numQubitsPerDim, int numDims)
void setFullStateDiagMatrFromMultiVarFunc(FullStateDiagMatr out, qcomp(*func)(qindex *), int *numQubitsPerVar, int numVars, int areSigned)
void setInlineCompMatr(CompMatr matr, int numQb, std::vector< std::vector< qcomp > > in)
void setDiagMatr(DiagMatr out, qcomp *in)
void setCompMatr(CompMatr matr, qcomp **vals)
void setFullStateDiagMatr(FullStateDiagMatr out, qindex startInd, qcomp *in, qindex numElems)
void setDiagMatrFromMultiVarFunc(DiagMatr out, qcomp(*func)(qindex *), int *numQubitsPerVar, int numVars, int areSigned)
void setFullStateDiagMatrFromPauliStrSum(FullStateDiagMatr out, PauliStrSum in)
void setInlineFullStateDiagMatr(FullStateDiagMatr matr, qindex startInd, qindex numElems, std::vector< qcomp > in)
void syncFullStateDiagMatr(FullStateDiagMatr matr)
void syncDiagMatr(DiagMatr matr)
void syncCompMatr(CompMatr matr)
qmatrix getZeroMatrix(size_t dim)
TEST_CASE("getCompMatr1", TEST_CATEGORY)
int * isStrictlyNonNegative