-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultistate.cpp
More file actions
50 lines (40 loc) · 1.41 KB
/
Copy pathmultistate.cpp
File metadata and controls
50 lines (40 loc) · 1.41 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
#include "multistate.h"
#include "multistate.h"
#include <cmath>
#include <stdexcept>
//constructor
MultiState::MultiState(std::vector<std::complex<double>> input)
: StateVector(input)
{
size_t n = input.size();
// return error if user tries to assign type Qbit to multistate
if(n==2) {
throw std::invalid_argument("Qbit is not multistate assign a type Qbit instead.");
}
// Check for empty state
if (n == 0)
throw std::invalid_argument("State vector cannot be empty.");
// Check if n is a power of 2
if ((n & (n - 1)) != 0)
throw std::invalid_argument("Invalid state vector length: must be 2^n for some n.");
// check normalization
double norm = 0.0;
for (auto& amp : input) norm += std::norm(amp);
if (std::abs(norm - 1.0) > 1e-9)
throw std::invalid_argument("Invalid state: amplitudes must be normalized (|ψ|² = 1).");
}
//operator overloading for printing multistate system
std::ostream& operator<<(std::ostream& out, const MultiState& obj)
{
size_t n = obj.StateVector.size();
int numQubits = static_cast<int>(std::log2(n));
for (size_t i = 0; i < n; ++i) {
out << "(" << obj.StateVector[i] << ")|";
// print binary representation of i as basis state
for (int j = numQubits - 1; j >= 0; --j)
out << ((i >> j) & 1);
out << ">";
if (i != n - 1) out << " + ";
}
return out;
}