Skip to content

Commit

Permalink
use np.ascontiguousarray and remove unnecessary copy (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsung authored Jun 23, 2024
1 parent 5d9a1a3 commit eda1c6e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions python/ffsim/gates/orbital_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _apply_orbital_rotation_spinless(
vec: np.ndarray, mat: np.ndarray, norb: int, nelec: int
):
givens_rotations, phase_shifts = givens_decomposition(mat)
vec = vec.reshape((-1, 1))
vec = np.ascontiguousarray(vec.reshape((-1, 1)))
for c, s, i, j in givens_rotations:
_apply_orbital_rotation_adjacent_spin_in_place(
vec, c, s.conjugate(), (i, j), norb, nelec
Expand All @@ -124,7 +124,7 @@ def _apply_orbital_rotation_spinful(
n_alpha, n_beta = nelec
dim_a = math.comb(norb, n_alpha)
dim_b = math.comb(norb, n_beta)
vec = vec.reshape((dim_a, dim_b))
vec = np.ascontiguousarray(vec.reshape((dim_a, dim_b)))

if givens_decomp_a is not None:
# transform alpha
Expand All @@ -140,7 +140,7 @@ def _apply_orbital_rotation_spinful(
if givens_decomp_b is not None:
# transform beta
# copy transposed vector to align memory layout
vec = vec.T.copy()
vec = np.ascontiguousarray(vec.T)
givens_rotations, phase_shifts = givens_decomp_b
for c, s, i, j in givens_rotations:
_apply_orbital_rotation_adjacent_spin_in_place(
Expand All @@ -149,7 +149,7 @@ def _apply_orbital_rotation_spinful(
for i, phase_shift in enumerate(phase_shifts):
indices = _one_subspace_indices(norb, n_beta, (i,))
apply_phase_shift_in_place(vec, phase_shift, indices)
vec = vec.T.copy()
vec = vec.T

return vec.reshape(-1)

Expand Down

0 comments on commit eda1c6e

Please sign in to comment.