Skip to content

Commit 3eee403

Browse files
Strilancbabbush
authored andcommitted
Switch fourier_transform to Grid (#52)
1 parent 1e74868 commit 3eee403

File tree

3 files changed

+16
-34
lines changed

3 files changed

+16
-34
lines changed

examples/fermilib_demo.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@
625625
"print(momentum_qubit_operator)\n",
626626
"\n",
627627
"# Fourier transform the Hamiltonian to the position basis.\n",
628-
"position_hamiltonian = fourier_transform(momentum_hamiltonian, grid.dimensions, grid.length, grid.scale, spinless)\n",
628+
"position_hamiltonian = fourier_transform(momentum_hamiltonian, grid, spinless)\n",
629629
"position_qubit_operator = jordan_wigner(position_hamiltonian)\n",
630630
"position_qubit_operator.compress()\n",
631631
"print('')\n",

src/fermilib/utils/_plane_wave_hamiltonian.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ def plane_wave_hamiltonian(grid, geometry,
149149
return jellium_op + external_potential
150150

151151

152-
def fourier_transform(hamiltonian, n_dimensions, grid_length, length_scale,
153-
spinless):
154-
"""Apply Fourier tranform to change hamiltonian in plane wave basis.
152+
def fourier_transform(hamiltonian, grid, spinless):
153+
"""Apply Fourier transform to change hamiltonian in plane wave basis.
155154
156155
.. math::
157156
@@ -160,27 +159,22 @@ def fourier_transform(hamiltonian, n_dimensions, grid_length, length_scale,
160159
161160
Args:
162161
hamiltonian: The hamiltonian in plane wave basis.
163-
n_dimensions: An int giving the number of dimensions for the model.
164-
grid_length: Int, the number of points in one dimension of the grid.
165-
length_scale: Float, the real space length of a box dimension.
162+
grid: The discretization to use.
166163
spinless: Bool, whether to use the spinless model or not.
167164
168165
Returns:
169166
hamiltonian_t: An instance of the FermionOperator class.
170167
"""
171168
return _fourier_transform_helper(hamiltonian=hamiltonian,
172-
n_dimensions=n_dimensions,
173-
grid_length=grid_length,
174-
length_scale=length_scale,
169+
grid=grid,
175170
spinless=spinless,
176171
factor=+1,
177172
vec_func_1=momentum_vector,
178173
vec_func_2=position_vector)
179174

180175

181-
def inverse_fourier_transform(hamiltonian, n_dimensions, grid_length,
182-
length_scale, spinless):
183-
"""Apply Fourier tranform to change hamiltonian in plane wave dual basis.
176+
def inverse_fourier_transform(hamiltonian, grid, spinless):
177+
"""Apply Fourier transform to change hamiltonian in plane wave dual basis.
184178
185179
.. math::
186180
@@ -189,31 +183,23 @@ def inverse_fourier_transform(hamiltonian, n_dimensions, grid_length,
189183
190184
Args:
191185
hamiltonian: The hamiltonian in plane wave dual basis.
192-
n_dimensions: An int giving the number of dimensions for the model.
193-
grid_length: Int, the number of points in one dimension of the grid.
194-
length_scale: Float, the real space length of a box dimension.
186+
grid: The discretization to use.
195187
spinless: Bool, whether to use the spinless model or not.
196188
197189
Returns:
198190
hamiltonian_t: An instance of the FermionOperator class.
199191
"""
200192
return _fourier_transform_helper(hamiltonian=hamiltonian,
201-
n_dimensions=n_dimensions,
202-
grid_length=grid_length,
203-
length_scale=length_scale,
193+
grid=grid,
204194
spinless=spinless,
205195
factor=-1,
206196
vec_func_1=position_vector,
207197
vec_func_2=momentum_vector)
208198

209199

210-
def _fourier_transform_helper(hamiltonian, n_dimensions, grid_length,
211-
length_scale, spinless, factor,
212-
vec_func_1, vec_func_2):
200+
def _fourier_transform_helper(hamiltonian, grid, spinless,
201+
factor, vec_func_1, vec_func_2):
213202
hamiltonian_t = None
214-
grid = Grid(dimensions=n_dimensions,
215-
length=grid_length,
216-
scale=length_scale)
217203

218204
for term in hamiltonian.terms:
219205
transformed_term = None
@@ -227,7 +213,7 @@ def _fourier_transform_helper(hamiltonian, n_dimensions, grid_length,
227213
spin = None
228214
else:
229215
spin = ladder_operator[0] % 2
230-
orbital = orbital_id(grid_length, indices_2, spin)
216+
orbital = orbital_id(grid.length, indices_2, spin)
231217
exp_index = factor * 1.0j * numpy.dot(vec_1, vec_2)
232218
if ladder_operator[1] == 1:
233219
exp_index *= -1.0
@@ -239,7 +225,7 @@ def _fourier_transform_helper(hamiltonian, n_dimensions, grid_length,
239225
else:
240226
new_basis += element
241227

242-
new_basis *= numpy.sqrt(1.0/float(grid_length**n_dimensions))
228+
new_basis *= numpy.sqrt(1.0/float(grid.num_points()))
243229

244230
if transformed_term is None:
245231
transformed_term = new_basis

src/fermilib/utils/_plane_wave_hamiltonian_test.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def test_fourier_transform(self):
4040
grid, geometry, spinless, True)
4141
h_dual_basis = plane_wave_hamiltonian(
4242
grid, geometry, spinless, False)
43-
h_plane_wave_t = fourier_transform(
44-
h_plane_wave, grid.dimensions, grid.length,
45-
grid.scale, spinless)
43+
h_plane_wave_t = fourier_transform(h_plane_wave, grid, spinless)
4644
self.assertTrue(normal_ordered(h_plane_wave_t).isclose(
4745
normal_ordered(h_dual_basis)))
4846

@@ -56,8 +54,7 @@ def test_inverse_fourier_transform_1d(self):
5654
h_dual_basis = plane_wave_hamiltonian(
5755
grid, geometry, spinless, False)
5856
h_dual_basis_t = inverse_fourier_transform(
59-
h_dual_basis, grid.dimensions, grid.length,
60-
grid.scale, spinless)
57+
h_dual_basis, grid, spinless)
6158
self.assertTrue(normal_ordered(h_dual_basis_t).isclose(
6259
normal_ordered(h_plane_wave)))
6360

@@ -68,8 +65,7 @@ def test_inverse_fourier_transform_2d(self):
6865
h_plane_wave = plane_wave_hamiltonian(grid, geometry, spinless, True)
6966
h_dual_basis = plane_wave_hamiltonian(grid, geometry, spinless, False)
7067
h_dual_basis_t = inverse_fourier_transform(
71-
h_dual_basis, grid.dimensions, grid.length, grid.scale,
72-
spinless)
68+
h_dual_basis, grid, spinless)
7369
self.assertTrue(normal_ordered(h_dual_basis_t).isclose(
7470
normal_ordered(h_plane_wave)))
7571

0 commit comments

Comments
 (0)