blob: bd18b521cb5090c5279ab123e5175ebd94b1a148 (
plain)
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
|
use crate::{ColumnVector, Complex, Matrix, VectorMatrix};
pub type QuantumRegister = ColumnVector<Complex<f64>>;
pub type QuantumBit = ColumnVector<Complex<f64>>;
#[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 bits: [QuantumBit; N] = [$($bit),*];
QuantumRegister::from(&bits)
}
};
}
impl QuantumBit {
pub fn get_state(&self) -> i32 {
(self[1] != Complex::new(0.0, 0.0)) as i32
}
}
impl QuantumRegister {
pub fn from(bits: &[QuantumBit]) -> QuantumRegister {
let matrices: Vec<Matrix<Complex<f64>>> = bits.iter().map(|bit| bit.to_matrix()).collect();
let mut result = matrices[0].clone();
for matrix in &matrices[1..] {
result = result.kronecker(matrix);
}
ColumnVector::from_matrix(&result)
}
}
|