The Quantum Exact Simulation Toolkit v4.0.0
Loading...
Searching...
No Matches
main.cpp
1/** @file
2 * Entry-point for all unit and integration tests.
3 *
4 * @author Tyson Jones
5 *
6 * @defgroup tests 🧪 Tests
7 *
8 * @defgroup testutils Utilities
9 * @ingroup tests
10 * @brief
11 * Testing utilities which include un-optimised, reference
12 * implementations of common quantum simulation routines
13 * using serial linear algebra.
14 *
15 * @defgroup unittests Unit tests
16 * @ingroup tests
17 * @brief
18 * Tests of each QuEST API function in isolation for all
19 * possible input states and parameters (where feasible),
20 * validated against numerical reference implementations
21 * using relatively small Quregs.
22 *
23 * @defgroup integrationtests Integration tests
24 * @ingroup tests
25 * @brief
26 * Tests which combine many QuEST API functions to perform
27 * computations using relatively large Quregs, validated
28 * against known analytic results.
29 *
30 * @defgroup deprecatedtests Deprecated tests
31 * @ingroup tests
32 * @deprecated
33 * @brief
34 * Unit tests of QuEST's deprecated v3 API functions.
35 *
36 * @defgroup deprecatedutils Deprecated utilities
37 * @ingroup tests
38 * @deprecated
39 * @brief
40 * Utilities for testing QuEST's deprecated v3 API functions.
41 */
42
43#include <catch2/catch_session.hpp>
44#include <catch2/catch_test_macros.hpp>
45#include <catch2/reporters/catch_reporter_event_listener.hpp>
46#include <catch2/reporters/catch_reporter_registrars.hpp>
47
48#include <stdexcept>
49#include <iostream>
50#include <string>
51
52#include "quest/include/quest.h"
53#include "tests/utils/cache.hpp"
54#include "tests/utils/macros.hpp"
55#include "tests/utils/random.hpp"
56
57
58/*
59 * recast QuEST errors into exceptions which Catch2 can intercept
60 */
61
62/// @private
63extern "C" void validationErrorHandler(const char* errFunc, const char* errMsg) {
64
65 throw std::runtime_error(std::string(errFunc) + ": " + std::string(errMsg));
66}
67
68
69/*
70 * report QuEST env when tests start
71 */
72
73/// @private
74class startListener : public Catch::EventListenerBase {
75public:
76 using Catch::EventListenerBase::EventListenerBase;
77 void testRunStarting(Catch::TestRunInfo const&) override {
78
79 /// @todo
80 /// near term: add #threads (needs to be added to QuESTEnv
81 /// we hook into private QuEST functions)
82 /// far term: replace with something less hideous?
83
84 // a full report is too verbose...
85 // reportQuESTEnv();
86
87 // so we summarise the important info
88 QuESTEnv env = getQuESTEnv();
89 std::cout << std::endl;
90 std::cout << "QuEST execution environment:" << std::endl;
91 std::cout << " precision: " << FLOAT_PRECISION << std::endl;
92 std::cout << " multithreaded: " << env.isMultithreaded << std::endl;
93 std::cout << " distributed: " << env.isDistributed << std::endl;
94 std::cout << " GPU-accelerated: " << env.isGpuAccelerated << std::endl;
95 std::cout << " cuQuantum: " << (env.isGpuAccelerated && COMPILE_CUQUANTUM) << std::endl;
96 std::cout << " num nodes: " << env.numNodes << std::endl;
97 std::cout << " num qubits: " << getNumCachedQubits() << std::endl;
98 std::cout << " num qubit perms: " << TEST_MAX_NUM_QUBIT_PERMUTATIONS << std::endl;
99 std::cout << std::endl;
100
101 std::cout << "Tested Qureg deployments:" << std::endl;
102 for (auto& [label, qureg]: getCachedStatevecs())
103 std::cout << " " << label << std::endl;
104 std::cout << std::endl;
105 }
106};
107
108CATCH_REGISTER_LISTENER(startListener)
109
110
111/*
112 * setup QuEST before Catch2 session
113 */
114
115int main(int argc, char* argv[]) {
116
117 // prepare QuEST before anything else, since many
118 // testing utility functions repurpose QuEST ones
119 initQuESTEnv();
120 setInputErrorHandler(validationErrorHandler);
121
122 // ensure RNG consensus among all nodes
123 setRandomTestStateSeeds();
124
125 // prepare persistent Quregs
126 createCachedQuregs();
127 createCachedFullStateDiagMatrs();
128
129 // disable Catch2 output on non-root nodes
130 if (getQuESTEnv().rank != 0)
131 std::cout.rdbuf(nullptr);
132
133 // launch Catch2, triggering above event listener
134 int result = Catch::Session().run( argc, argv );
135
136 destroyCachedFullStateDiagMatrs();
137 destroyCachedQuregs();
139 return result;
140}
void setInputErrorHandler(void(*callback)(const char *func, const char *msg))
Definition debug.cpp:74
finalizeQuESTEnv()
QuESTEnv getQuESTEnv()
void initQuESTEnv()
const int FLOAT_PRECISION
Definition precision.h:103
const int TEST_MAX_NUM_QUBIT_PERMUTATIONS
Definition macros.hpp:54