14#ifndef _GPU_STABILIZER_H
15#define _GPU_STABILIZER_H 1
27 explicit GpuStabilizer(
const std::shared_ptr<GpuLibrary> &lib,
int device = -1)
28 : device(device == -1 && lib ? lib->GetCreationDevice() : device), lib(lib), obj(
nullptr)
32 int GetGpuDevice()
const
34 return lib ? lib->GetStabilizerGpuId(obj) : -1;
37 GpuStabilizer() =
delete;
38 GpuStabilizer(
const GpuStabilizer &) =
delete;
39 GpuStabilizer &operator=(
const GpuStabilizer &) =
delete;
40 GpuStabilizer(GpuStabilizer &&) =
default;
41 GpuStabilizer &operator=(GpuStabilizer &&) =
default;
46 lib->DestroyStabilizerSimulator(obj);
49 bool CreateSimulator(
long long int numQubits,
long long int numShots,
long long int numMeasurements,
long long int numDetectors)
53 auto lock = lib->LockInitialization();
54 if (!lib->SetGpuDevice(device))
57 lib->DestroyStabilizerSimulator(obj);
59 obj = lib->CreateStabilizerSimulator(numQubits, numShots, numMeasurements, numDetectors);
60 return obj !=
nullptr;
66 bool ExecuteCircuit(
const std::string &circuitStr,
int randomizeMeasurements,
unsigned long long int seed)
69 return lib->ExecuteStabilizerCircuit(obj, circuitStr.c_str(), randomizeMeasurements, seed);
77 lib->DestroyStabilizerSimulator(obj);
85 long long GetNumQubits()
88 return lib->GetStabilizerNumQubits(obj);
92 long long GetNumShots()
95 return lib->GetStabilizerNumShots(obj);
99 long long GetNumMeasurements()
102 return lib->GetStabilizerNumMeasurements(obj);
106 long long GetNumDetectors()
109 return lib->GetStabilizerNumDetectors(obj);
113 bool IsCreated()
const
115 return obj !=
nullptr;
118 std::vector<std::vector<bool>> GetXTable()
121 return std::vector<std::vector<bool>>();
123 std::vector<unsigned int> xTableRaw(GetStabilizerXZTableSize());
124 lib->CopyStabilizerXTable(obj, xTableRaw.data());
126 return ConvertToBoolVectorVector(xTableRaw, GetNumQubits(), GetNumShots());
129 std::vector<std::vector<bool>> GetZTable()
132 return std::vector<std::vector<bool>>();
134 std::vector<unsigned int> zTableRaw(GetStabilizerXZTableSize());
135 lib->CopyStabilizerZTable(obj, zTableRaw.data());
137 return ConvertToBoolVectorVector(zTableRaw, GetNumQubits(), GetNumShots());
140 std::vector<std::vector<bool>> GetMTable()
143 return std::vector<std::vector<bool>>();
145 std::vector<unsigned int> mTableRaw(GetStabilizerMTableSize());
146 lib->CopyStabilizerMTable(obj, mTableRaw.data());
148 return ConvertToBoolVectorVector(mTableRaw, GetNumMeasurements(), GetNumShots());
151 bool InitXTable(
const std::vector<std::vector<bool>> &xTable)
155 const long long shots = GetNumShots();
156 const long long words_per_qubitormeas = (shots + 31) / 32;
157 const long long numQubits = GetNumQubits();
158 std::vector<unsigned int> xTableRaw(numQubits * words_per_qubitormeas, 0);
159 for (
long long qubit = 0; qubit < numQubits; ++qubit)
161 for (
long long shot = 0; shot < shots; ++shot)
163 if (xTable[qubit][shot])
165 xTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |= (1U << (shot % 32));
169 return lib->InitStabilizerXTable(obj, xTableRaw.data()) == 1;
172 bool InitXTableRepeat(
const std::vector<bool> &xTable)
177 const long long shots = GetNumShots();
178 const long long words_per_qubitormeas = (shots + 31) / 32;
179 const long long numQubits = GetNumQubits();
180 std::vector<unsigned int> xTableRaw(numQubits * words_per_qubitormeas, 0);
181 for (
long long qubit = 0; qubit < numQubits; ++qubit)
183 for (
long long shot = 0; shot < shots; ++shot)
187 xTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |= (1U << (shot % 32));
192 return lib->InitStabilizerXTable(obj, xTableRaw.data()) == 1;
195 bool InitZTable(
const std::vector<std::vector<bool>> &zTable)
199 const long long shots = GetNumShots();
200 const long long words_per_qubitormeas = (shots + 31) / 32;
201 const long long numQubits = GetNumQubits();
202 std::vector<unsigned int> zTableRaw(numQubits * words_per_qubitormeas, 0);
203 for (
long long qubit = 0; qubit < numQubits; ++qubit)
205 for (
long long shot = 0; shot < shots; ++shot)
207 if (zTable[qubit][shot])
209 zTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |= (1U << (shot % 32));
213 return lib->InitStabilizerZTable(obj, zTableRaw.data()) == 1;
216 bool InitZTableRepeat(
const std::vector<bool> &zTable)
221 const long long shots = GetNumShots();
222 const long long words_per_qubitormeas = (shots + 31) / 32;
223 const long long numQubits = GetNumQubits();
224 std::vector<unsigned int> zTableRaw(numQubits * words_per_qubitormeas, 0);
225 for (
long long qubit = 0; qubit < numQubits; ++qubit)
227 for (
long long shot = 0; shot < shots; ++shot)
231 zTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |= (1U << (shot % 32));
235 return lib->InitStabilizerZTable(obj, zTableRaw.data()) == 1;
239 std::vector<std::vector<bool>> ConvertToBoolVectorVector(
const std::vector<unsigned int> &tableRaw,
long long int num,
long long int shots)
241 const long long words_per_qubitormeas = (shots + 31) / 32;
243 std::vector<std::vector<bool>> result(num);
245 for (
long long qubitormeas = 0; qubitormeas < num; ++qubitormeas)
247 for (
long long shot = 0; shot < shots; ++shot)
249 const unsigned int word = tableRaw[qubitormeas * words_per_qubitormeas + (shot / 32)];
250 const bool bit = ((word >> (shot % 32)) & 1) == 1;
251 result[qubitormeas].push_back(bit);
258 long long GetStabilizerXZTableSize()
261 return lib->GetStabilizerXZTableSize(obj);
265 long long GetStabilizerMTableSize()
268 return lib->GetStabilizerMTableSize(obj);
272 long long GetStabilizerTableStrideMajor()
275 return lib->GetStabilizerTableStrideMajor(obj);
281 GpuDeviceContext lib;
unsigned long int CreateSimulator(int simType, int simExecType)
Bounded online routing based on current physical bond dimensions.