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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::gates::Gate;
pub struct H
{
}
impl H
{
pub fn new() -> Self
{
H { }
}
pub fn transform(mut state: crate::cmatrix::CVecSliceMut)
{
assert!(state.len() % 2 == 0, "Number of rows is not even.");
let n = state.len() / 2;
let s1_copy = state.slice(s![n..]).to_owned();
let (mut s0, mut s1) = state.view_mut().split_at(ndarray::Axis(0), n);
s1 -= &s0;
s1 *= -crate::cmatrix::COMPLEX_HSQRT2;
s0 += &s1_copy;
s0 *= crate::cmatrix::COMPLEX_HSQRT2;
}
pub fn transform_mat(mut state: crate::cmatrix::CMatSliceMut)
{
assert!(state.rows() % 2 == 0, "Number of rows is not even.");
let n = state.rows() / 2;
let s1_copy = state.slice(s![n.., ..]).to_owned();
let (mut s0, mut s1) = state.view_mut().split_at(ndarray::Axis(0), n);
s1 -= &s0;
s1 *= -crate::cmatrix::COMPLEX_HSQRT2;
s0 += &s1_copy;
s0 *= crate::cmatrix::COMPLEX_HSQRT2;
}
pub fn cost() -> f64
{
crate::gates::U2::cost()
}
}
impl crate::gates::Gate for H
{
fn cost(&self) -> f64
{
Self::cost()
}
fn description(&self) -> &str
{
"H"
}
fn nr_affected_bits(&self) -> usize
{
1
}
fn matrix(&self) -> crate::cmatrix::CMatrix
{
let x = crate::cmatrix::COMPLEX_HSQRT2;
array![[x, x], [x, -x]]
}
fn apply_slice(&self, state: crate::cmatrix::CVecSliceMut)
{
Self::transform(state);
}
fn apply_mat_slice(&self, state: crate::cmatrix::CMatSliceMut)
{
Self::transform_mat(state);
}
}
impl crate::export::OpenQasm for H
{
fn open_qasm(&self, bit_names: &[String], bits: &[usize])
-> crate::error::Result<String>
{
Ok(format!("h {}", bit_names[bits[0]]))
}
}
impl crate::export::CQasm for H
{
fn c_qasm(&self, bit_names: &[String], bits: &[usize])
-> crate::error::Result<String>
{
Ok(format!("h {}", bit_names[bits[0]]))
}
}
impl crate::export::Latex for H
{
fn latex(&self, bits: &[usize], state: &mut crate::export::LatexExportState)
-> crate::error::Result<()>
{
self.check_nr_bits(bits)?;
state.add_block_gate(bits, "H")
}
}
#[cfg(test)]
mod tests
{
use crate::gates::{gate_test, Gate, H};
use crate::export::{Latex, LatexExportState, OpenQasm, CQasm};
#[test]
fn test_description()
{
let gate = H::new();
assert_eq!(gate.description(), "H");
}
#[test]
fn test_cost()
{
let gate = H::new();
assert_eq!(gate.cost(), 104.0);
}
#[test]
fn test_matrix()
{
let h = H::new();
let s = crate::cmatrix::COMPLEX_HSQRT2;
assert_complex_matrix_eq!(h.matrix(), array![[s, s], [s, -s]]);
}
#[test]
fn test_apply()
{
let z = crate::cmatrix::COMPLEX_ZERO;
let o = crate::cmatrix::COMPLEX_ONE;
let x = crate::cmatrix::COMPLEX_HSQRT2;
let mut state = array![[o, z, x, x], [z, o, x, -x]];
let result = array![[x, x, o, z], [x, -x, z, o]];
gate_test(H::new(), &mut state, &result);
}
#[test]
fn test_open_qasm()
{
let bit_names = [String::from("qb")];
let qasm = H::new().open_qasm(&bit_names, &[0]);
assert_eq!(qasm, Ok(String::from("h qb")));
}
#[test]
fn test_c_qasm()
{
let bit_names = [String::from("qb")];
let qasm = H::new().c_qasm(&bit_names, &[0]);
assert_eq!(qasm, Ok(String::from("h qb")));
}
#[test]
fn test_latex()
{
let gate = H::new();
let mut state = LatexExportState::new(1, 0);
assert_eq!(gate.latex(&[0], &mut state), Ok(()));
assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
\lstick{\ket{0}} & \gate{H} & \qw \\
}
"#);
}
}