13
13
14
14
using namespace dd ::literals;
15
15
16
- int main (int argc, char ** argv) {
16
+ int main (int argc, char ** argv) { // NOLINT(bugprone-exception-escape)
17
17
cxxopts::Options options (" FRQI" , " with MQT DDSIM by https://www.cda.cit.tum.de/ -- Allowed options" );
18
18
// clang-format off
19
19
options.add_options ()
@@ -24,22 +24,22 @@ int main(int argc, char** argv) {
24
24
;
25
25
// clang-format on
26
26
auto vm = options.parse (argc, argv);
27
- if (vm.count (" help" )) {
27
+ if (vm.count (" help" ) > 0 ) {
28
28
std::cout << options.help ();
29
29
std::exit (0 );
30
30
}
31
31
32
- unsigned int numOfShots = vm[" shots" ].as <unsigned int >();
33
- std::string filename = vm[" file" ].as <std::string>()
32
+ const unsigned int numOfShots = vm[" shots" ].as <unsigned int >();
33
+ const std::string filename = vm[" file" ].as <std::string>();
34
34
35
- cv::Mat image,
36
- dest;
37
- cv::Mat genimg;
38
- cv::Size size (32 , 32 );
39
- dd::QubitCount nqubits = 11 ;
35
+ cv::Mat image;
36
+ cv::Mat dest;
37
+ const cv::Mat genimg;
38
+ const cv::Size size (32 , 32 );
39
+ const dd::QubitCount nqubits = 11 ;
40
40
41
41
image = imread (filename, cv::IMREAD_GRAYSCALE);
42
- if (! image.data ) {
42
+ if (image.data == nullptr ) {
43
43
std::cerr << " Could not open or find the image '" << filename << " '" << std::endl;
44
44
return -1 ;
45
45
}
@@ -53,7 +53,9 @@ int main(int argc, char** argv) {
53
53
cv::resizeWindow (" original" , 128 , 128 );
54
54
cv::imshow (" original" , dest);
55
55
56
- cv::MatIterator_<double > it, end;
56
+ cv::MatIterator_<double > it;
57
+ cv::MatIterator_<double > end;
58
+
57
59
for (it = dest.begin <double >(), end = dest.end <double >(); it != end; ++it) {
58
60
*it = std::asin (*it);
59
61
}
@@ -62,24 +64,24 @@ int main(int argc, char** argv) {
62
64
/* Preparation of the quantum circuit */
63
65
std::unique_ptr<qc::QuantumComputation> qc = std::make_unique<qc::QuantumComputation>(nqubits);
64
66
65
- for (dd ::Qubit i = 1 ; i < 11 ; i++) {
67
+ for (qc ::Qubit i = 1 ; i < 11 ; i++) {
66
68
qc->h (i); // add Hadamard gates
67
69
}
68
70
69
71
int cnt = 0 ; // start with the x/y location at 0000000000b
70
72
for (it = dest.begin <double >(), end = dest.end <double >(); it != end; ++it) {
71
73
if (*it != 0 ) {
72
- dd ::Controls controls;
74
+ qc ::Controls controls;
73
75
for (int i = 0 ; i < 10 ; i++) {
74
- controls.insert ({static_cast <dd ::Qubit>(i + 1 ), (cnt & 1LL << i) ? dd ::Control::Type::pos : dd ::Control::Type::neg });
76
+ controls.insert ({static_cast <qc ::Qubit>(i + 1 ), (cnt & 1LL << i) != 0 ? qc ::Control::Type::Pos : qc ::Control::Type::Neg });
75
77
}
76
78
qc->ry (0 , controls, (*it) * 2 );
77
79
}
78
80
cnt++;
79
81
}
80
82
const auto t0end = std::chrono::steady_clock::now ();
81
83
82
- if (vm.count (" verbose" )) {
84
+ if (vm.count (" verbose" ) > 0 ) {
83
85
qc->print (std::cout);
84
86
}
85
87
@@ -88,34 +90,33 @@ int main(int argc, char** argv) {
88
90
const auto t1start = std::chrono::steady_clock::now ();
89
91
CircuitSimulator<> ddsim (std::move (qc));
90
92
91
- std::vector<unsigned int > counts (1024 , 0 );
92
- int cols = dest.cols ;
93
- int rows = dest.rows ;
94
- int colrows = cols * rows;
93
+ const int cols = dest.cols ;
94
+ const int rows = dest.rows ;
95
+ const int colrows = cols * rows;
95
96
96
- std::map<std::string, std::size_t > m_counter = ddsim.Simulate (numOfShots);
97
+ const std::map<std::string, std::size_t > mCounter = ddsim.simulate (numOfShots);
97
98
98
99
const auto t1end = std::chrono::steady_clock::now ();
99
100
100
101
std::clog << " Elapsed time for quantum simulation and measurement " << std::chrono::duration<float >(t1end - t1start).count () << " s" << std::endl;
101
102
std::clog << " size of image: " << colrows << std::endl;
102
103
103
104
unsigned int result = 0 ;
104
- for (auto & elem: m_counter ) {
105
- result += elem. second ;
105
+ for (const auto & [bitstring, count]: mCounter ) {
106
+ result += count ;
106
107
}
107
- std::clog << " Number of distinct measurements: " << m_counter .size () << " \n " ;
108
+ std::clog << " Number of distinct measurements: " << mCounter .size () << " \n " ;
108
109
std::clog << " RESULT: " << result << std::endl;
109
110
110
111
std::vector<double > prob (1024 , 0 );
111
112
112
- for (auto & elem: m_counter ) {
113
- std::size_t fixed_index = std::stoi (elem. first .substr (0 , 10 ), nullptr , 2 );
114
- double tmp = std::sqrt (static_cast <double >(elem. second ) / numOfShots) * 32 . * 255 .;
115
- if (vm.count (" verbose" )) {
116
- std::clog << elem. first << " (" << fixed_index << " ) : " << elem. second << " (" << tmp << " )\n " ;
113
+ for (const auto & [bitstring, count]: mCounter ) {
114
+ const std::size_t fixedIndex = std::stoul (bitstring .substr (0 , 10 ), nullptr , 2 );
115
+ const double tmp = std::sqrt (static_cast <double >(count ) / numOfShots) * 32 . * 255 .;
116
+ if (vm.count (" verbose" ) > 0 ) {
117
+ std::clog << bitstring << " (" << fixedIndex << " ) : " << count << " (" << tmp << " )\n " ;
117
118
}
118
- prob.at (fixed_index ) = tmp;
119
+ prob.at (fixedIndex ) = tmp;
119
120
}
120
121
121
122
cv::Mat final = cv::Mat (prob).reshape (0 , 32 );
0 commit comments