Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Individual.h
Go to the documentation of this file.
1
12#ifndef _INDIVIDUAL_H
13#define _INDIVIDUAL_H
14
15#ifdef INCLUDED_BY_FACTORY
16
17#include "../Utils/Alias.h"
18#include "Factory.h"
19#include <unordered_map>
20
21namespace Simulators {
22
23// TODO: Maybe use the pimpl idiom
24// https://en.cppreference.com/w/cpp/language/pimpl to hide the implementation
25// for good but during development this should be good enough
26namespace Private {
27
28class CompositeSimulator;
29
38class IndividualSimulator : public ISimulator {
39 friend class CompositeSimulator;
40
41 public:
48 IndividualSimulator(SimulatorType type =
49#ifdef NO_QISKIT_AER
51#else
53#endif
54 ) noexcept
55 : simulator(SimulatorsFactory::CreateSimulatorUnique(
57 }
58
66 void Reset() override { simulator->Reset(); }
67
79 inline void Join(size_t simId,
80 const std::unique_ptr<IndividualSimulator> &other,
81 std::vector<size_t> &qubitsMapToSim,
82 bool enableMultithreading) {
83 // 1. grab the state of both simulators (in the first phase, using
84 // Amplitude, but maybe something faster can be done)
85 // 2. join the states by a tensor product
86 const size_t nrQubits1 = GetNumberOfQubits();
87 const size_t nrBasisStates1 = 1ULL << nrQubits1;
88 const size_t nrQubits2 = other->GetNumberOfQubits();
89 const size_t nrBasisStates2 = 1ULL << nrQubits2;
90
91 const size_t newNrQubits = nrQubits1 + nrQubits2;
92 const size_t nrBasisStates = 1ULL << newNrQubits;
93
95 other->SaveStateToInternalDestructive();
96
97 if (GetType() == SimulatorType::kQCSim) {
98 if (enableMultithreading && nrBasisStates > OmpLimitJoin)
99 JoinOmpQcsim(nrQubits1, nrBasisStates1, nrBasisStates2, newNrQubits,
100 nrBasisStates, other, enableMultithreading);
101 else {
102 Eigen::VectorXcd newAmplitudes;
103 newAmplitudes.resize(nrBasisStates);
104
105 for (size_t state2 = 0; state2 < nrBasisStates2; ++state2) {
106 const auto ampl2 = other->AmplitudeRaw(state2);
107 const size_t state2Mask = state2 << nrQubits1;
108 for (size_t state1 = 0; state1 < nrBasisStates1; ++state1)
109 newAmplitudes[state2Mask | state1] = AmplitudeRaw(state1) * ampl2;
110 }
111
112 // 3. set the state of the current simulator to the joined state
113 // the original qubits of this simulator get mapped as they are
114 // the other ones get shifted to the left by the number of qubits of
115 // this simulator so transfer mapping keeping this in mind
116
117 // simulator->SetMultithreading(enableMultithreading);
118 simulator->InitializeState(
119 newNrQubits,
120 newAmplitudes); // this will end up by swapping the data from
121 // newAmplitudes to the simulator, no allocation
122 // and copying is done
123 }
124 }
125#ifndef NO_QISKIT_AER
126 else {
127 if (enableMultithreading && nrBasisStates > OmpLimitJoin)
128 JoinOmpAer(nrQubits1, nrBasisStates1, nrBasisStates2, newNrQubits,
129 nrBasisStates, other, enableMultithreading);
130 else {
131 AER::Vector<std::complex<double>> newAmplitudes(
132 nrBasisStates, false); // the false here avoids data
133 // initialization, it will be set anyway
134
135 for (size_t state2 = 0; state2 < nrBasisStates2; ++state2) {
136 const auto ampl2 = other->AmplitudeRaw(state2);
137 const size_t state2Mask = state2 << nrQubits1;
138 for (size_t state1 = 0; state1 < nrBasisStates1; ++state1)
139 newAmplitudes[state2Mask | state1] = AmplitudeRaw(state1) * ampl2;
140 }
141
142 // 3. set the state of the current simulator to the joined state
143 // the original qubits of this simulator get mapped as they are
144 // the other ones get shifted to the left by the number of qubits of
145 // this simulator so transfer mapping keeping this in mind
146 // simulator->SetMultithreading(enableMultithreading);
147 simulator->InitializeState(
148 newNrQubits,
149 newAmplitudes); // this will move the data from newAmplitudes to
150 // the simulator, no allocation and copying is done
151 }
152 }
153#endif
154
155 for (auto [origq, mapq] : other->GetQubitsMap()) {
156 qubitsMap[origq] = mapq + nrQubits1;
157 qubitsMapToSim[origq] = simId;
158 }
159 }
160
174 inline std::unique_ptr<IndividualSimulator> Split(size_t qubit,
175 bool qubitOutcome,
176 bool enableMultithreading) {
177 const size_t oldNrQubits = GetNumberOfQubits();
178 const size_t newNrQubits = oldNrQubits - 1;
179 const size_t nrBasisStates = 1ULL << newNrQubits;
180 const size_t localQubit = qubitsMap[qubit];
181
182 // the new simulator split from this one
183 // a one qubit one, containing the qubit that was measured or reset
184 // TODO: this can be optimized a little bit by directly initializing with
185 // the proper amplitudes, but I'm not sure if it's worth it
186 auto newSimulator = std::make_unique<IndividualSimulator>(GetType());
187 newSimulator->AllocateQubits(1);
188 newSimulator->GetQubitsMap()[qubit] =
189 0; // the qubit is mapped to the only local qubit in the new simulator,
190 // which is 0
191 newSimulator->SetMultithreading(enableMultithreading);
192 newSimulator->Initialize();
193 if (qubitOutcome) {
194 newSimulator->ApplyX(qubit);
195 // newSimulator->Flush();
196 }
197
198 qubitsMap.erase(qubit); // the qubit is removed from the current simulator
199
201
202 if (GetType() == SimulatorType::kQCSim) {
203 /*
204 if (nrBasisStates > OmpLimitSplit) // parallelization for assignment and
205 some bit manipulations, I must do some benchmarks to see if it's worth it
206 and find where the limit is SplitOmpQcsim(localQubit, newNrQubits,
207 nrBasisStates, qubitOutcome); else
208 */
209 {
210 // now the adjusted current simulator, without the removed qubit
211 Eigen::VectorXcd newAmplitudes;
212 newAmplitudes.resize(nrBasisStates);
213
214 // compute the new amplitudes
215
216 const size_t localQubitMask = 1ULL << localQubit;
217 const size_t maskLow = localQubitMask - 1ULL;
218 const size_t maskHigh = ~maskLow;
219 const size_t qubitMask = qubitOutcome ? localQubitMask : 0ULL;
220
221 for (size_t state = 0; state < nrBasisStates; ++state) {
222 const size_t stateLow = state & maskLow;
223 const size_t stateHigh = (state & maskHigh) << 1ULL;
224
225 newAmplitudes[state] = AmplitudeRaw(stateLow | stateHigh | qubitMask);
226 }
227
228 // simulator->SetMultithreading(enableMultithreading);
229 simulator->InitializeState(
230 newNrQubits,
231 newAmplitudes); // this will end up by swapping the data from
232 // newAmplitudes to the simulator, no allocation
233 // and copying is done
234 }
235 }
236#ifndef NO_QISKIT_AER
237 else {
238 /*
239 if (nrBasisStates > OmpLimitSplit) // parallelization for assignment and
240 some bit manipulations, I must do some benchmarks to see if it's worth it
241 and find where the limit is SplitOmpAer(localQubit, newNrQubits,
242 nrBasisStates, qubitOutcome); else
243 */
244 {
245 // now the adjusted current simulator, without the removed qubit
246 AER::Vector<std::complex<double>> newAmplitudes(
247 nrBasisStates, false); // the false here avoids data
248 // initialization, it will be set anyway
249
250 // compute the new amplitudes
251 const size_t localQubitMask = 1ULL << localQubit;
252 const size_t maskLow = localQubitMask - 1ULL;
253 const size_t maskHigh = ~maskLow;
254 const size_t qubitMask = qubitOutcome ? localQubitMask : 0ULL;
255
256 for (size_t state = 0; state < nrBasisStates; ++state) {
257 const size_t stateLow = state & maskLow;
258 const size_t stateHigh = (state & maskHigh) << 1ULL;
259
260 newAmplitudes[state] = AmplitudeRaw(stateLow | stateHigh | qubitMask);
261 }
262
263 // simulator->SetMultithreading(enableMultithreading);
264 simulator->InitializeState(
265 newNrQubits,
266 newAmplitudes); // this will move the data from newAmplitudes to
267 // the simulator, no allocation and copying is done
268 }
269 }
270#endif
271
272 // now adjust the local qubits map
273 for (auto &mapped : qubitsMap)
274 if (mapped.second > localQubit) --mapped.second;
275
276 return newSimulator;
277 }
278
288 std::complex<double> AmplitudeRaw(Types::qubit_t outcome) override {
289 return simulator->AmplitudeRaw(outcome);
290 }
291
301 void SaveStateToInternalDestructive() override {
302 simulator->SaveStateToInternalDestructive();
303 }
304
312 simulator->RestoreInternalDestructiveSavedState();
313 }
314
324 inline Types::qubits_vector ConvertQubits(
325 const Types::qubits_vector &qubits) {
326 Types::qubits_vector converted;
327 converted.reserve(qubits.size());
328
329 for (auto qubit : qubits)
330 if (HasQubit(qubit)) converted.emplace_back(qubitsMap[qubit]);
331
332 return converted;
333 }
334
343 inline bool HasQubit(Types::qubit_t qubit) const {
344 return qubitsMap.find(qubit) != qubitsMap.end();
345 }
346
355 inline Types::qubit_t ConvertOutcomeFromLocal(Types::qubit_t outcome) const {
356 Types::qubit_t res = 0;
357
358 for (auto [origQubit, localQubit] : qubitsMap)
359 if (outcome & (1ULL << localQubit)) res |= (1ULL << origQubit);
360
361 return res;
362 }
363
372 inline Types::qubit_t ConvertOutcomeFromGlobal(Types::qubit_t outcome) const {
373 Types::qubit_t res = 0;
374
375 for (auto [origQubit, localQubit] : qubitsMap)
376 if (outcome & (1ULL << origQubit)) res |= (1ULL << localQubit);
377
378 return res;
379 }
380
386 void SaveState() {
387 if (!simulator) return;
388 const size_t nrBasisStates = 1ULL << simulator->GetNumberOfQubits();
389 savedState.reserve(nrBasisStates);
390
391 for (Types::qubit_t state = 0; state < nrBasisStates; ++state)
392 savedState.emplace_back(simulator->Amplitude(state));
393 }
394
400 void ClearSavedState() { savedState.clear(); }
401
407 void RestoreState() {
408 if (!simulator) return;
409 const size_t nrQubits = simulator->GetNumberOfQubits();
410
411 simulator->Clear();
412 simulator->InitializeState(nrQubits, savedState);
413 ClearSavedState();
414 }
415
424 inline std::unordered_map<Types::qubit_t, Types::qubit_t> &GetQubitsMap() {
425 return qubitsMap;
426 }
427
436 inline const std::unordered_map<Types::qubit_t, Types::qubit_t> &
437 GetQubitsMap() const {
438 return qubitsMap;
439 }
440
447 void Initialize() override { simulator->Initialize(); }
448
461 void InitializeState(size_t num_qubits,
462 std::vector<std::complex<double>> &amplitudes) override {
463 simulator->InitializeState(num_qubits, amplitudes);
464 }
465
478 /*
479 void InitializeState(size_t num_qubits, std::vector<std::complex<double>,
480 avoid_init_allocator<std::complex<double>>>& amplitudes) override
481 {
482 simulator->InitializeState(num_qubits, amplitudes);
483 }
484 */
485
498#ifndef NO_QISKIT_AER
499 void InitializeState(size_t num_qubits,
500 AER::Vector<std::complex<double>> &amplitudes) override {
501 simulator->InitializeState(num_qubits, amplitudes);
502 }
503#endif
504
517 void InitializeState(size_t num_qubits,
518 Eigen::VectorXcd &amplitudes) override {
519 simulator->InitializeState(num_qubits, amplitudes);
520 }
521
530 void Configure(const char *key, const char *value) override {
531 simulator->Configure(key, value);
532 }
533
541 std::string GetConfiguration(const char *key) const override {
542 if (!simulator) return "";
543
544 return simulator->GetConfiguration(key);
545 }
546
554 size_t AllocateQubits(size_t num_qubits) override {
555 return simulator->AllocateQubits(num_qubits);
556 }
557
564 size_t GetNumberOfQubits() const override {
565 return simulator->GetNumberOfQubits();
566 }
567
575 void Clear() override { simulator->Clear(); }
576
586 size_t Measure(const Types::qubits_vector &qubits) override {
587 return ConvertOutcomeFromLocal(simulator->Measure(ConvertQubits(qubits)));
588 }
589
596 void ApplyReset(const Types::qubits_vector &qubits) override {
597 simulator->ApplyReset(ConvertQubits(qubits));
598 }
599
611 double Probability(Types::qubit_t outcome) override {
612 return simulator->Probability(ConvertOutcomeFromGlobal(outcome));
613 }
614
625 std::complex<double> Amplitude(Types::qubit_t outcome) override {
626 return simulator->Amplitude(ConvertOutcomeFromGlobal(outcome));
627 }
628
638 std::vector<double> AllProbabilities() override {
639 return simulator->AllProbabilities();
640 }
641
653 std::vector<double> Probabilities(
654 const Types::qubits_vector &qubits) override {
655 return simulator->Probabilities(ConvertQubits(qubits));
656 }
657
673 std::unordered_map<Types::qubit_t, Types::qubit_t> SampleCounts(
674 const Types::qubits_vector &qubits, size_t shots = 1000) override {
675 std::unordered_map<Types::qubit_t, Types::qubit_t> res;
676
677 const auto sc = simulator->SampleCounts(ConvertQubits(qubits), shots);
678
679 for (auto [outcome, count] : sc)
680 res[ConvertOutcomeFromLocal(outcome)] = count;
681
682 return res;
683 }
684
696 double ExpectationValue(const std::string &pauliString) override {
697 return simulator->ExpectationValue(pauliString);
698 }
699
707 SimulatorType GetType() const override { return simulator->GetType(); }
708
717 SimulationType GetSimulationType() const override {
718 return SimulationType::kStatevector;
719 }
720
728 void Flush() override { simulator->Flush(); }
729
730 // WARNING: for all the following functions, the call is supposed to be made
731 // after the proper joining (or splitting)!
732
740 void ApplyP(Types::qubit_t qubit, double lambda) override {
741 simulator->ApplyP(qubitsMap[qubit], lambda);
742 }
743
750 void ApplyX(Types::qubit_t qubit) override {
751 simulator->ApplyX(qubitsMap[qubit]);
752 }
753
760 void ApplyY(Types::qubit_t qubit) override {
761 simulator->ApplyY(qubitsMap[qubit]);
762 }
763
770 void ApplyZ(Types::qubit_t qubit) override {
771 simulator->ApplyZ(qubitsMap[qubit]);
772 }
773
780 void ApplyH(Types::qubit_t qubit) override {
781 simulator->ApplyH(qubitsMap[qubit]);
782 }
783
790 void ApplyS(Types::qubit_t qubit) override {
791 simulator->ApplyS(qubitsMap[qubit]);
792 }
793
800 void ApplySDG(Types::qubit_t qubit) override {
801 simulator->ApplySDG(qubitsMap[qubit]);
802 }
803
810 void ApplyT(Types::qubit_t qubit) override {
811 simulator->ApplyT(qubitsMap[qubit]);
812 }
813
820 void ApplyTDG(Types::qubit_t qubit) override {
821 simulator->ApplyTDG(qubitsMap[qubit]);
822 }
823
830 void ApplySx(Types::qubit_t qubit) override {
831 simulator->ApplySx(qubitsMap[qubit]);
832 }
833
840 void ApplySxDAG(Types::qubit_t qubit) override {
841 simulator->ApplySxDAG(qubitsMap[qubit]);
842 }
843
850 void ApplyK(Types::qubit_t qubit) override {
851 simulator->ApplyK(qubitsMap[qubit]);
852 }
853
861 void ApplyRx(Types::qubit_t qubit, double theta) override {
862 simulator->ApplyRx(qubitsMap[qubit], theta);
863 }
864
872 void ApplyRy(Types::qubit_t qubit, double theta) override {
873 simulator->ApplyRy(qubitsMap[qubit], theta);
874 }
875
883 void ApplyRz(Types::qubit_t qubit, double theta) override {
884 simulator->ApplyRz(qubitsMap[qubit], theta);
885 }
886
897 void ApplyU(Types::qubit_t qubit, double theta, double phi, double lambda,
898 double gamma) override {
899 simulator->ApplyU(qubitsMap[qubit], theta, phi, lambda, gamma);
900 }
901
909 void ApplyCX(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) override {
910 simulator->ApplyCX(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit]);
911 }
912
920 void ApplyCY(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) override {
921 simulator->ApplyCY(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit]);
922 }
923
931 void ApplyCZ(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) override {
932 simulator->ApplyCZ(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit]);
933 }
934
943 void ApplyCP(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit,
944 double lambda) override {
945 simulator->ApplyCP(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit], lambda);
946 }
947
956 void ApplyCRx(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit,
957 double theta) override {
958 simulator->ApplyCRx(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit], theta);
959 }
960
969 void ApplyCRy(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit,
970 double theta) override {
971 simulator->ApplyCRy(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit], theta);
972 }
973
982 void ApplyCRz(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit,
983 double theta) override {
984 simulator->ApplyCRz(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit], theta);
985 }
986
994 void ApplyCH(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) override {
995 simulator->ApplyCH(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit]);
996 }
997
1005 void ApplyCSx(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) override {
1006 simulator->ApplyCSx(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit]);
1007 }
1008
1016 void ApplyCSxDAG(Types::qubit_t ctrl_qubit,
1017 Types::qubit_t tgt_qubit) override {
1018 simulator->ApplyCSxDAG(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit]);
1019 }
1020
1028 void ApplySwap(Types::qubit_t qubit0, Types::qubit_t qubit1) override {
1029 simulator->ApplySwap(qubitsMap[qubit0], qubitsMap[qubit1]);
1030 }
1031
1040 void ApplyCCX(Types::qubit_t qubit0, Types::qubit_t qubit1,
1041 Types::qubit_t qubit2) override {
1042 simulator->ApplyCCX(qubitsMap[qubit0], qubitsMap[qubit1],
1043 qubitsMap[qubit2]);
1044 }
1045
1054 void ApplyCSwap(Types::qubit_t ctrl_qubit, Types::qubit_t qubit0,
1055 Types::qubit_t qubit1) override {
1056 simulator->ApplyCSwap(qubitsMap[ctrl_qubit], qubitsMap[qubit0],
1057 qubitsMap[qubit1]);
1058 }
1059
1071 void ApplyCU(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit,
1072 double theta, double phi, double lambda, double gamma) override {
1073 simulator->ApplyCU(qubitsMap[ctrl_qubit], qubitsMap[tgt_qubit], theta, phi,
1074 lambda, gamma);
1075 }
1076
1077 void ApplyNop() override { simulator->ApplyNop(); }
1078
1087 void SetMultithreading(bool multithreading = true) override {
1088 if (simulator) simulator->SetMultithreading(multithreading);
1089
1090 processor_count =
1091 multithreading ? QC::QubitRegister<>::GetNumberOfThreads() : 1;
1092 }
1093
1101 bool GetMultithreading() const override {
1102 if (simulator) return simulator->GetMultithreading();
1103
1104 return false;
1105 }
1106
1117 bool IsQcsim() const override {
1118 return GetType() == Simulators::SimulatorType::kQCSim;
1119 }
1120
1134 Types::qubit_t MeasureNoCollapse() override {
1135 return ConvertOutcomeFromLocal(simulator->MeasureNoCollapse());
1136 }
1137
1148 std::unique_ptr<ISimulator> Clone() override {
1149 auto cloned = std::make_unique<IndividualSimulator>();
1150
1151 cloned->qubitsMap =
1152 qubitsMap;
1154 cloned->savedState =
1155 savedState;
1157 cloned->simulator = simulator->Clone();
1159 return cloned;
1160 }
1161
1162 Types::qubit_t SampleFromAlias() {
1163 if (!alias || !simulator) return 0;
1164
1165 double prob = 0.0;
1166 if (GetType() == SimulatorType::kQCSim) {
1167 // qcsim - convert 'simulator' to qcsim simulator and access 'state' (from
1168 // there the statevector is accessible)
1169 QCSimSimulator *qcsim = dynamic_cast<QCSimSimulator *>(simulator.get());
1170 prob = 1. - qcsim->uniformZeroOne(qcsim->rng);
1171 }
1172#ifndef NO_QISKIT_AER
1173 else {
1174 // qiskit aer - convert 'simulator' to qiskit aer simulator and access
1175 // 'savedAmplitudes' (assumes destructive saving of the state)
1176#ifndef NO_QISKIT_AER
1177 AerSimulator *aer = dynamic_cast<AerSimulator *>(simulator.get());
1178 prob = 1 - aer->uniformZeroOne(aer->rng);
1179#else
1180 return 0;
1181#endif
1182 }
1183#endif
1184
1185 const size_t measRaw = alias->Sample(prob);
1186
1187 return ConvertOutcomeFromLocal(measRaw);
1188 }
1189
1190 private:
1191 void InitializeAlias() {
1192 // TODO: implement it!
1193 if (GetType() == SimulatorType::kQCSim) {
1194 // qcsim - convert 'simulator' to qcsim simulator and access 'state' (from
1195 // there the statevector is accessible)
1196 QCSimSimulator *qcsim = dynamic_cast<QCSimSimulator *>(simulator.get());
1197
1198 alias = std::unique_ptr<Utils::Alias>(
1199 new Utils::Alias(qcsim->state->getRegisterStorage()));
1200 }
1201#ifndef NO_QISKIT_AER
1202 else {
1203 // qiskit aer - convert 'simulator' to qiskit aer simulator and access
1204 // 'savedAmplitudes' (assumes destructive saving of the state)
1205#ifndef NO_QISKIT_AER
1206 AerSimulator *aer = dynamic_cast<AerSimulator *>(simulator.get());
1207 if (aer) {
1208 alias = std::unique_ptr<Utils::Alias>(
1209 new Utils::Alias(aer->savedAmplitudes));
1210 }
1211#else
1212 // If we are here, it means it's not QCSim, but Qiskit is disabled.
1213 throw std::runtime_error("Qiskit Aer is disabled in this build.");
1214#endif
1215 }
1216#endif
1217 }
1218
1219 void ClearAlias() { alias = nullptr; }
1220
1233#ifndef NO_QISKIT_AER
1234 inline void JoinOmpAer(size_t nrQubits1, size_t nrBasisStates1,
1235 size_t nrBasisStates2, size_t newNrQubits,
1236 size_t nrBasisStates,
1237 const std::unique_ptr<IndividualSimulator> &other,
1238 bool enableMultithreading) {
1239 AER::Vector<std::complex<double>> newAmplitudes(
1240 nrBasisStates, false); // the false here avoids data initialization, it
1241 // will be set anyway
1242
1243 /*
1244 const size_t state1Mask = nrBasisStates1 - 1ULL;
1245
1246#pragma omp parallel for num_threads(processor_count) schedule(static,
1247OmpLimitJoin / divSchedule) for (long long int state = 0; state <
1248static_cast<long long int>(nrBasisStates); ++state) newAmplitudes[state] =
1249AmplitudeRaw(state & state1Mask) * other->AmplitudeRaw(state >> nrQubits1);
1250 */
1251
1252 // TODO: check if this is better
1253#pragma omp parallel for num_threads(processor_count)
1254 for (long long int state2 = 0;
1255 state2 < static_cast<long long int>(nrBasisStates2); ++state2) {
1256 const auto ampl2 = other->AmplitudeRaw(state2);
1257 const size_t state2Mask = state2 << nrQubits1;
1258 for (size_t state1 = 0; state1 < nrBasisStates1; ++state1)
1259 newAmplitudes[state2Mask | state1] = AmplitudeRaw(state1) * ampl2;
1260 }
1261
1262 // 3. set the state of the current simulator to the joined state
1263 // the original qubits of this simulator get mapped as they are
1264 // the other ones get shifted to the left by the number of qubits of this
1265 // simulator so transfer mapping keeping this in mind
1266 // simulator->SetMultithreading(enableMultithreading);
1267 simulator->InitializeState(
1268 newNrQubits,
1269 newAmplitudes); // this will move the data from newAmplitudes to the
1270 // simulator, no allocation and copying is done
1271 }
1272#endif
1273
1284 /*
1285 inline void SplitOmpAer(size_t localQubit, size_t newNrQubits, size_t
1286nrBasisStates, bool qubitOutcome = false)
1287 {
1288 // now the adjusted current simulator, without the removed qubit
1289 AER::Vector<std::complex<double>> newAmplitudes(nrBasisStates, false);
1290// the false here avoids data initialization, it will be set anyway
1291
1292 // compute the new amplitudes
1293
1294 const size_t localQubitMask = 1ULL << localQubit;
1295 const size_t maskLow = localQubitMask - 1ULL;
1296 const size_t maskHigh = ~maskLow;
1297 const size_t qubitMask = qubitOutcome ? localQubitMask : 0ULL;
1298
1299#pragma omp parallel for num_threads(processor_count) schedule(static,
1300OmpLimitSplit / divSchedule) for (long long int state = 0; state <
1301static_cast<long long int>(nrBasisStates); ++state)
1302 {
1303 const size_t stateLow = state & maskLow;
1304 const size_t stateHigh = (state & maskHigh) << 1ULL;
1305
1306 newAmplitudes[state] = AmplitudeRaw(stateLow | stateHigh |
1307qubitMask);
1308 }
1309
1310 simulator->InitializeState(newNrQubits, newAmplitudes); // this will
1311move the data from newAmplitudes to the simulator, no allocation and copying is
1312done
1313 }
1314 */
1315
1328 inline void JoinOmpQcsim(size_t nrQubits1, size_t nrBasisStates1,
1329 size_t nrBasisStates2, size_t newNrQubits,
1330 size_t nrBasisStates,
1331 const std::unique_ptr<IndividualSimulator> &other,
1332 bool enableMultithreading) {
1333 Eigen::VectorXcd newAmplitudes;
1334 newAmplitudes.resize(nrBasisStates);
1335
1336 /*
1337 const size_t state1Mask = nrBasisStates1 - 1ULL;
1338
1339#pragma omp parallel for num_threads(processor_count) schedule(static,
1340OmpLimitJoin / divSchedule) for (long long int state = 0; state <
1341static_cast<long long int>(nrBasisStates); ++state) newAmplitudes[state] =
1342AmplitudeRaw(state & state1Mask) * other->AmplitudeRaw(state >> nrQubits1);
1343 */
1344
1345 // TODO: check if this is better
1346#pragma omp parallel for num_threads(processor_count)
1347 for (long long int state2 = 0;
1348 state2 < static_cast<long long int>(nrBasisStates2); ++state2) {
1349 const auto ampl2 = other->AmplitudeRaw(state2);
1350 const size_t state2Mask = state2 << nrQubits1;
1351 for (size_t state1 = 0; state1 < nrBasisStates1; ++state1)
1352 newAmplitudes[state2Mask | state1] = AmplitudeRaw(state1) * ampl2;
1353 }
1354
1355 // 3. set the state of the current simulator to the joined state
1356 // the original qubits of this simulator get mapped as they are
1357 // the other ones get shifted to the left by the number of qubits of this
1358 // simulator so transfer mapping keeping this in mind
1359 // simulator->SetMultithreading(enableMultithreading);
1360 simulator->InitializeState(
1361 newNrQubits, newAmplitudes); // this will end up by swapping the data
1362 // from newAmplitudes to the simulator, no
1363 // allocation and copying is done
1364 }
1365
1376 /*
1377 inline void SplitOmpQcsim(size_t localQubit, size_t newNrQubits, size_t
1378nrBasisStates, bool qubitOutcome = false)
1379 {
1380 // now the adjusted current simulator, without the removed qubit
1381 Eigen::VectorXcd newAmplitudes;
1382 newAmplitudes.resize(nrBasisStates);
1383
1384 // compute the new amplitudes
1385 const size_t localQubitMask = 1ULL << localQubit;
1386 const size_t maskLow = localQubitMask - 1ULL;
1387 const size_t maskHigh = ~maskLow;
1388 const size_t qubitMask = qubitOutcome ? localQubitMask : 0ULL;
1389
1390#pragma omp parallel for num_threads(processor_count) schedule(static,
1391OmpLimitSplit / divSchedule) for (long long int state = 0; state <
1392static_cast<long long int>(nrBasisStates); ++state)
1393 {
1394 const size_t stateLow = state & maskLow;
1395 const size_t stateHigh = (state & maskHigh) << 1ULL;
1396
1397 newAmplitudes[state] = AmplitudeRaw(stateLow | stateHigh |
1398qubitMask);
1399 }
1400
1401 simulator->InitializeState(newNrQubits, newAmplitudes); // this will
1402end up by swapping the data from newAmplitudes to the simulator, no allocation
1403and copying is done
1404 }
1405 */
1406
1407 std::unordered_map<Types::qubit_t, Types::qubit_t>
1408 qubitsMap;
1410 std::unique_ptr<ISimulator> simulator;
1411 std::vector<std::complex<double>>
1412 savedState;
1415 std::unique_ptr<Utils::Alias>
1416 alias;
1418 int processor_count =
1419 QC::QubitRegister<>::GetNumberOfThreads();
1423 // constexpr static int divSchedule = 4;
1424 constexpr static size_t OmpLimitJoin = 4096 * 2;
1425 // constexpr static size_t OmpLimitSplit = OmpLimitJoin * 16;
1426};
1427
1428} // namespace Private
1429} // namespace Simulators
1430
1431#endif
1432#endif
int ApplyK(void *sim, int qubit)
double Probability(void *sim, unsigned long long int outcome)
char * GetConfiguration(void *sim, const char *key)
int RestoreState(void *sim)
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 ApplyCRy(void *sim, int controlQubit, int targetQubit, double theta)
int ApplyTDG(void *sim, int qubit)
int ApplyS(void *sim, int qubit)
int ApplyCX(void *sim, int controlQubit, int targetQubit)
unsigned long int AllocateQubits(void *sim, unsigned long int nrQubits)
int ApplyCRz(void *sim, int controlQubit, int targetQubit, double theta)
unsigned long int GetNumberOfQubits(void *sim)
double * AllProbabilities(void *sim)
unsigned long long int MeasureNoCollapse(void *sim)
int ApplyCP(void *sim, int controlQubit, int targetQubit, double theta)
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)
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)
int ApplySwap(void *sim, int qubit1, int qubit2)
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 GetSimulationType(void *sim)
unsigned long long int * SampleCounts(void *sim, const unsigned long long int *qubits, unsigned long int nrQubits, unsigned long int shots)
int ApplyCZ(void *sim, int controlQubit, int targetQubit)
int ApplyRz(void *sim, int qubit, double theta)
int RestoreInternalDestructiveSavedState(void *sim)
int ApplyT(void *sim, int qubit)
int ApplyCRx(void *sim, int controlQubit, int targetQubit, double theta)
int IsQcsim(void *sim)
int SaveState(void *sim)
SimulationType
The type of simulation.
Definition State.h:79
@ kStatevector
statevector simulation type
SimulatorType
The type of simulator.
Definition State.h:63
@ kQCSim
qcsim simulator type
@ kQiskitAer
qiskit aer simulator type