Skip to content

Commit 7d20e39

Browse files
Strilancbabbush
authored andcommitted
Switch orbital_id to Grid (#57)
1 parent ce43e99 commit 7d20e39

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

src/fermilib/utils/_jellium.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class OrbitalSpecificationError(Exception):
2424
pass
2525

2626

27-
def orbital_id(grid_length, grid_coordinates, spin=None):
27+
def orbital_id(grid, grid_coordinates, spin=None):
2828
"""Return the tensor factor of a orbital with given coordinates and spin.
2929
3030
Args:
31-
grid_length: Int, the number of points in one dimension of the grid.
31+
grid (Grid): The discretization to use.
3232
grid_coordinates: List or tuple of ints giving coordinates of grid
3333
element. Acceptable to provide an int (instead of tuple or list)
3434
for 1D case.
@@ -47,8 +47,8 @@ def orbital_id(grid_length, grid_coordinates, spin=None):
4747
for dimension, grid_coordinate in enumerate(grid_coordinates):
4848

4949
# Make sure coordinate is an integer in the correct bounds.
50-
if isinstance(grid_coordinate, int) and grid_coordinate < grid_length:
51-
tensor_factor += grid_coordinate * (grid_length ** dimension)
50+
if isinstance(grid_coordinate, int) and grid_coordinate < grid.length:
51+
tensor_factor += grid_coordinate * (grid.length ** dimension)
5252

5353
else:
5454
# Raise for invalid model.
@@ -164,7 +164,7 @@ def momentum_kinetic_operator(grid, spinless=False):
164164

165165
# Loop over spins.
166166
for spin in spins:
167-
orbital = orbital_id(grid.length, momenta_indices, spin)
167+
orbital = orbital_id(grid, momenta_indices, spin)
168168

169169
# Add interaction term.
170170
operators = ((orbital, 1), (orbital, 0))
@@ -220,15 +220,11 @@ def momentum_potential_operator(grid, spinless=False):
220220

221221
# Loop over spins.
222222
for spin_a in spins:
223-
orbital_a = orbital_id(
224-
grid.length, grid_indices_a, spin_a)
225-
orbital_d = orbital_id(
226-
grid.length, shifted_indices_d, spin_a)
223+
orbital_a = orbital_id(grid, grid_indices_a, spin_a)
224+
orbital_d = orbital_id(grid, shifted_indices_d, spin_a)
227225
for spin_b in spins:
228-
orbital_b = orbital_id(
229-
grid.length, grid_indices_b, spin_b)
230-
orbital_c = orbital_id(
231-
grid.length, shifted_indices_c, spin_b)
226+
orbital_b = orbital_id(grid, grid_indices_b, spin_b)
227+
orbital_c = orbital_id(grid, shifted_indices_c, spin_b)
232228

233229
# Add interaction term.
234230
if (orbital_a != orbital_b) and \
@@ -277,8 +273,8 @@ def position_kinetic_operator(grid, spinless=False):
277273

278274
# Loop over spins and identify interacting orbitals.
279275
for spin in spins:
280-
orbital_a = orbital_id(grid.length, grid_indices_a, spin)
281-
orbital_b = orbital_id(grid.length, grid_indices_b, spin)
276+
orbital_a = orbital_id(grid, grid_indices_a, spin)
277+
orbital_b = orbital_id(grid, grid_indices_b, spin)
282278

283279
# Add interaction term.
284280
operators = ((orbital_a, 1), (orbital_b, 0))
@@ -325,9 +321,9 @@ def position_potential_operator(grid, spinless=False):
325321

326322
# Loop over spins and identify interacting orbitals.
327323
for spin_a in spins:
328-
orbital_a = orbital_id(grid.length, grid_indices_a, spin_a)
324+
orbital_a = orbital_id(grid, grid_indices_a, spin_a)
329325
for spin_b in spins:
330-
orbital_b = orbital_id(grid.length, grid_indices_b, spin_b)
326+
orbital_b = orbital_id(grid, grid_indices_b, spin_b)
331327

332328
# Add interaction term.
333329
if orbital_a != orbital_b:

src/fermilib/utils/_jellium_test.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,25 @@ class JelliumTest(unittest.TestCase):
3737
def test_orbital_id(self):
3838

3939
# Test in 1D with spin.
40-
grid_length = 5
40+
grid = Grid(dimensions=1, length=5, scale=1.0)
4141
input_coords = [0, 1, 2, 3, 4]
4242
tensor_factors_up = [1, 3, 5, 7, 9]
4343
tensor_factors_down = [0, 2, 4, 6, 8]
4444

45-
test_output_up = [orbital_id(
46-
grid_length, i, 1) for i in input_coords]
47-
test_output_down = [orbital_id(
48-
grid_length, i, 0) for i in input_coords]
45+
test_output_up = [orbital_id(grid, i, 1) for i in input_coords]
46+
test_output_down = [orbital_id(grid, i, 0) for i in input_coords]
4947

5048
self.assertEqual(test_output_up, tensor_factors_up)
5149
self.assertEqual(test_output_down, tensor_factors_down)
5250

5351
with self.assertRaises(OrbitalSpecificationError):
54-
orbital_id(5, 6, 1)
52+
orbital_id(grid, 6, 1)
5553

5654
# Test in 2D without spin.
57-
grid_length = 3
55+
grid = Grid(dimensions=2, length=3, scale=1.0)
5856
input_coords = [(0, 0), (0, 1), (1, 2)]
5957
tensor_factors = [0, 3, 7]
60-
test_output = [orbital_id(
61-
grid_length, i) for i in input_coords]
58+
test_output = [orbital_id(grid, i) for i in input_coords]
6259
self.assertEqual(test_output, tensor_factors)
6360

6461
def test_position_vector(self):
@@ -253,10 +250,8 @@ def test_coefficients(self):
253250
for spin_a in spins:
254251
for spin_b in spins:
255252

256-
p = orbital_id(
257-
grid.length, indices_a, spin_a)
258-
q = orbital_id(
259-
grid.length, indices_b, spin_b)
253+
p = orbital_id(grid, indices_a, spin_a)
254+
q = orbital_id(grid, indices_b, spin_b)
260255

261256
if p == q:
262257
continue

src/fermilib/utils/_plane_wave_hamiltonian.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ def dual_basis_u_operator(grid, geometry, spinless):
5858
numpy.exp(exp_index))
5959

6060
for spin_p in spins:
61-
orbital_p = orbital_id(
62-
grid.length, pos_indices, spin_p)
61+
orbital_p = orbital_id(grid, pos_indices, spin_p)
6362
operators = ((orbital_p, 1), (orbital_p, 0))
6463
if operator is None:
6564
operator = FermionOperator(operators, coefficient)
@@ -108,8 +107,8 @@ def plane_wave_u_operator(grid, geometry, spinless):
108107
numpy.exp(exp_index))
109108

110109
for spin in spins:
111-
orbital_p = orbital_id(grid.length, indices_p, spin)
112-
orbital_q = orbital_id(grid.length, indices_q, spin)
110+
orbital_p = orbital_id(grid, indices_p, spin)
111+
orbital_q = orbital_id(grid, indices_q, spin)
113112
operators = ((orbital_p, 1), (orbital_q, 0))
114113
if operator is None:
115114
operator = FermionOperator(operators, coefficient)
@@ -215,7 +214,7 @@ def _fourier_transform_helper(hamiltonian, grid, spinless,
215214
spin = None
216215
else:
217216
spin = ladder_operator[0] % 2
218-
orbital = orbital_id(grid.length, indices_2, spin)
217+
orbital = orbital_id(grid, indices_2, spin)
219218
exp_index = factor * 1.0j * numpy.dot(vec_1, vec_2)
220219
if ladder_operator[1] == 1:
221220
exp_index *= -1.0

0 commit comments

Comments
 (0)