Skip to content

Commit

Permalink
Correct XY Hamiltonian interaction term (#744)
Browse files Browse the repository at this point in the history
* Correct XY interaction term calculation

* Adapt tutorial
  • Loading branch information
HGSilveri authored Oct 4, 2024
1 parent 32fbbb0 commit 96ab5c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 51 deletions.
26 changes: 12 additions & 14 deletions pulser-simulation/pulser_simulation/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,22 +457,20 @@ def make_xy_term(q1: QubitId, q2: QubitId) -> qutip.Qobj:
The units are given so that the coefficient
includes a 1/hbar factor.
"""
dist = np.linalg.norm(self._qdict[q1] - self._qdict[q2])
coords_dim = len(self._qdict[q1])
mag_field = cast(np.ndarray, self.samples_obj._magnetic_field)[
:coords_dim
]
diff_vector = np.zeros(3, dtype=float)
diff_vector[: len(self._qdict[q1])] = (
self._qdict[q1] - self._qdict[q2]
)
dist = np.linalg.norm(diff_vector)
mag_field = cast(np.ndarray, self.samples_obj._magnetic_field)
mag_norm = np.linalg.norm(mag_field)
if mag_norm < 1e-8:
cosine = 0.0
else:
cosine = np.dot(
(self._qdict[q1] - self._qdict[q2]),
mag_field,
) / (dist * mag_norm)
assert mag_norm > 0, "There must be a magnetic field in XY mode."
cosine = np.dot(
diff_vector,
mag_field,
) / (dist * mag_norm)
U = (
0.5
* cast(float, self._device.interaction_coeff_xy)
cast(float, self._device.interaction_coeff_xy)
* (1 - 3 * cosine**2)
/ dist**3
)
Expand Down
13 changes: 6 additions & 7 deletions tests/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,11 +1189,10 @@ def test_get_xy_hamiltonian():
simple_sim.get_hamiltonian(-10)
# Constant detuning, so |ud><du| term is C_3/r^3 - 2*detuning for any time
simple_ham = simple_sim.get_hamiltonian(143)
assert simple_ham[1, 2] == 0.5 * MockDevice.interaction_coeff_xy / 10**3
assert simple_ham[1, 2] == MockDevice.interaction_coeff_xy / 10**3
assert (
np.abs(
simple_ham[1, 4]
- (-2 * 0.5 * MockDevice.interaction_coeff_xy / 10**3)
simple_ham[1, 4] - (-2 * MockDevice.interaction_coeff_xy / 10**3)
)
< 1e-10
)
Expand Down Expand Up @@ -1236,10 +1235,10 @@ def test_run_xy():
assert sim.samples_obj._measurement == "XY"


res1 = {"0000": 892, "1000": 47, "0100": 25, "0001": 19, "0010": 17}
res2 = {"0000": 962, "0010": 13, "1000": 13, "0100": 12}
res3 = {"0000": 904, "0100": 43, "0010": 24, "1000": 19, "0001": 10}
res4 = {"0000": 969, "0001": 18, "1000": 13}
res1 = {"0000": 950, "0100": 19, "0001": 21, "0010": 10}
res2 = {"0000": 944, "0010": 15, "1000": 33, "0100": 8}
res3 = {"0000": 950, "0100": 19, "0010": 10, "0001": 21}
res4 = {"0000": 951, "0100": 19, "1000": 30}


@pytest.mark.parametrize(
Expand Down

Large diffs are not rendered by default.

0 comments on commit 96ab5c4

Please sign in to comment.