@@ -92,42 +92,37 @@ def grid_indices(qubit_id, n_dimensions, grid_length, spinless):
9292 return grid_indices
9393
9494
95- def position_vector (position_indices , grid_length , length_scale ):
95+ def position_vector (position_indices , grid ):
9696 """Given grid point coordinate, return position vector with dimensions.
9797
9898 Args:
9999 position_indices: List or tuple of integers giving grid point
100100 coordinate. Allowed values are ints in [0, grid_length).
101- grid_length (int): The number of points in one dimension of the grid.
102- length_scale (float): The real space length of a box dimension.
101+ grid (Grid): The discretization to use.
103102
104103 Returns:
105- position_vector: A numpy array giving the position vector with
106- dimensions.
104+ position_vector (numpy.ndarray[float])
107105 """
108106 # Raise exceptions.
109107 if isinstance (position_indices , int ):
110108 position_indices = [position_indices ]
111- if (not isinstance (grid_length , int ) or
112- max (position_indices ) >= grid_length or
113- min (position_indices ) < 0. ):
109+ if not all (0 <= e < grid .length for e in position_indices ):
114110 raise OrbitalSpecificationError (
115111 'Position indices must be integers in [0, grid_length).' )
116112
117113 # Compute position vector.
118- adjusted_vector = numpy .array (position_indices , float ) - grid_length // 2
119- position_vector = length_scale * adjusted_vector / float (grid_length )
114+ adjusted_vector = numpy .array (position_indices , float ) - grid . length // 2
115+ position_vector = grid . scale * adjusted_vector / float (grid . length )
120116 return position_vector
121117
122118
123- def momentum_vector (momentum_indices , grid_length , length_scale ):
119+ def momentum_vector (momentum_indices , grid ):
124120 """Given grid point coordinate, return momentum vector with dimensions.
125121
126122 Args:
127123 momentum_indices: List or tuple of integers giving momentum indices.
128124 Allowed values are ints in [0, grid_length).
129- grid_length: Int, the number of points in one dimension of the grid.
130- length_scale: Float, the real space length of a box dimension.
125+ grid (Grid): The discretization to use.
131126
132127 Returns:
133128 momentum_vector: A numpy array giving the momentum vector with
@@ -136,15 +131,13 @@ def momentum_vector(momentum_indices, grid_length, length_scale):
136131 # Raise exceptions.
137132 if isinstance (momentum_indices , int ):
138133 momentum_indices = [momentum_indices ]
139- if (not isinstance (grid_length , int ) or
140- max (momentum_indices ) >= grid_length or
141- min (momentum_indices ) < 0. ):
134+ if not all (0 <= e < grid .length for e in momentum_indices ):
142135 raise OrbitalSpecificationError (
143136 'Momentum indices must be integers in [0, grid_length).' )
144137
145138 # Compute momentum vector.
146- adjusted_vector = numpy .array (momentum_indices , float ) - grid_length // 2
147- momentum_vector = 2. * numpy .pi * adjusted_vector / length_scale
139+ adjusted_vector = numpy .array (momentum_indices , float ) - grid . length // 2
140+ momentum_vector = 2. * numpy .pi * adjusted_vector / grid . scale
148141 return momentum_vector
149142
150143
@@ -167,7 +160,7 @@ def momentum_kinetic_operator(grid, spinless=False):
167160
168161 # Loop once through all plane waves.
169162 for momenta_indices in grid .all_points_indices ():
170- momenta = momentum_vector (momenta_indices , grid . length , grid . scale )
163+ momenta = momentum_vector (momenta_indices , grid )
171164 coefficient = momenta .dot (momenta ) / 2.
172165
173166 # Loop over spins.
@@ -206,8 +199,7 @@ def momentum_potential_operator(grid, spinless=False):
206199 index in omega_indices ]
207200
208201 # Get the momenta vectors.
209- omega_momenta = momentum_vector (
210- omega_indices , grid .length , grid .scale )
202+ omega_momenta = momentum_vector (omega_indices , grid )
211203
212204 # Skip if omega momentum is zero.
213205 if not omega_momenta .any ():
@@ -270,18 +262,15 @@ def position_kinetic_operator(grid, spinless=False):
270262
271263 # Loop once through all lattice sites.
272264 for grid_indices_a in grid .all_points_indices ():
273- coordinates_a = position_vector (
274- grid_indices_a , grid .length , grid .scale )
265+ coordinates_a = position_vector (grid_indices_a , grid )
275266 for grid_indices_b in grid .all_points_indices ():
276- coordinates_b = position_vector (
277- grid_indices_b , grid .length , grid .scale )
267+ coordinates_b = position_vector (grid_indices_b , grid )
278268 differences = coordinates_b - coordinates_a
279269
280270 # Compute coefficient.
281271 coefficient = 0.
282272 for momenta_indices in grid .all_points_indices ():
283- momenta = momentum_vector (
284- momenta_indices , grid .length , grid .scale )
273+ momenta = momentum_vector (momenta_indices , grid )
285274 if momenta .any ():
286275 coefficient += (
287276 numpy .cos (momenta .dot (differences )) *
@@ -321,18 +310,15 @@ def position_potential_operator(grid, spinless=False):
321310
322311 # Loop once through all lattice sites.
323312 for grid_indices_a in grid .all_points_indices ():
324- coordinates_a = position_vector (
325- grid_indices_a , grid .length , grid .scale )
313+ coordinates_a = position_vector (grid_indices_a , grid )
326314 for grid_indices_b in grid .all_points_indices ():
327- coordinates_b = position_vector (
328- grid_indices_b , grid .length , grid .scale )
315+ coordinates_b = position_vector (grid_indices_b , grid )
329316 differences = coordinates_b - coordinates_a
330317
331318 # Compute coefficient.
332319 coefficient = 0.
333320 for momenta_indices in grid .all_points_indices ():
334- momenta = momentum_vector (
335- momenta_indices , grid .length , grid .scale )
321+ momenta = momentum_vector (momenta_indices , grid )
336322 if momenta .any ():
337323 coefficient += (
338324 prefactor * numpy .cos (momenta .dot (differences )) /
@@ -400,7 +386,7 @@ def jordan_wigner_position_jellium(grid, spinless=False):
400386 # Compute the identity coefficient.
401387 identity_coefficient = 0.
402388 for k_indices in grid .all_points_indices ():
403- momenta = momentum_vector (k_indices , grid . length , grid . scale )
389+ momenta = momentum_vector (k_indices , grid )
404390 if momenta .any ():
405391 identity_coefficient += momenta .dot (momenta ) / 2.
406392 identity_coefficient -= (numpy .pi * float (n_orbitals ) /
@@ -415,7 +401,7 @@ def jordan_wigner_position_jellium(grid, spinless=False):
415401 # Compute coefficient of local Z terms.
416402 z_coefficient = 0.
417403 for k_indices in grid .all_points_indices ():
418- momenta = momentum_vector (k_indices , grid . length , grid . scale )
404+ momenta = momentum_vector (k_indices , grid )
419405 if momenta .any ():
420406 z_coefficient += numpy .pi / (momenta .dot (momenta ) * volume )
421407 z_coefficient -= momenta .dot (momenta ) / (4. * float (n_orbitals ))
@@ -429,17 +415,17 @@ def jordan_wigner_position_jellium(grid, spinless=False):
429415 prefactor = numpy .pi / volume
430416 for p in range (n_qubits ):
431417 index_p = grid_indices (p , grid .dimensions , grid .length , spinless )
432- position_p = position_vector (index_p , grid . length , grid . scale )
418+ position_p = position_vector (index_p , grid )
433419 for q in range (p + 1 , n_qubits ):
434420 index_q = grid_indices (q , grid .dimensions , grid .length , spinless )
435- position_q = position_vector (index_q , grid . length , grid . scale )
421+ position_q = position_vector (index_q , grid )
436422
437423 differences = position_p - position_q
438424
439425 # Loop through momenta.
440426 zpzq_coefficient = 0.
441427 for k_indices in grid .all_points_indices ():
442- momenta = momentum_vector (k_indices , grid . length , grid . scale )
428+ momenta = momentum_vector (k_indices , grid )
443429 if momenta .any ():
444430 zpzq_coefficient += prefactor * numpy .cos (
445431 momenta .dot (differences )) / momenta .dot (momenta )
@@ -452,20 +438,20 @@ def jordan_wigner_position_jellium(grid, spinless=False):
452438 prefactor = .25 / float (n_orbitals )
453439 for p in range (n_qubits ):
454440 index_p = grid_indices (p , grid .dimensions , grid .length , spinless )
455- position_p = position_vector (index_p , grid . length , grid . scale )
441+ position_p = position_vector (index_p , grid )
456442 for q in range (p + 1 , n_qubits ):
457443 if not spinless and (p + q ) % 2 :
458444 continue
459445
460446 index_q = grid_indices (q , grid .dimensions , grid .length , spinless )
461- position_q = position_vector (index_q , grid . length , grid . scale )
447+ position_q = position_vector (index_q , grid )
462448
463449 differences = position_p - position_q
464450
465451 # Loop through momenta.
466452 term_coefficient = 0.
467453 for k_indices in grid .all_points_indices ():
468- momenta = momentum_vector (k_indices , grid . length , grid . scale )
454+ momenta = momentum_vector (k_indices , grid )
469455 if momenta .any ():
470456 term_coefficient += prefactor * momenta .dot (momenta ) * \
471457 numpy .cos (momenta .dot (differences ))
0 commit comments