The Quantum Exact Simulation Toolkit v4.0.0
Loading...
Searching...
No Matches
cache.cpp
1/** @file
2 * Testing utilities which create Quregs across all
3 * available hardware deployments
4 *
5 * @author Tyson Jones
6 */
7
8#include "quest/include/quest.h"
9
10#include "macros.hpp"
11#include "qvector.hpp"
12#include "qmatrix.hpp"
13#include "macros.hpp"
14#include "linalg.hpp"
15#include "cache.hpp"
16
17#include <unordered_map>
18#include <tuple>
19#include <vector>
20#include <string>
21
22using std::tuple;
23using std::vector;
24using std::string;
25
26
27
28/*
29 * caches of Quregs and FullStateDiagMatr
30 * which persist between calls of get-cache()
31 */
32
33quregCache statevecs1;
34quregCache statevecs2;
35quregCache densmatrs1;
36quregCache densmatrs2;
37matrixCache matrices;
38
39
40
41/*
42 * while the number of qubits in the unit-test Quregs/matr
43 * is fixed, it is defined privately here (with internal
44 * linkage) so that it can be changed between compilations
45 * without having to recompiling the entire test suite
46 */
47
48static constexpr int NUM_QUBITS_IN_CACHE = 6;
49
50int getNumCachedQubits() {
51 return NUM_QUBITS_IN_CACHE;
52}
53
54
55
56/*
57 * deployments in cache
58 */
59
60deployInfo getSupportedDeployments() {
61
62 deployInfo out;
63
64 // determine which Qureg deployments are supported
65 QuESTEnv env = getQuESTEnv();
66 bool omp = env.isMultithreaded;
67 bool mpi = env.isDistributed;
68 bool gpu = env.isGpuAccelerated;
69
70 // return only the "most-accelerated" deployment, unless all are desired
71 bool one = ! TEST_ALL_DEPLOYMENTS;
72
73 // add only those supported to the output list, in order of preference.
74 // flag order is (MPI, GPU, OMP), matching createCustomQureg
75 if (gpu && omp && mpi) { out.push_back({"GPU + OMP + MPI", 1, 1, 1}); if (one) return out; }
76 if (gpu && mpi) { out.push_back({"GPU + MPI", 1, 1, 0}); if (one) return out; }
77 if (gpu && omp) { out.push_back({"GPU + OMP", 0, 1, 1}); if (one) return out; }
78 if (gpu) { out.push_back({"GPU", 0, 1, 0}); if (one) return out; }
79 if (mpi && omp) { out.push_back({"CPU + OMP + MPI", 1, 0, 1}); if (one) return out; }
80 if (mpi) { out.push_back({"CPU + MPI", 1, 0, 0}); if (one) return out; }
81 if (omp) { out.push_back({"CPU + OMP", 0, 0, 1}); if (one) return out; }
82 if (true) { out.push_back({"CPU", 0, 0, 0}); if (one) return out; }
83
84 // always contains CPU obviously, but this makes it explicit
85 DEMAND( !out.empty() );
86
87 // return all supported deployments
88 return out;
89}
90
91
92
93/*
94 * manage cached quregs
95 */
96
97quregCache createCachedStatevecsOrDensmatrs(bool isDensMatr) {
98
99 quregCache out;
100
101 // only add supported-deployment quregs to the cache
102 for (auto [label, mpi, gpu, omp] : getSupportedDeployments())
103 out[label] = createCustomQureg(NUM_QUBITS_IN_CACHE, isDensMatr, mpi, gpu, omp);
104
105 return out;
106}
107
108void createCachedQuregs() {
109
110 // must not be called twice nor pre-creation
111 DEMAND( statevecs1.empty() );
112 DEMAND( statevecs2.empty() );
113 DEMAND( densmatrs1.empty() );
114 DEMAND( densmatrs2.empty() );
115
116 statevecs1 = createCachedStatevecsOrDensmatrs(false);
117 statevecs2 = createCachedStatevecsOrDensmatrs(false);
118 densmatrs1 = createCachedStatevecsOrDensmatrs(true);
119 densmatrs2 = createCachedStatevecsOrDensmatrs(true);
120}
121
122void destroyCachedQuregs() {
123
124 // must not be called twice nor pre-creation
125 DEMAND( ! statevecs1.empty() );
126 DEMAND( ! statevecs2.empty() );
127 DEMAND( ! densmatrs1.empty() );
128 DEMAND( ! densmatrs2.empty() );
129
130 auto caches = {
131 statevecs1, statevecs2,
132 densmatrs1, densmatrs2};
133
134 for (auto& cache : caches)
135 for (auto& [label, qureg]: cache)
136 destroyQureg(qureg);
137
138 statevecs1.clear();
139 statevecs2.clear();
140 densmatrs1.clear();
141 densmatrs2.clear();
142}
143
144quregCache getCachedStatevecs() {
145
146 // must not be called pre-creation nor post-destruction
147 DEMAND( !statevecs1.empty() );
148
149 return statevecs1;
150}
151quregCache getCachedDensmatrs() {
152
153 // must not be called pre-creation nor post-destruction
154 DEMAND( !densmatrs1.empty() );
155
156 return densmatrs1;
157}
158
159quregCache getAltCachedStatevecs() {
160
161 // must not be called pre-creation nor post-destruction
162 DEMAND( !statevecs2.empty() );
163
164 return statevecs2;
165}
166quregCache getAltCachedDensmatrs() {
167
168 // must not be called pre-creation nor post-destruction
169 DEMAND( !densmatrs2.empty() );
170
171 return densmatrs2;
172}
173
174
175
176/*
177 * manage cached FullStateDiagMatr
178 */
179
180void createCachedFullStateDiagMatrs() {
181
182 // must not be called twice
183 DEMAND( matrices.empty() );
184
185 // only add supported-deployment matrices to the cache
186 for (auto [label, mpi, gpu, omp] : getSupportedDeployments())
187 matrices[label] = createCustomFullStateDiagMatr(NUM_QUBITS_IN_CACHE, mpi, gpu, omp);
188}
189
190void destroyCachedFullStateDiagMatrs() {
191
192 // must not be called twice
193 DEMAND( !matrices.empty() );
194
195 for (auto& [label, matrix]: matrices)
197
198 matrices.clear();
199}
200
201matrixCache getCachedFullStateDiagMatrs() {
202
203 // must not be called pre-creation nor post-destruction
204 DEMAND( !matrices.empty() );
205
206 return matrices;
207}
208
209
210/*
211 * reference states of equivalent
212 * dimension to the cached quregs
213 */
214
215qvector getRefStatevec() {
216 return getZeroVector(getPow2(NUM_QUBITS_IN_CACHE));
217}
218qmatrix getRefDensmatr() {
219 return getZeroMatrix(getPow2(NUM_QUBITS_IN_CACHE));
220}
QuESTEnv getQuESTEnv()
FullStateDiagMatr createCustomFullStateDiagMatr(int numQubits, int useDistrib, int useGpuAccel, int useMultithread)
Definition matrices.cpp:322
void destroyFullStateDiagMatr(FullStateDiagMatr matrix)
Definition matrices.cpp:404
Qureg createCustomQureg(int numQubits, int isDensMatr, int useDistrib, int useGpuAccel, int useMultithread)
Definition qureg.cpp:273
void destroyQureg(Qureg qureg)
Definition qureg.cpp:330
const int TEST_ALL_DEPLOYMENTS
Definition macros.hpp:60
qmatrix getZeroMatrix(size_t dim)
Definition qmatrix.cpp:18