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
|
use crate::{column_vector, complex, ColumnVector, Complex, Matrix, Vector, VectorMatrix};
use core::{fmt, ops};
// TODO(Hachem): Redo these macros to work with the new function definition.
#[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)
}
};
}
pub type QuantumState = ColumnVector<Complex<f64>>;
impl QuantumState {
pub fn state_0() -> QuantumState {
column_vector![complex!(1.0, 0.0), complex!(0.0, 0.0)]
}
pub fn state_1() -> QuantumState {
column_vector![complex!(0.0, 0.0), complex!(1.0, 0.0)]
}
}
#[derive(Clone)]
pub struct QuantumBit<'a> {
state: QuantumState,
name: &'a str,
}
#[derive(Clone)]
pub struct QuantumRegister<'a> {
state_vector: QuantumState,
name: &'a str,
qubits: Vec<QuantumBit<'a>>,
}
#[derive(Clone)]
pub struct QuantumGate<'a> {
pub name: &'a str,
pub matrix: Matrix<Complex<f64>>,
}
impl<'a> QuantumBit<'a> {
pub fn new(name: &'a str, state: QuantumState) -> QuantumBit<'a> {
QuantumBit { name, state }
}
pub fn get_state(&self) -> QuantumState {
self.state.clone()
}
pub fn get_name(&self) -> &'a str {
self.name
}
}
impl<'a> QuantumRegister<'a> {
pub fn new(name: &'a str, names: &[&'a str]) -> QuantumRegister<'a> {
let mut bits: Vec<QuantumBit<'a>> = Vec::new();
for i in 0..names.len() {
bits.push(QuantumBit::new(names[i], QuantumState::state_0()))
}
QuantumRegister::from(name, &mut bits)
}
pub fn from(name: &'a str, bits: &mut [QuantumBit<'a>]) -> QuantumRegister<'a> {
let mut register = QuantumRegister {
name,
qubits: bits.to_vec(),
state_vector: ColumnVector::new(vec![]),
};
register.update();
register
}
fn update(&mut self) {
let matrices: Vec<Matrix<Complex<f64>>> = self
.qubits
.iter()
.map(|qubit| qubit.state.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 get_bits(&self) -> Vec<QuantumBit> {
self.qubits.clone()
}
pub fn get_state(&self) -> QuantumState {
self.state_vector.clone()
}
pub fn get_name(&self) -> &'a str {
self.name
}
}
impl<'a> ops::Index<usize> for QuantumRegister<'a> {
type Output = QuantumBit<'a>;
fn index(&self, index: usize) -> &Self::Output {
&self.qubits[index]
}
}
impl<'a> ops::IndexMut<usize> for QuantumRegister<'a> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.qubits[index]
}
}
impl<'a> fmt::Display for QuantumGate<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
|