Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Simulator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "MaestroLib.hpp"
4
5class SimpleSimulator : protected MaestroLibrary {
6 public:
7 SimpleSimulator() noexcept {}
8
9 virtual ~SimpleSimulator() {
10 if (handle) DestroySimpleSimulator(handle);
11 }
12
13 bool Init(const char *libName) noexcept override {
14 if (MaestroLibrary::Init(libName)) return true;
15
16 return false;
17 }
18
19 unsigned long int CreateSimpleSimulator(int nrQubits) override {
20 if (handle) DestroySimpleSimulator(handle);
21
23
24 return handle;
25 }
26
27 bool RemoveAllOptimizationSimulatorsAndAdd(int simType, int simExecType) {
28 if (handle)
30 handle, simType, simExecType) == 1;
31
32 return false;
33 }
34
35 bool AddOptimizationSimulator(int simType, int simExecType) {
36 if (handle)
37 return MaestroLibrary::AddOptimizationSimulator(handle, simType,
38 simExecType) == 1;
39
40 return false;
41 }
42
43 char *SimpleExecute(const char *jsonCircuit, const char *jsonConfig) {
44 return MaestroLibrary::SimpleExecute(handle, jsonCircuit, jsonConfig);
45 }
46
47 char *SimpleEstimate(const char *jsonCircuit, const char *observableStr,
48 const char *jsonConfig) {
49 return MaestroLibrary::SimpleEstimate(handle, jsonCircuit, observableStr,
50 jsonConfig);
51 }
52
53 void FreeResult(char *result) override { MaestroLibrary::FreeResult(result); }
54
55 private:
56 unsigned long int handle = 0;
57};
58
59class Simulator : protected MaestroLibrary {
60 public:
61 Simulator() noexcept {}
62
63 virtual ~Simulator() {
64 if (handle) DestroySimulator(handle);
65 }
66
67 bool Init(const char *libName) noexcept override {
68 if (MaestroLibrary::Init(libName)) return true;
69
70 return false;
71 }
72
73 unsigned long int CreateSimulator(int simType, int simExecType) override {
74 if (handle) DestroySimulator(handle);
75
76 handle = MaestroLibrary::CreateSimulator(simType, simExecType);
77
78 if (handle) simulatorPtr = MaestroLibrary::GetSimulator(handle);
79
80 return handle;
81 }
82
83 void *GetSimulator() { return MaestroLibrary::GetSimulator(handle); }
84
85 void FreeResult(char *result) { MaestroLibrary::FreeResult(result); }
86
88 if (simulatorPtr) return MaestroLibrary::InitializeSimulator(simulatorPtr);
89 return 0;
90 }
91
93 if (simulatorPtr) return MaestroLibrary::ResetSimulator(simulatorPtr);
94 return 0;
95 }
96
97 int ConfigureSimulator(const char *key, const char *value) {
98 if (simulatorPtr)
99 return MaestroLibrary::ConfigureSimulator(simulatorPtr, key, value);
100 return 0;
101 }
102
103 char *GetConfiguration(const char *key) {
104 if (simulatorPtr)
105 return MaestroLibrary::GetConfiguration(simulatorPtr, key);
106 return nullptr;
107 }
108
109 unsigned long int AllocateQubits(unsigned long int nrQubits) {
110 if (simulatorPtr)
111 return MaestroLibrary::AllocateQubits(simulatorPtr, nrQubits);
112 return 0;
113 }
114
115 unsigned long int GetNumberOfQubits() {
116 if (simulatorPtr) return MaestroLibrary::GetNumberOfQubits(simulatorPtr);
117 return 0;
118 }
119
121 if (simulatorPtr) return MaestroLibrary::ClearSimulator(simulatorPtr);
122 return 0;
123 }
124
125 unsigned long long int Measure(const unsigned long int *qubits,
126 unsigned long int nrQubits) {
127 if (simulatorPtr)
128 return MaestroLibrary::Measure(simulatorPtr, qubits, nrQubits);
129
130 return 0;
131 }
132
133 int ApplyReset(const unsigned long int *qubits, unsigned long int nrQubits) {
134 if (simulatorPtr)
135 return MaestroLibrary::ApplyReset(simulatorPtr, qubits, nrQubits);
136 return 0;
137 }
138
139 double Probability(unsigned long long int outcome) {
140 if (simulatorPtr) return MaestroLibrary::Probability(simulatorPtr, outcome);
141
142 return 0.0;
143 }
144
145 void FreeDoubleVector(double *vec) override {
147 }
148
149 void FreeULLIVector(unsigned long long int *vec) override {
151 }
152
153 double *Amplitude(unsigned long long int outcome) {
154 if (simulatorPtr) return MaestroLibrary::Amplitude(simulatorPtr, outcome);
155 return nullptr;
156 }
157
159 if (simulatorPtr) return MaestroLibrary::AllProbabilities(simulatorPtr);
160 return nullptr;
161 }
162
163 double *Probabilities(const unsigned long long int *qubits,
164 unsigned long int nrQubits) {
165 if (simulatorPtr)
166 return MaestroLibrary::Probabilities(simulatorPtr, qubits, nrQubits);
167 return nullptr;
168 }
169
170 unsigned long long int *SampleCounts(const unsigned long long int *qubits,
171 unsigned long int nrQubits,
172 unsigned long int shots) {
173 if (simulatorPtr)
174 return MaestroLibrary::SampleCounts(simulatorPtr, qubits, nrQubits,
175 shots);
176 return nullptr;
177 }
178
180 if (simulatorPtr) return MaestroLibrary::GetSimulatorType(simulatorPtr);
181 return -1;
182 }
183
185 if (simulatorPtr) return MaestroLibrary::GetSimulationType(simulatorPtr);
186 return -1;
187 }
188
190 if (simulatorPtr) return MaestroLibrary::FlushSimulator(simulatorPtr);
191 return 0;
192 }
193
195 if (simulatorPtr)
197 return 0;
198 }
199
201 if (simulatorPtr)
203 return 0;
204 }
205
206 int SaveState() {
207 if (simulatorPtr) return MaestroLibrary::SaveState(simulatorPtr);
208 return 0;
209 }
210
212 if (simulatorPtr) return MaestroLibrary::RestoreState(simulatorPtr);
213 return 0;
214 }
215
216 int SetMultithreading(int multithreading) {
217 if (simulatorPtr)
218 return MaestroLibrary::SetMultithreading(simulatorPtr, multithreading);
219 return 0;
220 }
221
223 if (simulatorPtr) return MaestroLibrary::GetMultithreading(simulatorPtr);
224 return 0;
225 }
226
227 int IsQcsim() {
228 if (simulatorPtr) return MaestroLibrary::IsQcsim(simulatorPtr);
229 return 0;
230 }
231
232 unsigned long long int MeasureNoCollapse() {
233 if (simulatorPtr) return MaestroLibrary::MeasureNoCollapse(simulatorPtr);
234 return 0;
235 }
236
237 int ApplyX(int qubit) {
238 if (simulatorPtr) return MaestroLibrary::ApplyX(simulatorPtr, qubit);
239 return 0;
240 }
241
242 int ApplyY(int qubit) {
243 if (simulatorPtr) return MaestroLibrary::ApplyY(simulatorPtr, qubit);
244 return 0;
245 }
246
247 int ApplyZ(int qubit) {
248 if (simulatorPtr) return MaestroLibrary::ApplyZ(simulatorPtr, qubit);
249 return 0;
250 }
251
252 int ApplyH(int qubit) {
253 if (simulatorPtr) return MaestroLibrary::ApplyH(simulatorPtr, qubit);
254 return 0;
255 }
256
257 int ApplyS(int qubit) {
258 if (simulatorPtr) return MaestroLibrary::ApplyS(simulatorPtr, qubit);
259 return 0;
260 }
261
262 int ApplySDG(int qubit) {
263 if (simulatorPtr) return MaestroLibrary::ApplySDG(simulatorPtr, qubit);
264 return 0;
265 }
266
267 int ApplyT(int qubit) {
268 if (simulatorPtr) return MaestroLibrary::ApplyT(simulatorPtr, qubit);
269 return 0;
270 }
271
272 int ApplyTDG(int qubit) {
273 if (simulatorPtr) return MaestroLibrary::ApplyTDG(simulatorPtr, qubit);
274 return 0;
275 }
276
277 int ApplySX(int qubit) {
278 if (simulatorPtr) return MaestroLibrary::ApplySX(simulatorPtr, qubit);
279 return 0;
280 }
281
282 int ApplySXDG(int qubit) {
283 if (simulatorPtr) return MaestroLibrary::ApplySXDG(simulatorPtr, qubit);
284 return 0;
285 }
286
287 int ApplyK(int qubit) {
288 if (simulatorPtr) return MaestroLibrary::ApplyK(simulatorPtr, qubit);
289 return 0;
290 }
291
292 int ApplyP(int qubit, double theta) {
293 if (simulatorPtr) return MaestroLibrary::ApplyP(simulatorPtr, qubit, theta);
294 return 0;
295 }
296
297 int ApplyRx(int qubit, double theta) {
298 if (simulatorPtr)
299 return MaestroLibrary::ApplyRx(simulatorPtr, qubit, theta);
300 return 0;
301 }
302
303 int ApplyRy(int qubit, double theta) {
304 if (simulatorPtr)
305 return MaestroLibrary::ApplyRy(simulatorPtr, qubit, theta);
306 return 0;
307 }
308
309 int ApplyRz(int qubit, double theta) {
310 if (simulatorPtr)
311 return MaestroLibrary::ApplyRz(simulatorPtr, qubit, theta);
312 return 0;
313 }
314
315 int ApplyU(int qubit, double theta, double phi, double lambda, double gamma) {
316 if (simulatorPtr)
317 return MaestroLibrary::ApplyU(simulatorPtr, qubit, theta, phi, lambda,
318 gamma);
319 return 0;
320 }
321
322 int ApplyCX(int controlQubit, int targetQubit) {
323 if (simulatorPtr)
324 return MaestroLibrary::ApplyCX(simulatorPtr, controlQubit, targetQubit);
325 return 0;
326 }
327
328 int ApplyCY(int controlQubit, int targetQubit) {
329 if (simulatorPtr)
330 return MaestroLibrary::ApplyCY(simulatorPtr, controlQubit, targetQubit);
331 return 0;
332 }
333
334 int ApplyCZ(int controlQubit, int targetQubit) {
335 if (simulatorPtr)
336 return MaestroLibrary::ApplyCZ(simulatorPtr, controlQubit, targetQubit);
337 return 0;
338 }
339
340 int ApplyCH(int controlQubit, int targetQubit) {
341 if (simulatorPtr)
342 return MaestroLibrary::ApplyCH(simulatorPtr, controlQubit, targetQubit);
343 return 0;
344 }
345
346 int ApplyCSX(int controlQubit, int targetQubit) {
347 if (simulatorPtr)
348 return MaestroLibrary::ApplyCSX(simulatorPtr, controlQubit, targetQubit);
349 return 0;
350 }
351
352 int ApplyCSXDG(int controlQubit, int targetQubit) {
353 if (simulatorPtr)
354 return MaestroLibrary::ApplyCSXDG(simulatorPtr, controlQubit,
355 targetQubit);
356 return 0;
357 }
358
359 int ApplyCP(int controlQubit, int targetQubit, double theta) {
360 if (simulatorPtr)
361 return MaestroLibrary::ApplyCP(simulatorPtr, controlQubit, targetQubit,
362 theta);
363 return 0;
364 }
365
366 int ApplyCRx(int controlQubit, int targetQubit, double theta) {
367 if (simulatorPtr)
368 return MaestroLibrary::ApplyCRx(simulatorPtr, controlQubit, targetQubit,
369 theta);
370 return 0;
371 }
372
373 int ApplyCRy(int controlQubit, int targetQubit, double theta) {
374 if (simulatorPtr)
375 return MaestroLibrary::ApplyCRy(simulatorPtr, controlQubit, targetQubit,
376 theta);
377 return 0;
378 }
379
380 int ApplyCRz(int controlQubit, int targetQubit, double theta) {
381 if (simulatorPtr)
382 return MaestroLibrary::ApplyCRz(simulatorPtr, controlQubit, targetQubit,
383 theta);
384 return 0;
385 }
386
387 int ApplyCCX(int controlQubit1, int controlQubit2, int targetQubit) {
388 if (simulatorPtr)
389 return MaestroLibrary::ApplyCCX(simulatorPtr, controlQubit1,
390 controlQubit2, targetQubit);
391 return 0;
392 }
393
394 int ApplySwap(int qubit1, int qubit2) {
395 if (simulatorPtr)
396 return MaestroLibrary::ApplySwap(simulatorPtr, qubit1, qubit2);
397 return 0;
398 }
399
400 int ApplyCSwap(int controlQubit, int qubit1, int qubit2) {
401 if (simulatorPtr)
402 return MaestroLibrary::ApplyCSwap(simulatorPtr, controlQubit, qubit1,
403 qubit2);
404 return 0;
405 }
406
407 int ApplyCU(int controlQubit, int targetQubit, double theta, double phi,
408 double lambda, double gamma) {
409 if (simulatorPtr)
410 return MaestroLibrary::ApplyCU(simulatorPtr, controlQubit, targetQubit,
411 theta, phi, lambda, gamma);
412 return 0;
413 }
414
415 private:
416 unsigned long int handle = 0;
417 void *simulatorPtr = nullptr;
418};
int ApplyCCX(void *sim, int controlQubit1, int controlQubit2, int targetQubit)
int ResetSimulator(void *sim)
void * GetSimulator(unsigned long int simHandle)
int ApplyCZ(void *sim, int controlQubit, int targetQubit)
unsigned long int AllocateQubits(void *sim, unsigned long int nrQubits)
int ConfigureSimulator(void *sim, const char *key, const char *value)
unsigned long long int * SampleCounts(void *sim, const unsigned long long int *qubits, unsigned long int nrQubits, unsigned long int shots)
int ApplySXDG(void *sim, int qubit)
int ApplyS(void *sim, int qubit)
int GetSimulationType(void *sim)
int GetSimulatorType(void *sim)
int ApplyH(void *sim, int qubit)
double * AllProbabilities(void *sim)
unsigned long long int Measure(void *sim, const unsigned long int *qubits, unsigned long int nrQubits)
int ApplySDG(void *sim, int qubit)
int RemoveAllOptimizationSimulatorsAndAdd(unsigned long int simHandle, int simType, int simExecType)
int ApplyT(void *sim, int qubit)
int ApplyY(void *sim, int qubit)
int ApplySX(void *sim, int qubit)
int SaveState(void *sim)
int ApplyX(void *sim, int qubit)
int ApplyCY(void *sim, int controlQubit, int targetQubit)
int ApplyCX(void *sim, int controlQubit, int targetQubit)
void DestroySimpleSimulator(unsigned long int simHandle)
int ApplyCRx(void *sim, int controlQubit, int targetQubit, double theta)
int ClearSimulator(void *sim)
int ApplyCSX(void *sim, int controlQubit, int targetQubit)
int ApplyK(void *sim, int qubit)
char * SimpleExecute(unsigned long int simpleSim, const char *jsonCircuit, const char *jsonConfig)
int RestoreState(void *sim)
int ApplyCRz(void *sim, int controlQubit, int targetQubit, double theta)
double * Amplitude(void *sim, unsigned long long int outcome)
int AddOptimizationSimulator(unsigned long int simHandle, int simType, int simExecType)
virtual unsigned long int CreateSimpleSimulator(int nrQubits)
char * SimpleEstimate(unsigned long int simpleSim, const char *jsonCircuit, const char *observableStr, const char *jsonConfig)
double Probability(void *sim, unsigned long long int outcome)
int ApplyCH(void *sim, int controlQubit, int targetQubit)
int ApplyCSwap(void *sim, int controlQubit, int qubit1, int qubit2)
virtual void FreeULLIVector(unsigned long long int *vec)
int IsQcsim(void *sim)
virtual unsigned long int CreateSimulator(int simType, int simExecType)
int ApplyCSXDG(void *sim, int controlQubit, int targetQubit)
int SaveStateToInternalDestructive(void *sim)
virtual void FreeDoubleVector(double *vec)
int ApplyCRy(void *sim, int controlQubit, int targetQubit, double theta)
double * Probabilities(void *sim, const unsigned long long int *qubits, unsigned long int nrQubits)
int ApplyRz(void *sim, int qubit, double theta)
int ApplySwap(void *sim, int qubit1, int qubit2)
void DestroySimulator(unsigned long int simHandle)
int ApplyU(void *sim, int qubit, double theta, double phi, double lambda, double gamma)
int ApplyZ(void *sim, int qubit)
int RestoreInternalDestructiveSavedState(void *sim)
int ApplyCU(void *sim, int controlQubit, int targetQubit, double theta, double phi, double lambda, double gamma)
int ApplyReset(void *sim, const unsigned long int *qubits, unsigned long int nrQubits)
int InitializeSimulator(void *sim)
int ApplyP(void *sim, int qubit, double theta)
int ApplyRy(void *sim, int qubit, double theta)
bool Init(const char *libName) noexcept override
int ApplyCP(void *sim, int controlQubit, int targetQubit, double theta)
int FlushSimulator(void *sim)
int GetMultithreading(void *sim)
virtual void FreeResult(char *result)
int ApplyRx(void *sim, int qubit, double theta)
unsigned long int GetNumberOfQubits(void *sim)
char * GetConfiguration(void *sim, const char *key)
int ApplyTDG(void *sim, int qubit)
unsigned long long int MeasureNoCollapse(void *sim)
int SetMultithreading(void *sim, int multithreading)
SimpleSimulator() noexcept
Definition Simulator.hpp:7
bool AddOptimizationSimulator(int simType, int simExecType)
Definition Simulator.hpp:35
virtual ~SimpleSimulator()
Definition Simulator.hpp:9
char * SimpleEstimate(const char *jsonCircuit, const char *observableStr, const char *jsonConfig)
Definition Simulator.hpp:47
unsigned long int CreateSimpleSimulator(int nrQubits) override
Definition Simulator.hpp:19
char * SimpleExecute(const char *jsonCircuit, const char *jsonConfig)
Definition Simulator.hpp:43
void FreeResult(char *result) override
Definition Simulator.hpp:53
bool Init(const char *libName) noexcept override
Definition Simulator.hpp:13
bool RemoveAllOptimizationSimulatorsAndAdd(int simType, int simExecType)
Definition Simulator.hpp:27
int ApplySXDG(int qubit)
int ApplyCSXDG(int controlQubit, int targetQubit)
int ApplyS(int qubit)
bool Init(const char *libName) noexcept override
Definition Simulator.hpp:67
int ApplyTDG(int qubit)
int ApplyZ(int qubit)
unsigned long int AllocateQubits(unsigned long int nrQubits)
int ApplyCH(int controlQubit, int targetQubit)
int IsQcsim()
int ApplyCSwap(int controlQubit, int qubit1, int qubit2)
int ApplyCY(int controlQubit, int targetQubit)
int ApplyCZ(int controlQubit, int targetQubit)
int SaveStateToInternalDestructive()
unsigned long int CreateSimulator(int simType, int simExecType) override
Definition Simulator.hpp:73
double * Probabilities(const unsigned long long int *qubits, unsigned long int nrQubits)
double * AllProbabilities()
int ApplyT(int qubit)
int ApplyCRy(int controlQubit, int targetQubit, double theta)
void FreeResult(char *result)
Definition Simulator.hpp:85
int ApplyP(int qubit, double theta)
void FreeULLIVector(unsigned long long int *vec) override
int GetSimulationType()
int RestoreState()
int InitializeSimulator()
Definition Simulator.hpp:87
int ApplyCP(int controlQubit, int targetQubit, double theta)
double * Amplitude(unsigned long long int outcome)
char * GetConfiguration(const char *key)
int ApplyCRz(int controlQubit, int targetQubit, double theta)
Simulator() noexcept
Definition Simulator.hpp:61
void FreeDoubleVector(double *vec) override
int ApplySwap(int qubit1, int qubit2)
int ApplyRy(int qubit, double theta)
int RestoreInternalDestructiveSavedState()
int ApplyRz(int qubit, double theta)
double Probability(unsigned long long int outcome)
int ApplyY(int qubit)
int ApplyCX(int controlQubit, int targetQubit)
int ApplySX(int qubit)
int ApplyCSX(int controlQubit, int targetQubit)
int ApplyH(int qubit)
int GetSimulatorType()
void * GetSimulator()
Definition Simulator.hpp:83
int ApplyCRx(int controlQubit, int targetQubit, double theta)
virtual ~Simulator()
Definition Simulator.hpp:63
int ResetSimulator()
Definition Simulator.hpp:92
int GetMultithreading()
int ApplySDG(int qubit)
int SaveState()
unsigned long long int MeasureNoCollapse()
int ConfigureSimulator(const char *key, const char *value)
Definition Simulator.hpp:97
unsigned long long int Measure(const unsigned long int *qubits, unsigned long int nrQubits)
unsigned long long int * SampleCounts(const unsigned long long int *qubits, unsigned long int nrQubits, unsigned long int shots)
int ApplyU(int qubit, double theta, double phi, double lambda, double gamma)
int ApplyRx(int qubit, double theta)
int ApplyX(int qubit)
int ApplyK(int qubit)
int ApplyReset(const unsigned long int *qubits, unsigned long int nrQubits)
int ClearSimulator()
unsigned long int GetNumberOfQubits()
int FlushSimulator()
int SetMultithreading(int multithreading)
int ApplyCCX(int controlQubit1, int controlQubit2, int targetQubit)
int ApplyCU(int controlQubit, int targetQubit, double theta, double phi, double lambda, double gamma)