aboutsummaryrefslogtreecommitdiff
path: root/libpsi-core/src/core/component.rs
blob: 7748ac5a8aee5da845cf3af4eded4cdc517d21a8 (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
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
use crate::{complex, ColumnVector, Complex, Matrix, Vector, VectorMatrix};
use core::{fmt, ops};

pub type QuantumState = ColumnVector<Complex<f64>>;
pub type QuantumBit = QuantumState;

#[derive(Clone)]
pub struct QuantumGate {
    pub name: &'static str,
    pub matrix: Matrix<Complex<f64>>,
}

#[derive(Clone)]
pub struct ClassicalRegister {
    bits: Vec<i32>,
}

#[derive(Clone)]
pub struct QuantumRegister {
    state_vector: QuantumState,
    qubits: Vec<QuantumBit>,
}

#[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)
        }
    };
}

impl QuantumBit {
    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 fmt::Display for QuantumGate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

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_vector = 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_vector: ColumnVector::new(vec![]),
        };

        register.update();
        register
    }

    pub fn get_bits(&self) -> Vec<QuantumBit> {
        self.qubits.clone()
    }

    pub fn get_state(&self) -> QuantumState {
        self.state_vector.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]
    }
}