From cca6bd5eb3becfa737d708f970e3a33fe18d8e34 Mon Sep 17 00:00:00 2001 From: HGSilveri Date: Tue, 29 Aug 2023 15:52:27 +0200 Subject: [PATCH] Fix XY mode conventions --- .../pulser_simulation/qutip_result.py | 6 +++-- .../pulser_simulation/simulation.py | 8 +++--- tests/test_simresults.py | 3 ++- tests/test_simulation.py | 7 ++++- .../Spin chain of 3 atoms in XY mode.ipynb | 26 ++++++++++--------- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/pulser-simulation/pulser_simulation/qutip_result.py b/pulser-simulation/pulser_simulation/qutip_result.py index ccfae19b5..41de1cbf5 100644 --- a/pulser-simulation/pulser_simulation/qutip_result.py +++ b/pulser-simulation/pulser_simulation/qutip_result.py @@ -83,9 +83,11 @@ def _weights(self) -> np.ndarray: # State vector ordered with r first for 'ground_rydberg' # e.g. n=2: [rr, rg, gr, gg] -> [11, 10, 01, 00] # Invert the order -> [00, 01, 10, 11] correspondence - # The same applies in XY mode, which is ordered with u first + # In the XY and digital bases, the order is cannonical weights = ( - probs if self.meas_basis == "digital" else probs[::-1] + probs[::-1] + if self.meas_basis == "ground-rydberg" + else probs ) else: # Only 000...000 is measured diff --git a/pulser-simulation/pulser_simulation/simulation.py b/pulser-simulation/pulser_simulation/simulation.py index e2f36b1ba..62433e18b 100644 --- a/pulser-simulation/pulser_simulation/simulation.py +++ b/pulser-simulation/pulser_simulation/simulation.py @@ -402,7 +402,7 @@ def set_initial_state( if isinstance(state, str) and state == "all-ground": self._initial_state = qutip.tensor( [ - self.basis["d" if self._interaction == "XY" else "g"] + self.basis["u" if self._interaction == "XY" else "g"] for _ in range(self._size) ] ) @@ -683,7 +683,7 @@ def make_xy_term(q1: QubitId, q2: QubitId) -> qutip.Qobj: """Construct the XY interaction Term. For each pair of qubits, calculate the distance between them, - then assign the local operator "sigma_du * sigma_ud" at each pair. + then assign the local operator "sigma_ud * sigma_du" at each pair. The units are given so that the coefficient includes a 1/hbar factor. """ @@ -707,7 +707,7 @@ def make_xy_term(q1: QubitId, q2: QubitId) -> qutip.Qobj: / dist**3 ) return U * self.build_operator( - [("sigma_du", [q1]), ("sigma_ud", [q2])] + [("sigma_ud", [q1]), ("sigma_du", [q2])] ) def make_interaction_term(masked: bool = False) -> qutip.Qobj: @@ -752,7 +752,7 @@ def build_coeffs_ops(basis: str, addr: str) -> list[list]: elif basis == "digital": op_ids = ["sigma_hg", "sigma_gg"] elif basis == "XY": - op_ids = ["sigma_du", "sigma_dd"] + op_ids = ["sigma_du", "sigma_uu"] terms = [] if addr == "Global": diff --git a/tests/test_simresults.py b/tests/test_simresults.py index 767146dc7..ae31944e1 100644 --- a/tests/test_simresults.py +++ b/tests/test_simresults.py @@ -360,8 +360,9 @@ def test_results_xy(reg, pi_pulse): assert results_._size == 2 assert results_._basis_name == "XY" assert results_._meas_basis == "XY" + # Checks the initial state assert results_.states[0] == qutip.tensor( - [qutip.basis(2, 1), qutip.basis(2, 1)] + [qutip.basis(2, 0), qutip.basis(2, 0)] ) with pytest.raises(TypeError, match="Can't reduce a system in"): diff --git a/tests/test_simulation.py b/tests/test_simulation.py index 28cb3a970..54d0029ad 100644 --- a/tests/test_simulation.py +++ b/tests/test_simulation.py @@ -914,7 +914,12 @@ def test_get_xy_hamiltonian(): < 1e-10 ) assert simple_ham[0, 1] == 0.5 * amp - assert simple_ham[3, 3] == -2 * detun + # |udd> 1 atom in |u> + assert simple_ham[3, 3] == -detun + # |udd> 3 atom in |u> + assert simple_ham[0, 0] == -3 * detun + # |ddd> no atom in |u> + assert simple_ham[7, 7] == 0 def test_run_xy(): diff --git a/tutorials/quantum_simulation/Spin chain of 3 atoms in XY mode.ipynb b/tutorials/quantum_simulation/Spin chain of 3 atoms in XY mode.ipynb index 76df69e12..aa9802b7d 100644 --- a/tutorials/quantum_simulation/Spin chain of 3 atoms in XY mode.ipynb +++ b/tutorials/quantum_simulation/Spin chain of 3 atoms in XY mode.ipynb @@ -14,7 +14,7 @@ "We introduce here a new mode of interaction between the atoms driven by the dipole-dipole interaction. This mode is called the XY mode. The interaction hamiltonian of a system of $N$ qubits is now given by \n", "\n", "$$\n", - "H_{XY} = \\frac{1}{2}\\sum_{i<1| projector operator on site j.\"\"\"\n", " prod = [qutip.qeye(2) for _ in range(total_sites)]\n", - " prod[j] = (qutip.sigmaz() + qutip.qeye(2)) / 2\n", + " prod[j] = (qutip.qeye(2) - qutip.sigmaz()) / 2\n", " return qutip.tensor(prod)\n", "\n", "\n", - "magn = magnetization(0, 1)\n", + "excited = excitation(0, 1)\n", "plt.figure(figsize=[16, 6])\n", - "results.plot(magn)\n", + "results.plot(excited)\n", "plt.xlabel(\"Pulse duration (ns)\", fontsize=\"x-large\")\n", "plt.ylabel(\"Excitation of the atom\", fontsize=\"x-large\")\n", "plt.show()" @@ -174,9 +175,9 @@ "metadata": {}, "outputs": [], "source": [ - "magn_list = [magnetization(j, 3) for j in range(3)]\n", + "excited_list = [excitation(j, 3) for j in range(3)]\n", "\n", - "expectations = results.expect(magn_list)\n", + "expectations = results.expect(excited_list)\n", "\n", "plt.figure(figsize=[16, 18])\n", "plt.subplot(311)\n", @@ -214,7 +215,7 @@ "An external magnetic field can be added to the experiment, and will modify the hamiltonian. The XY Hamiltonian is then\n", "\n", "$$\n", - "H_{XY} = \\frac{1}{2}\\sum_{i