Maestro 0.3.1
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
DistributedGpuLibrary.h
Go to the documentation of this file.
1// Dynamic loader for the local distributed statevector plugin.
2// The C ABI boundary is covered by the Maestro Plugin Linking Exception.
3#pragma once
4#ifdef __linux__
5#include "../Utils/Library.h"
6#include "DistributedGpuApi.h"
7#include <atomic>
8#include <cstdlib>
9#include <memory>
10#include <mutex>
11#include <stdexcept>
12#include <string>
13
14namespace Simulators {
15class DistributedGpuLibrary : public Utils::Library {
16 public:
17 // Storage lives in the core library, including for hidden-visibility callers.
18 static std::shared_ptr<DistributedGpuLibrary> GetInstance();
19 ~DistributedGpuLibrary() override {
20 if (context && FreeLib) FreeLib();
21 }
22 bool Load() {
23 std::lock_guard<std::recursive_mutex> lock(mutex);
24 if (loaded) return true;
25 const char* path = std::getenv(mpi ? "MAESTRO_DIST_MPI_GPU_LIBRARY"
26 : "MAESTRO_DIST_GPU_LIBRARY");
27 if (!path || !*path)
28 path = mpi ? "libmaestro_gpu_distributed_mpi.so"
29 : "libmaestro_gpu_distributed.so";
30 SetMute(true);
31 if (!Utils::Library::Init(path)) return false;
32 // Resolve every required entry before permitting any state construction.
33 SetGpuDevice = reinterpret_cast<DistributedGpuApi::SetGpuDeviceFn>(
34 GetFunction("SetGpuDevice"));
35 if (!SetGpuDevice)
36 throw std::runtime_error("Distributed GPU plugin missing SetGpuDevice");
37 GetGpuDeviceCount =
38 reinterpret_cast<DistributedGpuApi::GetGpuDeviceCountFn>(
39 GetFunction("GetGpuDeviceCount"));
40 if (!GetGpuDeviceCount)
41 throw std::runtime_error(
42 "Distributed GPU plugin missing GetGpuDeviceCount");
43 ValidateLicense = reinterpret_cast<DistributedGpuApi::ValidateLicenseFn>(
44 GetFunction("ValidateLicense"));
45 if (!ValidateLicense)
46 throw std::runtime_error(
47 "Distributed GPU plugin missing ValidateLicense");
48 CheckLicense = reinterpret_cast<DistributedGpuApi::CheckLicenseFn>(
49 GetFunction("CheckLicense"));
50 if (!CheckLicense)
51 throw std::runtime_error("Distributed GPU plugin missing CheckLicense");
52 InitLib =
53 reinterpret_cast<DistributedGpuApi::InitLibFn>(GetFunction("InitLib"));
54 if (!InitLib)
55 throw std::runtime_error("Distributed GPU plugin missing InitLib");
56 FreeLib =
57 reinterpret_cast<DistributedGpuApi::FreeLibFn>(GetFunction("FreeLib"));
58 if (!FreeLib)
59 throw std::runtime_error("Distributed GPU plugin missing FreeLib");
60 CreateStateVector =
61 reinterpret_cast<DistributedGpuApi::CreateStateVectorFn>(
62 GetFunction("CreateStateVector"));
63 if (!CreateStateVector)
64 throw std::runtime_error(
65 "Distributed GPU plugin missing CreateStateVector");
66 CreateStateVectorWithBackend =
67 reinterpret_cast<DistributedGpuApi::CreateStateVectorWithBackendFn>(
68 GetFunction("CreateStateVectorWithBackend"));
69 if (!CreateStateVectorWithBackend)
70 throw std::runtime_error(
71 "Distributed GPU plugin missing CreateStateVectorWithBackend");
72 GetBackend = reinterpret_cast<DistributedGpuApi::GetBackendFn>(
73 GetFunction("GetBackend"));
74 if (!GetBackend)
75 throw std::runtime_error("Distributed GPU plugin missing GetBackend");
76 SetExExecutionConfig =
77 reinterpret_cast<DistributedGpuApi::SetExExecutionConfigFn>(
78 GetFunction("SetExExecutionConfig"));
79 if (!SetExExecutionConfig)
80 throw std::runtime_error(
81 "Distributed GPU plugin missing SetExExecutionConfig");
82 DestroyStateVector =
83 reinterpret_cast<DistributedGpuApi::DestroyStateVectorFn>(
84 GetFunction("DestroyStateVector"));
85 if (!DestroyStateVector)
86 throw std::runtime_error(
87 "Distributed GPU plugin missing DestroyStateVector");
88 SetDataType = reinterpret_cast<DistributedGpuApi::SetDataTypeFn>(
89 GetFunction("SetDataType"));
90 if (!SetDataType)
91 throw std::runtime_error("Distributed GPU plugin missing SetDataType");
92 SetSeed =
93 reinterpret_cast<DistributedGpuApi::SetSeedFn>(GetFunction("SetSeed"));
94 if (!SetSeed)
95 throw std::runtime_error("Distributed GPU plugin missing SetSeed");
96 IsDoublePrecision =
97 reinterpret_cast<DistributedGpuApi::IsDoublePrecisionFn>(
98 GetFunction("IsDoublePrecision"));
99 if (!IsDoublePrecision)
100 throw std::runtime_error(
101 "Distributed GPU plugin missing IsDoublePrecision");
102 GetNrQubits = reinterpret_cast<DistributedGpuApi::GetNrQubitsFn>(
103 GetFunction("GetNrQubits"));
104 if (!GetNrQubits)
105 throw std::runtime_error("Distributed GPU plugin missing GetNrQubits");
106 GetStateVectorGpuId =
107 reinterpret_cast<DistributedGpuApi::GetStateVectorGpuIdFn>(
108 GetFunction("GetStateVectorGpuId"));
109 if (!GetStateVectorGpuId)
110 throw std::runtime_error(
111 "Distributed GPU plugin missing GetStateVectorGpuId");
112 Create =
113 reinterpret_cast<DistributedGpuApi::CreateFn>(GetFunction("Create"));
114 if (!Create)
115 throw std::runtime_error("Distributed GPU plugin missing Create");
116 CreateWithState = reinterpret_cast<DistributedGpuApi::CreateWithStateFn>(
117 GetFunction("CreateWithState"));
118 if (!CreateWithState)
119 throw std::runtime_error(
120 "Distributed GPU plugin missing CreateWithState");
121 Reset = reinterpret_cast<DistributedGpuApi::ResetFn>(GetFunction("Reset"));
122 if (!Reset)
123 throw std::runtime_error("Distributed GPU plugin missing Reset");
124 MeasureQubitCollapse =
125 reinterpret_cast<DistributedGpuApi::MeasureQubitCollapseFn>(
126 GetFunction("MeasureQubitCollapse"));
127 if (!MeasureQubitCollapse)
128 throw std::runtime_error(
129 "Distributed GPU plugin missing MeasureQubitCollapse");
130 MeasureQubitNoCollapse =
131 reinterpret_cast<DistributedGpuApi::MeasureQubitNoCollapseFn>(
132 GetFunction("MeasureQubitNoCollapse"));
133 if (!MeasureQubitNoCollapse)
134 throw std::runtime_error(
135 "Distributed GPU plugin missing MeasureQubitNoCollapse");
136 MeasureQubitsCollapse =
137 reinterpret_cast<DistributedGpuApi::MeasureQubitsCollapseFn>(
138 GetFunction("MeasureQubitsCollapse"));
139 if (!MeasureQubitsCollapse)
140 throw std::runtime_error(
141 "Distributed GPU plugin missing MeasureQubitsCollapse");
142 MeasureQubitsNoCollapse =
143 reinterpret_cast<DistributedGpuApi::MeasureQubitsNoCollapseFn>(
144 GetFunction("MeasureQubitsNoCollapse"));
145 if (!MeasureQubitsNoCollapse)
146 throw std::runtime_error(
147 "Distributed GPU plugin missing MeasureQubitsNoCollapse");
148 MeasureAllQubitsCollapse =
149 reinterpret_cast<DistributedGpuApi::MeasureAllQubitsCollapseFn>(
150 GetFunction("MeasureAllQubitsCollapse"));
151 if (!MeasureAllQubitsCollapse)
152 throw std::runtime_error(
153 "Distributed GPU plugin missing MeasureAllQubitsCollapse");
154 MeasureAllQubitsNoCollapse =
155 reinterpret_cast<DistributedGpuApi::MeasureAllQubitsNoCollapseFn>(
156 GetFunction("MeasureAllQubitsNoCollapse"));
157 if (!MeasureAllQubitsNoCollapse)
158 throw std::runtime_error(
159 "Distributed GPU plugin missing MeasureAllQubitsNoCollapse");
160 SaveState = reinterpret_cast<DistributedGpuApi::SaveStateFn>(
161 GetFunction("SaveState"));
162 if (!SaveState)
163 throw std::runtime_error("Distributed GPU plugin missing SaveState");
164 SaveStateToHost = reinterpret_cast<DistributedGpuApi::SaveStateToHostFn>(
165 GetFunction("SaveStateToHost"));
166 if (!SaveStateToHost)
167 throw std::runtime_error(
168 "Distributed GPU plugin missing SaveStateToHost");
169 SaveStateDestructive =
170 reinterpret_cast<DistributedGpuApi::SaveStateDestructiveFn>(
171 GetFunction("SaveStateDestructive"));
172 if (!SaveStateDestructive)
173 throw std::runtime_error(
174 "Distributed GPU plugin missing SaveStateDestructive");
175 RestoreStateFreeSaved =
176 reinterpret_cast<DistributedGpuApi::RestoreStateFreeSavedFn>(
177 GetFunction("RestoreStateFreeSaved"));
178 if (!RestoreStateFreeSaved)
179 throw std::runtime_error(
180 "Distributed GPU plugin missing RestoreStateFreeSaved");
181 RestoreStateNoFreeSaved =
182 reinterpret_cast<DistributedGpuApi::RestoreStateNoFreeSavedFn>(
183 GetFunction("RestoreStateNoFreeSaved"));
184 if (!RestoreStateNoFreeSaved)
185 throw std::runtime_error(
186 "Distributed GPU plugin missing RestoreStateNoFreeSaved");
187 FreeSavedState = reinterpret_cast<DistributedGpuApi::FreeSavedStateFn>(
188 GetFunction("FreeSavedState"));
189 if (!FreeSavedState)
190 throw std::runtime_error("Distributed GPU plugin missing FreeSavedState");
191 Clone = reinterpret_cast<DistributedGpuApi::CloneFn>(GetFunction("Clone"));
192 if (!Clone)
193 throw std::runtime_error("Distributed GPU plugin missing Clone");
194 Sample =
195 reinterpret_cast<DistributedGpuApi::SampleFn>(GetFunction("Sample"));
196 if (!Sample)
197 throw std::runtime_error("Distributed GPU plugin missing Sample");
198 SampleAll = reinterpret_cast<DistributedGpuApi::SampleAllFn>(
199 GetFunction("SampleAll"));
200 if (!SampleAll)
201 throw std::runtime_error("Distributed GPU plugin missing SampleAll");
202 Amplitude = reinterpret_cast<DistributedGpuApi::AmplitudeFn>(
203 GetFunction("Amplitude"));
204 if (!Amplitude)
205 throw std::runtime_error("Distributed GPU plugin missing Amplitude");
206 Probability = reinterpret_cast<DistributedGpuApi::ProbabilityFn>(
207 GetFunction("Probability"));
208 if (!Probability)
209 throw std::runtime_error("Distributed GPU plugin missing Probability");
210 BasisStateProbability =
211 reinterpret_cast<DistributedGpuApi::BasisStateProbabilityFn>(
212 GetFunction("BasisStateProbability"));
213 if (!BasisStateProbability)
214 throw std::runtime_error(
215 "Distributed GPU plugin missing BasisStateProbability");
216 AllProbabilities = reinterpret_cast<DistributedGpuApi::AllProbabilitiesFn>(
217 GetFunction("AllProbabilities"));
218 if (!AllProbabilities)
219 throw std::runtime_error(
220 "Distributed GPU plugin missing AllProbabilities");
221 ExpectationValue = reinterpret_cast<DistributedGpuApi::ExpectationValueFn>(
222 GetFunction("ExpectationValue"));
223 if (!ExpectationValue)
224 throw std::runtime_error(
225 "Distributed GPU plugin missing ExpectationValue");
226 ApplyX =
227 reinterpret_cast<DistributedGpuApi::ApplyXFn>(GetFunction("ApplyX"));
228 if (!ApplyX)
229 throw std::runtime_error("Distributed GPU plugin missing ApplyX");
230 ApplyY =
231 reinterpret_cast<DistributedGpuApi::ApplyYFn>(GetFunction("ApplyY"));
232 if (!ApplyY)
233 throw std::runtime_error("Distributed GPU plugin missing ApplyY");
234 ApplyZ =
235 reinterpret_cast<DistributedGpuApi::ApplyZFn>(GetFunction("ApplyZ"));
236 if (!ApplyZ)
237 throw std::runtime_error("Distributed GPU plugin missing ApplyZ");
238 ApplyH =
239 reinterpret_cast<DistributedGpuApi::ApplyHFn>(GetFunction("ApplyH"));
240 if (!ApplyH)
241 throw std::runtime_error("Distributed GPU plugin missing ApplyH");
242 ApplyS =
243 reinterpret_cast<DistributedGpuApi::ApplySFn>(GetFunction("ApplyS"));
244 if (!ApplyS)
245 throw std::runtime_error("Distributed GPU plugin missing ApplyS");
246 ApplySDG = reinterpret_cast<DistributedGpuApi::ApplySDGFn>(
247 GetFunction("ApplySDG"));
248 if (!ApplySDG)
249 throw std::runtime_error("Distributed GPU plugin missing ApplySDG");
250 ApplyT =
251 reinterpret_cast<DistributedGpuApi::ApplyTFn>(GetFunction("ApplyT"));
252 if (!ApplyT)
253 throw std::runtime_error("Distributed GPU plugin missing ApplyT");
254 ApplyTDG = reinterpret_cast<DistributedGpuApi::ApplyTDGFn>(
255 GetFunction("ApplyTDG"));
256 if (!ApplyTDG)
257 throw std::runtime_error("Distributed GPU plugin missing ApplyTDG");
258 ApplySX =
259 reinterpret_cast<DistributedGpuApi::ApplySXFn>(GetFunction("ApplySX"));
260 if (!ApplySX)
261 throw std::runtime_error("Distributed GPU plugin missing ApplySX");
262 ApplySXDG = reinterpret_cast<DistributedGpuApi::ApplySXDGFn>(
263 GetFunction("ApplySXDG"));
264 if (!ApplySXDG)
265 throw std::runtime_error("Distributed GPU plugin missing ApplySXDG");
266 ApplyK =
267 reinterpret_cast<DistributedGpuApi::ApplyKFn>(GetFunction("ApplyK"));
268 if (!ApplyK)
269 throw std::runtime_error("Distributed GPU plugin missing ApplyK");
270 ApplyP =
271 reinterpret_cast<DistributedGpuApi::ApplyPFn>(GetFunction("ApplyP"));
272 if (!ApplyP)
273 throw std::runtime_error("Distributed GPU plugin missing ApplyP");
274 ApplyRx =
275 reinterpret_cast<DistributedGpuApi::ApplyRxFn>(GetFunction("ApplyRx"));
276 if (!ApplyRx)
277 throw std::runtime_error("Distributed GPU plugin missing ApplyRx");
278 ApplyRy =
279 reinterpret_cast<DistributedGpuApi::ApplyRyFn>(GetFunction("ApplyRy"));
280 if (!ApplyRy)
281 throw std::runtime_error("Distributed GPU plugin missing ApplyRy");
282 ApplyRz =
283 reinterpret_cast<DistributedGpuApi::ApplyRzFn>(GetFunction("ApplyRz"));
284 if (!ApplyRz)
285 throw std::runtime_error("Distributed GPU plugin missing ApplyRz");
286 ApplyU =
287 reinterpret_cast<DistributedGpuApi::ApplyUFn>(GetFunction("ApplyU"));
288 if (!ApplyU)
289 throw std::runtime_error("Distributed GPU plugin missing ApplyU");
290 ApplyCX =
291 reinterpret_cast<DistributedGpuApi::ApplyCXFn>(GetFunction("ApplyCX"));
292 if (!ApplyCX)
293 throw std::runtime_error("Distributed GPU plugin missing ApplyCX");
294 ApplyCY =
295 reinterpret_cast<DistributedGpuApi::ApplyCYFn>(GetFunction("ApplyCY"));
296 if (!ApplyCY)
297 throw std::runtime_error("Distributed GPU plugin missing ApplyCY");
298 ApplyCZ =
299 reinterpret_cast<DistributedGpuApi::ApplyCZFn>(GetFunction("ApplyCZ"));
300 if (!ApplyCZ)
301 throw std::runtime_error("Distributed GPU plugin missing ApplyCZ");
302 ApplyCH =
303 reinterpret_cast<DistributedGpuApi::ApplyCHFn>(GetFunction("ApplyCH"));
304 if (!ApplyCH)
305 throw std::runtime_error("Distributed GPU plugin missing ApplyCH");
306 ApplyCSX = reinterpret_cast<DistributedGpuApi::ApplyCSXFn>(
307 GetFunction("ApplyCSX"));
308 if (!ApplyCSX)
309 throw std::runtime_error("Distributed GPU plugin missing ApplyCSX");
310 ApplyCSXDG = reinterpret_cast<DistributedGpuApi::ApplyCSXDGFn>(
311 GetFunction("ApplyCSXDG"));
312 if (!ApplyCSXDG)
313 throw std::runtime_error("Distributed GPU plugin missing ApplyCSXDG");
314 ApplyCP =
315 reinterpret_cast<DistributedGpuApi::ApplyCPFn>(GetFunction("ApplyCP"));
316 if (!ApplyCP)
317 throw std::runtime_error("Distributed GPU plugin missing ApplyCP");
318 ApplyCRx = reinterpret_cast<DistributedGpuApi::ApplyCRxFn>(
319 GetFunction("ApplyCRx"));
320 if (!ApplyCRx)
321 throw std::runtime_error("Distributed GPU plugin missing ApplyCRx");
322 ApplyCRy = reinterpret_cast<DistributedGpuApi::ApplyCRyFn>(
323 GetFunction("ApplyCRy"));
324 if (!ApplyCRy)
325 throw std::runtime_error("Distributed GPU plugin missing ApplyCRy");
326 ApplyCRz = reinterpret_cast<DistributedGpuApi::ApplyCRzFn>(
327 GetFunction("ApplyCRz"));
328 if (!ApplyCRz)
329 throw std::runtime_error("Distributed GPU plugin missing ApplyCRz");
330 ApplyCCX = reinterpret_cast<DistributedGpuApi::ApplyCCXFn>(
331 GetFunction("ApplyCCX"));
332 if (!ApplyCCX)
333 throw std::runtime_error("Distributed GPU plugin missing ApplyCCX");
334 ApplySwap = reinterpret_cast<DistributedGpuApi::ApplySwapFn>(
335 GetFunction("ApplySwap"));
336 if (!ApplySwap)
337 throw std::runtime_error("Distributed GPU plugin missing ApplySwap");
338 ApplyCSwap = reinterpret_cast<DistributedGpuApi::ApplyCSwapFn>(
339 GetFunction("ApplyCSwap"));
340 if (!ApplyCSwap)
341 throw std::runtime_error("Distributed GPU plugin missing ApplyCSwap");
342 ApplyCU =
343 reinterpret_cast<DistributedGpuApi::ApplyCUFn>(GetFunction("ApplyCU"));
344 if (!ApplyCU)
345 throw std::runtime_error("Distributed GPU plugin missing ApplyCU");
346 GetApiVersion = reinterpret_cast<DistributedGpuApi::GetApiVersionFn>(
347 GetFunction("GetApiVersion"));
348 if (!GetApiVersion)
349 throw std::runtime_error("Distributed GPU plugin missing GetApiVersion");
350 GetCapabilities = reinterpret_cast<DistributedGpuApi::GetCapabilitiesFn>(
351 GetFunction("GetCapabilities"));
352 if (!GetCapabilities)
353 throw std::runtime_error(
354 "Distributed GPU plugin missing GetCapabilities");
355 GetLastError = reinterpret_cast<DistributedGpuApi::GetLastErrorFn>(
356 GetFunction("GetLastError"));
357 if (!GetLastError)
358 throw std::runtime_error("Distributed GPU plugin missing GetLastError");
359 ConfigureDistribution =
360 reinterpret_cast<DistributedGpuApi::ConfigureDistributionFn>(
361 GetFunction("ConfigureDistribution"));
362 if (!ConfigureDistribution)
363 throw std::runtime_error(
364 "Distributed GPU plugin missing ConfigureDistribution");
365 GetShardDevices = reinterpret_cast<DistributedGpuApi::GetShardDevicesFn>(
366 GetFunction("GetShardDevices"));
367 if (!GetShardDevices)
368 throw std::runtime_error(
369 "Distributed GPU plugin missing GetShardDevices");
370 GetGlobalQubits = reinterpret_cast<DistributedGpuApi::GetGlobalQubitsFn>(
371 GetFunction("GetGlobalQubits"));
372 if (!GetGlobalQubits)
373 throw std::runtime_error(
374 "Distributed GPU plugin missing GetGlobalQubits");
375 GetQubitLayout = reinterpret_cast<DistributedGpuApi::GetQubitLayoutFn>(
376 GetFunction("GetQubitLayout"));
377 if (!GetQubitLayout)
378 throw std::runtime_error("Distributed GPU plugin missing GetQubitLayout");
379 Redistribute = reinterpret_cast<DistributedGpuApi::RedistributeFn>(
380 GetFunction("Redistribute"));
381 if (!Redistribute)
382 throw std::runtime_error("Distributed GPU plugin missing Redistribute");
383 SwapGlobalLocalQubits =
384 reinterpret_cast<DistributedGpuApi::SwapGlobalLocalQubitsFn>(
385 GetFunction("SwapGlobalLocalQubits"));
386 if (!SwapGlobalLocalQubits)
387 throw std::runtime_error(
388 "Distributed GPU plugin missing SwapGlobalLocalQubits");
389 Synchronize = reinterpret_cast<DistributedGpuApi::SynchronizeFn>(
390 GetFunction("Synchronize"));
391 if (!Synchronize)
392 throw std::runtime_error("Distributed GPU plugin missing Synchronize");
393 GetStateRange = reinterpret_cast<DistributedGpuApi::GetStateRangeFn>(
394 GetFunction("GetStateRange"));
395 if (!GetStateRange)
396 throw std::runtime_error("Distributed GPU plugin missing GetStateRange");
397 SetStateRange = reinterpret_cast<DistributedGpuApi::SetStateRangeFn>(
398 GetFunction("SetStateRange"));
399 if (!SetStateRange)
400 throw std::runtime_error("Distributed GPU plugin missing SetStateRange");
401 GetLocalStateBounds =
402 reinterpret_cast<DistributedGpuApi::GetLocalStateBoundsFn>(
403 GetFunction("GetLocalStateBounds"));
404 if (!GetLocalStateBounds)
405 throw std::runtime_error(
406 "Distributed GPU plugin missing GetLocalStateBounds");
407 GetLocalStateRange =
408 reinterpret_cast<DistributedGpuApi::GetLocalStateRangeFn>(
409 GetFunction("GetLocalStateRange"));
410 if (!GetLocalStateRange)
411 throw std::runtime_error(
412 "Distributed GPU plugin missing GetLocalStateRange");
413 SetLocalStateRange =
414 reinterpret_cast<DistributedGpuApi::SetLocalStateRangeFn>(
415 GetFunction("SetLocalStateRange"));
416 if (!SetLocalStateRange)
417 throw std::runtime_error(
418 "Distributed GPU plugin missing SetLocalStateRange");
419 CreateWithBasisState =
420 reinterpret_cast<DistributedGpuApi::CreateWithBasisStateFn>(
421 GetFunction("CreateWithBasisState"));
422 if (!CreateWithBasisState)
423 throw std::runtime_error(
424 "Distributed GPU plugin missing CreateWithBasisState");
425 ApplyOneQubitMatrix =
426 reinterpret_cast<DistributedGpuApi::ApplyOneQubitMatrixFn>(
427 GetFunction("ApplyOneQubitMatrix"));
428 if (!ApplyOneQubitMatrix)
429 throw std::runtime_error(
430 "Distributed GPU plugin missing ApplyOneQubitMatrix");
431 ApplyOneQubitMatrixWithLayout =
432 reinterpret_cast<DistributedGpuApi::ApplyOneQubitMatrixWithLayoutFn>(
433 GetFunction("ApplyOneQubitMatrixWithLayout"));
434 if (!ApplyOneQubitMatrixWithLayout)
435 throw std::runtime_error(
436 "Distributed GPU plugin missing ApplyOneQubitMatrixWithLayout");
437 ApplyTwoQubitMatrix =
438 reinterpret_cast<DistributedGpuApi::ApplyTwoQubitMatrixFn>(
439 GetFunction("ApplyTwoQubitMatrix"));
440 if (!ApplyTwoQubitMatrix)
441 throw std::runtime_error(
442 "Distributed GPU plugin missing ApplyTwoQubitMatrix");
443 ApplyTwoQubitMatrixWithLayout =
444 reinterpret_cast<DistributedGpuApi::ApplyTwoQubitMatrixWithLayoutFn>(
445 GetFunction("ApplyTwoQubitMatrixWithLayout"));
446 if (!ApplyTwoQubitMatrixWithLayout)
447 throw std::runtime_error(
448 "Distributed GPU plugin missing ApplyTwoQubitMatrixWithLayout");
449 if (GetApiVersion() != 1)
450 throw std::runtime_error("Unsupported distributed GPU API version");
451 loaded = true;
452 return true;
453 }
454 virtual void* CreateNative(int device, int backend) {
455 std::lock_guard<std::recursive_mutex> lock(mutex);
456 RequireLoaded();
457 if (!context) {
458 Check(ValidateLicense(std::getenv("MAESTRO_LICENSE_KEY")),
459 "ValidateLicense");
460 context = InitLib();
461 if (!context) Fail("InitLib");
462 }
463 Check(SetGpuDevice(device), "SetGpuDevice");
464 auto obj = CreateStateVectorWithBackend(context, backend);
465 if (!obj) Fail("CreateStateVectorWithBackend");
466 ++liveStates;
467 return obj;
468 }
469 void DestroyNative(void* obj) noexcept {
470 if (obj) {
471 DestroyStateVector(obj);
472 --liveStates;
473 }
474 }
475 void* CloneNative(void* obj) {
476 auto copy = Clone(obj);
477 if (!copy) Fail("Clone");
478 ++liveStates;
479 return copy;
480 }
481 void RequireLoaded() {
482 if (!Load())
483 throw std::runtime_error(
484 "Unable to load distributed GPU plugin; set MAESTRO_DIST_GPU_LIBRARY "
485 "or MAESTRO_DIST_MPI_GPU_LIBRARY");
486 }
487 [[noreturn]] void Fail(const char* operation) const {
488 const char* error = GetLastError ? GetLastError() : nullptr;
489 throw std::runtime_error(std::string("Distributed GPU ") + operation +
490 ": " +
491 (error && *error ? error : "operation failed"));
492 }
493 void Check(int status, const char* operation) const {
494 if (status != 1) Fail(operation);
495 }
496 DistributedGpuApi::SetGpuDeviceFn SetGpuDevice = nullptr;
497 DistributedGpuApi::GetGpuDeviceCountFn GetGpuDeviceCount = nullptr;
498 DistributedGpuApi::ValidateLicenseFn ValidateLicense = nullptr;
499 DistributedGpuApi::CheckLicenseFn CheckLicense = nullptr;
500 DistributedGpuApi::InitLibFn InitLib = nullptr;
501 DistributedGpuApi::FreeLibFn FreeLib = nullptr;
502 DistributedGpuApi::CreateStateVectorFn CreateStateVector = nullptr;
503 DistributedGpuApi::CreateStateVectorWithBackendFn
504 CreateStateVectorWithBackend = nullptr;
505 DistributedGpuApi::GetBackendFn GetBackend = nullptr;
506 DistributedGpuApi::SetExExecutionConfigFn SetExExecutionConfig = nullptr;
507 DistributedGpuApi::DestroyStateVectorFn DestroyStateVector = nullptr;
508 DistributedGpuApi::SetDataTypeFn SetDataType = nullptr;
509 DistributedGpuApi::SetSeedFn SetSeed = nullptr;
510 DistributedGpuApi::IsDoublePrecisionFn IsDoublePrecision = nullptr;
511 DistributedGpuApi::GetNrQubitsFn GetNrQubits = nullptr;
512 DistributedGpuApi::GetStateVectorGpuIdFn GetStateVectorGpuId = nullptr;
513 DistributedGpuApi::CreateFn Create = nullptr;
514 DistributedGpuApi::CreateWithStateFn CreateWithState = nullptr;
515 DistributedGpuApi::ResetFn Reset = nullptr;
516 DistributedGpuApi::MeasureQubitCollapseFn MeasureQubitCollapse = nullptr;
517 DistributedGpuApi::MeasureQubitNoCollapseFn MeasureQubitNoCollapse = nullptr;
518 DistributedGpuApi::MeasureQubitsCollapseFn MeasureQubitsCollapse = nullptr;
519 DistributedGpuApi::MeasureQubitsNoCollapseFn MeasureQubitsNoCollapse =
520 nullptr;
521 DistributedGpuApi::MeasureAllQubitsCollapseFn MeasureAllQubitsCollapse =
522 nullptr;
523 DistributedGpuApi::MeasureAllQubitsNoCollapseFn MeasureAllQubitsNoCollapse =
524 nullptr;
525 DistributedGpuApi::SaveStateFn SaveState = nullptr;
526 DistributedGpuApi::SaveStateToHostFn SaveStateToHost = nullptr;
527 DistributedGpuApi::SaveStateDestructiveFn SaveStateDestructive = nullptr;
528 DistributedGpuApi::RestoreStateFreeSavedFn RestoreStateFreeSaved = nullptr;
529 DistributedGpuApi::RestoreStateNoFreeSavedFn RestoreStateNoFreeSaved =
530 nullptr;
531 DistributedGpuApi::FreeSavedStateFn FreeSavedState = nullptr;
532 DistributedGpuApi::CloneFn Clone = nullptr;
533 DistributedGpuApi::SampleFn Sample = nullptr;
534 DistributedGpuApi::SampleAllFn SampleAll = nullptr;
535 DistributedGpuApi::AmplitudeFn Amplitude = nullptr;
536 DistributedGpuApi::ProbabilityFn Probability = nullptr;
537 DistributedGpuApi::BasisStateProbabilityFn BasisStateProbability = nullptr;
538 DistributedGpuApi::AllProbabilitiesFn AllProbabilities = nullptr;
539 DistributedGpuApi::ExpectationValueFn ExpectationValue = nullptr;
540 DistributedGpuApi::ApplyXFn ApplyX = nullptr;
541 DistributedGpuApi::ApplyYFn ApplyY = nullptr;
542 DistributedGpuApi::ApplyZFn ApplyZ = nullptr;
543 DistributedGpuApi::ApplyHFn ApplyH = nullptr;
544 DistributedGpuApi::ApplySFn ApplyS = nullptr;
545 DistributedGpuApi::ApplySDGFn ApplySDG = nullptr;
546 DistributedGpuApi::ApplyTFn ApplyT = nullptr;
547 DistributedGpuApi::ApplyTDGFn ApplyTDG = nullptr;
548 DistributedGpuApi::ApplySXFn ApplySX = nullptr;
549 DistributedGpuApi::ApplySXDGFn ApplySXDG = nullptr;
550 DistributedGpuApi::ApplyKFn ApplyK = nullptr;
551 DistributedGpuApi::ApplyPFn ApplyP = nullptr;
552 DistributedGpuApi::ApplyRxFn ApplyRx = nullptr;
553 DistributedGpuApi::ApplyRyFn ApplyRy = nullptr;
554 DistributedGpuApi::ApplyRzFn ApplyRz = nullptr;
555 DistributedGpuApi::ApplyUFn ApplyU = nullptr;
556 DistributedGpuApi::ApplyCXFn ApplyCX = nullptr;
557 DistributedGpuApi::ApplyCYFn ApplyCY = nullptr;
558 DistributedGpuApi::ApplyCZFn ApplyCZ = nullptr;
559 DistributedGpuApi::ApplyCHFn ApplyCH = nullptr;
560 DistributedGpuApi::ApplyCSXFn ApplyCSX = nullptr;
561 DistributedGpuApi::ApplyCSXDGFn ApplyCSXDG = nullptr;
562 DistributedGpuApi::ApplyCPFn ApplyCP = nullptr;
563 DistributedGpuApi::ApplyCRxFn ApplyCRx = nullptr;
564 DistributedGpuApi::ApplyCRyFn ApplyCRy = nullptr;
565 DistributedGpuApi::ApplyCRzFn ApplyCRz = nullptr;
566 DistributedGpuApi::ApplyCCXFn ApplyCCX = nullptr;
567 DistributedGpuApi::ApplySwapFn ApplySwap = nullptr;
568 DistributedGpuApi::ApplyCSwapFn ApplyCSwap = nullptr;
569 DistributedGpuApi::ApplyCUFn ApplyCU = nullptr;
570 DistributedGpuApi::GetApiVersionFn GetApiVersion = nullptr;
571 DistributedGpuApi::GetCapabilitiesFn GetCapabilities = nullptr;
572 DistributedGpuApi::GetLastErrorFn GetLastError = nullptr;
573 DistributedGpuApi::ConfigureDistributionFn ConfigureDistribution = nullptr;
574 DistributedGpuApi::GetShardDevicesFn GetShardDevices = nullptr;
575 DistributedGpuApi::GetGlobalQubitsFn GetGlobalQubits = nullptr;
576 DistributedGpuApi::GetQubitLayoutFn GetQubitLayout = nullptr;
577 DistributedGpuApi::RedistributeFn Redistribute = nullptr;
578 DistributedGpuApi::SwapGlobalLocalQubitsFn SwapGlobalLocalQubits = nullptr;
579 DistributedGpuApi::SynchronizeFn Synchronize = nullptr;
580 DistributedGpuApi::GetStateRangeFn GetStateRange = nullptr;
581 DistributedGpuApi::SetStateRangeFn SetStateRange = nullptr;
582 DistributedGpuApi::GetLocalStateBoundsFn GetLocalStateBounds = nullptr;
583 DistributedGpuApi::GetLocalStateRangeFn GetLocalStateRange = nullptr;
584 DistributedGpuApi::SetLocalStateRangeFn SetLocalStateRange = nullptr;
585 DistributedGpuApi::CreateWithBasisStateFn CreateWithBasisState = nullptr;
586 DistributedGpuApi::ApplyOneQubitMatrixFn ApplyOneQubitMatrix = nullptr;
587 DistributedGpuApi::ApplyOneQubitMatrixWithLayoutFn
588 ApplyOneQubitMatrixWithLayout = nullptr;
589 DistributedGpuApi::ApplyTwoQubitMatrixFn ApplyTwoQubitMatrix = nullptr;
590 DistributedGpuApi::ApplyTwoQubitMatrixWithLayoutFn
591 ApplyTwoQubitMatrixWithLayout = nullptr;
592
593 protected:
594 explicit DistributedGpuLibrary(bool mpi = false) : mpi(mpi) {}
595 std::recursive_mutex mutex;
596 std::atomic_size_t liveStates{0};
597 void* context = nullptr;
598 bool mpi;
599 bool loaded = false;
600};
601} // namespace Simulators
602#endif
int ApplyK(void *sim, int qubit)
double Probability(void *sim, unsigned long long int outcome)
int ApplyRx(void *sim, int qubit, double theta)
int ApplyX(void *sim, int qubit)
int ApplyU(void *sim, int qubit, double theta, double phi, double lambda, double gamma)
int ApplyCRy(void *sim, int controlQubit, int targetQubit, double theta)
int ApplyTDG(void *sim, int qubit)
int ApplyCSXDG(void *sim, int controlQubit, int targetQubit)
int ApplyS(void *sim, int qubit)
int ApplyCX(void *sim, int controlQubit, int targetQubit)
int ApplyCRz(void *sim, int controlQubit, int targetQubit, double theta)
double * AllProbabilities(void *sim)
int ApplyCP(void *sim, int controlQubit, int targetQubit, double theta)
int ApplySXDG(void *sim, int qubit)
int ApplySDG(void *sim, int qubit)
int ApplyCSwap(void *sim, int controlQubit, int qubit1, int qubit2)
int ApplyCCX(void *sim, int controlQubit1, int controlQubit2, int targetQubit)
int ApplyY(void *sim, int qubit)
double * Amplitude(void *sim, unsigned long long int outcome)
int ApplyZ(void *sim, int qubit)
int ApplyH(void *sim, int qubit)
int ApplyCY(void *sim, int controlQubit, int targetQubit)
int ApplyCU(void *sim, int controlQubit, int targetQubit, double theta, double phi, double lambda, double gamma)
int ApplySwap(void *sim, int qubit1, int qubit2)
int ApplyRy(void *sim, int qubit, double theta)
int ApplyP(void *sim, int qubit, double theta)
int ApplyCH(void *sim, int controlQubit, int targetQubit)
int ApplySX(void *sim, int qubit)
int ApplyCZ(void *sim, int controlQubit, int targetQubit)
int ApplyRz(void *sim, int qubit, double theta)
int ApplyT(void *sim, int qubit)
int ApplyCRx(void *sim, int controlQubit, int targetQubit, double theta)
int ApplyCSX(void *sim, int controlQubit, int targetQubit)
int SaveState(void *sim)
virtual bool Init(const char *libName) noexcept
Definition Library.h:78