Skip to content

Commit fecf595

Browse files
Fix slice index bug in orbital_rotation.rs (#513)
* Fix slice index panic in * Add test for spinful orbital rotation bug
1 parent 7cc85ca commit fecf595

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/gates/orbital_rotation.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ pub fn apply_givens_rotation_in_place(
7575
std::thread::scope(|scope| {
7676
for k in 0..n_threads {
7777
let start = k * chunk_size;
78+
if start >= n_pairs {
79+
break;
80+
}
7881
let end = (start + chunk_size).min(n_pairs);
7982
let slice1_chunk = &slice1_slice[start..end];
8083
let slice2_chunk = &slice2_slice[start..end];

tests/python/gates/orbital_rotation_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,18 @@ def test_apply_orbital_rotation_no_side_effects_special_case():
238238
assert ffsim.linalg.is_unitary(original_mat)
239239
assert ffsim.linalg.is_unitary(mat)
240240
np.testing.assert_allclose(mat, original_mat, atol=1e-12)
241+
242+
243+
def test_apply_orbital_rotation_large_spinful_regression():
244+
"""Regression test for slice index panic in orbital rotation."""
245+
norb = 12
246+
nelec = 6, 6
247+
dim = ffsim.dim(norb, nelec)
248+
249+
rng = np.random.default_rng(123)
250+
vec = ffsim.random.random_state_vector(dim, seed=rng)
251+
mat_a = ffsim.random.random_unitary(norb, seed=rng)
252+
mat_b = ffsim.random.random_unitary(norb, seed=rng)
253+
254+
result = ffsim.apply_orbital_rotation(vec, (mat_a, mat_b), norb, nelec)
255+
np.testing.assert_allclose(np.linalg.norm(result), 1.0)

0 commit comments

Comments
 (0)