-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinteractive_cli.cpp
More file actions
129 lines (118 loc) · 4.18 KB
/
interactive_cli.cpp
File metadata and controls
129 lines (118 loc) · 4.18 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <limits>
#include <algorithm>
#include <MaQrel/QuantumCircuitParallel.h>
#include <MaQrel/QuantumCircuitBase.h>
using namespace std;
// Function to handle gate application logic
void applyGate(QuantumCircuitParallel &qc, int qubit_count) {
cout << "\nEnter gate to apply (H, X, Z, Y, S, T, Tdg, C for CNOT, M for Measuring): ";
string gate_type;
cin >> gate_type;
transform(gate_type.begin(), gate_type.end(), gate_type.begin(), ::toupper);
int target = -1, control = -1;
try {
if (gate_type == "C") {
cout << "Enter control qubit: ";
cin >> control;
cout << "Enter target qubit: ";
cin >> target;
qc.CX(control, target);
cout << "Applied CNOT(" << control << ", " << target << ")" << endl;
} else {
cout << "Enter target qubit: ";
cin >> target;
if (gate_type == "H") {
qc.H(target);
cout << "Applied H on qubit " << target << endl;
} else if (gate_type == "X") {
qc.X(target);
cout << "Applied X on qubit " << target << endl;
} else if (gate_type == "Z") {
qc.Z(target);
cout << "Applied Z on qubit " << target << endl;
} else if (gate_type == "Y"){
qc.Y(target);
cout << "Applied Y on qubit " << target << endl;
}else if (gate_type == "T"){
qc.T(target);
cout << "Applied T on qubit " << target << endl;
}else if (gate_type == "TDG"){
qc.Tdg(target);
cout << "Applied T dagger on qubit " << target << endl;
}else if(gate_type == "M"){
qc.measure_single_qubit(target);
} else {
cout << "Invalid gate type." << endl;
}
}
} catch (const out_of_range& e) {
cerr << "Error: " << e.what() << ". Qubit index must be between 0 and " << qubit_count - 1 << "." << endl;
} catch (const invalid_argument& e) {
cerr << "Error: " << e.what() << endl;
}
}
int main() {
cout << "--- Quantum Circuit Simulator ---" << "\n";
int n;
cout << "Enter the number of qubits: ";
cin >> n;
if (cin.fail() || n <= 0) {
cout << "Invalid number of qubits. Exiting." << endl;
return 1;
}
QuantumCircuitParallel qc(n);
bool running = true;
while (running) {
cout << "\n--- Menu ---\n";
cout << "1. Apply Gate (includes measurement of single qubits)\n";
cout << "2. Display Circuit\n";
cout << "3. Measure Circuit(Collapses)\n";
cout << "4. Print State Vector (Debug)\n";
cout << "5. Print probabilities (Debug)\n";
cout << "6. Display probability graph\n";
cout << "7. Display heat map representation\n";
cout << "8. Exit\n";
cout << "Your choice: ";
int choice;
cin >> choice;
if (cin.fail()) {
cout << "Invalid input. Please enter a number." << endl;
cin.clear(); // Clear error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard bad input
continue;
}
switch (choice) {
case 1:
applyGate(qc, n);
break;
case 2:
qc.printCircuit();
break;
case 3:
cout << "\nMeasuring the current state." << endl;
qc.collapse();
break;
case 4:
qc.printState();
break;
case 5:
cout << "\nProbabilities of states." << endl;
qc.printProbabilities();
break;
case 6:
qc.displayGraph();
break;
case 7:
qc.displayHeatMap();
break;
case 8:
running = false;
cout << "Exiting simulator. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please enter a number from 1 to 7." << endl;
}
}
return 0;
}