The Quantum Exact Simulation Toolkit v4.2.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.h"
53#include "tests/utils/config.hpp"
54#include "tests/utils/cache.hpp"
55#include "tests/utils/macros.hpp"
56#include "tests/utils/random.hpp"
57
58
59/*
60 * recast QuEST errors into exceptions which Catch2 can intercept
61 */
62
63/// @private
64extern "C" void validationErrorHandler(const char* errFunc, const char* errMsg) {
65
66 throw std::runtime_error(std::string(errFunc) + ": " + std::string(errMsg));
67}
68
69
70/*
71 * report QuEST env when tests start
72 */
73
74/// @private
75class startListener : public Catch::EventListenerBase {
76public:
77 using Catch::EventListenerBase::EventListenerBase;
78 void testRunStarting(Catch::TestRunInfo const&) override {
79
80 /// @todo
81 /// near term: add #threads (needs to be added to QuESTEnv
82 /// we hook into private QuEST functions)
83 /// far term: replace with something less hideous?
84
85 // a full report is too verbose...
86 // reportQuESTEnv();
87
88 // so we summarise the important info
89 QuESTEnv env = getQuESTEnv();
90 std::cout << std::endl;
91 std::cout << "QuEST execution environment:" << std::endl;
92 std::cout << " precision: " << FLOAT_PRECISION << std::endl;
93 std::cout << " multithreaded: " << env.isMultithreaded << std::endl;
94 std::cout << " distributed: " << env.isDistributed << std::endl;
95 std::cout << " GPU-accelerated: " << env.isGpuAccelerated << std::endl;
96 std::cout << " GPU-sharing ok: " << env.isGpuSharingEnabled << std::endl;
97 std::cout << " cuQuantum: " << env.isCuQuantumEnabled << std::endl;
98 std::cout << " num nodes: " << env.numNodes << std::endl;
99 std::cout << std::endl;
100
101 std::cout << "Testing configuration:" << std::endl;
102 std::cout << " test all deployments: " << getWhetherToTestAllDeployments() << std::endl;
103 std::cout << " num qubits in qureg: " << getNumCachedQubits() << std::endl;
104 std::cout << " max num qubit perms: " << getMaxNumTestedQubitPermutations() << std::endl;
105 std::cout << " max num superop targs: " << getMaxNumTestedSuperoperatorTargets() << std::endl;
106 std::cout << " num mixed-deploy reps: " << getNumTestedMixedDeploymentRepetitions() << std::endl;
107 std::cout << std::endl;
108
109 std::cout << "Tested Qureg deployments:" << std::endl;
110 for (auto& [label, qureg]: getCachedStatevecs())
111 std::cout << " " << label << std::endl;
112 std::cout << std::endl;
113 }
114};
115
116CATCH_REGISTER_LISTENER(startListener)
117
118
119/*
120 * setup QuEST before Catch2 session
121 */
122
123int main(int argc, char* argv[]) {
124
125 // prepare QuEST before anything else, since many
126 // testing utility functions repurpose QuEST ones
127 initQuESTEnv();
128 setInputErrorHandler(validationErrorHandler);
129
130 // ensure RNG consensus among all nodes
131 setRandomTestStateSeeds();
132
133 // prepare persistent Quregs
134 createCachedQuregs();
135 createCachedFullStateDiagMatrs();
136
137 // disable Catch2 output on non-root nodes
138 if (getQuESTEnv().rank != 0)
139 std::cout.rdbuf(nullptr);
140
141 // launch Catch2, triggering above event listener
142 int result = Catch::Session().run( argc, argv );
143
144 destroyCachedFullStateDiagMatrs();
145 destroyCachedQuregs();
147 return result;
148}
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:100