Skip to content

Commit 4f71c9f

Browse files
Strilancbabbush
authored andcommitted
Switch momentum_potential_operator to Grid (#45)
1 parent 234f4fb commit 4f71c9f

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

src/fermilib/utils/_jellium.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,18 @@ def momentum_kinetic_operator(grid, spinless=False):
183183
return operator
184184

185185

186-
def momentum_potential_operator(n_dimensions, grid_length,
187-
length_scale, spinless=False):
186+
def momentum_potential_operator(grid, spinless=False):
188187
"""Return the potential operator in momentum second quantization.
189188
190189
Args:
191-
n_dimensions: An int giving the number of dimensions for the model.
192-
grid_length: Int, the number of points in one dimension of the grid.
193-
length_scale: Float, the real space length of a box dimension.
190+
grid (Grid): The discretization to use.
194191
spinless: Boole, whether to use the spinless model or not.
195192
196193
Returns:
197194
operator: An instance of the FermionOperator class.
198195
"""
199196
# Initialize.
200-
n_points = grid_length ** n_dimensions
201-
volume = length_scale ** float(n_dimensions)
197+
volume = grid.volume_scale()
202198
prefactor = 2. * numpy.pi / volume
203199
operator = FermionOperator((), 0.0)
204200
if spinless:
@@ -207,14 +203,13 @@ def momentum_potential_operator(n_dimensions, grid_length,
207203
spins = [0, 1]
208204

209205
# Loop once through all plane waves.
210-
for omega_indices in itertools.product(range(grid_length),
211-
repeat=n_dimensions):
212-
shifted_omega_indices = [index - grid_length // 2 for
206+
for omega_indices in grid.all_points_indices():
207+
shifted_omega_indices = [index - grid.length // 2 for
213208
index in omega_indices]
214209

215210
# Get the momenta vectors.
216211
omega_momenta = momentum_vector(
217-
omega_indices, grid_length, length_scale)
212+
omega_indices, grid.length, grid.scale)
218213

219214
# Skip if omega momentum is zero.
220215
if not omega_momenta.any():
@@ -224,28 +219,27 @@ def momentum_potential_operator(n_dimensions, grid_length,
224219
coefficient = prefactor / \
225220
omega_momenta.dot(omega_momenta)
226221

227-
for grid_indices_a in itertools.product(range(grid_length),
228-
repeat=n_dimensions):
222+
for grid_indices_a in grid.all_points_indices():
229223
shifted_indices_d = [
230-
(grid_indices_a[i] - shifted_omega_indices[i]) %
231-
grid_length for i in range(n_dimensions)]
232-
for grid_indices_b in itertools.product(range(grid_length),
233-
repeat=n_dimensions):
224+
(grid_indices_a[i] - shifted_omega_indices[i]) % grid.length
225+
for i in range(grid.dimensions)]
226+
for grid_indices_b in grid.all_points_indices():
234227
shifted_indices_c = [
235228
(grid_indices_b[i] + shifted_omega_indices[i]) %
236-
grid_length for i in range(n_dimensions)]
229+
grid.length
230+
for i in range(grid.dimensions)]
237231

238232
# Loop over spins.
239233
for spin_a in spins:
240234
orbital_a = orbital_id(
241-
grid_length, grid_indices_a, spin_a)
235+
grid.length, grid_indices_a, spin_a)
242236
orbital_d = orbital_id(
243-
grid_length, shifted_indices_d, spin_a)
237+
grid.length, shifted_indices_d, spin_a)
244238
for spin_b in spins:
245239
orbital_b = orbital_id(
246-
grid_length, grid_indices_b, spin_b)
240+
grid.length, grid_indices_b, spin_b)
247241
orbital_c = orbital_id(
248-
grid_length, shifted_indices_c, spin_b)
242+
grid.length, shifted_indices_c, spin_b)
249243

250244
# Add interaction term.
251245
if (orbital_a != orbital_b) and \
@@ -386,10 +380,7 @@ def jellium_model(grid, spinless=False, momentum_space=True):
386380

387381
if momentum_space:
388382
hamiltonian = momentum_kinetic_operator(grid, spinless)
389-
hamiltonian += momentum_potential_operator(grid.dimensions,
390-
grid.length,
391-
grid.scale,
392-
spinless)
383+
hamiltonian += momentum_potential_operator(grid, spinless)
393384
else:
394385
hamiltonian = position_kinetic_operator(grid, spinless)
395386
hamiltonian += position_potential_operator(grid.dimensions,

src/fermilib/utils/_jellium_test.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,11 @@ def test_potential_integration(self):
148148

149149
# Compute potential energy operator in both momentum and position
150150
# space.
151-
n_dimensions = 2
152-
grid_length = 3
153-
length_scale = 2.
151+
grid = Grid(dimensions=2, length=3, scale = 2.)
154152
spinless = 1
155-
momentum_potential = momentum_potential_operator(
156-
n_dimensions, grid_length, length_scale, spinless)
153+
momentum_potential = momentum_potential_operator(grid, spinless)
157154
position_potential = position_potential_operator(
158-
n_dimensions, grid_length, length_scale, spinless)
155+
grid.dimensions, grid.length, grid.scale, spinless)
159156

160157
# Diagonalize and confirm the same energy.
161158
jw_momentum = jordan_wigner(momentum_potential)

0 commit comments

Comments
 (0)