Skip to content

Commit 2bd9211

Browse files
authored
Merge pull request #131 from qsimulate/more-orbitals
More orbitals
2 parents 11c165f + b941a5d commit 2bd9211

File tree

7 files changed

+46
-27
lines changed

7 files changed

+46
-27
lines changed

src/fqe/bitstring.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from fqe.lib.bitstring import _lexicographic_bitstring_generator, _count_bits, \
2626
_get_occupation
27-
import fqe.settings
27+
from fqe.settings import use_accelerated_code, c_string_max_norb
2828

2929

3030
def gbit_index(str0: int) -> Generator[int, None, None]:
@@ -68,7 +68,7 @@ def integer_index(string: int) -> 'Nparray':
6868
Nparray: a list of integers indicating the index of each occupied \
6969
orbital
7070
"""
71-
if fqe.settings.use_accelerated_code:
71+
if use_accelerated_code:
7272
return _get_occupation(string)
7373
else:
7474
return numpy.array(list(gbit_index(int(string)))).astype(numpy.int32)
@@ -107,7 +107,7 @@ def lexicographic_bitstring_generator(nele: int, norb: int) -> 'Nparray':
107107
if nele > norb:
108108
raise ValueError("nele cannot be larger than norb")
109109

110-
if fqe.settings.use_accelerated_code:
110+
if use_accelerated_code and norb <= c_string_max_norb:
111111
out = numpy.zeros((int(special.comb(norb, nele)),), dtype=numpy.uint64)
112112
_lexicographic_bitstring_generator(out, norb, nele)
113113
return out
@@ -127,7 +127,7 @@ def count_bits(string: int) -> int:
127127
Returns:
128128
int: the number of bits equal to 1
129129
"""
130-
if fqe.settings.use_accelerated_code:
130+
if use_accelerated_code:
131131
return _count_bits(string)
132132
else:
133133
return bin(int(string)).count('1')
@@ -144,7 +144,7 @@ def get_bit(string: int, pos: int) -> int:
144144
Returns:
145145
int: 0 if the bit is 0, 2**pos if the bit is 1
146146
"""
147-
return int(string) & (1 << pos)
147+
return int(string) & (1 << int(pos))
148148

149149

150150
def set_bit(string: int, pos: int) -> int:
@@ -158,7 +158,7 @@ def set_bit(string: int, pos: int) -> int:
158158
Returns:
159159
int: string with the pos bit set to 1
160160
"""
161-
return int(string) | (1 << pos)
161+
return int(string) | (1 << int(pos))
162162

163163

164164
def unset_bit(string: int, pos: int) -> int:
@@ -172,7 +172,7 @@ def unset_bit(string: int, pos: int) -> int:
172172
Returns:
173173
int: string with the pos bit set to 0
174174
"""
175-
return int(string) & ~(1 << pos)
175+
return int(string) & ~(1 << int(pos))
176176

177177

178178
def count_bits_above(string: int, pos: int) -> int:
@@ -186,7 +186,7 @@ def count_bits_above(string: int, pos: int) -> int:
186186
Returns:
187187
int: the number of 1 bits above pos
188188
"""
189-
return count_bits(int(string) & ~((1 << (pos + 1)) - 1))
189+
return count_bits(int(string) & ~((1 << (int(pos) + 1)) - 1))
190190

191191

192192
def count_bits_below(string: int, pos: int) -> int:
@@ -200,7 +200,7 @@ def count_bits_below(string: int, pos: int) -> int:
200200
Returns:
201201
int: the number of 1 bits below pos
202202
"""
203-
return count_bits(int(string) & ((1 << pos) - 1))
203+
return count_bits(int(string) & ((1 << int(pos)) - 1))
204204

205205

206206
def count_bits_between(string: int, pos1: int, pos2: int) -> int:

src/fqe/fci_graph.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
_calculate_string_address, \
2929
_c_map_to_deexc_alpha_icol, \
3030
_make_mapping_each, _calculate_Z_matrix
31-
import fqe.settings
31+
from fqe.settings import use_accelerated_code, c_string_max_norb
3232

3333
Spinmap = Dict[Tuple[int, ...], Nparray]
3434

@@ -54,7 +54,7 @@ def map_to_deexc(mappings: Spinmap, states: int, norbs: int,
5454
index = numpy.zeros((states,), dtype=numpy.uint32)
5555
for (i, j), values in mappings.items():
5656
idx = i * norbs + j
57-
if fqe.settings.use_accelerated_code:
57+
if use_accelerated_code and norbs <= c_string_max_norb:
5858
_map_deexc(dexc, values, index, idx)
5959
else:
6060
for state, target, parity in values:
@@ -82,7 +82,7 @@ def _get_Z_matrix(norb: int, nele: int) -> Nparray:
8282
if Z.size == 0:
8383
return Z
8484

85-
if fqe.settings.use_accelerated_code:
85+
if use_accelerated_code and norb <= c_string_max_norb:
8686
_calculate_Z_matrix(Z, norb, nele)
8787
else:
8888
for k in range(1, nele):
@@ -217,7 +217,7 @@ def _build_mapping(self, strings: Nparray, nele: int,
217217
"""
218218
norb = self._norb
219219

220-
if fqe.settings.use_accelerated_code:
220+
if use_accelerated_code and norb <= c_string_max_norb:
221221
return _build_mapping_strings(strings, _get_Z_matrix(norb, nele),
222222
nele, norb)
223223
else:
@@ -315,7 +315,7 @@ def _build_strings(self, nele: int,
315315
norb = self._norb
316316
blist = lexicographic_bitstring_generator(nele, norb)
317317

318-
if fqe.settings.use_accelerated_code:
318+
if use_accelerated_code and norb <= c_string_max_norb:
319319
Z = _get_Z_matrix(norb, nele)
320320
string_list = _calculate_string_address(Z, nele, norb, blist)
321321
else:
@@ -470,7 +470,7 @@ def _map_to_deexc_alpha_icol(self):
470470
length2,
471471
), dtype=numpy.int32)
472472
astrings = self.string_alpha_all()
473-
if fqe.settings.use_accelerated_code:
473+
if use_accelerated_code and norb <= c_string_max_norb:
474474
_c_map_to_deexc_alpha_icol(exc, diag, index, astrings, norb,
475475
self._alpha_map)
476476
else:
@@ -538,7 +538,7 @@ def make_mapping_each(self, result: 'Nparray', alpha: bool, dag: List[int],
538538
strings = self.string_beta_all()
539539
length = self.lenb()
540540

541-
if fqe.settings.use_accelerated_code:
541+
if use_accelerated_code:
542542
count = _make_mapping_each(result, strings, length,
543543
numpy.array(dag, dtype=numpy.int32),
544544
numpy.array(undag, dtype=numpy.int32))

src/fqe/fci_graph_set.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import scipy
2424
from scipy import special
2525

26-
import fqe.settings
26+
from fqe.settings import use_accelerated_code, c_string_max_norb
2727
from fqe.fci_graph import FciGraph
2828
from fqe.util import alpha_beta_electrons
2929
from fqe.bitstring import integer_index, count_bits_between
@@ -171,7 +171,7 @@ def _postprocess(spinmap, dnv, index0, index1):
171171

172172
if dna != 0:
173173
(iasec, jasec) = (isec, jsec) if dna < 0 else (jsec, isec)
174-
if fqe.settings.use_accelerated_code:
174+
if use_accelerated_code and norb <= c_string_max_norb:
175175
ndowna, nupa = _make_mapping_each_set(iasec.string_alpha_all(),
176176
abs(dna), norb,
177177
iasec.nalpha())
@@ -192,7 +192,7 @@ def _postprocess(spinmap, dnv, index0, index1):
192192

193193
if dnb != 0:
194194
(ibsec, jbsec) = (isec, jsec) if dnb < 0 else (jsec, isec)
195-
if fqe.settings.use_accelerated_code:
195+
if use_accelerated_code and norb <= c_string_max_norb:
196196
ndownb, nupb = _make_mapping_each_set(ibsec.string_beta_all(),
197197
abs(dnb), norb,
198198
ibsec.nbeta())

src/fqe/fqe_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ def _apply_array_spatial12_lowfilling_fast(self, h1e: 'Nparray',
802802
nlt = norb * (norb + 1) // 2
803803

804804
h2ecomp = numpy.zeros((nlt, nlt), dtype=self._dtype)
805-
h2etemp = numpy.ascontiguousarray(h2e)
805+
h2etemp = numpy.ascontiguousarray(h2e, dtype=self._dtype)
806806
_make_Hcomp(norb, nlt, h2etemp, h2ecomp)
807807

808808
if nalpha - 2 >= 0:

src/fqe/lib/bitstring.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ void lexicographic_bitstring_generator(uint64_t *out,
3333
out[0] = 0;
3434
return;
3535
}
36-
int64_t combo = (1ll << nele) - 1;
37-
while (combo < (1ll << norb)) {
36+
uint64_t combo = (1ull << nele) - 1;
37+
while (combo < (1ull << norb)) {
3838
*out++ = combo;
39-
const int64_t x = combo & -combo;
40-
const int64_t y = combo + x;
41-
const int64_t z = (combo & ~y);
39+
const uint64_t x = combo & -combo;
40+
const uint64_t y = combo + x;
41+
const uint64_t z = (combo & ~y);
4242
combo = z / x;
4343
combo >>= 1;
4444
combo |= y;

src/fqe/lib/bitstring.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@
2727
inline int gbit_index(uint64_t *str, int *bit_index) {
2828
#ifdef __GNUC__
2929
const int pos = __builtin_ffsll(*str);
30-
*str >>= pos;
30+
if (pos == 64) {
31+
*str = 0ull;
32+
} else {
33+
*str >>= pos;
34+
}
3135
*bit_index += pos;
3236
return pos;
3337
#else
34-
if (*bit_index == -1) { *str = *str << 1; }
38+
if (*bit_index == -1) {
39+
//*str = *str << 1;
40+
*bit_index += 1;
41+
if (*str & 1) { return 1; }
42+
}
3543
while (*str) {
3644
*str = *str >> 1;
3745
*bit_index += 1;

src/fqe/settings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@ class CodePath(Flag):
3838
"""
3939
A switch to check if accelerated code is used. Default is true if accelerated code is available
4040
"""
41+
42+
global_max_norb = 64
43+
"""
44+
The global maximum number of orbitals that can be handled by FQE.
45+
"""
46+
47+
c_string_max_norb = 63
48+
"""
49+
The max number of orbitals correctly handled by the C codepath
50+
for generating determinant strings.
51+
"""

0 commit comments

Comments
 (0)