Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Interface.cpp
Go to the documentation of this file.
1
8
9#include "Interface.h"
10
11#ifdef COMPOSER
12#include "../../composer/composer/Estimators/ExecutionEstimator.h"
13#endif
14
16
17#include "Maestro.h"
18
19#include "Json.h"
20
21#include <boost/json/src.hpp>
22
23#include <atomic>
24#include <memory>
25
26#include "../Utils/LogFile.h"
27#include "../qasm/QasmCirc.h"
28
29static std::atomic_bool isInitialized = false;
30static std::unique_ptr<Maestro> maestroInstance = nullptr;
31
32extern "C" void *GetMaestroObject() {
33 if (!isInitialized.exchange(true)) {
34#ifdef __linux__
36#endif
37
38#ifdef COMPOSER
39 Estimators::ExecutionEstimator<>::InitializeRegressors();
40#endif
41
42 maestroInstance = std::make_unique<Maestro>();
43 }
44
45 return (void *)maestroInstance.get();
46}
47
48extern "C" void *GetMaestroObjectWithMute() {
49 if (!isInitialized.exchange(true)) {
50#ifdef __linux__
51 Simulators::SimulatorsFactory::InitGpuLibraryWithMute();
52#endif
53
54#ifdef COMPOSER
55 Estimators::ExecutionEstimator<>::InitializeRegressors();
56#endif
57
58 maestroInstance = std::make_unique<Maestro>();
59 }
60
61 return (void *)maestroInstance.get();
62}
63
64extern "C" unsigned long int CreateSimpleSimulator(int nrQubits) {
65 if (!maestroInstance)
66 return 0;
67
68 return maestroInstance->CreateSimpleSimulator(nrQubits);
69}
70
71extern "C" void DestroySimpleSimulator(unsigned long int simHandle) {
72 if (!maestroInstance || simHandle == 0)
73 return;
74
75 maestroInstance->DestroySimpleSimulator(simHandle);
76}
77
78extern "C" int
79RemoveAllOptimizationSimulatorsAndAdd(unsigned long int simHandle, int simType,
80 int simExecType) {
81 if (!maestroInstance || simHandle == 0)
82 return 0;
83
84 return maestroInstance->RemoveAllOptimizationSimulatorsAndAdd(
85 simHandle, static_cast<Simulators::SimulatorType>(simType),
86 static_cast<Simulators::SimulationType>(simExecType));
87}
88
89extern "C" int AddOptimizationSimulator(unsigned long int simHandle,
90 int simType, int simExecType) {
91 if (!maestroInstance || simHandle == 0)
92 return 0;
93
94 return maestroInstance->AddOptimizationSimulator(
95 simHandle, static_cast<Simulators::SimulatorType>(simType),
96 static_cast<Simulators::SimulationType>(simExecType));
97}
98
99extern "C" char *SimpleExecute(unsigned long int simpleSim,
100 const char *circuitStr, const char *jsonConfig) {
101 if (simpleSim == 0 || !circuitStr || !jsonConfig || !maestroInstance)
102 return nullptr;
103
104 auto network = maestroInstance->GetSimpleSimulator(simpleSim);
105
106 // step 1: Parse the JSON circuit and configuration strings
107 // convert the JSON circuit into a Circuit object
108
109 // I'm unsure here on how it deals with the classical registers, more
110 // precisely with stuff like "other_measure_name" and "meas" (see below) since
111 // in the example it seems to just use the cbit number
112
113 // This is the json format:
114 // {"instructions":
115 // [{"name": "h", "qubits": [0], "params": []},
116 // {"name": "cx", "qubits": [0, 1], "params": []},
117 // {"name": "rx", "qubits": [0], "params": [0.39528385768119634]},
118 // {"name": "measure", "qubits": [0], "memory": [0]}],
119 //
120 // "num_qubits": 2, "num_clbits": 4,
121 // "quantum_registers": {"q": [0, 1]},
122 // "classical_registers": {"c": [0, 1], "other_measure_name": [2], "meas":
123 // [3]}}
124
125 std::shared_ptr<Circuits::Circuit<>> circuit;
126
127 if (circuitStr[0] == '{' || circuitStr[0] == '[') {
128 // assume JSON format only if either object or array
129 Json::JsonParserMaestro<> jsonParser;
130 circuit = jsonParser.ParseCircuit(circuitStr);
131 } else {
132 // QASM 2.0 format
133 qasm::QasmToCirc<> parser;
134 std::string qasmInput(circuitStr);
135 circuit = parser.ParseAndTranslate(qasmInput);
136 }
137
138 // check if the circuit has measurements only at the end
139
140 // get the number of shots from the configuration
141 size_t nrShots = 1; // default value
142
143 const auto configJson = Json::JsonParserMaestro<>::ParseString(jsonConfig);
144
145 if (configJson.is_object()) {
146 const auto configObject = configJson.as_object();
147 // get whatever else is needed from the configuration
148 // maybe simulator type, allowed simulator types, bond dimension limit, etc.
149
150 // execute the circuit in the network object
151 if (configObject.contains("shots") &&
152 configObject.at("shots").is_number()) {
153 auto number = configObject.at("shots");
154 nrShots = number.is_int64() ? (size_t)number.as_int64()
155 : (size_t)number.as_uint64();
156 }
157 }
158
159 bool configured = false;
160
161 const std::string maxBondDim = Json::JsonParserMaestro<>::GetConfigString(
162 "matrix_product_state_max_bond_dimension", configJson);
163 if (!maxBondDim.empty()) {
164 configured = true;
165 if (network->GetSimulator())
166 network->GetSimulator()->Clear();
167 network->Configure("matrix_product_state_max_bond_dimension",
168 maxBondDim.c_str());
169 }
170
171 const std::string singularValueThreshold =
173 "matrix_product_state_truncation_threshold", configJson);
174 if (!singularValueThreshold.empty()) {
175 configured = true;
176 if (network->GetSimulator())
177 network->GetSimulator()->Clear();
178 network->Configure("matrix_product_state_truncation_threshold",
179 singularValueThreshold.c_str());
180 }
181
182 const std::string mpsSample = Json::JsonParserMaestro<>::GetConfigString(
183 "mps_sample_measure_algorithm", configJson);
184 if (!mpsSample.empty()) {
185 configured = true;
186 if (network->GetSimulator())
187 network->GetSimulator()->Clear();
188 network->Configure("mps_sample_measure_algorithm", mpsSample.c_str());
189 }
190
191 if (configured || !network->GetSimulator())
192 network->CreateSimulator();
193
194 // TODO: get from config the allowed simulators types and so on, if set
195 auto start = std::chrono::high_resolution_clock::now();
196 auto results = network->RepeatedExecuteOnHost(circuit, 0, nrShots);
197 auto end = std::chrono::high_resolution_clock::now();
198
199 std::chrono::duration<double> duration = end - start;
200 double time_taken = duration.count();
201 std::string timeStr = std::to_string(time_taken);
202
203 // convert the results into a JSON string
204 // allocate memory for the result string and copy the JSON result into it
205 // return the result string
206
207 boost::json::object jsonResult;
208 jsonResult.reserve(results.size());
209
210 for (auto &result : results) {
211 boost::json::string bits;
212 bits.reserve(result.first.size());
213 for (const auto bit : result.first)
214 bits.append(bit ? "1" : "0");
215
216 jsonResult.emplace(std::move(bits), std::move(result.second));
217 }
218
219 boost::json::object response;
220 response.reserve(4);
221
222 response.emplace("counts", std::move(jsonResult));
223 response.emplace("time_taken", timeStr);
224
225 auto simulatorType = network->GetLastSimulatorType();
226
227 switch (simulatorType) {
228#ifndef NO_QISKIT_AER
230 response.emplace("simulator", "aer");
231 break;
232#endif
234 response.emplace("simulator", "qcsim");
235 break;
236#ifndef NO_QISKIT_AER
238 response.emplace("simulator", "composite_aer");
239 break;
240#endif
242 response.emplace("simulator", "composite_qcsim");
243 break;
244#ifdef __linux__
245 case Simulators::SimulatorType::kGpuSim:
246 response.emplace("simulator", "gpu_simulator");
247 break;
248#endif
249 default:
250 response.emplace("simulator", "unknown");
251 break;
252 }
253
254 auto simulationType = network->GetLastSimulationType();
255 switch (simulationType) {
257 response.emplace("method", "statevector");
258 break;
260 response.emplace("method", "matrix_product_state");
261 break;
263 response.emplace("method", "stabilizer");
264 break;
266 response.emplace("method", "tensor_network");
267 break;
268 default:
269 response.emplace("method", "unknown");
270 break;
271 }
272
273 const std::string responseStr = boost::json::serialize(response);
274 const size_t responseSize = responseStr.length();
275 char *result = new char[responseSize + 1];
276
277 const char *responseData = responseStr.c_str();
278 std::copy(responseData, responseData + responseSize, result);
279
280 result[responseSize] = 0; // ensure null-termination
281
282 return result;
283}
284
285extern "C" void FreeResult(char *result) {
286 if (result)
287 delete[] result;
288}
289
290extern "C" unsigned long int CreateSimulator(int simType, int simExecType) {
291 if (!maestroInstance)
292 return 0;
293
294 return maestroInstance->CreateSimulator(
295 static_cast<Simulators::SimulatorType>(simType),
296 static_cast<Simulators::SimulationType>(simExecType));
297}
298
299extern "C" void *GetSimulator(unsigned long int simHandle) {
300 if (!maestroInstance || simHandle == 0)
301 return nullptr;
302 return maestroInstance->GetSimulator(simHandle);
303}
304
305extern "C" void DestroySimulator(unsigned long int simHandle) {
306 if (!maestroInstance || simHandle == 0)
307 return;
308 maestroInstance->DestroySimulator(simHandle);
309}
310
311extern "C" int ApplyX(void *sim, int qubit) {
312 if (!sim)
313 return 0;
314
315 auto simulator = static_cast<Simulators::ISimulator *>(sim);
316 simulator->ApplyX(qubit);
317
318 return 1;
319}
320
321extern "C" int ApplyY(void *sim, int qubit) {
322 if (!sim)
323 return 0;
324 auto simulator = static_cast<Simulators::ISimulator *>(sim);
325 simulator->ApplyY(qubit);
326
327 return 1;
328}
329
330extern "C" int ApplyZ(void *sim, int qubit) {
331 if (!sim)
332 return 0;
333
334 auto simulator = static_cast<Simulators::ISimulator *>(sim);
335 simulator->ApplyZ(qubit);
336 return 1;
337}
338
339extern "C" int ApplyH(void *sim, int qubit) {
340 if (!sim)
341 return 0;
342 auto simulator = static_cast<Simulators::ISimulator *>(sim);
343 simulator->ApplyH(qubit);
344
345 return 1;
346}
347
348extern "C" int ApplyS(void *sim, int qubit) {
349 if (!sim)
350 return 0;
351 auto simulator = static_cast<Simulators::ISimulator *>(sim);
352 simulator->ApplyS(qubit);
353
354 return 1;
355}
356
357extern "C" int ApplySDG(void *sim, int qubit) {
358 if (!sim)
359 return 0;
360 auto simulator = static_cast<Simulators::ISimulator *>(sim);
361 simulator->ApplySDG(qubit);
362
363 return 1;
364}
365
366extern "C" int ApplyT(void *sim, int qubit) {
367 if (!sim)
368 return 0;
369 auto simulator = static_cast<Simulators::ISimulator *>(sim);
370 simulator->ApplyT(qubit);
371
372 return 1;
373}
374
375extern "C" int ApplyTDG(void *sim, int qubit) {
376 if (!sim)
377 return 0;
378 auto simulator = static_cast<Simulators::ISimulator *>(sim);
379 simulator->ApplyTDG(qubit);
380
381 return 1;
382}
383
384extern "C" int ApplySX(void *sim, int qubit) {
385 if (!sim)
386 return 0;
387 auto simulator = static_cast<Simulators::ISimulator *>(sim);
388 simulator->ApplySx(qubit);
389
390 return 1;
391}
392
393extern "C" int ApplySXDG(void *sim, int qubit) {
394 if (!sim)
395 return 0;
396 auto simulator = static_cast<Simulators::ISimulator *>(sim);
397 simulator->ApplySxDAG(qubit);
398
399 return 1;
400}
401
402extern "C" int ApplyK(void *sim, int qubit) {
403 if (!sim)
404 return 0;
405 auto simulator = static_cast<Simulators::ISimulator *>(sim);
406 simulator->ApplyK(qubit);
407
408 return 1;
409}
410
411extern "C" int ApplyP(void *sim, int qubit, double theta) {
412 if (!sim)
413 return 0;
414 auto simulator = static_cast<Simulators::ISimulator *>(sim);
415 simulator->ApplyP(qubit, theta);
416
417 return 1;
418}
419
420extern "C" int ApplyRx(void *sim, int qubit, double theta) {
421 if (!sim)
422 return 0;
423 auto simulator = static_cast<Simulators::ISimulator *>(sim);
424 simulator->ApplyRx(qubit, theta);
425
426 return 1;
427}
428
429extern "C" int ApplyRy(void *sim, int qubit, double theta) {
430 if (!sim)
431 return 0;
432 auto simulator = static_cast<Simulators::ISimulator *>(sim);
433 simulator->ApplyRy(qubit, theta);
434
435 return 1;
436}
437
438extern "C" int ApplyRz(void *sim, int qubit, double theta) {
439 if (!sim)
440 return 0;
441 auto simulator = static_cast<Simulators::ISimulator *>(sim);
442 simulator->ApplyRz(qubit, theta);
443
444 return 1;
445}
446
447extern "C" int ApplyU(void *sim, int qubit, double theta, double phi,
448 double lambda, double gamma) {
449 if (!sim)
450 return 0;
451 auto simulator = static_cast<Simulators::ISimulator *>(sim);
452 simulator->ApplyU(qubit, theta, phi, lambda, gamma);
453
454 return 1;
455}
456
457extern "C" int ApplyCX(void *sim, int controlQubit, int targetQubit) {
458 if (!sim)
459 return 0;
460 auto simulator = static_cast<Simulators::ISimulator *>(sim);
461 simulator->ApplyCX(controlQubit, targetQubit);
462
463 return 1;
464}
465
466extern "C" int ApplyCY(void *sim, int controlQubit, int targetQubit) {
467 if (!sim)
468 return 0;
469 auto simulator = static_cast<Simulators::ISimulator *>(sim);
470 simulator->ApplyCY(controlQubit, targetQubit);
471
472 return 1;
473}
474
475extern "C" int ApplyCZ(void *sim, int controlQubit, int targetQubit) {
476 if (!sim)
477 return 0;
478 auto simulator = static_cast<Simulators::ISimulator *>(sim);
479 simulator->ApplyCZ(controlQubit, targetQubit);
480
481 return 1;
482}
483
484extern "C" int ApplyCH(void *sim, int controlQubit, int targetQubit) {
485 if (!sim)
486 return 0;
487 auto simulator = static_cast<Simulators::ISimulator *>(sim);
488 simulator->ApplyCH(controlQubit, targetQubit);
489
490 return 1;
491}
492
493extern "C" int ApplyCSX(void *sim, int controlQubit, int targetQubit) {
494 if (!sim)
495 return 0;
496 auto simulator = static_cast<Simulators::ISimulator *>(sim);
497 simulator->ApplyCSx(controlQubit, targetQubit);
498
499 return 1;
500}
501
502extern "C" int ApplyCSXDG(void *sim, int controlQubit, int targetQubit) {
503 if (!sim)
504 return 0;
505 auto simulator = static_cast<Simulators::ISimulator *>(sim);
506 simulator->ApplyCSxDAG(controlQubit, targetQubit);
507
508 return 1;
509}
510
511extern "C" int ApplyCP(void *sim, int controlQubit, int targetQubit,
512 double theta) {
513 if (!sim)
514 return 0;
515 auto simulator = static_cast<Simulators::ISimulator *>(sim);
516 simulator->ApplyCP(controlQubit, targetQubit, theta);
517
518 return 1;
519}
520
521extern "C" int ApplyCRx(void *sim, int controlQubit, int targetQubit,
522 double theta) {
523 if (!sim)
524 return 0;
525 auto simulator = static_cast<Simulators::ISimulator *>(sim);
526 simulator->ApplyCRx(controlQubit, targetQubit, theta);
527
528 return 1;
529}
530
531extern "C" int ApplyCRy(void *sim, int controlQubit, int targetQubit,
532 double theta) {
533 if (!sim)
534 return 0;
535 auto simulator = static_cast<Simulators::ISimulator *>(sim);
536 simulator->ApplyCRy(controlQubit, targetQubit, theta);
537
538 return 1;
539}
540
541extern "C" int ApplyCRz(void *sim, int controlQubit, int targetQubit,
542 double theta) {
543 if (!sim)
544 return 0;
545 auto simulator = static_cast<Simulators::ISimulator *>(sim);
546 simulator->ApplyCRz(controlQubit, targetQubit, theta);
547
548 return 1;
549}
550
551extern "C" int ApplyCCX(void *sim, int controlQubit1, int controlQubit2,
552 int targetQubit) {
553 if (!sim)
554 return 0;
555 auto simulator = static_cast<Simulators::ISimulator *>(sim);
556 simulator->ApplyCCX(controlQubit1, controlQubit2, targetQubit);
557
558 return 1;
559}
560
561extern "C" int ApplySwap(void *sim, int qubit1, int qubit2) {
562 if (!sim)
563 return 0;
564 auto simulator = static_cast<Simulators::ISimulator *>(sim);
565 simulator->ApplySwap(qubit1, qubit2);
566
567 return 1;
568}
569
570extern "C" int ApplyCSwap(void *sim, int controlQubit, int qubit1, int qubit2) {
571 if (!sim)
572 return 0;
573 auto simulator = static_cast<Simulators::ISimulator *>(sim);
574 simulator->ApplyCSwap(controlQubit, qubit1, qubit2);
575
576 return 1;
577}
578
579extern "C" int ApplyCU(void *sim, int controlQubit, int targetQubit,
580 double theta, double phi, double lambda, double gamma) {
581 if (!sim)
582 return 0;
583 auto simulator = static_cast<Simulators::ISimulator *>(sim);
584 simulator->ApplyCU(controlQubit, targetQubit, theta, phi, lambda, gamma);
585
586 return 1;
587}
588
589extern "C" int InitializeSimulator(void *sim) {
590 if (!sim)
591 return 0;
592 auto simulator = static_cast<Simulators::ISimulator *>(sim);
593 simulator->Initialize();
594 return 1;
595}
596
597extern "C" int ResetSimulator(void *sim) {
598 if (!sim)
599 return 0;
600 auto simulator = static_cast<Simulators::ISimulator *>(sim);
601 simulator->Reset();
602 return 1;
603}
604
605extern "C" int ConfigureSimulator(void *sim, const char *key,
606 const char *value) {
607 if (!sim || !key || !value)
608 return 0;
609 auto simulator = static_cast<Simulators::ISimulator *>(sim);
610 simulator->Configure(key, value);
611 return 1;
612}
613
614extern "C" char *GetConfiguration(void *sim, const char *key) {
615 if (!sim || !key)
616 return nullptr;
617 auto simulator = static_cast<Simulators::ISimulator *>(sim);
618 std::string value = simulator->GetConfiguration(key);
619 if (value.empty())
620 return nullptr;
621 // allocate memory for the result string and copy the configuration value into
622 // it
623 const size_t valueSize = value.length();
624 char *result = new char[valueSize + 1];
625 std::copy(value.c_str(), value.c_str() + valueSize, result);
626 result[valueSize] = 0; // ensure null-termination
627 return result;
628}
629
630extern "C" unsigned long int AllocateQubits(void *sim,
631 unsigned long int nrQubits) {
632 if (!sim || nrQubits == 0)
633 return 0;
634 auto simulator = static_cast<Simulators::ISimulator *>(sim);
635 const size_t res = simulator->AllocateQubits(nrQubits);
636
637 return static_cast<unsigned long int>(res);
638}
639
640extern "C" unsigned long int GetNumberOfQubits(void *sim) {
641 if (!sim)
642 return 0;
643 auto simulator = static_cast<Simulators::ISimulator *>(sim);
644 const size_t res = simulator->GetNumberOfQubits();
645 return static_cast<unsigned long int>(res);
646}
647
648extern "C" int ClearSimulator(void *sim) {
649 if (!sim)
650 return 0;
651 auto simulator = static_cast<Simulators::ISimulator *>(sim);
652 simulator->Clear();
653 return 1;
654}
655
656extern "C" unsigned long long int Measure(void *sim,
657 const unsigned long int *qubits,
658 unsigned long int nrQubits) {
659 if (!sim || !qubits || nrQubits == 0)
660 return 0;
661 auto simulator = static_cast<Simulators::ISimulator *>(sim);
662 Types::qubits_vector qubitVector(qubits, qubits + nrQubits);
663 const size_t res = simulator->Measure(qubitVector);
664 return static_cast<unsigned long long int>(res);
665}
666
667extern "C" int ApplyReset(void *sim, const unsigned long int *qubits,
668 unsigned long int nrQubits) {
669 if (!sim || !qubits || nrQubits == 0)
670 return 0;
671 auto simulator = static_cast<Simulators::ISimulator *>(sim);
672 Types::qubits_vector qubitVector(qubits, qubits + nrQubits);
673 simulator->ApplyReset(qubitVector);
674 return 1;
675}
676
677extern "C" double Probability(void *sim, unsigned long long int outcome) {
678 if (!sim)
679 return 0.0;
680 auto simulator = static_cast<Simulators::ISimulator *>(sim);
681 const double res = simulator->Probability(outcome);
682 return res;
683}
684
685extern "C" void FreeDoubleVector(double *vec) {
686 if (vec)
687 delete[] vec;
688}
689
690extern "C" void FreeULLIVector(unsigned long long int *vec) {
691 if (vec)
692 delete[] vec;
693}
694
695extern "C" double *Amplitude(void *sim, unsigned long long int outcome) {
696 if (!sim)
697 return nullptr;
698 auto simulator = static_cast<Simulators::ISimulator *>(sim);
699 const std::complex<double> amp = simulator->Amplitude(outcome);
700
701 double *result = new double[2];
702 result[0] = amp.real();
703 result[1] = amp.imag();
704 return result;
705}
706
707extern "C" double *AllProbabilities(void *sim) {
708 if (!sim)
709 return nullptr;
710 auto simulator = static_cast<Simulators::ISimulator *>(sim);
711 const auto probabilities = simulator->AllProbabilities();
712
713 double *result = new double[probabilities.size()];
714 std::copy(probabilities.begin(), probabilities.end(), result);
715 return result;
716}
717
718extern "C" double *Probabilities(void *sim,
719 const unsigned long long int *qubits,
720 unsigned long int nrQubits) {
721 if (!sim || !qubits || nrQubits == 0)
722 return nullptr;
723 auto simulator = static_cast<Simulators::ISimulator *>(sim);
724 Types::qubits_vector qubitVector(qubits, qubits + nrQubits);
725 const auto probabilities = simulator->Probabilities(qubitVector);
726
727 double *result = new double[probabilities.size()];
728 std::copy(probabilities.begin(), probabilities.end(), result);
729 return result;
730}
731
732extern "C" unsigned long long int *
733SampleCounts(void *sim, const unsigned long long int *qubits,
734 unsigned long int nrQubits, unsigned long int shots) {
735 if (!sim || !qubits || nrQubits == 0 || shots == 0)
736 return nullptr;
737
738 auto simulator = static_cast<Simulators::ISimulator *>(sim);
739 Types::qubits_vector qubitVector(qubits, qubits + nrQubits);
740 const auto counts = simulator->SampleCounts(qubitVector, shots);
741
742 unsigned long long int *result =
743 new unsigned long long int[counts.size() * 2];
744 size_t index = 0;
745 for (const auto &count : counts) {
746 result[index] = count.first; // outcome
747 ++index;
748 result[index] = count.second; // count
749 ++index;
750 }
751 return result;
752}
753
754extern "C" int GetSimulatorType(void *sim) {
755 if (!sim)
756 return -1;
757 auto simulator = static_cast<Simulators::ISimulator *>(sim);
758 return static_cast<int>(simulator->GetType());
759}
760
761extern "C" int GetSimulationType(void *sim) {
762 if (!sim)
763 return -1;
764 auto simulator = static_cast<Simulators::ISimulator *>(sim);
765 return static_cast<int>(simulator->GetSimulationType());
766}
767
768extern "C" int FlushSimulator(void *sim) {
769 if (!sim)
770 return 0;
771 auto simulator = static_cast<Simulators::ISimulator *>(sim);
772 simulator->Flush();
773 return 1;
774}
775
776extern "C" int SaveStateToInternalDestructive(void *sim) {
777 if (!sim)
778 return 0;
779 auto simulator = static_cast<Simulators::ISimulator *>(sim);
781 return 1;
782}
783
784extern "C" int RestoreInternalDestructiveSavedState(void *sim) {
785 if (!sim)
786 return 0;
787 auto simulator = static_cast<Simulators::ISimulator *>(sim);
789 return 1;
790}
791
792extern "C" int SaveState(void *sim) {
793 if (!sim)
794 return 0;
795 auto simulator = static_cast<Simulators::ISimulator *>(sim);
796 simulator->SaveState();
797 return 1;
798}
799
800extern "C" int RestoreState(void *sim) {
801 if (!sim)
802 return 0;
803 auto simulator = static_cast<Simulators::ISimulator *>(sim);
804 simulator->RestoreState();
805 return 1;
806}
807
808extern "C" int SetMultithreading(void *sim, int multithreading) {
809 if (!sim)
810 return 0;
811 auto simulator = static_cast<Simulators::ISimulator *>(sim);
812 simulator->SetMultithreading(multithreading != 0);
813 return 1;
814}
815
816extern "C" int GetMultithreading(void *sim) {
817 if (!sim)
818 return 0;
819 auto simulator = static_cast<Simulators::ISimulator *>(sim);
820 return simulator->GetMultithreading() ? 1 : 0;
821}
822
823extern "C" int IsQcsim(void *sim) {
824 if (!sim)
825 return 0;
826 auto simulator = static_cast<Simulators::ISimulator *>(sim);
827 return simulator->IsQcsim() ? 1 : 0;
828}
829
830extern "C" unsigned long long int MeasureNoCollapse(void *sim) {
831 if (!sim)
832 return 0;
833 auto simulator = static_cast<Simulators::ISimulator *>(sim);
834 return static_cast<unsigned long long int>(simulator->MeasureNoCollapse());
835}
int ApplyK(void *sim, int qubit)
double Probability(void *sim, unsigned long long int outcome)
char * GetConfiguration(void *sim, const char *key)
int InitializeSimulator(void *sim)
int RestoreState(void *sim)
void FreeDoubleVector(double *vec)
int ApplyRx(void *sim, int qubit, double theta)
int ApplyReset(void *sim, const unsigned long int *qubits, unsigned long int nrQubits)
int ApplyX(void *sim, int qubit)
int ApplyU(void *sim, int qubit, double theta, double phi, double lambda, double gamma)
int AddOptimizationSimulator(unsigned long int simHandle, int simType, int simExecType)
Definition Interface.cpp:89
unsigned long int CreateSimulator(int simType, int simExecType)
int ApplyCRy(void *sim, int controlQubit, int targetQubit, double theta)
int ApplyTDG(void *sim, int qubit)
int ApplyCSXDG(void *sim, int controlQubit, int targetQubit)
static std::atomic_bool isInitialized
Definition Interface.cpp:29
void FreeResult(char *result)
int ApplyS(void *sim, int qubit)
int ApplyCX(void *sim, int controlQubit, int targetQubit)
unsigned long int AllocateQubits(void *sim, unsigned long int nrQubits)
char * SimpleExecute(unsigned long int simpleSim, const char *circuitStr, const char *jsonConfig)
Definition Interface.cpp:99
int ApplyCRz(void *sim, int controlQubit, int targetQubit, double theta)
unsigned long int GetNumberOfQubits(void *sim)
double * AllProbabilities(void *sim)
void * GetMaestroObjectWithMute()
Definition Interface.cpp:48
unsigned long long int MeasureNoCollapse(void *sim)
int ApplyCP(void *sim, int controlQubit, int targetQubit, double theta)
int ApplySXDG(void *sim, int qubit)
void * GetMaestroObject()
Definition Interface.cpp:32
int GetMultithreading(void *sim)
int ApplySDG(void *sim, int qubit)
unsigned long long int Measure(void *sim, const unsigned long int *qubits, unsigned long int nrQubits)
int ApplyCSwap(void *sim, int controlQubit, int qubit1, int qubit2)
void DestroySimpleSimulator(unsigned long int simHandle)
Definition Interface.cpp:71
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)
double * Probabilities(void *sim, const unsigned long long int *qubits, unsigned long int nrQubits)
int SetMultithreading(void *sim, int multithreading)
int ApplyCU(void *sim, int controlQubit, int targetQubit, double theta, double phi, double lambda, double gamma)
void DestroySimulator(unsigned long int simHandle)
int ApplySwap(void *sim, int qubit1, int qubit2)
void * GetSimulator(unsigned long int simHandle)
static std::unique_ptr< Maestro > maestroInstance
Definition Interface.cpp:30
int ApplyRy(void *sim, int qubit, double theta)
int ApplyP(void *sim, int qubit, double theta)
int SaveStateToInternalDestructive(void *sim)
int ApplyCH(void *sim, int controlQubit, int targetQubit)
int FlushSimulator(void *sim)
unsigned long int CreateSimpleSimulator(int nrQubits)
Definition Interface.cpp:64
int GetSimulationType(void *sim)
int ResetSimulator(void *sim)
unsigned long long int * SampleCounts(void *sim, const unsigned long long int *qubits, unsigned long int nrQubits, unsigned long int shots)
int ApplySX(void *sim, int qubit)
int ApplyCZ(void *sim, int controlQubit, int targetQubit)
int ApplyRz(void *sim, int qubit, double theta)
int GetSimulatorType(void *sim)
int RestoreInternalDestructiveSavedState(void *sim)
int ApplyT(void *sim, int qubit)
int ApplyCRx(void *sim, int controlQubit, int targetQubit, double theta)
int ConfigureSimulator(void *sim, const char *key, const char *value)
int ApplyCSX(void *sim, int controlQubit, int targetQubit)
int ClearSimulator(void *sim)
int IsQcsim(void *sim)
int RemoveAllOptimizationSimulatorsAndAdd(unsigned long int simHandle, int simType, int simExecType)
Definition Interface.cpp:79
void FreeULLIVector(unsigned long long int *vec)
int SaveState(void *sim)
static boost::json::value ParseString(const char *str)
Parses a string containing json.
Definition Json.h:37
std::shared_ptr< Circuits::Circuit< Time > > ParseCircuit(const char *str) const
Definition Json.h:44
static std::string GetConfigString(const std::string &config, const boost::json::value &jsonConfig)
Definition Json.h:77
Interface class for a quantum computing simulator.
Definition Simulator.h:32
virtual void ApplyCRz(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta)=0
Applies a CRz gate to the qubits.
virtual void ApplySDG(Types::qubit_t qubit)=0
Applies a S dagger gate to the qubit.
virtual void ApplyCCX(Types::qubit_t qubit0, Types::qubit_t qubit1, Types::qubit_t qubit2)=0
Applies a controlled controlled not gate to the qubits.
virtual void ApplyX(Types::qubit_t qubit)=0
Applies a not gate to the qubit.
virtual void ApplyCSxDAG(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CSx dagger gate to the qubits.
virtual void ApplyU(Types::qubit_t qubit, double theta, double phi, double lambda, double gamma)=0
Applies a U gate to the qubit.
virtual void ApplyP(Types::qubit_t qubit, double lambda)=0
Applies a phase shift gate to the qubit.
virtual void ApplySx(Types::qubit_t qubit)=0
Applies a Sx gate to the qubit.
virtual void ApplyTDG(Types::qubit_t qubit)=0
Applies a T dagger gate to the qubit.
virtual void ApplyCU(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta, double phi, double lambda, double gamma)=0
Applies a controlled U gate to the qubits.
virtual void ApplyCRy(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta)=0
Applies a CRy gate to the qubits.
virtual void ApplyCX(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CX gate to the qubits.
virtual void ApplyRy(Types::qubit_t qubit, double theta)=0
Applies a Ry gate to the qubit.
virtual void ApplyCH(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CH gate to the qubits.
virtual void ApplySwap(Types::qubit_t qubit0, Types::qubit_t qubit1)=0
Applies a swap gate to the qubits.
virtual void ApplyCSx(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CSx gate to the qubits.
virtual void ApplyK(Types::qubit_t qubit)=0
Applies a K gate to the qubit.
virtual void ApplyY(Types::qubit_t qubit)=0
Applies a Y gate to the qubit.
virtual void ApplyT(Types::qubit_t qubit)=0
Applies a T gate to the qubit.
virtual void ApplyS(Types::qubit_t qubit)=0
Applies a S gate to the qubit.
virtual void ApplyCSwap(Types::qubit_t ctrl_qubit, Types::qubit_t qubit0, Types::qubit_t qubit1)=0
Applies a controlled swap gate to the qubits.
virtual void ApplyZ(Types::qubit_t qubit)=0
Applies a Z gate to the qubit.
virtual void ApplyRz(Types::qubit_t qubit, double theta)=0
Applies a Rz gate to the qubit.
virtual void ApplyCRx(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta)=0
Applies a CRx gate to the qubits.
virtual void ApplyCY(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CY gate to the qubits.
virtual void ApplyCZ(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CZ gate to the qubits.
virtual void ApplyH(Types::qubit_t qubit)=0
Applies a Hadamard gate to the qubit.
virtual void ApplyRx(Types::qubit_t qubit, double theta)=0
Applies a Rx gate to the qubit.
virtual void ApplySxDAG(Types::qubit_t qubit)=0
Applies a Sx dagger gate to the qubit.
virtual void ApplyCP(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double lambda)=0
Applies a CP gate to the qubits.
virtual bool IsQcsim() const =0
Returns if the simulator is a qcsim simulator.
virtual void SaveStateToInternalDestructive()=0
Saves the state to internal storage.
virtual void RestoreState()=0
Restores the state from the internally saved state.
virtual void SaveState()=0
Saves the state to internal storage.
virtual double Probability(Types::qubit_t outcome)=0
Returns the probability of the specified outcome.
virtual void Initialize()=0
Initializes the state.
virtual size_t AllocateQubits(size_t num_qubits)=0
Allocates qubits.
virtual std::vector< double > AllProbabilities()=0
Returns the probabilities of all possible outcomes.
virtual void RestoreInternalDestructiveSavedState()=0
Restores the state from the internally saved state.
virtual void SetMultithreading(bool multithreading=true)=0
Enable/disable multithreading.
virtual std::complex< double > Amplitude(Types::qubit_t outcome)=0
Returns the amplitude of the specified state.
virtual size_t GetNumberOfQubits() const =0
Returns the number of qubits.
virtual void Flush()=0
Flushes the applied operations.
virtual void Configure(const char *key, const char *value)=0
Configures the state.
virtual void Clear()=0
Clears the state.
virtual bool GetMultithreading() const =0
Get the multithreading flag.
virtual void Reset()=0
Just resets the state to 0.
virtual std::string GetConfiguration(const char *key) const =0
Returns configuration value.
std::shared_ptr< Circuits::Circuit< Time > > ParseAndTranslate(std::string &qasmInput)
Definition QasmCirc.h:29
SimulationType
The type of simulation.
Definition State.h:82
@ kStatevector
statevector simulation type
Definition State.h:83
@ kMatrixProductState
matrix product state simulation type
Definition State.h:84
@ kStabilizer
Clifford gates simulation type.
Definition State.h:85
@ kTensorNetwork
Tensor network simulation type.
Definition State.h:86
SimulatorType
The type of simulator.
Definition State.h:63
@ kCompositeQCSim
composite qcsim simulator type
Definition State.h:71
@ kQCSim
qcsim simulator type
Definition State.h:67
@ kQiskitAer
qiskit aer simulator type
Definition State.h:65
@ kCompositeQiskitAer
composite qiskit aer simulator type
Definition State.h:69
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
Definition Types.h:21