Skip to content

Commit

Permalink
Improve cistring.addr2str for large address (issue pyscf#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed Jan 31, 2024
1 parent a20dfb3 commit 3ccde25
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pyscf/fci/cistring.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,13 @@ def addr2str(norb, nelec, addr):
'''Convert CI determinant address to string'''
if norb >= 64:
raise NotImplementedError('norb >= 64')
assert num_strings(norb, nelec) > addr
max_addr = num_strings(norb, nelec)
assert max_addr > addr

if addr < 2**31:
if max_addr < 2**31:
return addrs2str(norb, nelec, [addr])[0]

return _addr2str(norb, nelec, addr)
else:
return _addr2str(norb, nelec, addr)

def _addr2str(norb, nelec, addr):
if addr == 0 or nelec == norb or nelec == 0:
Expand Down
6 changes: 6 additions & 0 deletions pyscf/fci/test/test_cistring.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ def test_addr2str(self):
self.assertEqual(bin(cistring.addr2str(6, 3, 8)), '0b11010')
self.assertEqual(bin(cistring.addr2str(7, 4, 9)), '0b110011')

# Test large addresss
string = 0b101101111101101111001110111111110111111100
address = cistring.str2addr(norb=63, nelec=32, string=string)
string2 = cistring.addr2str(norb=63, nelec=32, addr=address)
self.assertEqual(string, string2)

def test_str2addr(self):
self.assertEqual(str2addr(6, 3, int('0b11001' ,2)), cistring.str2addr(6, 3, int('0b11001' ,2)))
self.assertEqual(str2addr(6, 3, int('0b11010' ,2)), cistring.str2addr(6, 3, int('0b11010' ,2)))
Expand Down

0 comments on commit 3ccde25

Please sign in to comment.