Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Maestro.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#ifdef MAESTRO_ENABLE_COMPOSER
13#include <composer/Network/SimpleFullyConnectedNetwork.h>
14#else
16#endif
17
19
21{
22 public:
23 Maestro() = default;
24 ~Maestro() = default;
25
26 // Add methods to expose functionality to Maestro here.
27 // For example, methods to create circuits, simulators, run simulations, etc.
28 unsigned long int CreateSimpleSimulator(int nrQubits, int nrCbits = -1)
29 {
30 if (nrQubits <= 0)
31 return 0;
32
33 const std::vector<Types::qubit_t> qubits{static_cast<Types::qubit_t>(nrQubits)};
34 const std::vector<size_t> cbits{static_cast<size_t>(nrCbits < 0 ? nrQubits : nrCbits)};
35
36#ifdef MAESTRO_ENABLE_COMPOSER
37 auto network = std::make_shared<Network::SimpleNetwork<>>(qubits, cbits);
38#else
39 auto network = std::make_shared<Network::SimpleDisconnectedNetwork<>>(qubits, cbits);
40#endif
41
42 // lock the mutex to safely add the simulator
43 std::lock_guard<std::mutex> lock(simpleSimulatorsMutex);
44
45 if (curHandle == std::numeric_limits<unsigned long int>::max())
46 {
47 // Handle overflow, reset to 0
48 curHandle = 0;
49 }
50 const unsigned long int handle = ++curHandle;
51
52 simpleSimulators[handle] = network;
53
54 return handle;
55 }
56
57 void DestroySimpleSimulator(unsigned long int simHandle)
58 {
59 std::lock_guard<std::mutex> lock(simpleSimulatorsMutex);
60
61 simpleSimulators.erase(simHandle);
62 }
63
64 std::shared_ptr<Network::INetwork<>> GetSimpleSimulator(unsigned long int simHandle)
65 {
66 std::lock_guard<std::mutex> lock(simpleSimulatorsMutex);
67 auto it = simpleSimulators.find(simHandle);
68 if (it != simpleSimulators.end())
69 return it->second;
70
71 return nullptr;
72 }
73
75 {
76 auto sim = GetSimpleSimulator(simHandle);
77 if (!sim)
78 return 0;
79
80 sim->RemoveAllOptimizationSimulatorsAndAdd(simType, simExecType);
81
82 return 1;
83 }
84
85 int AddOptimizationSimulator(unsigned long int simHandle, Simulators::SimulatorType simType, Simulators::SimulationType simExecType)
86 {
87 auto sim = GetSimpleSimulator(simHandle);
88 if (!sim)
89 return 0;
90 sim->AddOptimizationSimulator(simType, simExecType);
91 return 1;
92 }
93
96 {
97 auto simulator = Simulators::SimulatorsFactory::CreateSimulator(simType, simExecType);
98
99 std::lock_guard<std::mutex> lock(simulatorsMutex);
100 if (curSimulatorHandle == std::numeric_limits<unsigned long int>::max())
101 {
102 // Handle overflow, reset to 0
103 curSimulatorHandle = 0;
104 }
105 const unsigned long int handle = ++curSimulatorHandle;
106
107 simulators[handle] = simulator;
108
109 return handle;
110 }
111
112 void *GetSimulator(unsigned long int simHandle)
113 {
114 std::lock_guard<std::mutex> lock(simulatorsMutex);
115 auto it = simulators.find(simHandle);
116 if (it != simulators.end())
117 return it->second.get();
118
119 return nullptr;
120 }
121
122 void DestroySimulator(unsigned long int simHandle)
123 {
124 std::lock_guard<std::mutex> lock(simulatorsMutex);
125 simulators.erase(simHandle);
126 }
127
128 private:
129 // allow multithreaded access
130 std::mutex simpleSimulatorsMutex;
131 std::mutex simulatorsMutex;
132
133 std::unordered_map<unsigned long int, std::shared_ptr<Network::INetwork<>>> simpleSimulators; // map for network simulators
134 std::unordered_map<unsigned long int, std::shared_ptr<Simulators::ISimulator>> simulators; // map for simulators
135
136 unsigned long int curHandle = 0;
137 unsigned long int curSimulatorHandle = 0; // current handle for simulators
138};
Maestro()=default
int RemoveAllOptimizationSimulatorsAndAdd(unsigned long int simHandle, Simulators::SimulatorType simType, Simulators::SimulationType simExecType)
Definition Maestro.h:74
unsigned long int CreateSimulator(Simulators::SimulatorType simType=Simulators::SimulatorType::kQCSim, Simulators::SimulationType simExecType=Simulators::SimulationType::kMatrixProductState)
Definition Maestro.h:94
void DestroySimulator(unsigned long int simHandle)
Definition Maestro.h:122
unsigned long int CreateSimpleSimulator(int nrQubits, int nrCbits=-1)
Definition Maestro.h:28
int AddOptimizationSimulator(unsigned long int simHandle, Simulators::SimulatorType simType, Simulators::SimulationType simExecType)
Definition Maestro.h:85
void DestroySimpleSimulator(unsigned long int simHandle)
Definition Maestro.h:57
std::shared_ptr< Network::INetwork<> > GetSimpleSimulator(unsigned long int simHandle)
Definition Maestro.h:64
~Maestro()=default
void * GetSimulator(unsigned long int simHandle)
Definition Maestro.h:112
static MAESTRO_FACTORY_EXPORT std::shared_ptr< ISimulator > CreateSimulator(SimulatorType t=SimulatorType::kQCSim, SimulationType method=SimulationType::kMatrixProductState)
Create a quantum computing simulator.
Definition Factory.cpp:45
SimulationType
The type of simulation.
Definition State.h:109
@ kMatrixProductState
matrix product state simulation type
Definition State.h:111
SimulatorType
The type of simulator.
Definition State.h:79
@ kQCSim
qcsim simulator type
Definition State.h:83
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:23