-
Notifications
You must be signed in to change notification settings - Fork 21
/
Architecture.hpp
548 lines (476 loc) · 19.4 KB
/
Architecture.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//
// This file is part of the MQT QMAP library released under the MIT license.
// See README.md or go to https://github.com/cda-tum/qmap for more information.
//
#pragma once
#include "configuration/AvailableArchitecture.hpp"
#include "nlohmann/json.hpp"
#include "utils.hpp"
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <regex>
#include <unordered_map>
#include <unordered_set>
#include <vector>
constexpr std::uint8_t GATES_OF_BIDIRECTIONAL_SWAP = 3U;
constexpr std::uint8_t GATES_OF_UNIDIRECTIONAL_SWAP = 7U;
constexpr std::uint8_t GATES_OF_DIRECTION_REVERSE = 4U;
constexpr std::uint8_t GATES_OF_TELEPORTATION = 7U;
constexpr std::uint32_t COST_SINGLE_QUBIT_GATE = 1;
constexpr std::uint32_t COST_CNOT_GATE = 10;
constexpr std::uint32_t COST_MEASUREMENT = 10;
constexpr std::uint32_t COST_UNIDIRECTIONAL_SWAP =
3 * COST_CNOT_GATE + 4 * COST_SINGLE_QUBIT_GATE;
constexpr std::uint32_t COST_BIDIRECTIONAL_SWAP = 3 * COST_CNOT_GATE;
constexpr std::uint32_t COST_TELEPORTATION =
2 * COST_CNOT_GATE + COST_MEASUREMENT + 4 * COST_SINGLE_QUBIT_GATE;
constexpr std::uint32_t COST_DIRECTION_REVERSE = 4 * COST_SINGLE_QUBIT_GATE;
constexpr std::uint16_t MAX_DEVICE_QUBITS = 128;
class Architecture {
public:
class Properties {
protected:
template <class KeyType, class ValueType> class Property {
public:
Property() = default;
[[nodiscard]] auto& get(const KeyType& key) { return props[key]; }
[[nodiscard]] const auto& get(const KeyType& key) const {
return props.at(key);
}
[[nodiscard]] const auto& get() const { return props; }
void set(const KeyType& key, const ValueType& value) {
props[key] = value;
}
[[nodiscard, gnu::pure]] bool available(const KeyType& key) const {
return props.find(key) != props.end();
}
void clear() { props.clear(); }
[[nodiscard]] bool empty() const { return props.empty(); }
protected:
std::map<KeyType, ValueType> props{};
};
public:
Properties() = default;
[[nodiscard]] std::string getName() const { return name; }
void setName(const std::string& propertiesName) { name = propertiesName; }
[[nodiscard]] std::uint16_t getNqubits() const { return nq; }
void setNqubits(std::uint16_t nqs) { nq = nqs; }
Property<std::uint16_t, Property<qc::OpType, double>>
singleQubitErrorRate{};
Property<std::uint16_t,
Property<std::uint16_t, Property<qc::OpType, double>>>
twoQubitErrorRate{};
Property<std::uint16_t, double> readoutErrorRate{};
Property<std::uint16_t, double> t1Time{};
Property<std::uint16_t, double> t2Time{};
Property<std::uint16_t, double> qubitFrequency{};
Property<std::uint16_t, std::string> calibrationDate{};
// convenience functions
void setSingleQubitErrorRate(std::uint16_t qubit,
const std::string& operation,
double errorRate) {
singleQubitErrorRate.get(qubit).set(qc::opTypeFromString(operation),
errorRate);
}
[[nodiscard]] double
getSingleQubitErrorRate(std::uint16_t qubit,
const std::string& operation) const {
return singleQubitErrorRate.get(qubit).get(
qc::opTypeFromString(operation));
}
[[nodiscard]] double
getAverageSingleQubitErrorRate(const std::uint16_t qubit) const {
double avgErrorRate = 0.0;
for (const auto& [opType, error] :
singleQubitErrorRate.get(qubit).get()) {
avgErrorRate += error;
}
return avgErrorRate /
static_cast<double>(singleQubitErrorRate.get(qubit).get().size());
}
void setTwoQubitErrorRate(std::uint16_t qubit1, std::uint16_t qubit2,
double errorRate,
const std::string& operation = "cx") {
twoQubitErrorRate.get(qubit1).get(qubit2).set(
qc::opTypeFromString(operation), errorRate);
}
[[nodiscard]] double
getTwoQubitErrorRate(std::uint16_t qubit1, std::uint16_t qubit2,
const std::string& operation = "cx") const {
return twoQubitErrorRate.get(qubit1).get(qubit2).get(
qc::opTypeFromString(operation));
}
[[nodiscard]] bool
twoQubitErrorRateAvailable(std::uint16_t qubit1, std::uint16_t qubit2,
const std::string& operation = "cx") const {
return twoQubitErrorRate.available(qubit1) &&
twoQubitErrorRate.get(qubit1).available(qubit2) &&
twoQubitErrorRate.get(qubit1).get(qubit2).available(
qc::opTypeFromString(operation));
}
void clear() {
singleQubitErrorRate.clear();
twoQubitErrorRate.clear();
readoutErrorRate.clear();
t1Time.clear();
t2Time.clear();
qubitFrequency.clear();
calibrationDate.clear();
}
[[nodiscard]] bool empty() const {
return singleQubitErrorRate.empty() && twoQubitErrorRate.empty() &&
readoutErrorRate.empty() && t1Time.empty() && t2Time.empty() &&
qubitFrequency.empty() && calibrationDate.empty();
}
[[nodiscard]] nlohmann::json json() const {
nlohmann::json json;
if (empty()) {
return json;
}
json["name"] = name;
json["qubits"] = {};
for (std::uint16_t i = 0U; i < nq; ++i) {
auto& qubitProperties = json["qubits"][std::to_string(i)];
if (singleQubitErrorRate.available(i)) {
auto& singleQubitErrorRates =
qubitProperties["single_qubit_error_rate"];
for (const auto& [operation, error] :
singleQubitErrorRate.get(i).get()) {
singleQubitErrorRates[qc::toString(operation)] = error;
}
}
if (t1Time.available(i)) {
qubitProperties["t1_time"] = t1Time.get(i);
}
if (t2Time.available(i)) {
qubitProperties["t2_time"] = t2Time.get(i);
}
if (qubitFrequency.available(i)) {
qubitProperties["frequency"] = qubitFrequency.get(i);
}
if (calibrationDate.available(i)) {
qubitProperties["calibration_date"] = calibrationDate.get(i);
}
if (readoutErrorRate.available(i)) {
qubitProperties["readout_error_rate"] = readoutErrorRate.get(i);
}
if (twoQubitErrorRate.available(i)) {
auto& twoQubitErrorRates = qubitProperties["two_qubit_error_rate"];
for (const auto& [qubit2, errorRates] :
twoQubitErrorRate.get(i).get()) {
const std::string pair =
'(' + std::to_string(i) + ',' + std::to_string(qubit2) + ')';
auto& qubits = twoQubitErrorRates[pair];
for (const auto& [operation, error] : errorRates.get()) {
qubits[qc::toString(operation)] = error;
}
}
}
}
return json;
}
[[nodiscard]] std::string toString() const { return json().dump(2); }
protected:
std::string name{};
std::uint16_t nq{};
};
void loadCouplingMap(std::istream& is);
void loadCouplingMap(std::istream&& is);
void loadCouplingMap(const std::string& filename);
void loadCouplingMap(std::uint16_t nQ, const CouplingMap& cm);
void loadCouplingMap(AvailableArchitecture architecture);
void loadProperties(std::istream& is);
void loadProperties(std::istream&& is);
void loadProperties(const std::string& filename);
void loadProperties(const Properties& props);
Architecture() = default;
explicit Architecture(const std::string& cmFilename) {
loadCouplingMap(cmFilename);
}
Architecture(const std::string& cmFilename, const std::string& propsFilename)
: Architecture(cmFilename) {
loadProperties(propsFilename);
}
Architecture(std::uint16_t nQ, const CouplingMap& cm);
Architecture(std::uint16_t nQ, const CouplingMap& cm,
const Properties& props);
[[nodiscard]] std::uint16_t getNqubits() const { return nqubits; }
void setNqubits(std::uint16_t nQ) { nqubits = nQ; }
[[nodiscard]] const std::string& getName() const { return name; }
void setName(const std::string& architectureName) { name = architectureName; }
[[nodiscard]] const CouplingMap& getCouplingMap() const {
return couplingMap;
}
[[nodiscard]] CouplingMap& getCouplingMap() { return couplingMap; }
void setCouplingMap(const CouplingMap& cm) {
couplingMap = cm;
createDistanceTable();
}
[[nodiscard]] bool
isEdgeConnected(const Edge& edge, const bool considerDirection = true) const {
if (considerDirection) {
return couplingMap.find(edge) != couplingMap.end();
}
return couplingMap.find(edge) != couplingMap.end() ||
couplingMap.find({edge.second, edge.first}) != couplingMap.end();
}
[[nodiscard]] bool isEdgeBidirectional(const Edge& edge) const {
return couplingMap.find(edge) != couplingMap.end() &&
couplingMap.find({edge.second, edge.first}) != couplingMap.end();
}
CouplingMap& getCurrentTeleportations() { return currentTeleportations; }
std::vector<std::pair<std::int16_t, std::int16_t>>& getTeleportationQubits() {
return teleportationQubits;
}
[[nodiscard]] const Matrix&
getDistanceTable(bool includeReversalCost = true) const {
if (includeReversalCost) {
return distanceTableReversals;
}
return distanceTable;
}
[[nodiscard]] const Properties& getProperties() const { return properties; }
[[nodiscard]] Properties& getProperties() { return properties; }
void setProperties(const Properties& props) {
properties = props;
createFidelityTable();
}
[[nodiscard]] bool isFidelityAvailable() const { return fidelityAvailable; }
[[nodiscard]] const std::vector<Matrix>& getFidelityDistanceTables() const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
return fidelityDistanceTables;
}
[[nodiscard]] const Matrix&
getFidelityDistanceTable(std::size_t skipEdges) const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
if (skipEdges >= fidelityDistanceTables.size()) {
const static Matrix DEFAULT_MATRIX(nqubits,
std::vector<double>(nqubits, 0.0));
return DEFAULT_MATRIX;
}
return fidelityDistanceTables.at(skipEdges);
}
[[nodiscard]] const Matrix& getFidelityDistanceTable() const {
return getFidelityDistanceTable(0);
}
[[nodiscard]] double fidelityDistance(std::uint16_t q1, std::uint16_t q2,
std::size_t skipEdges) const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
if (q1 >= nqubits) {
throw QMAPException("Qubit out of range.");
}
if (q2 >= nqubits) {
throw QMAPException("Qubit out of range.");
}
if (skipEdges >= fidelityDistanceTables.size()) {
return 0.;
}
return fidelityDistanceTables.at(skipEdges).at(q1).at(q2);
}
[[nodiscard]] double fidelityDistance(std::uint16_t q1,
std::uint16_t q2) const {
return fidelityDistance(q1, q2, 0);
}
[[nodiscard]] const Matrix& getFidelityTable() const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
return fidelityTable;
}
[[nodiscard]] const std::vector<double>& getSingleQubitFidelities() const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
return singleQubitFidelities;
}
[[nodiscard]] const std::vector<double>& getSingleQubitFidelityCosts() const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
return singleQubitFidelityCosts;
}
[[nodiscard]] double getSingleQubitFidelityCost(std::uint16_t qbit) const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
if (qbit >= nqubits) {
throw QMAPException("Qubit out of range.");
}
return singleQubitFidelityCosts.at(qbit);
}
[[nodiscard]] const Matrix& getTwoQubitFidelityCosts() const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
return twoQubitFidelityCosts;
}
[[nodiscard]] double getTwoQubitFidelityCost(std::uint16_t q1,
std::uint16_t q2) const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
if (q1 >= nqubits) {
throw QMAPException("Qubit out of range.");
}
if (q2 >= nqubits) {
throw QMAPException("Qubit out of range.");
}
return twoQubitFidelityCosts.at(q1).at(q2);
}
[[nodiscard]] const Matrix& getSwapFidelityCosts() const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
return swapFidelityCosts;
}
[[nodiscard]] double getSwapFidelityCost(std::uint16_t q1,
std::uint16_t q2) const {
if (!fidelityAvailable) {
throw QMAPException("No fidelity data available.");
}
if (q1 >= nqubits) {
throw QMAPException("Qubit out of range.");
}
if (q2 >= nqubits) {
throw QMAPException("Qubit out of range.");
}
return swapFidelityCosts.at(q1).at(q2);
}
/** true if the coupling map contains no unidirectional edges */
[[nodiscard]] bool bidirectional() const { return isBidirectional; }
/** true if the coupling map contains no bidirectional edges */
[[nodiscard]] bool unidirectional() const { return isUnidirectional; }
[[nodiscard]] bool isArchitectureAvailable() const {
return !(name.empty()) && nqubits != 0;
}
[[nodiscard]] bool isCalibrationDataAvailable() const {
return !(name.empty()) && !properties.empty();
}
void reset() {
name = "";
nqubits = 0;
couplingMap.clear();
distanceTable.clear();
distanceTableReversals.clear();
isBidirectional = true;
isUnidirectional = true;
properties.clear();
fidelityAvailable = false;
fidelityTable.clear();
singleQubitFidelities.clear();
singleQubitFidelityCosts.clear();
twoQubitFidelityCosts.clear();
swapFidelityCosts.clear();
fidelityDistanceTables.clear();
}
[[nodiscard]] double distance(std::uint16_t control, std::uint16_t target,
bool includeReversalCost = true) const {
if (currentTeleportations.empty()) {
if (includeReversalCost) {
return distanceTableReversals.at(control).at(target);
}
return distanceTable.at(control).at(target);
}
return static_cast<double>(bfs(control, target, currentTeleportations));
}
[[nodiscard]] std::set<std::uint16_t> getQubitSet() const {
std::set<std::uint16_t> result{};
for (std::uint16_t i = 0; i < nqubits; ++i) {
result.insert(result.end(),
i); // should be constant with gcc, or at most O(nqubits)
}
return result;
}
std::uint64_t minimumNumberOfSwaps(std::vector<std::uint16_t>& permutation,
std::int64_t limit = -1);
void minimumNumberOfSwaps(std::vector<std::uint16_t>& permutation,
std::vector<Edge>& swaps);
struct Node {
std::uint64_t nswaps = 0U;
std::vector<Edge> swaps{};
std::unordered_map<std::uint16_t, std::uint16_t> permutation{};
void print(std::ostream& out) {
out << swaps.size() << ": ";
for (const auto& p : permutation) {
out << p.first << "->" << p.second << " ";
}
out << " | ";
for (const auto& swap : swaps) {
out << swap.first << "<->" << swap.second << " ";
}
out << std::endl;
}
};
[[nodiscard]] std::size_t getCouplingLimit() const;
[[nodiscard]] std::size_t
getCouplingLimit(const std::set<std::uint16_t>& qubitChoice) const;
void getHighestFidelityCouplingMap(std::uint16_t subsetSize,
CouplingMap& reducedMap) const;
[[nodiscard]] std::vector<QubitSubset>
getAllConnectedSubsets(std::uint16_t subsetSize) const;
void getReducedCouplingMaps(std::uint16_t subsetSize,
std::vector<CouplingMap>& couplingMaps) const;
void getReducedCouplingMap(const QubitSubset& qubitChoice,
CouplingMap& reducedMap) const;
[[nodiscard]] static double
getAverageArchitectureFidelity(const CouplingMap& cm,
const QubitSubset& qubitChoice,
const Properties& props);
[[nodiscard]] static QubitSubset getQubitSet(const CouplingMap& cm);
[[nodiscard]] static std::vector<std::uint16_t>
getQubitList(const CouplingMap& cm) {
const auto qubitSet = getQubitSet(cm);
return {qubitSet.begin(), qubitSet.end()};
}
static bool isConnected(const QubitSubset& qubitChoice,
const CouplingMap& reducedCouplingMap);
static void printCouplingMap(const CouplingMap& cm, std::ostream& os);
protected:
std::string name;
std::uint16_t nqubits = 0;
CouplingMap couplingMap = {};
CouplingMap currentTeleportations = {};
/** true if the coupling map contains no unidirectional edges */
bool isBidirectional = true;
/** true if the coupling map contains no bidirectional edges */
bool isUnidirectional = true;
// by this definition the empty coupling map is both bidirectional and
// unidirectional, and coupling maps containing both bidirectional and
// unidirectional edges are neither bidirectional nor unidirectional
Matrix distanceTable = {};
Matrix distanceTableReversals = {};
std::vector<std::pair<std::int16_t, std::int16_t>> teleportationQubits{};
Properties properties = {};
bool fidelityAvailable = false;
Matrix fidelityTable = {};
std::vector<double> singleQubitFidelities = {};
std::vector<double> singleQubitFidelityCosts = {};
Matrix twoQubitFidelityCosts = {};
Matrix swapFidelityCosts = {};
std::vector<Matrix> fidelityDistanceTables = {};
void createDistanceTable();
void createFidelityTable();
// added for teleportation
static bool contains(const std::vector<int>& v, const int e) {
return std::find(v.begin(), v.end(), e) != v.end();
}
[[nodiscard]] std::uint64_t bfs(std::uint16_t start, std::uint16_t goal,
const std::set<Edge>& teleportations) const;
static std::size_t findCouplingLimit(const CouplingMap& cm,
std::uint16_t nQubits);
static std::size_t
findCouplingLimit(const CouplingMap& cm, std::uint16_t nQubits,
const std::set<std::uint16_t>& qubitChoice);
static void findCouplingLimit(
std::uint16_t node, std::uint16_t curSum,
const std::vector<std::unordered_set<std::uint16_t>>& connections,
std::vector<std::uint16_t>& d, std::vector<bool>& visited);
};