-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBenchmark.cpp
More file actions
112 lines (93 loc) · 3.5 KB
/
Benchmark.cpp
File metadata and controls
112 lines (93 loc) · 3.5 KB
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
#include <iostream>
#include <vector>
#include <random>
#include <omp.h>
#include <MaQrel/QuantumCircuitParallel.h>
#include <MaQrel/QuantumCircuitMPI.h>
using namespace std;
// A simple struct to hold a gate operation for our random circuit
enum GateType { H, X, Y, Z, S, T, Tdg, CX };
struct GateOp {
GateType type;
int target;
int control;
};
// Function to apply a specific gate from our GateOp list
void apply_gate_op(QuantumCircuitBase& qc, const GateOp& op) {
switch (op.type) {
case H: qc.H(op.target); break;
case X: qc.X(op.target); break;
case Y: qc.Y(op.target); break;
case Z: qc.Z(op.target); break;
case S: qc.S(op.target); break;
case T: qc.T(op.target); break;
case Tdg: qc.Tdg(op.target); break;
case CX: qc.CX(op.control, op.target); break;
}
}
int main() {
int num_qubits;
int num_threads;
int num_gates = 500;
cout << "--- Quantum Simulator Benchmark ---\n";
cout << "Enter the number of qubits (e.g., 10): ";
cin >> num_qubits;
cout << "Enter the number of threads for the parallel version: ";
cin >> num_threads;
if (cin.fail() || num_qubits <= 0 || num_threads <= 0) {
cerr << "Invalid input. Exiting.\n";
return 1;
}
cout << "\nPreparing to run a random circuit with " << num_gates << " gates on a " << num_qubits << "-qubit system.\n";
//The same random circuit for both cases
vector<GateOp> random_circuit;
random_circuit.reserve(num_gates);
// Setup random number generation
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> gate_dist(0, 7);
uniform_int_distribution<> qubit_dist(0, num_qubits - 1);
for (int i = 0; i < num_gates; ++i) {
GateType type = static_cast<GateType>(gate_dist(gen));
if (type == CX) {
int control = qubit_dist(gen);
int target = qubit_dist(gen);
// Ensure control and target are not the same
while (control == target) {
target = qubit_dist(gen);
}
random_circuit.push_back({type, target, control});
} else {
random_circuit.push_back({type, qubit_dist(gen), -1});
}
}
// ---PARALLEL VERSION ---
cout << "\n--- Running Parallel Benchmark (" << num_threads << " threads) ---\n";
QuantumCircuitParallel qc_parallel(num_qubits);
omp_set_num_threads(num_threads);
double start_parallel = omp_get_wtime();
for (const auto& op : random_circuit) {
apply_gate_op(qc_parallel, op);
}
double end_parallel = omp_get_wtime();
double parallel_time = end_parallel - start_parallel;
cout << "Parallel execution time: " << parallel_time << " seconds\n";
// ---SERIAL VERSION ---
cout << "\n--- Running Serial Benchmark ---\n";
QuantumCircuitBase qc_serial(num_qubits);
double start_serial = omp_get_wtime();
for (const auto& op : random_circuit) {
apply_gate_op(qc_serial, op);
}
double end_serial = omp_get_wtime();
double serial_time = end_serial - start_serial;
cout << "Serial execution time: " << serial_time << " seconds\n";
// ---CALCULATE AND DISPLAY SPEEDUP ---
if (parallel_time > 0) {
double speedup = serial_time / parallel_time;
cout << "\n-----------------------------------------------------------\n";
cout << "Speedup: " << speedup << "x\n";
cout << "--By Ashwin S, 2023BCS0044 & Elhan B Thomas, 2023BCS0119--\n";
}
return 0;
}