forked from cda-tum/mqt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleRandomCircuitSampling.cpp
151 lines (139 loc) · 4.28 KB
/
GoogleRandomCircuitSampling.cpp
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
#include "algorithms/GoogleRandomCircuitSampling.hpp"
#include <utility>
namespace qc {
GoogleRandomCircuitSampling::GoogleRandomCircuitSampling(
const std::string& filename) {
importGRCS(filename);
}
GoogleRandomCircuitSampling::GoogleRandomCircuitSampling(
std::string prefix, const std::uint16_t device, const std::uint16_t depth,
const std::uint16_t instance)
: layout(Bristlecone), pathPrefix(std::move(prefix)) {
std::stringstream ss;
ss << pathPrefix;
ss << "bristlecone/cz_v2/bris_";
ss << device;
ss << "/bris_";
ss << device;
ss << "_";
ss << depth;
ss << "_";
ss << instance;
ss << ".txt";
importGRCS(ss.str());
}
GoogleRandomCircuitSampling::GoogleRandomCircuitSampling(
std::string prefix, const std::uint16_t x, const std::uint16_t y,
const std::uint16_t depth, const std::uint16_t instance)
: pathPrefix(std::move(prefix)) {
std::stringstream ss;
ss << pathPrefix;
ss << "rectangular/cz_v2/";
ss << x;
ss << "x";
ss << y;
ss << "/inst_";
ss << x;
ss << "x";
ss << y;
ss << "_";
ss << depth;
ss << "_";
ss << instance;
ss << ".txt";
importGRCS(ss.str());
}
void GoogleRandomCircuitSampling::importGRCS(const std::string& filename) {
auto ifs = std::ifstream(filename);
if (!ifs.good()) {
std::cerr << "Error opening/reading from file: " << filename << "\n";
exit(3);
}
const std::size_t slash = filename.find_last_of('/');
const std::size_t dot = filename.find_last_of('.');
std::string benchmark = filename.substr(slash + 1, dot - slash - 1);
name = benchmark;
layout = (benchmark[0] == 'b') ? Bristlecone : Rectangular;
std::size_t nq{};
ifs >> nq;
addQubitRegister(nq);
addClassicalRegister(nq);
std::string line;
std::string identifier;
std::size_t control = 0;
std::size_t target = 0;
std::size_t cycle = 0;
while (std::getline(ifs, line)) {
if (line.empty()) {
continue;
}
std::stringstream ss(line);
ss >> cycle;
if (cycles.size() <= cycle) {
cycles.emplace_back();
}
ss >> identifier;
if (identifier == "cz") {
ss >> control;
ss >> target;
cycles[cycle].emplace_back(std::make_unique<StandardOperation>(
Control{static_cast<Qubit>(control)}, static_cast<Qubit>(target), Z));
} else if (identifier == "is") {
ss >> control;
ss >> target;
cycles[cycle].emplace_back(std::make_unique<StandardOperation>(
qc::Controls{}, static_cast<Qubit>(control),
static_cast<Qubit>(target), iSWAP));
} else {
ss >> target;
if (identifier == "h") {
cycles[cycle].emplace_back(
std::make_unique<StandardOperation>(static_cast<Qubit>(target), H));
} else if (identifier == "t") {
cycles[cycle].emplace_back(
std::make_unique<StandardOperation>(static_cast<Qubit>(target), T));
} else if (identifier == "x_1_2") {
cycles[cycle].emplace_back(std::make_unique<StandardOperation>(
static_cast<Qubit>(target), RX, std::vector{PI_2}));
} else if (identifier == "y_1_2") {
cycles[cycle].emplace_back(std::make_unique<StandardOperation>(
static_cast<Qubit>(target), RY, std::vector{PI_2}));
} else {
throw QFRException("Unknown gate '" + identifier);
}
}
}
}
std::size_t GoogleRandomCircuitSampling::getNops() const {
std::size_t nops = 0;
for (const auto& cycle : cycles) {
nops += cycle.size();
}
return nops;
}
std::ostream& GoogleRandomCircuitSampling::print(std::ostream& os) const {
std::size_t i = 0;
std::size_t j = 0;
for (const auto& cycle : cycles) {
os << "Cycle " << i++ << ":\n";
for (const auto& op : cycle) {
os << std::setw(static_cast<int>(std::log10(getNops()) + 1.)) << ++j
<< ": ";
op->print(os, initialLayout, 0U, nqubits);
os << "\n";
}
}
return os;
}
std::ostream&
GoogleRandomCircuitSampling::printStatistics(std::ostream& os) const {
os << "GoogleRandomCircuitSampling Statistics:\n";
os << "\tLayout: "
<< ((layout == Rectangular) ? "Rectangular" : "Bristlecone") << "\n";
os << "\tn: " << static_cast<std::size_t>(nqubits) << "\n";
os << "\tm: " << getNops() << "\n";
os << "\tc: 1 + " << cycles.size() - 2 << " + 1" << "\n";
os << "--------------" << "\n";
return os;
}
} // namespace qc