-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqbit.cpp
More file actions
83 lines (69 loc) · 2.49 KB
/
Copy pathqbit.cpp
File metadata and controls
83 lines (69 loc) · 2.49 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
#include "qbit.h"
#include "gate.h"
//Qbit constructor
Qbit::Qbit(std::complex<double> a, std::complex<double> b) {
double norm = std::norm(a) + std::norm(b);
//normalization check (valid quantum states only)
if (std::abs(norm - 1.0) > 1e-9) {
throw std::invalid_argument("Invalid Qbit initialization: |a|^2 + |b|^2 must equal 1.");
}
alpha = a;
beta = b;
}
//getter and setter
std::complex<double> Qbit::getalpha() const { return alpha; }
std::complex<double> Qbit::getbeta() const { return beta; }
void Qbit::setalpha(const std::complex<double>& c) { alpha = c; }
void Qbit::setbeta(const std::complex<double>& c) { beta = c; }
//operator overloading to print qubit
std::ostream& operator<<(std::ostream& out, const Qbit& obj) {
out << "(" << obj.alpha << ")|0> + (" << obj.beta << ")|1>";
return out;
}
// gate application function
void Qbit::applyGate(const Gate& gate) {
const auto& mat = gate.getMatrix();
// ensure 1 qubit system
if (mat.size() != 2 || mat[0].size() != 2)
throw std::invalid_argument("Gate is not a single-qubit (2x2) gate.");
// tensor product
std::complex<double> newAlpha = mat[0][0] * alpha + mat[0][1] * beta;
std::complex<double> newBeta = mat[1][0] * alpha + mat[1][1] * beta;
alpha = newAlpha;
beta = newBeta;
// Renormalization
double norm = std::sqrt(std::norm(alpha) + std::norm(beta));
alpha /= norm;
beta /= norm;
// Track gate usage
Qbit::gates.push_back(gate.getName());
}
// Utility function to print circuit
void Qbit::printCircuit() {
if (gates.empty()) {
std::cout << "No gates in the circuit yet.\n";
return;
}
std::cout << "\nQuantum Circuit:\n";
// simple pattern building
for (size_t i = 0; i < gates.size(); ++i) {
std::cout << "┌";
for (int j = 0; j < gates[i].size() + 2; ++j) std::cout << "─";
std::cout << "┐ ";
}
std::cout << "\n";
for (size_t i = 0; i < gates.size(); ++i) {
std::cout << "│ " << gates[i] << " │";
if (i != gates.size() - 1)
std::cout << "──▶";
else
std::cout << " ";
}
std::cout << "\n";
for (size_t i = 0; i < gates.size(); ++i) {
std::cout << "└";
for (int j = 0; j < gates[i].size() + 2; ++j) std::cout << "─";
std::cout << "┘ ";
}
std::cout << "\n";
}