14#ifndef _GPU_STABILIZER_H
15#define _GPU_STABILIZER_H 1
25 explicit GpuStabilizer(
const std::shared_ptr<GpuLibrary> &lib)
26 : lib(lib), obj(
nullptr) {}
28 GpuStabilizer() =
delete;
29 GpuStabilizer(
const GpuStabilizer &) =
delete;
30 GpuStabilizer &operator=(
const GpuStabilizer &) =
delete;
31 GpuStabilizer(GpuStabilizer &&) =
default;
32 GpuStabilizer &operator=(GpuStabilizer &&) =
default;
35 if (lib && obj) lib->DestroyStabilizerSimulator(obj);
39 long long int numMeasurements,
40 long long int numDetectors) {
42 obj = lib->CreateStabilizerSimulator(numQubits, numShots, numMeasurements,
44 return obj !=
nullptr;
50 bool ExecuteCircuit(
const std::string &circuitStr,
int randomizeMeasurements,
51 unsigned long long int seed) {
53 return lib->ExecuteStabilizerCircuit(obj, circuitStr.c_str(),
54 randomizeMeasurements, seed);
60 lib->DestroyStabilizerSimulator(obj);
68 long long GetNumQubits() {
69 if (obj)
return lib->GetStabilizerNumQubits(obj);
73 long long GetNumShots() {
74 if (obj)
return lib->GetStabilizerNumShots(obj);
78 long long GetNumMeasurements() {
79 if (obj)
return lib->GetStabilizerNumMeasurements(obj);
83 long long GetNumDetectors() {
84 if (obj)
return lib->GetStabilizerNumDetectors(obj);
88 bool IsCreated()
const {
return obj !=
nullptr; }
90 std::vector<std::vector<bool>> GetXTable() {
91 if (!obj)
return std::vector<std::vector<bool>>();
93 std::vector<unsigned int> xTableRaw(GetStabilizerXZTableSize());
94 lib->CopyStabilizerXTable(obj, xTableRaw.data());
96 return ConvertToBoolVectorVector(xTableRaw, GetNumQubits(), GetNumShots());
99 std::vector<std::vector<bool>> GetZTable() {
100 if (!obj)
return std::vector<std::vector<bool>>();
102 std::vector<unsigned int> zTableRaw(GetStabilizerXZTableSize());
103 lib->CopyStabilizerZTable(obj, zTableRaw.data());
105 return ConvertToBoolVectorVector(zTableRaw, GetNumQubits(), GetNumShots());
108 std::vector<std::vector<bool>> GetMTable() {
109 if (!obj)
return std::vector<std::vector<bool>>();
111 std::vector<unsigned int> mTableRaw(GetStabilizerMTableSize());
112 lib->CopyStabilizerMTable(obj, mTableRaw.data());
114 return ConvertToBoolVectorVector(mTableRaw, GetNumMeasurements(),
118 bool InitXTable(
const std::vector<std::vector<bool>> &xTable) {
119 if (!obj)
return false;
120 const long long shots = GetNumShots();
121 const long long words_per_qubitormeas = (shots + 31) / 32;
122 const long long numQubits = GetNumQubits();
123 std::vector<unsigned int> xTableRaw(numQubits * words_per_qubitormeas, 0);
124 for (
long long qubit = 0; qubit < numQubits; ++qubit) {
125 for (
long long shot = 0; shot < shots; ++shot) {
126 if (xTable[qubit][shot]) {
127 xTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |=
132 return lib->InitStabilizerXTable(obj, xTableRaw.data()) == 1;
135 bool InitXTableRepeat(
const std::vector<bool> &xTable) {
137 if (!obj)
return false;
138 const long long shots = GetNumShots();
139 const long long words_per_qubitormeas = (shots + 31) / 32;
140 const long long numQubits = GetNumQubits();
141 std::vector<unsigned int> xTableRaw(numQubits * words_per_qubitormeas, 0);
142 for (
long long qubit = 0; qubit < numQubits; ++qubit) {
143 for (
long long shot = 0; shot < shots; ++shot) {
145 xTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |=
151 return lib->InitStabilizerXTable(obj, xTableRaw.data()) == 1;
154 bool InitZTable(
const std::vector<std::vector<bool>> &zTable) {
155 if (!obj)
return false;
156 const long long shots = GetNumShots();
157 const long long words_per_qubitormeas = (shots + 31) / 32;
158 const long long numQubits = GetNumQubits();
159 std::vector<unsigned int> zTableRaw(numQubits * words_per_qubitormeas, 0);
160 for (
long long qubit = 0; qubit < numQubits; ++qubit) {
161 for (
long long shot = 0; shot < shots; ++shot) {
162 if (zTable[qubit][shot]) {
163 zTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |=
168 return lib->InitStabilizerZTable(obj, zTableRaw.data()) == 1;
171 bool InitZTableRepeat(
const std::vector<bool> &zTable) {
173 if (!obj)
return false;
174 const long long shots = GetNumShots();
175 const long long words_per_qubitormeas = (shots + 31) / 32;
176 const long long numQubits = GetNumQubits();
177 std::vector<unsigned int> zTableRaw(numQubits * words_per_qubitormeas, 0);
178 for (
long long qubit = 0; qubit < numQubits; ++qubit) {
179 for (
long long shot = 0; shot < shots; ++shot) {
181 zTableRaw[qubit * words_per_qubitormeas + (shot / 32)] |=
186 return lib->InitStabilizerZTable(obj, zTableRaw.data()) == 1;
190 std::vector<std::vector<bool>> ConvertToBoolVectorVector(
191 const std::vector<unsigned int> &tableRaw,
long long int num,
192 long long int shots) {
193 const long long words_per_qubitormeas = (shots + 31) / 32;
195 std::vector<std::vector<bool>> result(num);
197 for (
long long qubitormeas = 0; qubitormeas < num; ++qubitormeas) {
198 for (
long long shot = 0; shot < shots; ++shot) {
199 const unsigned int word =
200 tableRaw[qubitormeas * words_per_qubitormeas + (shot / 32)];
201 const bool bit = ((word >> (shot % 32)) & 1) == 1;
202 result[qubitormeas].push_back(bit);
209 long long GetStabilizerXZTableSize() {
210 if (obj)
return lib->GetStabilizerXZTableSize(obj);
214 long long GetStabilizerMTableSize() {
215 if (obj)
return lib->GetStabilizerMTableSize(obj);
219 long long GetStabilizerTableStrideMajor() {
220 if (obj)
return lib->GetStabilizerTableStrideMajor(obj);
225 std::shared_ptr<GpuLibrary> lib;
unsigned long int CreateSimulator(int simType, int simExecType)