1313"""This module constructs Hamiltonians for the uniform electron gas."""
1414from __future__ import absolute_import
1515
16- import itertools
1716import numpy
17+ from projectq .ops import QubitOperator
1818
1919from fermilib .ops import FermionOperator
2020
21- from projectq .ops import QubitOperator
22-
2321
2422# Exceptions.
2523class 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
0 commit comments