-
Notifications
You must be signed in to change notification settings - Fork 3
/
lintable.py
213 lines (170 loc) · 4.73 KB
/
lintable.py
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
# File: lintable.py
#
# Simple and quick implementation of Lin tables for indexing Sz spin states.
# Used in the fermihubbard.py implementation.
#
# Code released under a MIT license, by
# Kim G. L. Pedersen, 2015
# (unless otherwise noted in the function description)
#
# Waiver: No guarantees given. Please use **completely** at your own risk.
from __future__ import division, print_function
import numpy as np
from itertools import permutations # necessary in LinTable
class Table(object):
def __init__(self, n, nu, nd):
self.nu = nu
self.nd = nd
self.n = n
if nu > n or nd > n:
self.Jdu, self.Nu, self.basisu = (['1'], 1, [])
self.Jdv, self.Nd, self.basisd = (['1'], 1, [])
else:
self.Juv, self.Nu, self.basisu = states(self.n, self.nu)
self.Jdv, self.Nd, self.basisd = states(self.n, self.nd)
@property
def Js(self):
"""get J indices"""
return {'u': self.Juv, 'd': self.Jdv}
@property
def Ns(self):
"""Get the Ns"""
return (self.Nu, self.Nd)
@property
def N(self):
return np.prod(self.Ns)
@property
def ns(self):
return (self.nu, self.nd)
@property
def ne(self):
return self.nu + self.nd
def states(n, nu):
"""
Create all many-body spin states
Parameters
----------
n : int
number of sites
nu : int
number of on spin-specie
"""
x = [0]*(n-nu) + [1]*nu
states = np.array(unique_permutations(x), dtype=int)
N = states.shape[0]
Jv = bi2de(states)
return (Jv, N, states)
def state2index(states, Juv, Jdv=None):
"""
Parameters
----------
states : ndarray
states to index
Juv : list
indexes of the spin-up subspace
Jdv : list
index of the spin-down subspace
"""
Nu = Juv.shape[0]
Ju = {J: i for i, J in enumerate(Juv)}
if Jdv is None:
if len(states.shape) < 2:
states = np.array([states])
Js = np.array([Ju[i] for i in bi2de(states)])
else:
# Nd = Jdv.shape[0]
Jd = {J: i for i, J in enumerate(Jdv)}
n = states.shape[1]/2
Ius = bi2de(states[:, 1:n])
Ids = bi2de(states[:, n+1:])
Js = np.array([Jd[i] for i in Ids])*Nu + np.array([Ju[i] for i in Ius])
return Js
def index2state(Is, n, Juv, Jdv=None):
"""
Returns state with a given index
Parameters
----------
Is : ndarray
list of indices
n : int
number of sites
Juv : ndarray
Lin table of spin-up states
Jdv : ndarray
Lin table for spin-down states
"""
Nu = Juv.shape[0]
if Jdv is None:
Ius = np.mod(Is, Nu)
states_up = de2bi(Juv[Ius], n)
return states_up
else:
# Nd = Jdv.shape[0]
Ius = np.mod(Is, Nu)
Ids = np.floor(Is/Nu).astype(int)
states_up = de2bi(Juv[Ius], n)
states_down = de2bi(Jdv[Ids], n)
return (states_up, states_down)
def unique_permutations(elements):
"""
Get all unique permutations of a list of elements
Parameters
----------
elements : list
a list containing the elements
"""
n = len(elements)
uniques = list(set(elements))
nu = len(uniques)
if not elements:
return []
elif n == 1 or nu == 1:
return [elements]
elif n == nu:
ps = permutations(elements)
return [list(p) for p in ps]
else:
pu = []
# collect the results
for i in np.arange(nu):
# copy elements into v
v = list(elements)
# first instance of unique element
ind = elements.index(uniques[i])
# remove this element
del v[ind]
# extend the result
pu.extend([[uniques[i]] + perm for perm in unique_permutations(v)])
return pu
def bi2de(binaries):
"""
Parameters
----------
binaries : ndarray
Here one row is one binary number.
"""
n = binaries.shape[0]
if len(binaries.shape) > 1:
n = binaries.shape[1]
decimals = np.dot(binaries, np.power(2, np.arange(n-1, -1, -1)))
# print('d: {0}'.format(decimals))
# if (decimals.size == 1):
# return [decimals]
return decimals
def de2bi(decimals, n=None):
"""
Parameters
----------
decimals : ndarray
vector of decimals
n : int
number of binary digits
"""
decimals = np.array(decimals)
try:
nd = np.ceil(np.log2(np.max(decimals)))
except RuntimeWarning:
print('{0}:{1}'.format(decimals, n))
if n is None or n < nd:
n = nd
return np.remainder(np.floor(np.outer(decimals, np.power(2., np.arange(1-n,1)))), 2).astype(int)