Skip to content

Commit 1e74868

Browse files
Strilancbabbush
authored andcommitted
Switch grid_indices to Grid (#51)
* 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 * Switch position_vector to Grid * Switch momentum_vector to Grid * Switch grid_indices to Grid
1 parent 9f0084a commit 1e74868

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/fermilib/utils/_jellium.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ def orbital_id(grid_length, grid_coordinates, spin=None):
6464
return tensor_factor
6565

6666

67-
def grid_indices(qubit_id, n_dimensions, grid_length, spinless):
67+
def grid_indices(qubit_id, grid, spinless):
6868
"""This function is the inverse of orbital_id.
6969
7070
Args:
7171
qubit_id: The tensor factor to map to grid indices.
72-
n_dimensions: An int giving the number of dimensions for the model.
73-
grid_length (int): The number of points in one dimension of the grid.
72+
grid (Grid): The discretization to use.
7473
spinless (bool): Whether to use the spinless model or not.
7574
7675
Returns:
@@ -85,9 +84,9 @@ def grid_indices(qubit_id, n_dimensions, grid_length, spinless):
8584

8685
# Get grid indices.
8786
grid_indices = []
88-
for dimension in range(n_dimensions):
89-
remainder = orbital_id % (grid_length ** (dimension + 1))
90-
grid_index = remainder // (grid_length ** dimension)
87+
for dimension in range(grid.dimensions):
88+
remainder = orbital_id % (grid.length ** (dimension + 1))
89+
grid_index = remainder // (grid.length ** dimension)
9190
grid_indices += [grid_index]
9291
return grid_indices
9392

@@ -414,10 +413,10 @@ def jordan_wigner_position_jellium(grid, spinless=False):
414413
# Add ZZ terms.
415414
prefactor = numpy.pi / volume
416415
for p in range(n_qubits):
417-
index_p = grid_indices(p, grid.dimensions, grid.length, spinless)
416+
index_p = grid_indices(p, grid, spinless)
418417
position_p = position_vector(index_p, grid)
419418
for q in range(p + 1, n_qubits):
420-
index_q = grid_indices(q, grid.dimensions, grid.length, spinless)
419+
index_q = grid_indices(q, grid, spinless)
421420
position_q = position_vector(index_q, grid)
422421

423422
differences = position_p - position_q
@@ -437,13 +436,13 @@ def jordan_wigner_position_jellium(grid, spinless=False):
437436
# Add XZX + YZY terms.
438437
prefactor = .25 / float(n_orbitals)
439438
for p in range(n_qubits):
440-
index_p = grid_indices(p, grid.dimensions, grid.length, spinless)
439+
index_p = grid_indices(p, grid, spinless)
441440
position_p = position_vector(index_p, grid)
442441
for q in range(p + 1, n_qubits):
443442
if not spinless and (p + q) % 2:
444443
continue
445444

446-
index_q = grid_indices(q, grid.dimensions, grid.length, spinless)
445+
index_q = grid_indices(q, grid, spinless)
447446
position_q = position_vector(index_q, grid)
448447

449448
differences = position_p - position_q

src/fermilib/utils/_plane_wave_hamiltonian.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
"""Construct Hamiltonians in plan wave basis and its dual in 3D."""
1414
from __future__ import absolute_import
1515

16-
import itertools
17-
1816
import numpy
1917

2018
from fermilib.config import *
@@ -220,12 +218,10 @@ def _fourier_transform_helper(hamiltonian, n_dimensions, grid_length,
220218
for term in hamiltonian.terms:
221219
transformed_term = None
222220
for ladder_operator in term:
223-
indices_1 = grid_indices(ladder_operator[0], n_dimensions,
224-
grid_length, spinless)
221+
indices_1 = grid_indices(ladder_operator[0], grid, spinless)
225222
vec_1 = vec_func_1(indices_1, grid)
226223
new_basis = None
227-
for indices_2 in itertools.product(range(grid_length),
228-
repeat=n_dimensions):
224+
for indices_2 in grid.all_points_indices():
229225
vec_2 = vec_func_2(indices_2, grid)
230226
if spinless:
231227
spin = None

0 commit comments

Comments
 (0)