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
mod macros {
#![macro_use]
#[macro_export]
macro_rules! square {
($x:expr) => {
$x * $x
};
}
#[macro_export]
macro_rules! abs_square {
($re:expr, $im:expr) => {
square!($re) + square!($im)
};
}
}
pub mod qubit {
use float_cmp::ApproxEqUlps;
#[derive(Clone, Copy, Debug)]
pub struct NonEntangledQubit {
a_re: f64,
a_im: f64,
b_re: f64,
b_im: f64,
}
impl NonEntangledQubit {
pub fn new(a_re: f64, a_im: f64, b_re: f64, b_im: f64) -> NonEntangledQubit {
let candidate = NonEntangledQubit {
a_re: a_re,
a_im: a_im,
b_re: b_re,
b_im: b_im,
};
assert!(candidate.validate());
candidate
}
#[cfg(not(feature = "optimize"))]
pub fn validate(&self) -> bool {
let sample_space_sum: f64 = abs_square!(self.a_re, self.a_im) +
abs_square!(self.b_re, self.b_im);
sample_space_sum.approx_eq_ulps(&1.0f64, 10)
}
#[cfg(feature = "optimize")]
#[inline(always)]
pub fn validate(&self) -> bool {
true
}
}
#[test]
fn initialization_test() {
let sqrt2inv = 2.0f64.sqrt().recip();
let q1: NonEntangledQubit = NonEntangledQubit::new(0.5, 0.5, 0.5, 0.5);
let q2: NonEntangledQubit = NonEntangledQubit::new(sqrt2inv, sqrt2inv, 0.0, 0.0);
let q3: NonEntangledQubit = NonEntangledQubit::new(0.0, 0.0, sqrt2inv, sqrt2inv);
assert!(q1.validate());
assert!(q2.validate());
assert!(q3.validate());
}
#[test]
#[should_panic(expected = "assertion failed")]
#[cfg(not(feature = "optimize"))]
fn bad_initialization_test() {
NonEntangledQubit::new(0.0, 0.0, 0.0, 0.0);
}
}