Skip to content

Commit 234f4fb

Browse files
Strilancbabbush
authored andcommitted
Switch position_kinetic_operator to Grid (#44)
1 parent bbe99bb commit 234f4fb

File tree

2 files changed

+32
-58
lines changed

2 files changed

+32
-58
lines changed

src/fermilib/utils/_jellium.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -258,53 +258,47 @@ def momentum_potential_operator(n_dimensions, grid_length,
258258
return operator
259259

260260

261-
def position_kinetic_operator(n_dimensions, grid_length,
262-
length_scale, spinless=False):
261+
def position_kinetic_operator(grid, spinless=False):
263262
"""Return the kinetic operator in position space second quantization.
264263
265264
Args:
266-
n_dimensions: An int giving the number of dimensions for the model.
267-
grid_length: Int, the number of points in one dimension of the grid.
268-
length_scale: Float, the real space length of a box dimension.
265+
grid (Grid): The discretization to use.
269266
spinless: Bool, whether to use the spinless model or not.
270267
271268
Returns:
272269
operator: An instance of the FermionOperator class.
273270
"""
274271
# Initialize.
275-
n_points = grid_length ** n_dimensions
272+
n_points = grid.num_points()
276273
operator = FermionOperator()
277274
if spinless:
278275
spins = [None]
279276
else:
280277
spins = [0, 1]
281278

282279
# Loop once through all lattice sites.
283-
for grid_indices_a in itertools.product(range(grid_length),
284-
repeat=n_dimensions):
280+
for grid_indices_a in grid.all_points_indices():
285281
coordinates_a = position_vector(
286-
grid_indices_a, grid_length, length_scale)
287-
for grid_indices_b in itertools.product(range(grid_length),
288-
repeat=n_dimensions):
282+
grid_indices_a, grid.length, grid.scale)
283+
for grid_indices_b in grid.all_points_indices():
289284
coordinates_b = position_vector(
290-
grid_indices_b, grid_length, length_scale)
285+
grid_indices_b, grid.length, grid.scale)
291286
differences = coordinates_b - coordinates_a
292287

293288
# Compute coefficient.
294289
coefficient = 0.
295-
for momenta_indices in itertools.product(range(grid_length),
296-
repeat=n_dimensions):
290+
for momenta_indices in grid.all_points_indices():
297291
momenta = momentum_vector(
298-
momenta_indices, grid_length, length_scale)
292+
momenta_indices, grid.length, grid.scale)
299293
if momenta.any():
300294
coefficient += (
301295
numpy.cos(momenta.dot(differences)) *
302296
momenta.dot(momenta) / (2. * float(n_points)))
303297

304298
# Loop over spins and identify interacting orbitals.
305299
for spin in spins:
306-
orbital_a = orbital_id(grid_length, grid_indices_a, spin)
307-
orbital_b = orbital_id(grid_length, grid_indices_b, spin)
300+
orbital_a = orbital_id(grid.length, grid_indices_a, spin)
301+
orbital_b = orbital_id(grid.length, grid_indices_b, spin)
308302

309303
# Add interaction term.
310304
operators = ((orbital_a, 1), (orbital_b, 0))
@@ -397,10 +391,7 @@ def jellium_model(grid, spinless=False, momentum_space=True):
397391
grid.scale,
398392
spinless)
399393
else:
400-
hamiltonian = position_kinetic_operator(grid.dimensions,
401-
grid.length,
402-
grid.scale,
403-
spinless)
394+
hamiltonian = position_kinetic_operator(grid, spinless)
404395
hamiltonian += position_potential_operator(grid.dimensions,
405396
grid.length,
406397
grid.scale,

src/fermilib/utils/_jellium_test.py

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from __future__ import absolute_import
1414

15-
import itertools
1615
import unittest
1716

1817
import numpy
@@ -129,15 +128,10 @@ def test_momentum_vector(self):
129128
def test_kinetic_integration(self):
130129

131130
# Compute kinetic energy operator in both momentum and position space.
132-
n_dimensions = 2
133-
grid_length = 2
134-
length_scale = 3.
131+
grid = Grid(dimensions=2, length=2, scale=3.)
135132
spinless = False
136-
momentum_kinetic = momentum_kinetic_operator(
137-
Grid(n_dimensions, grid_length, length_scale),
138-
spinless)
139-
position_kinetic = position_kinetic_operator(
140-
n_dimensions, grid_length, length_scale, spinless)
133+
momentum_kinetic = momentum_kinetic_operator(grid, spinless)
134+
position_kinetic = position_kinetic_operator(grid, spinless)
141135

142136
# Diagonalize and confirm the same energy.
143137
jw_momentum = jordan_wigner(momentum_kinetic)
@@ -196,22 +190,19 @@ def test_model_integration(self):
196190
def test_coefficients(self):
197191

198192
# Test that the coefficients post-JW transform are as claimed in paper.
199-
n_dimensions = 2
200-
grid_length = 3
201-
length_scale = 2.
193+
grid = Grid(dimensions=2, length=3, scale=2.)
202194
spinless = 1
203-
n_orbitals = grid_length ** n_dimensions
195+
n_orbitals = grid.num_points()
204196
n_qubits = (2 ** (1 - spinless)) * n_orbitals
205-
volume = length_scale ** n_dimensions
197+
volume = grid.volume_scale()
206198

207199
# Kinetic operator.
208-
kinetic = position_kinetic_operator(
209-
n_dimensions, grid_length, length_scale, spinless)
200+
kinetic = position_kinetic_operator(grid, spinless)
210201
qubit_kinetic = jordan_wigner(kinetic)
211202

212203
# Potential operator.
213204
potential = position_potential_operator(
214-
n_dimensions, grid_length, length_scale, spinless)
205+
grid.dimensions, grid.length, grid.scale, spinless)
215206
qubit_potential = jordan_wigner(potential)
216207

217208
# Check identity.
@@ -221,10 +212,8 @@ def test_coefficients(self):
221212

222213
paper_kinetic_coefficient = 0.
223214
paper_potential_coefficient = 0.
224-
for indices in itertools.product(range(grid_length),
225-
repeat=n_dimensions):
226-
momenta = momentum_vector(
227-
indices, grid_length, length_scale)
215+
for indices in grid.all_points_indices():
216+
momenta = momentum_vector(indices, grid.length, grid.scale)
228217
paper_kinetic_coefficient += float(
229218
n_qubits) * momenta.dot(momenta) / float(4. * n_orbitals)
230219

@@ -246,10 +235,8 @@ def test_coefficients(self):
246235

247236
paper_kinetic_coefficient = 0.
248237
paper_potential_coefficient = 0.
249-
for indices in itertools.product(range(grid_length),
250-
repeat=n_dimensions):
251-
momenta = momentum_vector(
252-
indices, grid_length, length_scale)
238+
for indices in grid.all_points_indices():
239+
momenta = momentum_vector(indices, grid.length, grid.scale)
253240
paper_kinetic_coefficient -= momenta.dot(
254241
momenta) / float(4. * n_orbitals)
255242

@@ -269,27 +256,25 @@ def test_coefficients(self):
269256
else:
270257
spins = [0, 1]
271258

272-
for indices_a in itertools.product(range(grid_length),
273-
repeat=n_dimensions):
274-
for indices_b in itertools.product(range(grid_length),
275-
repeat=n_dimensions):
259+
for indices_a in grid.all_points_indices():
260+
for indices_b in grid.all_points_indices():
276261

277262
paper_kinetic_coefficient = 0.
278263
paper_potential_coefficient = 0.
279264

280265
position_a = position_vector(
281-
indices_a, grid_length, length_scale)
266+
indices_a, grid.length, grid.scale)
282267
position_b = position_vector(
283-
indices_b, grid_length, length_scale)
268+
indices_b, grid.length, grid.scale)
284269
differences = position_b - position_a
285270

286271
for spin_a in spins:
287272
for spin_b in spins:
288273

289274
p = orbital_id(
290-
grid_length, indices_a, spin_a)
275+
grid.length, indices_a, spin_a)
291276
q = orbital_id(
292-
grid_length, indices_b, spin_b)
277+
grid.length, indices_b, spin_b)
293278

294279
if p == q:
295280
continue
@@ -300,11 +285,9 @@ def test_coefficients(self):
300285
else:
301286
potential_coefficient = 0.
302287

303-
for indices_c in \
304-
itertools.product(range(grid_length),
305-
repeat=n_dimensions):
288+
for indices_c in grid.all_points_indices():
306289
momenta = momentum_vector(
307-
indices_c, grid_length, length_scale)
290+
indices_c, grid.length, grid.scale)
308291

309292
if momenta.any():
310293
potential_contribution = numpy.pi * numpy.cos(

0 commit comments

Comments
 (0)