Skip to content

Commit 4bd6740

Browse files
Strilancbabbush
authored andcommitted
Switch position_potential_operator to Grid (#48)
* Switch position_kinetic_operator to Grid * Switch momentum_potential_operator to Grid * Switch jordan_wigner_position_jellium to Grid * Switch position_potential_operator to Grid
1 parent 4f71c9f commit 4bd6740

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

src/fermilib/utils/_jellium.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
"""This module constructs Hamiltonians for the uniform electron gas."""
1414
from __future__ import absolute_import
1515

16-
import itertools
1716
import numpy
17+
from projectq.ops import QubitOperator
1818

1919
from fermilib.ops import FermionOperator
2020

21-
from projectq.ops import QubitOperator
22-
2321

2422
# Exceptions.
2523
class OrbitalSpecificationError(Exception):
@@ -302,22 +300,18 @@ def position_kinetic_operator(grid, spinless=False):
302300
return operator
303301

304302

305-
def position_potential_operator(n_dimensions, grid_length,
306-
length_scale, spinless=False):
303+
def position_potential_operator(grid, spinless=False):
307304
"""Return the potential operator in position space second quantization.
308305
309306
Args:
310-
n_dimensions: An int giving the number of dimensions for the model.
311-
grid_length: Int, the number of points in one dimension of the grid.
312-
length_scale: Float, the real space length of a box dimension.
307+
grid (Grid): The discretization to use.
313308
spinless: Boole, whether to use the spinless model or not.
314309
315310
Returns:
316311
operator: An instance of the FermionOperator class.
317312
"""
318313
# Initialize.
319-
n_points = grid_length ** n_dimensions
320-
volume = length_scale ** float(n_dimensions)
314+
volume = grid.volume_scale()
321315
prefactor = 2. * numpy.pi / volume
322316
operator = FermionOperator()
323317
if spinless:
@@ -326,32 +320,29 @@ def position_potential_operator(n_dimensions, grid_length,
326320
spins = [0, 1]
327321

328322
# Loop once through all lattice sites.
329-
for grid_indices_a in itertools.product(range(grid_length),
330-
repeat=n_dimensions):
323+
for grid_indices_a in grid.all_points_indices():
331324
coordinates_a = position_vector(
332-
grid_indices_a, grid_length, length_scale)
333-
for grid_indices_b in itertools.product(range(grid_length),
334-
repeat=n_dimensions):
325+
grid_indices_a, grid.length, grid.scale)
326+
for grid_indices_b in grid.all_points_indices():
335327
coordinates_b = position_vector(
336-
grid_indices_b, grid_length, length_scale)
328+
grid_indices_b, grid.length, grid.scale)
337329
differences = coordinates_b - coordinates_a
338330

339331
# Compute coefficient.
340332
coefficient = 0.
341-
for momenta_indices in itertools.product(range(grid_length),
342-
repeat=n_dimensions):
333+
for momenta_indices in grid.all_points_indices():
343334
momenta = momentum_vector(
344-
momenta_indices, grid_length, length_scale)
335+
momenta_indices, grid.length, grid.scale)
345336
if momenta.any():
346337
coefficient += (
347338
prefactor * numpy.cos(momenta.dot(differences)) /
348339
momenta.dot(momenta))
349340

350341
# Loop over spins and identify interacting orbitals.
351342
for spin_a in spins:
352-
orbital_a = orbital_id(grid_length, grid_indices_a, spin_a)
343+
orbital_a = orbital_id(grid.length, grid_indices_a, spin_a)
353344
for spin_b in spins:
354-
orbital_b = orbital_id(grid_length, grid_indices_b, spin_b)
345+
orbital_b = orbital_id(grid.length, grid_indices_b, spin_b)
355346

356347
# Add interaction term.
357348
if orbital_a != orbital_b:
@@ -383,10 +374,7 @@ def jellium_model(grid, spinless=False, momentum_space=True):
383374
hamiltonian += momentum_potential_operator(grid, spinless)
384375
else:
385376
hamiltonian = position_kinetic_operator(grid, spinless)
386-
hamiltonian += position_potential_operator(grid.dimensions,
387-
grid.length,
388-
grid.scale,
389-
spinless)
377+
hamiltonian += position_potential_operator(grid, spinless)
390378
return hamiltonian
391379

392380

src/fermilib/utils/_jellium_test.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ def test_potential_integration(self):
151151
grid = Grid(dimensions=2, length=3, scale = 2.)
152152
spinless = 1
153153
momentum_potential = momentum_potential_operator(grid, spinless)
154-
position_potential = position_potential_operator(
155-
grid.dimensions, grid.length, grid.scale, spinless)
154+
position_potential = position_potential_operator(grid, spinless)
156155

157156
# Diagonalize and confirm the same energy.
158157
jw_momentum = jordan_wigner(momentum_potential)
@@ -198,8 +197,7 @@ def test_coefficients(self):
198197
qubit_kinetic = jordan_wigner(kinetic)
199198

200199
# Potential operator.
201-
potential = position_potential_operator(
202-
grid.dimensions, grid.length, grid.scale, spinless)
200+
potential = position_potential_operator(grid, spinless)
203201
qubit_potential = jordan_wigner(potential)
204202

205203
# Check identity.

0 commit comments

Comments
 (0)