-
Notifications
You must be signed in to change notification settings - Fork 4
/
quantum_gates.c
220 lines (195 loc) · 5.88 KB
/
quantum_gates.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* quantum_gates.c: source file for quantum gate functions
*/
#include <math.h>
#include "quantum_gates.h"
#include "complex.h"
//#include <stdio.h> // DEBUG
#ifndef QUDA_GATE
#define QUDA_GATE
#endif
#ifndef FOR_EACH_STATE
#define FOR_EACH_STATE(qreg, i) for (i = 0; i < qreg->num_states; i++)
#define STATE(qreg, i) qreg->states[i].state
#define AMPLITUDE(qreg, i) qreg->states[i].amplitude
#endif
// One-bit quantum gates
#ifndef CUSTOM_HADAMARD
QUDA_GATE int quda_quantum_hadamard_gate(int target, quantum_reg* qreg) {
// If needed, enlarge qreg to make room for state splits resulting from this gate
int states = qreg->num_states;
int diff = 2*states - qreg->size;
if(diff > 0) {
if(quda_quantum_reg_enlarge(qreg,diff) == -1) return -1;
}
uint64_t mask = 1 << target;
int i;
FOR_EACH_STATE(qreg, i) {
// Flipped state must be created
STATE(qreg, qreg->num_states+i) = STATE(qreg, i) ^ mask;
// For this state, must just modify amplitude
AMPLITUDE(qreg, i) = quda_complex_rmul(AMPLITUDE(qreg, i),
ONE_OVER_SQRT_2);
// Copy amplitude to created state
AMPLITUDE(qreg, qreg->num_states+i) = AMPLITUDE(qreg, i);
if(STATE(qreg, i) & mask) {
AMPLITUDE(qreg, i) = quda_complex_neg(AMPLITUDE(qreg, i));
}
}
qreg->num_states = 2*states;
// TODO: Ideally, make this call optional or conditional
quda_quantum_reg_coalesce(qreg);
return 0;
}
#endif
QUDA_GATE void quda_quantum_pauli_x_gate(int target, quantum_reg* qreg) {
int i;
uint64_t mask = 1 << target;
FOR_EACH_STATE(qreg, i) {
STATE(qreg, i) = STATE(qreg, i) ^ mask;
}
}
QUDA_GATE void quda_quantum_pauli_y_gate(int target, quantum_reg* qreg) {
int i;
uint64_t mask = 1 << target;
FOR_EACH_STATE(qreg, i) {
STATE(qreg, i) = STATE(qreg, i) ^ mask;
AMPLITUDE(qreg, i) = quda_complex_mul_i(AMPLITUDE(qreg, i));
// TODO: Look at overhead of conditional mul_ni vs negation
if(STATE(qreg, i) & mask) {
AMPLITUDE(qreg, i) = quda_complex_neg(AMPLITUDE(qreg, i));
}
}
}
QUDA_GATE void quda_quantum_pauli_z_gate(int target, quantum_reg* qreg) {
int i;
uint64_t mask = 1 << target;
FOR_EACH_STATE(qreg, i) {
if(STATE(qreg, i) & mask) {
AMPLITUDE(qreg, i) = quda_complex_neg(AMPLITUDE(qreg, i));
}
}
}
QUDA_GATE void quda_quantum_phase_gate(int target, quantum_reg* qreg) {
int i;
uint64_t mask = 1 << target;
FOR_EACH_STATE(qreg, i) {
if(STATE(qreg, i) & mask) {
AMPLITUDE(qreg, i) = quda_complex_mul_i(AMPLITUDE(qreg, i));
}
}
}
QUDA_GATE void quda_quantum_pi_over_8_gate(int target, quantum_reg* qreg) {
complex_t c = { .real = ONE_OVER_SQRT_2, .imag = ONE_OVER_SQRT_2 };
int i;
uint64_t mask = 1 << target;
FOR_EACH_STATE(qreg, i) {
if(STATE(qreg, i) & mask) {
AMPLITUDE(qreg, i) = quda_complex_mul(AMPLITUDE(qreg, i),c);
}
}
}
QUDA_GATE void quda_quantum_rotate_k_gate(int target, quantum_reg* qreg, int k) {
float temp = QUDA_PI / (1 << (k-1));
complex_t c = { .real = cos(temp), .imag = sin(temp) };
uint64_t mask = 1 << target;
int i;
FOR_EACH_STATE(qreg, i) {
if(STATE(qreg, i) & mask) {
AMPLITUDE(qreg, i) = quda_complex_mul(AMPLITUDE(qreg, i),c);
}
}
}
// Two-bit quantum gates
QUDA_GATE void quda_quantum_swap_gate(int target1, int target2, quantum_reg* qreg) {
int i;
uint64_t mask = 1 << target1;
mask |= 1 << target2;
FOR_EACH_STATE(qreg, i) {
if((STATE(qreg, i) & mask) != 0 && (~STATE(qreg, i) & mask) != 0) {
STATE(qreg, i) = STATE(qreg, i) ^ mask;
}
}
}
QUDA_GATE void quda_quantum_controlled_not_gate(int control, int target, quantum_reg* qreg) {
int i;
uint64_t cmask = 1 << control;
uint64_t tmask = 1 << target;
FOR_EACH_STATE(qreg, i) {
if(STATE(qreg, i) & cmask) {
STATE(qreg, i) = STATE(qreg, i) ^ tmask;
}
}
}
QUDA_GATE void quda_quantum_controlled_y_gate(int control,int target, quantum_reg* qreg) {
int i;
uint64_t cmask = 1 << control;
uint64_t tmask = 1 << target;
FOR_EACH_STATE(qreg, i) {
// TODO: Look for ways to avoid nested conditionals
if(STATE(qreg, i) & cmask) {
STATE(qreg, i) = STATE(qreg, i) ^ tmask;
AMPLITUDE(qreg, i) = quda_complex_mul_i(AMPLITUDE(qreg, i));
// TODO: Look at overhead of conditional mul_ni vs negation
if(STATE(qreg, i) & tmask) {
AMPLITUDE(qreg, i) = quda_complex_neg(AMPLITUDE(qreg, i));
}
}
}
}
QUDA_GATE void quda_quantum_controlled_z_gate(int control, int target, quantum_reg* qreg) {
int i;
uint64_t mask = 1 << control;
mask |= 1 << target;
FOR_EACH_STATE(qreg, i) {
if((STATE(qreg, i) & mask) == mask) {
AMPLITUDE(qreg, i) = quda_complex_neg(AMPLITUDE(qreg, i));
}
}
}
QUDA_GATE void quda_quantum_controlled_phase_gate(int control, int target, quantum_reg* qreg) {
uint64_t mask = 1 << control;
mask |= 1 << target;
int i;
FOR_EACH_STATE(qreg, i) {
if((STATE(qreg, i) & mask) == mask) {
AMPLITUDE(qreg, i) = quda_complex_mul_i(AMPLITUDE(qreg, i));
}
}
}
QUDA_GATE void quda_quantum_controlled_rotate_k_gate(int control, int target, quantum_reg* qreg, int k) {
float temp = QUDA_PI / (1 << (k-1));
complex_t c = { .real = cos(temp), .imag = sin(temp) };
uint64_t mask = 1 << control;
mask |= 1 << target;
int i;
FOR_EACH_STATE(qreg, i) {
if((STATE(qreg, i) & mask) == mask) {
AMPLITUDE(qreg, i) = quda_complex_mul(AMPLITUDE(qreg, i),c);
}
}
}
// Three-bit quantum gates
QUDA_GATE void quda_quantum_toffoli_gate(int control1, int control2, int target, quantum_reg* qreg) {
int i;
uint64_t cmask = 1 << control1;
cmask |= 1 << control2;
uint64_t tmask = 1 << target;
FOR_EACH_STATE(qreg, i) {
if((STATE(qreg, i) & cmask) == cmask) {
STATE(qreg, i) = STATE(qreg, i) ^ tmask;
}
}
}
QUDA_GATE void quda_quantum_fredkin_gate(int control, int target1, int target2, quantum_reg* qreg) {
int i;
uint64_t cmask = 1 << control;
uint64_t tmask = 1 << target1;
tmask |= 1 << target2;
FOR_EACH_STATE(qreg, i) {
if((STATE(qreg, i) & cmask) == cmask
&& (STATE(qreg, i) & tmask) != 0
&& (~STATE(qreg, i) & tmask) != 0) {
STATE(qreg, i) = STATE(qreg, i) ^ tmask;
}
}
}