Skip to content

Commit

Permalink
add random molecular Hamiltonian to all gate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bartandrews committed Nov 22, 2024
1 parent 697bbc7 commit 1f7c322
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 22 deletions.
34 changes: 20 additions & 14 deletions docs/how-to-guides/lucj_mps.ipynb

Large diffs are not rendered by default.

57 changes: 51 additions & 6 deletions tests/python/tenpy/gates/basic_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
num_num_interaction,
on_site_interaction,
)
from ffsim.tenpy.hamiltonians.molecular_hamiltonian import MolecularHamiltonianMPOModel
from ffsim.tenpy.util import bitstring_to_mps


Expand Down Expand Up @@ -64,6 +65,16 @@ def test_givens_rotation(norb: int, nelec: tuple[int, int], spin: Spin):
mps = bitstring_to_mps((int(strings_a[0], 2), int(strings_b[0], 2)), norb)
original_mps = deepcopy(mps)

# generate a random molecular Hamiltonian
mol_hamiltonian = ffsim.random.random_molecular_hamiltonian(norb, seed=rng)
hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb, nelec)

# convert molecular Hamiltonian to MPO
mol_hamiltonian_mpo_model = MolecularHamiltonianMPOModel.from_molecular_hamiltonian(
mol_hamiltonian
)
mol_hamiltonian_mpo = mol_hamiltonian_mpo_model.H_MPO

# generate random Givens rotation parameters
theta = 2 * np.pi * rng.random()
phi = 2 * np.pi * rng.random()
Expand All @@ -76,11 +87,12 @@ def test_givens_rotation(norb: int, nelec: tuple[int, int], spin: Spin):

# apply random orbital rotation to MPS
eng = TEBDEngine(mps, None, {})
ffsim.tenpy.apply_two_site(eng, givens_rotation(theta, spin, phi=phi), (p, p + 1))
ffsim.tenpy.apply_two_site(eng, givens_rotation(theta, spin, phi=phi), (p + 1, p))

# test expectation is preserved
original_expectation = np.vdot(original_vec, vec)
mpo_expectation = original_mps.overlap(mps)
original_expectation = np.vdot(original_vec, hamiltonian @ vec)
mol_hamiltonian_mpo.apply_naively(mps)
mpo_expectation = mps.overlap(original_mps)
np.testing.assert_allclose(original_expectation, mpo_expectation)


Expand Down Expand Up @@ -121,6 +133,16 @@ def test_num_interaction(norb: int, nelec: tuple[int, int], spin: Spin):
mps = bitstring_to_mps((int(strings_a[0], 2), int(strings_b[0], 2)), norb)
original_mps = deepcopy(mps)

# generate a random molecular Hamiltonian
mol_hamiltonian = ffsim.random.random_molecular_hamiltonian(norb, seed=rng)
hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb, nelec)

# convert molecular Hamiltonian to MPO
mol_hamiltonian_mpo_model = MolecularHamiltonianMPOModel.from_molecular_hamiltonian(
mol_hamiltonian
)
mol_hamiltonian_mpo = mol_hamiltonian_mpo_model.H_MPO

# generate random number interaction parameters
theta = 2 * np.pi * rng.random()
p = rng.integers(0, norb)
Expand All @@ -133,7 +155,8 @@ def test_num_interaction(norb: int, nelec: tuple[int, int], spin: Spin):
ffsim.tenpy.apply_single_site(eng, num_interaction(theta, spin), p)

# test expectation is preserved
original_expectation = np.vdot(original_vec, vec)
original_expectation = np.vdot(original_vec, hamiltonian @ vec)
mol_hamiltonian_mpo.apply_naively(mps)
mpo_expectation = original_mps.overlap(mps)
np.testing.assert_allclose(original_expectation, mpo_expectation)

Expand Down Expand Up @@ -170,6 +193,16 @@ def test_on_site_interaction(
mps = bitstring_to_mps((int(strings_a[0], 2), int(strings_b[0], 2)), norb)
original_mps = deepcopy(mps)

# generate a random molecular Hamiltonian
mol_hamiltonian = ffsim.random.random_molecular_hamiltonian(norb, seed=rng)
hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb, nelec)

# convert molecular Hamiltonian to MPO
mol_hamiltonian_mpo_model = MolecularHamiltonianMPOModel.from_molecular_hamiltonian(
mol_hamiltonian
)
mol_hamiltonian_mpo = mol_hamiltonian_mpo_model.H_MPO

# generate random on-site interaction parameters
theta = 2 * np.pi * rng.random()
p = rng.integers(0, norb)
Expand All @@ -182,7 +215,8 @@ def test_on_site_interaction(
ffsim.tenpy.apply_single_site(eng, on_site_interaction(theta), p)

# test expectation is preserved
original_expectation = np.vdot(original_vec, vec)
original_expectation = np.vdot(original_vec, hamiltonian @ vec)
mol_hamiltonian_mpo.apply_naively(mps)
mpo_expectation = original_mps.overlap(mps)
np.testing.assert_allclose(original_expectation, mpo_expectation)

Expand Down Expand Up @@ -224,6 +258,16 @@ def test_num_num_interaction(norb: int, nelec: tuple[int, int], spin: Spin):
mps = bitstring_to_mps((int(strings_a[0], 2), int(strings_b[0], 2)), norb)
original_mps = deepcopy(mps)

# generate a random molecular Hamiltonian
mol_hamiltonian = ffsim.random.random_molecular_hamiltonian(norb, seed=rng)
hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb, nelec)

# convert molecular Hamiltonian to MPO
mol_hamiltonian_mpo_model = MolecularHamiltonianMPOModel.from_molecular_hamiltonian(
mol_hamiltonian
)
mol_hamiltonian_mpo = mol_hamiltonian_mpo_model.H_MPO

# generate random number-number interaction parameters
theta = 2 * np.pi * rng.random()
p = rng.integers(0, norb - 1)
Expand All @@ -238,6 +282,7 @@ def test_num_num_interaction(norb: int, nelec: tuple[int, int], spin: Spin):
ffsim.tenpy.apply_two_site(eng, num_num_interaction(theta, spin), (p, p + 1))

# test expectation is preserved
original_expectation = np.vdot(original_vec, vec)
original_expectation = np.vdot(original_vec, hamiltonian @ vec)
mol_hamiltonian_mpo.apply_naively(mps)
mpo_expectation = original_mps.overlap(mps)
np.testing.assert_allclose(original_expectation, mpo_expectation)
14 changes: 13 additions & 1 deletion tests/python/tenpy/gates/diag_coulomb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from tenpy.algorithms.tebd import TEBDEngine

import ffsim
from ffsim.tenpy.hamiltonians.molecular_hamiltonian import MolecularHamiltonianMPOModel
from ffsim.tenpy.util import bitstring_to_mps


Expand Down Expand Up @@ -49,6 +50,16 @@ def test_apply_diag_coulomb_evolution(norb: int, nelec: tuple[int, int]):
mps = bitstring_to_mps((int(strings_a[0], 2), int(strings_b[0], 2)), norb)
original_mps = deepcopy(mps)

# generate a random molecular Hamiltonian
mol_hamiltonian = ffsim.random.random_molecular_hamiltonian(norb, seed=rng)
hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb, nelec)

# convert molecular Hamiltonian to MPO
mol_hamiltonian_mpo_model = MolecularHamiltonianMPOModel.from_molecular_hamiltonian(
mol_hamiltonian
)
mol_hamiltonian_mpo = mol_hamiltonian_mpo_model.H_MPO

# generate random diagonal Coulomb evolution parameters
mat_aa = np.diag(rng.standard_normal(norb - 1), k=-1)
mat_aa += mat_aa.T
Expand All @@ -66,6 +77,7 @@ def test_apply_diag_coulomb_evolution(norb: int, nelec: tuple[int, int]):
ffsim.tenpy.apply_diag_coulomb_evolution(eng, diag_coulomb_mats[:2], time)

# test expectation is preserved
original_expectation = np.vdot(original_vec, vec)
original_expectation = np.vdot(original_vec, hamiltonian @ vec)
mol_hamiltonian_mpo.apply_naively(mps)
mpo_expectation = original_mps.overlap(mps)
np.testing.assert_allclose(original_expectation, mpo_expectation)
14 changes: 13 additions & 1 deletion tests/python/tenpy/gates/orbital_rotation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from tenpy.algorithms.tebd import TEBDEngine

import ffsim
from ffsim.tenpy.hamiltonians.molecular_hamiltonian import MolecularHamiltonianMPOModel
from ffsim.tenpy.util import bitstring_to_mps


Expand Down Expand Up @@ -52,6 +53,16 @@ def test_apply_orbital_rotation(
mps = bitstring_to_mps((int(strings_a[0], 2), int(strings_b[0], 2)), norb)
original_mps = deepcopy(mps)

# generate a random molecular Hamiltonian
mol_hamiltonian = ffsim.random.random_molecular_hamiltonian(norb, seed=rng)
hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb, nelec)

# convert molecular Hamiltonian to MPO
mol_hamiltonian_mpo_model = MolecularHamiltonianMPOModel.from_molecular_hamiltonian(
mol_hamiltonian
)
mol_hamiltonian_mpo = mol_hamiltonian_mpo_model.H_MPO

# generate a random orbital rotation
mat = ffsim.random.random_unitary(norb, seed=rng)

Expand All @@ -63,6 +74,7 @@ def test_apply_orbital_rotation(
ffsim.tenpy.apply_orbital_rotation(eng, mat)

# test expectation is preserved
original_expectation = np.vdot(original_vec, vec)
original_expectation = np.vdot(original_vec, hamiltonian @ vec)
mol_hamiltonian_mpo.apply_naively(mps)
mpo_expectation = mps.overlap(original_mps)
np.testing.assert_allclose(original_expectation, mpo_expectation)

0 comments on commit 1f7c322

Please sign in to comment.