-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
149 lines (126 loc) · 6.33 KB
/
main.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
#include "dd/DDDefinitions.hpp"
#include "dd/Export.hpp"
#include "dd/Package.hpp"
#include "src/Circuit.cpp"
#include <chrono>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main(int /*unused*/, char** /*unused*/) {
cout << "Hello, from tqsim-cpp!\n";
// H_2
Sequence hadSequence2 = {{4, 2}, {5, 2}, {4, -2}, {5, -2}, {4, 2},
{5, 4}, {4, -2}, {5, 2}, {4, 2}, {5, -2},
{4, 2}, {5, -2}, {4, 4}};
// CNOT_2->1
Sequence cnotSequence = {
{3, 1}, {4, 1}, {4, 1}, {3, 1}, {3, 1}, {4, 1}, {2, -1}, {3, -1},
{3, -1}, {2, -1}, {4, -1}, {3, -1}, {3, -1}, {4, -1}, {4, -1}, {3, -1},
{3, -1}, {4, -1}, {2, 1}, {3, 1}, {3, 1}, {2, 1}, {4, 1}, {3, 1},
{3, 1}, {4, 1}, {4, 1}, {3, 1}, {3, 1}, {4, 1}, {2, 1}, {3, 1},
{3, 1}, {2, 1}, {4, -1}, {3, -1}, {3, -1}, {4, -1}, {2, -1}, {3, -1},
{3, -1}, {2, -1}, {4, -1}, {3, -1}, {3, -1}, {4, -1}, {4, -1}, {3, -1},
{3, -1}, {4, -1}, {2, -1}, {3, -1}, {3, -1}, {2, -1}, {2, -1}, {3, -1},
{3, -1}, {2, -1}, {4, -1}, {3, -1}, {3, -1}, {4, -1}, {2, 1}, {3, 1},
{3, 1}, {2, 1}, {2, 1}, {3, 1}, {3, 1}, {2, 1}, {4, 1}, {3, 1},
{3, 1}, {4, 1}, {2, -1}, {3, -1}, {3, -1}, {2, -1}, {4, 1}, {3, 1},
{3, 1}, {4, 1}, {2, 1}, {3, 1}, {3, 1}, {2, 1}, {4, -1}, {3, -1},
{3, -1}, {4, -1}, {2, 1}, {3, 1}, {3, 1}, {2, 1}, {2, 1}, {3, 1},
{1, -1}, {2, -1}, {2, -1}, {1, -1}, {3, -1}, {2, -1}, {2, -1}, {3, -1},
{3, -1}, {2, -1}, {2, -1}, {3, -1}, {1, 1}, {2, 1}, {2, 1}, {1, 1},
{1, 1}, {2, 1}, {2, 1}, {1, 1}, {3, -1}, {2, -1}, {2, -1}, {3, -1},
{1, 1}, {2, 1}, {2, 1}, {1, 1}, {3, 1}, {2, 1}, {2, 1}, {3, 1},
{1, -1}, {2, -1}, {2, -1}, {1, -1}, {3, 1}, {2, 1}, {2, 1}, {3, 1},
{3, 1}, {2, 1}, {2, 1}, {3, 1}, {1, -1}, {2, -1}, {2, -1}, {1, -1},
{3, 1}, {2, 1}, {2, 1}, {3, 1}, {3, 1}, {2, 1}, {2, 1}, {3, 1},
{1, 1}, {2, 1}, {2, 1}, {1, 1}, {3, -1}, {2, -1}, {2, -1}, {3, -1},
{3, -1}, {2, -1}, {2, -1}, {3, -1}, {1, 1}, {2, 1}, {2, 1}, {1, 1},
{3, -1}, {2, -1}, {2, -1}, {3, -1}, {1, 1}, {2, 1}, {2, 1}, {1, 1},
{3, -1}, {2, -1}, {2, -1}, {3, -1}, {1, -1}, {2, -1}, {2, -1}, {1, -1},
{3, -1}, {2, -1}, {2, -1}, {3, -1}, {3, -1}, {2, -1}, {4, 1}, {3, 1},
{3, 1}, {4, 1}, {2, -1}, {3, -1}, {3, -1}, {2, -1}, {4, -1}, {3, -1},
{3, -1}, {4, -1}, {2, 1}, {3, 1}, {3, 1}, {2, 1}, {4, -1}, {3, -1},
{3, -1}, {4, -1}, {2, -1}, {3, -1}, {3, -1}, {2, -1}, {2, -1}, {3, -1},
{3, -1}, {2, -1}, {4, 1}, {3, 1}, {3, 1}, {4, 1}, {2, 1}, {3, 1},
{3, 1}, {2, 1}, {2, 1}, {3, 1}, {3, 1}, {2, 1}, {4, 1}, {3, 1},
{3, 1}, {4, 1}, {4, 1}, {3, 1}, {3, 1}, {4, 1}, {2, 1}, {3, 1},
{3, 1}, {2, 1}, {4, 1}, {3, 1}, {3, 1}, {4, 1}, {2, -1}, {3, -1},
{3, -1}, {2, -1}, {4, -1}, {3, -1}, {3, -1}, {4, -1}, {4, -1}, {3, -1},
{3, -1}, {4, -1}, {2, -1}, {3, -1}, {3, -1}, {2, -1}, {4, 1}, {3, 1},
{3, 1}, {4, 1}, {4, 1}, {3, 1}, {3, 1}, {4, 1}, {2, 1}, {3, 1},
{3, 1}, {2, 1}, {4, -1}, {3, -1}, {3, -1}, {4, -1}, {4, -1}, {3, -1}};
// Start the timer
auto start = std::chrono::high_resolution_clock::now();
const auto nrQubits = 2U;
Circuit circuit = Circuit(nrQubits, 3);
Eigen::VectorXcd initSequence(13);
initSequence << 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
circuit.initialize(initSequence);
circuit.braidSequence(hadSequence2);
circuit.braidSequence(cnotSequence);
circuit.measure();
int const shots = 10000;
Result const res = circuit.run(shots);
// Stop the timer
auto end = std::chrono::high_resolution_clock::now();
// Calculate the elapsed time
std::chrono::duration<double> const elapsed = end - start;
// Output the elapsed time in seconds
std::cout << "Number of shots: " << shots << '\n';
std::cout << "Elapsed time: " << elapsed.count() << " seconds" << '\n';
for (const auto& count : res.countsDict) {
std::cout << count.first << ": " << count.second << '\n';
}
std::vector<Eigen::MatrixXcd> const braidingOperators =
circuit.getBraidingOperators();
// Define the range for rows and columns you want to select
size_t const startRow = 1; // First row
auto const endRow =
static_cast<size_t>(pow(2, nrQubits)); // Fifth row (0-based indexing)
int const startCol = 1; // First column
auto const endCol =
static_cast<size_t>(pow(2, nrQubits)); // Fifth column (0-based indexing)
std::cout << "endRow: " << endRow << '\n';
std::cout << "endCol: " << endCol << '\n';
for (int index = 0; index < static_cast<int>(braidingOperators.size());
++index) {
// Create a std::ostringstream object
std::ostringstream oss;
// Format the string
oss << std::to_string(nrQubits) << "_qubit_"
<< "sigma_" << index + 1 << "_operator.dot";
// Get the formatted string
std::string const formattedString = oss.str();
// Print the formatted string
std::cout << formattedString << '\n';
// Select the sub-matrix
Eigen::MatrixXcd complexMatrix =
braidingOperators[static_cast<size_t>(index)]
.block(startRow, startCol, endRow - startRow + 1,
endCol - startCol + 1)
.cast<std::complex<double>>();
std::cout << "Braiding operator: \n"
<< braidingOperators[static_cast<uint64_t>(index)] << '\n';
// Initialize a std::vector<std::vector<std::complex<double>>> with the same
// dimensions as the Eigen matrix
dd::CMat sigmaMatrix(static_cast<size_t>(complexMatrix.rows()),
dd::CVec(static_cast<size_t>(complexMatrix.cols())));
// Copy elements from the Eigen matrix to the
// std::vector<std::vector<std::complex<double>>>
for (int i = 0; i < complexMatrix.rows(); ++i) {
for (int j = 0; j < complexMatrix.cols(); ++j) {
auto row = static_cast<size_t>(i);
auto col = static_cast<size_t>(j);
sigmaMatrix[row][col] = complexMatrix(i, j);
std::cout << sigmaMatrix[row][col] << " ";
}
std::cout << '\n';
}
std::cout << '\n';
const auto dd = std::make_unique<dd::Package<>>(nrQubits);
const auto matDD = dd->makeDDFromMatrix(sigmaMatrix);
export2Dot(matDD, formattedString, true, true, false, false, true);
}
return 0;
}