Skip to content

Commit

Permalink
add orbital_rotation argument to slater determinant one rdm (#66)
Browse files Browse the repository at this point in the history
* add orbital_rotation argument to slater determinant one rdm

* fix tests
  • Loading branch information
kevinsung authored Oct 30, 2023
1 parent c15f3c5 commit 27541a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions python/ffsim/states/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def hartree_fock_state(norb: int, nelec: tuple[int, int]) -> np.ndarray:
def slater_determinant_one_rdm(
norb: int,
occupied_orbitals: tuple[Sequence[int], Sequence[int]],
orbital_rotation: np.ndarray | None = None,
spin_summed: bool = True,
) -> np.ndarray:
"""Return the one-particle reduced density matrix of a Slater determinant.
Expand All @@ -155,6 +156,9 @@ def slater_determinant_one_rdm(
rdm_a[(alpha_orbitals, alpha_orbitals)] = 1
if len(beta_orbitals):
rdm_b[(beta_orbitals, beta_orbitals)] = 1
if orbital_rotation is not None:
rdm_a = orbital_rotation.conj() @ rdm_a @ orbital_rotation.T
rdm_b = orbital_rotation.conj() @ rdm_b @ orbital_rotation.T
if spin_summed:
return rdm_a + rdm_b
return scipy.linalg.block_diag(rdm_a, rdm_b)
Expand Down
14 changes: 11 additions & 3 deletions tests/states/states_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,21 @@ def test_slater_determinant_one_rdm(
occ_a, occ_b = occupied_orbitals
nelec = len(occ_a), len(occ_b)

vec = ffsim.slater_determinant(norb, occupied_orbitals)
rng = np.random.default_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_one_rdm(
norb, occupied_orbitals, spin_summed=spin_summed
norb,
occupied_orbitals,
orbital_rotation=orbital_rotation,
spin_summed=spin_summed,
)
expected = ffsim.rdm(vec, norb, nelec, spin_summed=spin_summed)

np.testing.assert_allclose(rdm, expected)
np.testing.assert_allclose(rdm, expected, atol=1e-12)


def test_indices_to_strings():
Expand Down
8 changes: 6 additions & 2 deletions tests/trotter/qdrift_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ def test_simulate_qdrift_double_factorized_h_chain(
occupied_orbitals = (range(n_alpha), range(n_beta))
initial_state = ffsim.slater_determinant(norb, occupied_orbitals)
original_state = initial_state.copy()
one_rdm = ffsim.slater_determinant_one_rdm(norb, occupied_orbitals)
one_rdm = ffsim.slater_determinant_one_rdm(
norb, occupied_orbitals, spin_summed=False
)

# compute exact state
exact_state = scipy.sparse.linalg.expm_multiply(
Expand Down Expand Up @@ -372,7 +374,9 @@ def test_simulate_qdrift_double_factorized_random(
occupied_orbitals = (range(n_alpha), range(n_beta))
initial_state = ffsim.slater_determinant(norb, occupied_orbitals)
original_state = initial_state.copy()
one_rdm = ffsim.slater_determinant_one_rdm(norb, occupied_orbitals)
one_rdm = ffsim.slater_determinant_one_rdm(
norb, occupied_orbitals, spin_summed=False
)

# compute exact state
exact_state = scipy.sparse.linalg.expm_multiply(
Expand Down

0 comments on commit 27541a8

Please sign in to comment.