1212
1313from __future__ import absolute_import
1414
15- import itertools
1615import unittest
1716
1817import 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