From 54cd90b710764d060917e74673c466a8338c4320 Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Wed, 4 Dec 2024 22:18:29 -0500 Subject: [PATCH 1/3] run cargo clippy --- src/fermion_operator.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fermion_operator.rs b/src/fermion_operator.rs index f91e0ca87..edeeba891 100644 --- a/src/fermion_operator.rs +++ b/src/fermion_operator.rs @@ -30,10 +30,10 @@ impl KeysIterator { fn __next__(&mut self) -> Option { Python::with_gil(|py| { - return self + self .keys .next() - .map(|vec| PyTuple::new_bound(py, &vec).to_object(py)); + .map(|vec| PyTuple::new_bound(py, &vec).to_object(py)) }) } } From c90a8ae9eafec906d7ff77ccc49a5e3d0a63776a Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Wed, 4 Dec 2024 23:44:04 -0500 Subject: [PATCH 2/3] cargo fmt --- src/fermion_operator.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/fermion_operator.rs b/src/fermion_operator.rs index edeeba891..e4f47906d 100644 --- a/src/fermion_operator.rs +++ b/src/fermion_operator.rs @@ -30,8 +30,7 @@ impl KeysIterator { fn __next__(&mut self) -> Option { Python::with_gil(|py| { - self - .keys + self.keys .next() .map(|vec| PyTuple::new_bound(py, &vec).to_object(py)) }) From 79781d423d8f7c630d2c87b81b3fdef0fdb10100 Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Wed, 4 Dec 2024 23:58:21 -0500 Subject: [PATCH 3/3] move slater tests to correct file (#346) * cargo fmt * move slater tests to correct file --- tests/python/states/slater_test.py | 162 ++++++++++++++++++++++++++++ tests/python/states/states_test.py | 163 ----------------------------- 2 files changed, 162 insertions(+), 163 deletions(-) diff --git a/tests/python/states/slater_test.py b/tests/python/states/slater_test.py index f4c2814f9..39baaeb49 100644 --- a/tests/python/states/slater_test.py +++ b/tests/python/states/slater_test.py @@ -21,6 +21,168 @@ import ffsim +@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) +def test_hartree_fock_state_spinful(norb: int, nelec: tuple[int, int]): + """Test Hartree-Fock state.""" + vec = ffsim.hartree_fock_state(norb, nelec) + dim = ffsim.dim(norb, nelec) + assert vec.shape == (dim,) + assert vec[0] == 1 + assert all(vec[1:] == 0) + + +@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nocc(range(5))) +def test_hartree_fock_state_spinless(norb: int, nelec: int): + """Test Hartree-Fock state.""" + vec = ffsim.hartree_fock_state(norb, nelec) + dim = ffsim.dim(norb, nelec) + assert vec.shape == (dim,) + assert vec[0] == 1 + assert all(vec[1:] == 0) + + +@pytest.mark.parametrize("norb, nocc", ffsim.testing.generate_norb_nocc(range(5))) +def test_slater_determinant_spinless(norb: int, nocc: int): + """Test Slater determinant with same rotation for both spins.""" + rng = np.random.default_rng() + + occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nocc, seed=rng) + + one_body_tensor = ffsim.random.random_hermitian(norb, seed=rng) + eigs, orbital_rotation = np.linalg.eigh(one_body_tensor) + eig = sum(eigs[occupied_orbitals]) + state = ffsim.slater_determinant( + norb, occupied_orbitals, orbital_rotation=orbital_rotation + ) + + hamiltonian = ffsim.contract.one_body_linop( + one_body_tensor, norb=norb, nelec=(nocc, 0) + ) + np.testing.assert_allclose(hamiltonian @ state, eig * state) + + +@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) +def test_slater_determinant_same_rotation(norb: int, nelec: tuple[int, int]): + """Test Slater determinant with same rotation for both spins.""" + rng = np.random.default_rng() + + occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) + occ_a, occ_b = occupied_orbitals + + one_body_tensor = ffsim.random.random_hermitian(norb, seed=rng) + eigs, orbital_rotation = np.linalg.eigh(one_body_tensor) + eig = sum(eigs[occ_a]) + sum(eigs[occ_b]) + state = ffsim.slater_determinant( + norb, occupied_orbitals, orbital_rotation=orbital_rotation + ) + + hamiltonian = ffsim.contract.one_body_linop(one_body_tensor, norb=norb, nelec=nelec) + np.testing.assert_allclose(hamiltonian @ state, eig * state) + + +@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) +def test_slater_determinant_diff_rotation(norb: int, nelec: tuple[int, int]): + """Test Slater determinant with different rotations for each spin.""" + rng = np.random.default_rng() + + occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) + occ_a, occ_b = occupied_orbitals + + orbital_rotation_a = ffsim.random.random_unitary(norb, seed=rng) + orbital_rotation_b = ffsim.random.random_unitary(norb, seed=rng) + + state = ffsim.slater_determinant( + norb, + occupied_orbitals, + orbital_rotation=(orbital_rotation_a, orbital_rotation_b), + ) + state_a = ffsim.slater_determinant( + norb, + (occ_a, []), + orbital_rotation=orbital_rotation_a, + ) + state_b = ffsim.slater_determinant( + norb, + ([], occ_b), + orbital_rotation=orbital_rotation_b, + ) + + np.testing.assert_allclose(state, np.kron(state_a, state_b)) + + +@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) +def test_slater_determinant_rdm1s_same_rotation(norb: int, nelec: tuple[int, int]): + """Test Slater determinant 1-RDM.""" + rng = np.random.default_rng() + + occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) + orbital_rotation = ffsim.random.random_unitary(norb, seed=rng) + + vec = ffsim.slater_determinant( + norb, occupied_orbitals, orbital_rotation=orbital_rotation + ) + rdm = ffsim.slater_determinant_rdms( + norb, + occupied_orbitals, + orbital_rotation=orbital_rotation, + ) + expected = ffsim.rdms(vec, norb, nelec) + + np.testing.assert_allclose(rdm, expected, atol=1e-12) + + +@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) +def test_slater_determinant_rdm1s_diff_rotation(norb: int, nelec: tuple[int, int]): + """Test Slater determinant 1-RDM.""" + rng = np.random.default_rng() + + occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) + orbital_rotation_a = ffsim.random.random_unitary(norb, seed=rng) + orbital_rotation_b = ffsim.random.random_unitary(norb, seed=rng) + + vec = ffsim.slater_determinant( + norb, + occupied_orbitals, + orbital_rotation=(orbital_rotation_a, orbital_rotation_b), + ) + expected = ffsim.rdms(vec, norb, nelec) + + rdm = ffsim.slater_determinant_rdms( + norb, + occupied_orbitals, + orbital_rotation=(orbital_rotation_a, orbital_rotation_b), + ) + np.testing.assert_allclose(rdm, expected, atol=1e-12) + + rdm = ffsim.slater_determinant_rdms( + norb, + occupied_orbitals, + orbital_rotation=np.stack([orbital_rotation_a, orbital_rotation_b]), + ) + np.testing.assert_allclose(rdm, expected, atol=1e-12) + + +@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nocc(range(5))) +def test_slater_determinant_rdm1s_spinless(norb: int, nelec: int): + """Test Slater determinant 1-RDM, spinless.""" + rng = np.random.default_rng() + + occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) + orbital_rotation = ffsim.random.random_unitary(norb, seed=rng) + + vec = ffsim.slater_determinant( + norb, occupied_orbitals, orbital_rotation=orbital_rotation + ) + rdm = ffsim.slater_determinant_rdms( + norb, + occupied_orbitals, + orbital_rotation=orbital_rotation, + ) + expected = ffsim.rdms(vec, norb, (nelec, 0), spin_summed=True) + + np.testing.assert_allclose(rdm, expected, atol=1e-12) + + @pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nocc([4, 5])) def test_slater_determinant_amplitudes_spinless(norb: int, nelec: int): """Test computing Slater determinant amplitudes, spinless.""" diff --git a/tests/python/states/states_test.py b/tests/python/states/states_test.py index 9e89af3ef..a4e92ae72 100644 --- a/tests/python/states/states_test.py +++ b/tests/python/states/states_test.py @@ -13,173 +13,10 @@ from __future__ import annotations import numpy as np -import pytest import ffsim -@pytest.mark.parametrize("norb, nocc", ffsim.testing.generate_norb_nocc(range(5))) -def test_slater_determinant_spinless(norb: int, nocc: int): - """Test Slater determinant with same rotation for both spins.""" - rng = np.random.default_rng() - - occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nocc, seed=rng) - - one_body_tensor = ffsim.random.random_hermitian(norb, seed=rng) - eigs, orbital_rotation = np.linalg.eigh(one_body_tensor) - eig = sum(eigs[occupied_orbitals]) - state = ffsim.slater_determinant( - norb, occupied_orbitals, orbital_rotation=orbital_rotation - ) - - hamiltonian = ffsim.contract.one_body_linop( - one_body_tensor, norb=norb, nelec=(nocc, 0) - ) - np.testing.assert_allclose(hamiltonian @ state, eig * state) - - -@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) -def test_slater_determinant_same_rotation(norb: int, nelec: tuple[int, int]): - """Test Slater determinant with same rotation for both spins.""" - rng = np.random.default_rng() - - occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) - occ_a, occ_b = occupied_orbitals - - one_body_tensor = ffsim.random.random_hermitian(norb, seed=rng) - eigs, orbital_rotation = np.linalg.eigh(one_body_tensor) - eig = sum(eigs[occ_a]) + sum(eigs[occ_b]) - state = ffsim.slater_determinant( - norb, occupied_orbitals, orbital_rotation=orbital_rotation - ) - - hamiltonian = ffsim.contract.one_body_linop(one_body_tensor, norb=norb, nelec=nelec) - np.testing.assert_allclose(hamiltonian @ state, eig * state) - - -@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) -def test_slater_determinant_diff_rotation(norb: int, nelec: tuple[int, int]): - """Test Slater determinant with different rotations for each spin.""" - rng = np.random.default_rng() - - occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) - occ_a, occ_b = occupied_orbitals - - orbital_rotation_a = ffsim.random.random_unitary(norb, seed=rng) - orbital_rotation_b = ffsim.random.random_unitary(norb, seed=rng) - - state = ffsim.slater_determinant( - norb, - occupied_orbitals, - orbital_rotation=(orbital_rotation_a, orbital_rotation_b), - ) - state_a = ffsim.slater_determinant( - norb, - (occ_a, []), - orbital_rotation=orbital_rotation_a, - ) - state_b = ffsim.slater_determinant( - norb, - ([], occ_b), - orbital_rotation=orbital_rotation_b, - ) - - np.testing.assert_allclose(state, np.kron(state_a, state_b)) - - -@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) -def test_hartree_fock_state_spinful(norb: int, nelec: tuple[int, int]): - """Test Hartree-Fock state.""" - vec = ffsim.hartree_fock_state(norb, nelec) - dim = ffsim.dim(norb, nelec) - assert vec.shape == (dim,) - assert vec[0] == 1 - assert all(vec[1:] == 0) - - -@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nocc(range(5))) -def test_hartree_fock_state_spinless(norb: int, nelec: int): - """Test Hartree-Fock state.""" - vec = ffsim.hartree_fock_state(norb, nelec) - dim = ffsim.dim(norb, nelec) - assert vec.shape == (dim,) - assert vec[0] == 1 - assert all(vec[1:] == 0) - - -@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) -def test_slater_determinant_rdm1s_same_rotation(norb: int, nelec: tuple[int, int]): - """Test Slater determinant 1-RDM.""" - rng = np.random.default_rng() - - occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) - orbital_rotation = ffsim.random.random_unitary(norb, seed=rng) - - vec = ffsim.slater_determinant( - norb, occupied_orbitals, orbital_rotation=orbital_rotation - ) - rdm = ffsim.slater_determinant_rdms( - norb, - occupied_orbitals, - orbital_rotation=orbital_rotation, - ) - expected = ffsim.rdms(vec, norb, nelec) - - np.testing.assert_allclose(rdm, expected, atol=1e-12) - - -@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nelec(range(5))) -def test_slater_determinant_rdm1s_diff_rotation(norb: int, nelec: tuple[int, int]): - """Test Slater determinant 1-RDM.""" - rng = np.random.default_rng() - - occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) - orbital_rotation_a = ffsim.random.random_unitary(norb, seed=rng) - orbital_rotation_b = ffsim.random.random_unitary(norb, seed=rng) - - vec = ffsim.slater_determinant( - norb, - occupied_orbitals, - orbital_rotation=(orbital_rotation_a, orbital_rotation_b), - ) - expected = ffsim.rdms(vec, norb, nelec) - - rdm = ffsim.slater_determinant_rdms( - norb, - occupied_orbitals, - orbital_rotation=(orbital_rotation_a, orbital_rotation_b), - ) - np.testing.assert_allclose(rdm, expected, atol=1e-12) - - rdm = ffsim.slater_determinant_rdms( - norb, - occupied_orbitals, - orbital_rotation=np.stack([orbital_rotation_a, orbital_rotation_b]), - ) - np.testing.assert_allclose(rdm, expected, atol=1e-12) - - -@pytest.mark.parametrize("norb, nelec", ffsim.testing.generate_norb_nocc(range(5))) -def test_slater_determinant_rdm1s_spinless(norb: int, nelec: int): - """Test Slater determinant 1-RDM, spinless.""" - rng = np.random.default_rng() - - occupied_orbitals = ffsim.testing.random_occupied_orbitals(norb, nelec, seed=rng) - orbital_rotation = ffsim.random.random_unitary(norb, seed=rng) - - vec = ffsim.slater_determinant( - norb, occupied_orbitals, orbital_rotation=orbital_rotation - ) - rdm = ffsim.slater_determinant_rdms( - norb, - occupied_orbitals, - orbital_rotation=orbital_rotation, - ) - expected = ffsim.rdms(vec, norb, (nelec, 0), spin_summed=True) - - np.testing.assert_allclose(rdm, expected, atol=1e-12) - - def test_sample_state_vector_spinful_string(): """Test sampling state vector, spinful, output type string.""" norb = 5