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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use float_cmp::approx_eq;
use num_complex::Complex64;
#[derive(Debug, Copy, Clone)]
pub struct Ket {
first: Complex64,
second: Complex64,
}
pub const COMPLEX_ZERO: Complex64 = Complex64 { re: 0.0, im: 0.0 };
pub const COMPLEX_ONE: Complex64 = Complex64 { re: 1.0, im: 0.0 };
pub const KET_ZERO: Ket = Ket {
first: COMPLEX_ONE,
second: COMPLEX_ZERO,
};
pub const KET_ONE: Ket = Ket {
first: COMPLEX_ZERO,
second: COMPLEX_ONE,
};
impl PartialEq for Ket {
fn eq(&self, other: &Self) -> bool {
self.first == other.first && self.second == other.second
}
}
impl Eq for Ket {}
#[test]
fn ket_zero_equal_to_itself() {
assert_eq!(KET_ZERO == KET_ZERO, true)
}
#[test]
fn ket_one_equal_to_itself() {
assert_eq!(KET_ONE == KET_ONE, true)
}
#[test]
fn ket_zero_not_equal_to_ket_one() {
assert_eq!(KET_ZERO != KET_ONE, true)
}
use std::ops::Add;
impl Add for Ket {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
first: self.first + other.first,
second: self.second + other.second,
}
}
}
#[test]
fn ket_zero_add_ket_zero() {
let sum = KET_ZERO + KET_ONE;
assert_eq!(
sum,
Ket {
first: COMPLEX_ONE,
second: COMPLEX_ONE,
}
)
}
use std::ops::Mul;
impl Mul<Complex64> for Ket {
type Output = Ket;
fn mul(self, rhs: Complex64) -> Ket {
Ket {
first: self.first * rhs,
second: self.second * rhs,
}
}
}
#[test]
fn mul_ket_zero_with_one() {
assert_eq!(KET_ZERO, KET_ZERO * COMPLEX_ONE);
}
#[test]
fn mul_ket_one_with_one() {
assert_eq!(KET_ONE, KET_ONE * COMPLEX_ONE);
}
impl Mul<Ket> for Complex64 {
type Output = Ket;
fn mul(self, rhs: Ket) -> Ket {
Ket {
first: self * rhs.first,
second: self * rhs.second,
}
}
}
#[test]
fn mul_one_with_ket_zero() {
assert_eq!(KET_ZERO * COMPLEX_ONE, KET_ZERO);
}
#[test]
fn mul_one_with_ket_one() {
assert_eq!(KET_ONE * COMPLEX_ONE, KET_ONE);
}
#[test]
fn ket_arithmetic() {
let a = Complex64::from(0.6) * KET_ZERO;
let b = Complex64::from(0.8) * KET_ONE;
let c = a + b;
assert_eq!(
c,
Ket {
first: Complex64::from(0.6),
second: Complex64::from(0.8),
}
)
}
pub trait ValidQuantumState {
fn is_valid(&self) -> bool;
}
impl ValidQuantumState for Ket {
fn is_valid(&self) -> bool {
let a = self.first * self.first;
let b = self.second * self.second;
let result = a + b;
approx_eq!(f64, result.re, COMPLEX_ONE.re, ulps = 2)
&& approx_eq!(f64, result.im, COMPLEX_ONE.im, ulps = 2)
}
}
#[test]
fn ket_zero_valid() {
assert_eq!(KET_ZERO.is_valid(), true);
}
#[test]
fn ket_one_valid() {
assert_eq!(KET_ONE.is_valid(), true);
}
#[test]
fn ket_invalid() {
assert_eq!((KET_ONE + KET_ZERO).is_valid(), false);
}
#[test]
fn ket_arithmetic_valid() {
let a = Complex64::from(0.6) * KET_ZERO;
let b = Complex64::from(0.8) * KET_ONE;
let c = a + b;
assert_eq!(c.is_valid(), true)
}