Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test complex one-body tensor for DF Hamiltonian #22

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions tests/hamiltonians/double_factorized_hamiltonian_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def test_double_factorized_hamiltonian(z_representation: bool):

# generate random Hamiltonian
dim = ffsim.dim(norb, nelec)
# TODO test with complex one-body tensor
one_body_tensor = np.real(ffsim.random.random_hermitian(norb, seed=2474))
one_body_tensor = ffsim.random.random_hermitian(norb, seed=2474)
two_body_tensor = ffsim.random.random_two_body_tensor_real(norb, seed=7054)
mol_hamiltonian = ffsim.MolecularHamiltonian(one_body_tensor, two_body_tensor)
hamiltonian = ffsim.linear_operator(
Expand Down
38 changes: 10 additions & 28 deletions tests/trotter/qdrift_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from pyscf import ao2mo, gto, mcscf, scf

import ffsim
from ffsim.contract.hamiltonian import hamiltonian_trace
from ffsim.trotter.qdrift import (
one_body_square_decomposition,
spectral_norm_diag_coulomb,
Expand Down Expand Up @@ -264,16 +263,12 @@ def test_simulate_qdrift_double_factorized_h_chain(
two_body_tensor = ao2mo.restore(1, mc.get_h2cas(), mc.ncas)
norb, _ = one_body_tensor.shape
nelec = mol.nelec
hamiltonian = ffsim.contract.hamiltonian_linop(
one_body_tensor=one_body_tensor,
two_body_tensor=two_body_tensor,
norb=norb,
nelec=nelec,
)
mol_hamiltonian = ffsim.MolecularHamiltonian(one_body_tensor, two_body_tensor)
hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb=norb, nelec=nelec)

# perform double factorization
df_hamiltonian = ffsim.double_factorized_hamiltonian(
ffsim.MolecularHamiltonian(one_body_tensor, two_body_tensor),
mol_hamiltonian,
z_representation=z_representation,
)

Expand All @@ -288,12 +283,7 @@ def test_simulate_qdrift_double_factorized_h_chain(
exact_state = scipy.sparse.linalg.expm_multiply(
-1j * time * hamiltonian,
initial_state,
traceA=hamiltonian_trace(
norb=norb,
nelec=nelec,
one_body_tensor=one_body_tensor,
two_body_tensor=two_body_tensor,
),
traceA=ffsim.trace(mol_hamiltonian, norb=norb, nelec=nelec),
)

# make sure time is not too small
Expand Down Expand Up @@ -364,21 +354,18 @@ def test_simulate_qdrift_double_factorized_random(
):
rng = np.random.default_rng(2030)
# generate random Hamiltonian
# TODO test with complex one-body tensor after fixing get_hamiltonian_linop
# TODO test with complex one-body tensor fails due to the following issue
# https://github.com/qiskit-community/ffsim/issues/14
one_body_tensor = ffsim.random.random_real_symmetric_matrix(norb, seed=rng)
two_body_tensor = ffsim.random.random_two_body_tensor_real(
norb, rank=norb, seed=rng
)
hamiltonian = ffsim.contract.hamiltonian_linop(
one_body_tensor=one_body_tensor,
two_body_tensor=two_body_tensor,
norb=norb,
nelec=nelec,
)
mol_hamiltonian = ffsim.MolecularHamiltonian(one_body_tensor, two_body_tensor)
hamiltonian = ffsim.linear_operator(mol_hamiltonian, norb=norb, nelec=nelec)

# perform double factorization
df_hamiltonian = ffsim.double_factorized_hamiltonian(
ffsim.MolecularHamiltonian(one_body_tensor, two_body_tensor),
mol_hamiltonian,
optimize=optimize,
z_representation=z_representation,
)
Expand All @@ -394,12 +381,7 @@ def test_simulate_qdrift_double_factorized_random(
exact_state = scipy.sparse.linalg.expm_multiply(
-1j * time * hamiltonian,
initial_state,
traceA=hamiltonian_trace(
norb=norb,
nelec=nelec,
one_body_tensor=one_body_tensor,
two_body_tensor=two_body_tensor,
),
traceA=ffsim.trace(mol_hamiltonian, norb=norb, nelec=nelec),
)

# make sure time is not too small
Expand Down
3 changes: 2 additions & 1 deletion tests/trotter/trotter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def test_simulate_trotter_double_factorized_random(
):
# generate random Hamiltonian
dim = ffsim.dim(norb, nelec)
# TODO test with complex one-body tensor after fixing get_hamiltonian_linop
# TODO test with complex one-body tensor fails due to the following issue
# https://github.com/qiskit-community/ffsim/issues/14
one_body_tensor = np.real(ffsim.random.random_hermitian(norb, seed=2474))
two_body_tensor = ffsim.random.random_two_body_tensor_real(norb, seed=7054)
mol_hamiltonian = ffsim.MolecularHamiltonian(one_body_tensor, two_body_tensor)
Expand Down