Skip to content

Commit

Permalink
updating tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
weinbe58 committed May 29, 2024
1 parent 1167ff0 commit f62fae6
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions tests/test_emulator_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from bloqade.atom_arrangement import Chain
import numpy as np
from itertools import product
from math import isclose


def test_zero_state():
Expand Down Expand Up @@ -44,8 +45,8 @@ def test_solution(N: int, phi: float, interaction: bool):
rabi_freq = 2 * np.pi
program = (
Chain(N, lattice_spacing=6)
.rydberg.detuning.uniform.constant(1.0, 4.0)
.amplitude.uniform.constant(rabi_freq, 4)
# .rydberg.detuning.uniform.constant(1.0, 4.0)
.rydberg.rabi.amplitude.uniform.constant(rabi_freq, 4.0)
)

if phi != 0:
Expand All @@ -54,36 +55,44 @@ def test_solution(N: int, phi: float, interaction: bool):
[emu] = program.bloqade.python().hamiltonian()

times = np.linspace(0, 4, 101)
state_iter = emu.evolve(times=times, interaction_picture=interaction)
state_iter = emu.evolve(
times=times, interaction_picture=interaction, atol=1e-10, rtol=1e-14
)

h = emu.hamiltonian.tocsr(0)

print(h.toarray())
e, v = np.linalg.eigh(h.toarray())

psi0 = emu.zero_state().data

for state, time in zip(state_iter, times):
assert str(state)
expected_data = v @ (np.diag(np.exp(-1j * e * time)) @ (v.T @ psi0))
expected_data = v @ (np.diag(np.exp(-1j * e * time)) @ (v.T.conj() @ psi0))
data = state.data

expected_average = np.vdot(expected_data, h.dot(expected_data))
expected_average = np.vdot(expected_data, h.dot(expected_data)).real
expected_variance = (
np.vdot(expected_data, (h @ h).dot(expected_data)) - expected_average**2
)
).real

print(emu.hamiltonian._apply(state.data, time) - h.dot(expected_data))

average = emu.hamiltonian.average(state, time=time)
variance = emu.hamiltonian.variance(state, time=time)
average_2, variance_2 = emu.hamiltonian.average_and_variance(state, time=time)

assert np.allclose(data, expected_data), "failed at time {}".format(time)
assert np.allclose(average, expected_average), "failed at time {}".format(time)
assert np.allclose(average_2, expected_average), "failed at time {}".format(
time
)
assert np.allclose(variance, expected_variance), "failed at time {}".format(
time
)
assert np.allclose(variance_2, expected_variance), "failed at time {}".format(
time
)
overlap = np.vdot(data, expected_data)
assert isclose(overlap, 1.0, abs_tol=1e-7), f"failed data at time {time}"
assert isclose(
average, expected_average, rel_tol=1e-7, abs_tol=1e-7
), f"failed average at time {time}"
assert isclose(
average_2, expected_average, rel_tol=1e-7, abs_tol=1e-7
), f"failed average_2 at time {time}"
assert isclose(
variance, expected_variance, rel_tol=1e-7, abs_tol=1e-7
), f"failed variance at time {time}"
assert isclose(
variance_2, expected_variance, rel_tol=1e-7, abs_tol=1e-7
), f"failed variance_2 at time {time}"

# assert False

0 comments on commit f62fae6

Please sign in to comment.