From 36aaa67f474c0ab971ad515c18e80061c108e884 Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Wed, 9 Jul 2025 09:47:29 +0100 Subject: [PATCH 01/13] Add test for get_basis --- tests/magnetic_test.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/magnetic_test.py diff --git a/tests/magnetic_test.py b/tests/magnetic_test.py new file mode 100644 index 00000000..387ae709 --- /dev/null +++ b/tests/magnetic_test.py @@ -0,0 +1,18 @@ +import numpy as np +import pytest + +import qse + + +@pytest.mark.parametrize( + "hsize, N", + [ + (8, 4), + (3, 3) + ] +) +def test_get_basis_returns_correct_qubit_space_shape( + hsize, N +): + ibasis = qse.magnetic.get_basis(hsize, N) + assert ibasis.shape == (hsize, N) \ No newline at end of file From 9db24af54a0f4cc23eb775e4bccf88e25bcbec47 Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Wed, 9 Jul 2025 12:45:54 +0100 Subject: [PATCH 02/13] Rename test get basis shape --- tests/magnetic_test.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/tests/magnetic_test.py b/tests/magnetic_test.py index 387ae709..21ef40f9 100644 --- a/tests/magnetic_test.py +++ b/tests/magnetic_test.py @@ -3,16 +3,24 @@ import qse +""" +@pytest.mark.parametrize( + "hsize, N", [(8, 4), (3, 3)]) +def test_get_basis_returns_correct_qubit_space_shape(hsize, N): + ibasis = qse.magnetic.get_basis(hsize, N) + assert ibasis.shape == (hsize, N) + +""" + +def add(x,y): + print("x = ", x) + print("y = ", y) + print('-----------') + + return x + y @pytest.mark.parametrize( - "hsize, N", - [ - (8, 4), - (3, 3) - ] + "x, y", [(2,2), (4,5)] ) -def test_get_basis_returns_correct_qubit_space_shape( - hsize, N -): - ibasis = qse.magnetic.get_basis(hsize, N) - assert ibasis.shape == (hsize, N) \ No newline at end of file +def test_add(x, y): + assert add(x, y) == x ** 2 \ No newline at end of file From 123ed10be0560b249a3e6124360acc772739e992 Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Fri, 11 Jul 2025 08:28:19 +0100 Subject: [PATCH 03/13] Add test for norm of the spin values --- tests/magnetic_test.py | 55 ++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/tests/magnetic_test.py b/tests/magnetic_test.py index 21ef40f9..e79fbee6 100644 --- a/tests/magnetic_test.py +++ b/tests/magnetic_test.py @@ -3,24 +3,49 @@ import qse -""" -@pytest.mark.parametrize( - "hsize, N", [(8, 4), (3, 3)]) -def test_get_basis_returns_correct_qubit_space_shape(hsize, N): + +@pytest.mark.parametrize("hsize, N", [(8, 4), (3, 3)]) +def test_get_basis_shape(hsize, N): + """Check that get_basis outputs a hsize * N shape""" ibasis = qse.magnetic.get_basis(hsize, N) assert ibasis.shape == (hsize, N) -""" - -def add(x,y): - print("x = ", x) - print("y = ", y) - print('-----------') - - return x + y @pytest.mark.parametrize( - "x, y", [(2,2), (4,5)] + "statevector, ibasis, results", + [ + ( + np.array([1 / np.sqrt(2), 1 / np.sqrt(2)], dtype=complex), + qse.magnetic.get_basis(2**1, 1), + np.array([1, 0, 0], dtype=complex).T.real, + ), + ( + np.array([1 / 2, 1 / 2, 1 / 2, 1 / 2], dtype=complex), + qse.magnetic.get_basis(2**2, 2), + np.array([[1, 0, 0], [1, 0, 0]], dtype=complex), + ), + ( + np.array([1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)], dtype=complex), + qse.magnetic.get_basis(2**2, 2), + np.array( + [[0, 0, 0], [0, 0, 0],], dtype=complex), + ), + ], ) -def test_add(x, y): - assert add(x, y) == x ** 2 \ No newline at end of file +def test_get_spins_on_simple_statevectors(statevector, ibasis, results): + """Test output of get_spins on simple quantum states""" + N = int(np.log2(statevector.shape[0])) + spins = qse.magnetic.get_spins(statevector, ibasis, N) + assert np.allclose(results, spins) + + +@pytest.mark.parametrize("k", [1,2,3,4]) +def test_spin_values_are_less_than_one(k): + "Test that the absolute values of the computed spins is less than 1" + real_part = np.random.rand(2 ** k) + imag_part = np.random.rand(2 ** k) + statevector = real_part + 1j * imag_part + statevector /= np.linalg.norm(statevector) + ibasis = qse.magnetic.get_basis(2**k, k) + spins = qse.magnetic.get_spins(statevector, ibasis, k) + assert np.all(np.abs(spins) <= 1) \ No newline at end of file From 3be115438ed851ae12b4338b59ade34a06e4c731 Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Fri, 11 Jul 2025 14:45:00 +0100 Subject: [PATCH 04/13] apply black formatting --- tests/magnetic_test.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/magnetic_test.py b/tests/magnetic_test.py index e79fbee6..cc40706d 100644 --- a/tests/magnetic_test.py +++ b/tests/magnetic_test.py @@ -28,7 +28,12 @@ def test_get_basis_shape(hsize, N): np.array([1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)], dtype=complex), qse.magnetic.get_basis(2**2, 2), np.array( - [[0, 0, 0], [0, 0, 0],], dtype=complex), + [ + [0, 0, 0], + [0, 0, 0], + ], + dtype=complex, + ), ), ], ) @@ -39,13 +44,13 @@ def test_get_spins_on_simple_statevectors(statevector, ibasis, results): assert np.allclose(results, spins) -@pytest.mark.parametrize("k", [1,2,3,4]) +@pytest.mark.parametrize("k", [1, 2, 3, 4]) def test_spin_values_are_less_than_one(k): "Test that the absolute values of the computed spins is less than 1" - real_part = np.random.rand(2 ** k) - imag_part = np.random.rand(2 ** k) + real_part = np.random.rand(2**k) + imag_part = np.random.rand(2**k) statevector = real_part + 1j * imag_part statevector /= np.linalg.norm(statevector) ibasis = qse.magnetic.get_basis(2**k, k) spins = qse.magnetic.get_spins(statevector, ibasis, k) - assert np.all(np.abs(spins) <= 1) \ No newline at end of file + assert np.all(np.abs(spins) <= 1) From e16aa5c477cfd62daeaab9d64b8b4622d628034f Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Fri, 11 Jul 2025 14:46:25 +0100 Subject: [PATCH 05/13] changed version = "0.2.4" --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 98aaccc0..c8378819 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ authors = [ {name = "James Nelson", email = "james.nelson@ichec.ie"}, {name = "Sherry Blair", email = "sherry.blair@ichec.ie"} ] -version = "0.2.3" +version = "0.2.4" requires-python = ">=3.10" dependencies = [ "ase", From a6d039f9dce04e7042557d49fea02e887fba24c6 Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Tue, 29 Jul 2025 12:10:06 +0100 Subject: [PATCH 06/13] Change directory magnetic_test.py --- tests/{ => qse}/magnetic_test.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{ => qse}/magnetic_test.py (100%) diff --git a/tests/magnetic_test.py b/tests/qse/magnetic_test.py similarity index 100% rename from tests/magnetic_test.py rename to tests/qse/magnetic_test.py From 91e0cf46d46977a83db80bcd84514b2c82a38fb8 Mon Sep 17 00:00:00 2001 From: pabloT97 <65724026+pabloT97@users.noreply.github.com> Date: Fri, 19 Sep 2025 08:13:45 +0100 Subject: [PATCH 07/13] Delete told magnetic tests --- tests/magnetic_test.py | 56 ------------------------------------------ 1 file changed, 56 deletions(-) delete mode 100644 tests/magnetic_test.py diff --git a/tests/magnetic_test.py b/tests/magnetic_test.py deleted file mode 100644 index cc40706d..00000000 --- a/tests/magnetic_test.py +++ /dev/null @@ -1,56 +0,0 @@ -import numpy as np -import pytest - -import qse - - -@pytest.mark.parametrize("hsize, N", [(8, 4), (3, 3)]) -def test_get_basis_shape(hsize, N): - """Check that get_basis outputs a hsize * N shape""" - ibasis = qse.magnetic.get_basis(hsize, N) - assert ibasis.shape == (hsize, N) - - -@pytest.mark.parametrize( - "statevector, ibasis, results", - [ - ( - np.array([1 / np.sqrt(2), 1 / np.sqrt(2)], dtype=complex), - qse.magnetic.get_basis(2**1, 1), - np.array([1, 0, 0], dtype=complex).T.real, - ), - ( - np.array([1 / 2, 1 / 2, 1 / 2, 1 / 2], dtype=complex), - qse.magnetic.get_basis(2**2, 2), - np.array([[1, 0, 0], [1, 0, 0]], dtype=complex), - ), - ( - np.array([1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)], dtype=complex), - qse.magnetic.get_basis(2**2, 2), - np.array( - [ - [0, 0, 0], - [0, 0, 0], - ], - dtype=complex, - ), - ), - ], -) -def test_get_spins_on_simple_statevectors(statevector, ibasis, results): - """Test output of get_spins on simple quantum states""" - N = int(np.log2(statevector.shape[0])) - spins = qse.magnetic.get_spins(statevector, ibasis, N) - assert np.allclose(results, spins) - - -@pytest.mark.parametrize("k", [1, 2, 3, 4]) -def test_spin_values_are_less_than_one(k): - "Test that the absolute values of the computed spins is less than 1" - real_part = np.random.rand(2**k) - imag_part = np.random.rand(2**k) - statevector = real_part + 1j * imag_part - statevector /= np.linalg.norm(statevector) - ibasis = qse.magnetic.get_basis(2**k, k) - spins = qse.magnetic.get_spins(statevector, ibasis, k) - assert np.all(np.abs(spins) <= 1) From e0856d8da59a50d1b6d7fb858b9c908b96edf009 Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Fri, 19 Sep 2025 10:34:40 +0100 Subject: [PATCH 08/13] Add test get_basis_shape --- tests/qse/magnetic_test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/qse/magnetic_test.py b/tests/qse/magnetic_test.py index 19294542..ef35cc7c 100644 --- a/tests/qse/magnetic_test.py +++ b/tests/qse/magnetic_test.py @@ -27,6 +27,13 @@ def expval(op, state): return (np.conj(state) @ (op @ state[:, None])).item() +@pytest.mark.parametrize("n_qubits, h_size", [(4, 2**4), (3, 2**3-1)]) +def test_get_basis_shape(n_qubits, h_size): + """Check that get_basis outputs a n_qubits * h_size shape""" + ibasis = qse.magnetic.get_basis(n_qubits, h_size) + assert ibasis.shape == (h_size, n_qubits) + + def test_spin_two_qubits(): """Check get_spins works for two qubits.""" n = 2 From 146f61176b764da59ab91d281f7959c12770f3e2 Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Fri, 19 Sep 2025 11:04:19 +0100 Subject: [PATCH 09/13] Add test_spin_values_are_less_than_one --- tests/qse/magnetic_test.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/qse/magnetic_test.py b/tests/qse/magnetic_test.py index ef35cc7c..2261bdd9 100644 --- a/tests/qse/magnetic_test.py +++ b/tests/qse/magnetic_test.py @@ -27,11 +27,19 @@ def expval(op, state): return (np.conj(state) @ (op @ state[:, None])).item() -@pytest.mark.parametrize("n_qubits, h_size", [(4, 2**4), (3, 2**3-1)]) -def test_get_basis_shape(n_qubits, h_size): +@pytest.mark.parametrize("n_qubits, hsize", [(4, 2**4), (3, 2**3-1)]) +def test_get_basis_shape(n_qubits, hsize): """Check that get_basis outputs a n_qubits * h_size shape""" - ibasis = qse.magnetic.get_basis(n_qubits, h_size) - assert ibasis.shape == (h_size, n_qubits) + ibasis = qse.magnetic.get_basis(n_qubits, hsize) + assert ibasis.shape == (hsize, n_qubits) + +@pytest.mark.parametrize("hdim", [1,2,3,4,5]) +def test_spin_values_are_less_than_one(hdim): + "Test that the absolute values of the computed spins is less than 1" + statevector = random_state(2**hdim) + basis = qse.magnetic.get_basis(hdim, 2**hdim) + spins = qse.magnetic.get_spins(statevector, hdim, basis) + assert np.all(np.abs(spins) <= 1) def test_spin_two_qubits(): From e537d37420967ccbd833cbadeac76384cbe8459f Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Fri, 19 Sep 2025 13:16:59 +0100 Subject: [PATCH 10/13] Add magnetic test --- tests/qse/magnetic_test.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/qse/magnetic_test.py b/tests/qse/magnetic_test.py index 2261bdd9..c1c03a64 100644 --- a/tests/qse/magnetic_test.py +++ b/tests/qse/magnetic_test.py @@ -27,18 +27,36 @@ def expval(op, state): return (np.conj(state) @ (op @ state[:, None])).item() -@pytest.mark.parametrize("n_qubits, hsize", [(4, 2**4), (3, 2**3-1)]) +def compute_sisj_2dim_algebraic(psi): + "Computes the elements s12 = s21 for a two qubit system" + a = psi[0] + b = psi[1] + c = psi[2] + d = psi[3] + r = ( + 2 * np.conj(b) * c + + 2 * np.conj(c) * b + + np.abs(a) ** 2 + - np.abs(b) ** 2 + - np.abs(c) ** 2 + + np.abs(d) ** 2 + ) + return r + + +@pytest.mark.parametrize("n_qubits, hsize", [(4, 2**4), (3, 2**3 - 1)]) def test_get_basis_shape(n_qubits, hsize): """Check that get_basis outputs a n_qubits * h_size shape""" ibasis = qse.magnetic.get_basis(n_qubits, hsize) assert ibasis.shape == (hsize, n_qubits) -@pytest.mark.parametrize("hdim", [1,2,3,4,5]) + +@pytest.mark.parametrize("hdim", [1, 2, 3, 4, 5]) def test_spin_values_are_less_than_one(hdim): "Test that the absolute values of the computed spins is less than 1" statevector = random_state(2**hdim) - basis = qse.magnetic.get_basis(hdim, 2**hdim) - spins = qse.magnetic.get_spins(statevector, hdim, basis) + ibasis = qse.magnetic.get_basis(hdim, 2**hdim) + spins = qse.magnetic.get_spins(statevector, hdim, ibasis) assert np.all(np.abs(spins) <= 1) @@ -47,7 +65,6 @@ def test_spin_two_qubits(): n = 2 statevector = random_state(2**n) spin_qse = qse.magnetic.get_spins(statevector, n) - op_q0 = [np.kron(p, np.eye(2)) for p in [pauli_x, pauli_y, pauli_z]] expval_q0 = [expval(op, statevector) for op in op_q0] op_q1 = [np.kron(np.eye(2), p) for p in [pauli_x, pauli_y, pauli_z]] From d77642f59128030f79f9b7c6f34b185183164489 Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Fri, 19 Sep 2025 13:17:11 +0100 Subject: [PATCH 11/13] changed version = "0.2.29" --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f21f72dc..e75f46be 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ authors = [ {name = "James Nelson", email = "james.nelson@ichec.ie"}, {name = "Sherry Blair", email = "sherry.blair@ichec.ie"} ] -version = "0.2.28" +version = "0.2.29" requires-python = ">=3.10" dependencies = [ "ase", From 171844207c59a38e9fa897d0a39095b624dd9095 Mon Sep 17 00:00:00 2001 From: Pablo Suarez Vieites Date: Fri, 19 Sep 2025 13:17:43 +0100 Subject: [PATCH 12/13] changed version = "0.2.30" --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e75f46be..9df69f6c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ authors = [ {name = "James Nelson", email = "james.nelson@ichec.ie"}, {name = "Sherry Blair", email = "sherry.blair@ichec.ie"} ] -version = "0.2.29" +version = "0.2.30" requires-python = ">=3.10" dependencies = [ "ase", From fea6c69ff33d4d4ad48d1f3dd02ed25957760882 Mon Sep 17 00:00:00 2001 From: pablosuvi Date: Wed, 5 Nov 2025 19:31:45 +0000 Subject: [PATCH 13/13] Add test get_sisj --- tests/qse/magnetic_test.py | 44 +++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/tests/qse/magnetic_test.py b/tests/qse/magnetic_test.py index c1c03a64..7825fd5a 100644 --- a/tests/qse/magnetic_test.py +++ b/tests/qse/magnetic_test.py @@ -9,6 +9,7 @@ def random_state(hdim): + "Outputs a random normalised state" statevector = np.random.rand(hdim) + 1j * np.random.rand(hdim) statevector /= np.linalg.norm(statevector) return statevector @@ -27,21 +28,21 @@ def expval(op, state): return (np.conj(state) @ (op @ state[:, None])).item() -def compute_sisj_2dim_algebraic(psi): - "Computes the elements s12 = s21 for a two qubit system" - a = psi[0] - b = psi[1] - c = psi[2] - d = psi[3] - r = ( - 2 * np.conj(b) * c - + 2 * np.conj(c) * b - + np.abs(a) ** 2 - - np.abs(b) ** 2 - - np.abs(c) ** 2 - + np.abs(d) ** 2 - ) - return r +def ij_operator(n, i, j, op_i, op_j): + ops = [np.eye(2) for _ in range(n)] + ops[i] = op_i + ops[j] = op_j + return kron_list(ops) + + +def get_sisj_algebraic(i, j, statevector): + n = int(np.log2(len(statevector))) + term_1 = expval(ij_operator(n, i, j, pauli_x, pauli_x), statevector) + term_2 = expval(ij_operator(n, i, j, pauli_y, pauli_y), statevector) + term_3 = expval(ij_operator(n, i, j, pauli_z, pauli_z), statevector) + return (1 / 3) * ( + term_1 + term_2 + term_3 + ).real # Normalise it to 1 for structure factor @pytest.mark.parametrize("n_qubits, hsize", [(4, 2**4), (3, 2**3 - 1)]) @@ -98,3 +99,16 @@ def test_number_operator_random(n): n_op = qse.magnetic.get_number_operator(psi, n) assert np.allclose(n_op, np_n_op) + + +@pytest.mark.parametrize("n", [2, 3, 4]) +def test_get_sisj(n): + """Tests the get_sisj function.""" + psi = random_state(2**n) + sisj = qse.magnetic.get_sisj(psi, n, qse.magnetic.get_basis(n, 2**n)) + sisj_algebraic = np.eye(n) + for i in range(n): + for j in range(i + 1, n): + sisj_algebraic[i][j] = get_sisj_algebraic(i, j, psi) + sisj_algebraic[j][i] = sisj_algebraic[i][j] + assert np.allclose(sisj, sisj_algebraic)