-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtime.c
186 lines (141 loc) · 4.51 KB
/
qtime.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
/* qtime.c: Time evolution of a quantum system
Copyright 2006-2013 Hendrik Weimer
This file is part of libquantum
libquantum is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
libquantum is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with libquantum; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA
*/
#include <math.h>
#include <string.h>
#include <stdio.h>
#include "qtime.h"
#include "qureg.h"
#include "complex.h"
#include "config.h"
/* Forth-order Runge-Kutta
Flags: QUANTUM_RK4_NODELETE: Do not delete quantum_reg returned by H
QUANTUM_RK4_IMAGINARY: Imaginary time evolution */
void
quantum_rk4(quantum_reg *reg, double t, double dt,
quantum_reg H(MAX_UNSIGNED, double), int flags)
{
quantum_reg k, out, tmp;
double r = 0;
int i;
void *hash;
int hashw;
COMPLEX_FLOAT step = dt;
hash = reg->hash;
reg->hash = 0;
hashw = reg->hashw;
reg->hashw = 0;
if(!(flags & QUANTUM_RK4_IMAGINARY))
step *= IMAGINARY;
/* k1 */
k = quantum_matrix_qureg(H, t, reg, flags & QUANTUM_RK4_NODELETE);
quantum_scalar_qureg(-step/2.0, &k);
tmp = quantum_vectoradd(reg, &k);
quantum_scalar_qureg(1.0/3.0, &k);
out = quantum_vectoradd(reg, &k);
quantum_delete_qureg(&k);
/* k2 */
k = quantum_matrix_qureg(H, t+dt/2.0, &tmp, flags & QUANTUM_RK4_NODELETE);
quantum_delete_qureg(&tmp);
quantum_scalar_qureg(-step/2.0, &k);
tmp = quantum_vectoradd(reg, &k);
quantum_scalar_qureg(2.0/3.0, &k);
quantum_vectoradd_inplace(&out, &k);
quantum_delete_qureg(&k);
/* k3 */
k = quantum_matrix_qureg(H, t+dt/2.0, &tmp, flags & QUANTUM_RK4_NODELETE);
quantum_delete_qureg(&tmp);
quantum_scalar_qureg(-step, &k);
tmp = quantum_vectoradd(reg, &k);
quantum_scalar_qureg(1.0/3.0, &k);
quantum_vectoradd_inplace(&out, &k);
quantum_delete_qureg(&k);
/* k4 */
k = quantum_matrix_qureg(H, t+dt, &tmp, flags & QUANTUM_RK4_NODELETE);
quantum_delete_qureg(&tmp);
quantum_scalar_qureg(-step/6.0, &k);
quantum_vectoradd_inplace(&out, &k);
quantum_delete_qureg(&k);
quantum_delete_qureg(reg);
/* Normalize quantum register */
if(flags & QUANTUM_RK4_IMAGINARY)
{
for(i=0; i<out.size; i++)
r += quantum_prob(out.amplitude[i]);
quantum_scalar_qureg(sqrt(1.0/r), &out);
}
out.hash = hash;
out.hashw = hashw;
*reg = out;
}
/* Adaptive Runge-Kutta. Stores the new stepsize in dt and returns the
stepsize actually used. For further details, see Press et al.,
Numerical Recipes in C (Second Edition, CUP, 1992), Sec. 16.3 */
double
quantum_rk4a(quantum_reg *reg, double t, double *dt, double epsilon,
quantum_reg H(MAX_UNSIGNED, double), int flags)
{
quantum_reg reg2, old;
double delta, r, dtused;
int i;
void *hash;
int hashw;
hash = reg->hash;
reg->hash = 0;
hashw = reg->hashw;
reg->hashw = 0;
quantum_copy_qureg(reg, &old);
quantum_copy_qureg(reg, ®2);
do
{
quantum_rk4(reg, t, *dt, H, flags);
quantum_rk4(®2, t, *dt/2.0, H, flags);
quantum_rk4(®2, t+*dt/2.0, *dt/2.0, H, flags);
delta = 0;
for(i=0;i<reg->size;i++)
{
r = 2*sqrt(quantum_prob(reg->amplitude[i] - reg2.amplitude[i])/
quantum_prob(reg->amplitude[i] + reg2.amplitude[i]));
if(r > delta)
delta = r;
}
dtused = *dt;
if(delta < epsilon)
*dt *= 0.9*pow(epsilon/delta, 0.2);
else
*dt *= 0.9*pow(epsilon/delta, 0.25);
if(*dt > 4*dtused)
*dt = 4*dtused;
else if(*dt < 0.25*dtused)
*dt = 0.25*dtused;
if(delta > epsilon)
{
memcpy(reg->amplitude, old.amplitude,
reg->size*sizeof(COMPLEX_FLOAT));
memcpy(reg2.amplitude, old.amplitude,
reg->size*sizeof(COMPLEX_FLOAT));
if(reg->state && old.state)
memcpy(reg->state, old.state, reg->size*sizeof(MAX_UNSIGNED));
if(reg2.state && old.state)
memcpy(reg2.state, old.state, reg->size*sizeof(MAX_UNSIGNED));
}
} while(delta > epsilon);
reg->hash = hash;
reg->hashw = hashw;
quantum_delete_qureg(&old);
quantum_delete_qureg(®2);
return dtused;
}