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
150
151
152
153
154
155
|
use rand::Rng;
use crate::{complex, ColumnVector, Complex, Matrix, Vector, VectorMatrix};
use core::ops;
pub type QuantumBit = ColumnVector<Complex<f64>>;
pub type QuantumState = Matrix<Complex<f64>>;
pub type QuantumGate = (&'static str, QuantumState);
#[macro_export]
macro_rules! count {
() => { 0 };
($head:expr $(,$tail:expr)*) => { 1 + count!($( $tail ),*) };
}
#[macro_export]
macro_rules! qubit {
($(($re:expr, $im:expr)),*) => {
{
let mut vector = Vec::new();
$(
vector.push(complex!($re, $im));
)*
QuantumBit::new(vector)
}
};
}
#[macro_export]
macro_rules! quantum_register {
($($bit:expr),*) => {
{
const N: usize = count!($($bit),*);
let mut bits: [QuantumBit; N] = [$($bit),*];
QuantumRegister::from(&mut bits)
}
};
}
#[derive(Clone)]
pub struct ClassicalRegister {
bits: Vec<i32>,
}
#[derive(Clone)]
pub struct QuantumRegister {
state: ColumnVector<Complex<f64>>,
qubits: Vec<QuantumBit>,
}
impl QuantumBit {
pub fn measure(&self) -> i32 {
let alpha_abs = self[0].abs();
let beta_abs = self[1].abs();
let alpha_norm = alpha_abs * alpha_abs;
let beta_norm = beta_abs * beta_abs;
let mut rng = rand::thread_rng();
let random_value = rng.gen_range(0.0..(alpha_norm as f32 + beta_norm as f32));
if random_value < alpha_norm as f32 {
0
} else {
1
}
}
pub fn state_0() -> QuantumBit {
QuantumBit::new(vec![complex!(1.0, 0.0), complex!(0.0, 0.0)])
}
pub fn state_1() -> QuantumBit {
QuantumBit::new(vec![complex!(0.0, 0.0), complex!(1.0, 0.0)])
}
}
impl ClassicalRegister {
pub fn new(count: usize) -> ClassicalRegister {
ClassicalRegister {
bits: Vec::with_capacity(count),
}
}
pub fn set_bits(&mut self, bits: Vec<i32>) {
self.bits = bits;
}
pub fn get_bits(&self) -> Vec<i32> {
self.bits.clone()
}
}
impl QuantumRegister {
fn update(&mut self) {
let matrices: Vec<Matrix<Complex<f64>>> =
self.qubits.iter().map(|qubit| qubit.to_matrix()).collect();
let mut new_result = matrices[0].clone();
for matrix in &matrices[1..] {
new_result = new_result.kronecker(matrix);
}
self.state = ColumnVector::from_matrix(&new_result);
}
pub fn new(count: usize) -> QuantumRegister {
QuantumRegister::from(&mut vec![QuantumBit::state_0(); count])
}
pub fn from(bits: &mut [QuantumBit]) -> QuantumRegister {
let mut register = QuantumRegister {
qubits: bits.to_vec(),
state: ColumnVector::new(vec![]),
};
register.update();
register
}
pub fn get_bits(&self) -> Vec<QuantumBit> {
self.qubits.clone()
}
pub fn get_state(&self) -> ColumnVector<Complex<f64>> {
self.state.clone()
}
}
impl ops::Index<usize> for QuantumRegister {
type Output = QuantumBit;
fn index(&self, index: usize) -> &Self::Output {
&self.qubits[index]
}
}
impl ops::IndexMut<usize> for QuantumRegister {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.qubits[index]
}
}
impl ops::Index<usize> for ClassicalRegister {
type Output = i32;
fn index(&self, index: usize) -> &Self::Output {
&self.bits[index]
}
}
impl ops::IndexMut<usize> for ClassicalRegister {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.bits[index]
}
}
|