Tatopenn commited on
Commit
51ca1e7
Β·
verified Β·
1 Parent(s): e1a5604

Sync test_quantum_scars.py from v8.1.33

Browse files
Files changed (1) hide show
  1. test_quantum_scars.py +148 -148
test_quantum_scars.py CHANGED
@@ -1,148 +1,148 @@
1
- """
2
- Unit tests for ui_pages/quantum_scars.py's numerical core (Hamiltonian
3
- construction, exact-diagonalization propagation, and the two projection
4
- strategies). Plain pytest against the module's private helpers directly --
5
- no separate core module like dashboard_core.py exists for this page.
6
- AppTest-level UI smoke testing was already done manually and is not
7
- duplicated here.
8
- """
9
-
10
- import matplotlib
11
- matplotlib.use("Agg")
12
-
13
- import numpy as np
14
- import pytest
15
-
16
- from ui_pages import quantum_scars as qs
17
-
18
- N_QUBITS = 6 # dim=64, diagonalizes instantly
19
-
20
-
21
- @pytest.fixture(scope="module")
22
- def pxp():
23
- return qs._build_pxp(N_QUBITS)
24
-
25
-
26
- def _random_state(dim, seed):
27
- rng = np.random.default_rng(seed)
28
- sv = rng.normal(size=dim) + 1j * rng.normal(size=dim)
29
- return sv / np.linalg.norm(sv)
30
-
31
-
32
- # ── _is_valid_state ─────────────────────────────────────────────────────
33
-
34
- def test_is_valid_state_rejects_adjacent_ones():
35
- # 0b110000 has two adjacent 1-bits -> invalid
36
- assert qs._is_valid_state(0b110000, N_QUBITS) is False
37
-
38
-
39
- def test_is_valid_state_accepts_neel_pattern():
40
- # 0b010101 alternates, never two adjacent 1-bits -> valid
41
- assert qs._is_valid_state(0b010101, N_QUBITS) is True
42
-
43
-
44
- def test_is_valid_state_all_zero_is_valid():
45
- assert qs._is_valid_state(0, N_QUBITS) is True
46
-
47
-
48
- def test_is_valid_state_matches_independent_bruteforce():
49
- # Cross-check every state against an independent string-based
50
- # implementation, unrelated to _build_pxp's own usage of the function.
51
- for i in range(2 ** N_QUBITS):
52
- expected = "11" not in format(i, f"0{N_QUBITS}b")
53
- assert qs._is_valid_state(i, N_QUBITS) == expected
54
-
55
-
56
- # ── _build_pxp ───────────────────────────────────────────────────────────
57
-
58
- def test_build_pxp_hilbert_dimension(pxp):
59
- assert pxp["dim"] == 2 ** N_QUBITS
60
- assert pxp["eigenvalues"].shape == (pxp["dim"],)
61
- assert pxp["eigenvectors"].shape == (pxp["dim"], pxp["dim"])
62
-
63
-
64
- def test_build_pxp_hamiltonian_is_hermitian(pxp):
65
- h = pxp["h_pxp"]
66
- np.testing.assert_allclose(h, h.conj().T, atol=1e-10)
67
-
68
-
69
- def test_build_pxp_valid_dim_bounded(pxp):
70
- assert 0 < pxp["valid_dim"] <= pxp["dim"]
71
-
72
-
73
- def test_build_pxp_tower_ceiling_bounded(pxp):
74
- assert 0 < pxp["tower_ceiling"] <= 1 + 1e-9
75
-
76
-
77
- # ── _propagate ───────────────────────────────────────────────────────────
78
-
79
- def test_propagate_fidelity_one_at_t_zero(pxp):
80
- neel = np.zeros(pxp["dim"], dtype=np.complex128)
81
- neel[pxp["neel_idx"]] = 1.0
82
- propagated = qs._propagate(pxp, neel, dt=0.0)
83
- fidelity = np.abs(np.vdot(neel, propagated)) ** 2
84
- assert fidelity == pytest.approx(1.0, abs=1e-9)
85
-
86
-
87
- def test_propagate_preserves_norm(pxp):
88
- neel = np.zeros(pxp["dim"], dtype=np.complex128)
89
- neel[pxp["neel_idx"]] = 1.0
90
- sv = neel.copy()
91
- for _ in range(20):
92
- sv = qs._propagate(pxp, sv, dt=0.2)
93
- assert np.linalg.norm(sv) == pytest.approx(1.0, abs=1e-9)
94
-
95
-
96
- # ── _project_tower ───────────────────────────────────────────────────────
97
-
98
- def test_project_tower_stays_normalized(pxp):
99
- sv = _random_state(pxp["dim"], seed=1)
100
- projected = qs._project_tower(pxp, sv)
101
- assert np.linalg.norm(projected) == pytest.approx(1.0, abs=1e-9)
102
-
103
-
104
- def test_project_tower_zero_weight_outside_subspace(pxp):
105
- sv = _random_state(pxp["dim"], seed=2)
106
- projected = qs._project_tower(pxp, sv)
107
- weight_in_tower = np.sum(np.abs(pxp["tower_vectors"].conj().T @ projected) ** 2)
108
- assert weight_in_tower == pytest.approx(1.0, abs=1e-9)
109
-
110
-
111
- def test_project_tower_orthogonal_state_returns_near_zero(pxp):
112
- # Build a state supported only on eigenvectors NOT in the tower ->
113
- # exercises the norm < 1e-12 safe-return branch without dividing by zero.
114
- tower_set = set(np.argsort(
115
- np.abs(pxp["eigenvectors"][pxp["neel_idx"], :]) ** 2
116
- )[::-1][: N_QUBITS + 1].tolist())
117
- non_tower_idx = next(i for i in range(pxp["dim"]) if i not in tower_set)
118
- sv = pxp["eigenvectors"][:, non_tower_idx]
119
- projected = qs._project_tower(pxp, sv)
120
- assert np.linalg.norm(projected) < 1e-9
121
-
122
-
123
- # ── _project_constraint ──────────────────────────────────────────────────
124
-
125
- def test_project_constraint_stays_normalized(pxp):
126
- sv = _random_state(pxp["dim"], seed=3)
127
- projected = qs._project_constraint(pxp, sv)
128
- assert np.linalg.norm(projected) == pytest.approx(1.0, abs=1e-9)
129
-
130
-
131
- def test_project_constraint_zero_weight_on_invalid_states(pxp):
132
- sv = _random_state(pxp["dim"], seed=4)
133
- projected = qs._project_constraint(pxp, sv)
134
- assert np.allclose(projected[~pxp["valid_mask"]], 0.0)
135
-
136
-
137
- # ── _invalid_weight ───────────────────────────────────────────────────────
138
-
139
- def test_invalid_weight_neel_state_is_zero(pxp):
140
- neel = np.zeros(pxp["dim"], dtype=np.complex128)
141
- neel[pxp["neel_idx"]] = 1.0
142
- assert qs._invalid_weight(pxp, neel) == pytest.approx(0.0, abs=1e-12)
143
-
144
-
145
- def test_invalid_weight_uniform_state_matches_complement_of_valid_dim(pxp):
146
- uniform = np.ones(pxp["dim"], dtype=np.complex128) / np.sqrt(pxp["dim"])
147
- expected = 1.0 - pxp["valid_dim"] / pxp["dim"]
148
- assert qs._invalid_weight(pxp, uniform) == pytest.approx(expected, abs=1e-9)
 
1
+ """
2
+ Unit tests for ui_pages/quantum_scars.py's numerical core (Hamiltonian
3
+ construction, exact-diagonalization propagation, and the two projection
4
+ strategies). Plain pytest against the module's private helpers directly --
5
+ no separate core module like dashboard_core.py exists for this page.
6
+ AppTest-level UI smoke testing was already done manually and is not
7
+ duplicated here.
8
+ """
9
+
10
+ import matplotlib
11
+ matplotlib.use("Agg")
12
+
13
+ import numpy as np
14
+ import pytest
15
+
16
+ from ui_pages import quantum_scars as qs
17
+
18
+ N_QUBITS = 6 # dim=64, diagonalizes instantly
19
+
20
+
21
+ @pytest.fixture(scope="module")
22
+ def pxp():
23
+ return qs._build_pxp(N_QUBITS)
24
+
25
+
26
+ def _random_state(dim, seed):
27
+ rng = np.random.default_rng(seed)
28
+ sv = rng.normal(size=dim) + 1j * rng.normal(size=dim)
29
+ return sv / np.linalg.norm(sv)
30
+
31
+
32
+ # ── _is_valid_state ─────────────────────────────────────────────────────
33
+
34
+ def test_is_valid_state_rejects_adjacent_ones():
35
+ # 0b110000 has two adjacent 1-bits -> invalid
36
+ assert qs._is_valid_state(0b110000, N_QUBITS) is False
37
+
38
+
39
+ def test_is_valid_state_accepts_neel_pattern():
40
+ # 0b010101 alternates, never two adjacent 1-bits -> valid
41
+ assert qs._is_valid_state(0b010101, N_QUBITS) is True
42
+
43
+
44
+ def test_is_valid_state_all_zero_is_valid():
45
+ assert qs._is_valid_state(0, N_QUBITS) is True
46
+
47
+
48
+ def test_is_valid_state_matches_independent_bruteforce():
49
+ # Cross-check every state against an independent string-based
50
+ # implementation, unrelated to _build_pxp's own usage of the function.
51
+ for i in range(2 ** N_QUBITS):
52
+ expected = "11" not in format(i, f"0{N_QUBITS}b")
53
+ assert qs._is_valid_state(i, N_QUBITS) == expected
54
+
55
+
56
+ # ── _build_pxp ───────────────────────────────────────────────────────────
57
+
58
+ def test_build_pxp_hilbert_dimension(pxp):
59
+ assert pxp["dim"] == 2 ** N_QUBITS
60
+ assert pxp["eigenvalues"].shape == (pxp["dim"],)
61
+ assert pxp["eigenvectors"].shape == (pxp["dim"], pxp["dim"])
62
+
63
+
64
+ def test_build_pxp_hamiltonian_is_hermitian(pxp):
65
+ h = pxp["h_pxp"]
66
+ np.testing.assert_allclose(h, h.conj().T, atol=1e-10)
67
+
68
+
69
+ def test_build_pxp_valid_dim_bounded(pxp):
70
+ assert 0 < pxp["valid_dim"] <= pxp["dim"]
71
+
72
+
73
+ def test_build_pxp_tower_ceiling_bounded(pxp):
74
+ assert 0 < pxp["tower_ceiling"] <= 1 + 1e-9
75
+
76
+
77
+ # ── _propagate ───────────────────────────────────────────────────────────
78
+
79
+ def test_propagate_fidelity_one_at_t_zero(pxp):
80
+ neel = np.zeros(pxp["dim"], dtype=np.complex128)
81
+ neel[pxp["neel_idx"]] = 1.0
82
+ propagated = qs._propagate(pxp, neel, dt=0.0)
83
+ fidelity = np.abs(np.vdot(neel, propagated)) ** 2
84
+ assert fidelity == pytest.approx(1.0, abs=1e-9)
85
+
86
+
87
+ def test_propagate_preserves_norm(pxp):
88
+ neel = np.zeros(pxp["dim"], dtype=np.complex128)
89
+ neel[pxp["neel_idx"]] = 1.0
90
+ sv = neel.copy()
91
+ for _ in range(20):
92
+ sv = qs._propagate(pxp, sv, dt=0.2)
93
+ assert np.linalg.norm(sv) == pytest.approx(1.0, abs=1e-9)
94
+
95
+
96
+ # ── _project_tower ───────────────────────────────────────────────────────
97
+
98
+ def test_project_tower_stays_normalized(pxp):
99
+ sv = _random_state(pxp["dim"], seed=1)
100
+ projected = qs._project_tower(pxp, sv)
101
+ assert np.linalg.norm(projected) == pytest.approx(1.0, abs=1e-9)
102
+
103
+
104
+ def test_project_tower_zero_weight_outside_subspace(pxp):
105
+ sv = _random_state(pxp["dim"], seed=2)
106
+ projected = qs._project_tower(pxp, sv)
107
+ weight_in_tower = np.sum(np.abs(pxp["tower_vectors"].conj().T @ projected) ** 2)
108
+ assert weight_in_tower == pytest.approx(1.0, abs=1e-9)
109
+
110
+
111
+ def test_project_tower_orthogonal_state_returns_near_zero(pxp):
112
+ # Build a state supported only on eigenvectors NOT in the tower ->
113
+ # exercises the norm < 1e-12 safe-return branch without dividing by zero.
114
+ tower_set = set(np.argsort(
115
+ np.abs(pxp["eigenvectors"][pxp["neel_idx"], :]) ** 2
116
+ )[::-1][: N_QUBITS + 1].tolist())
117
+ non_tower_idx = next(i for i in range(pxp["dim"]) if i not in tower_set)
118
+ sv = pxp["eigenvectors"][:, non_tower_idx]
119
+ projected = qs._project_tower(pxp, sv)
120
+ assert np.linalg.norm(projected) < 1e-9
121
+
122
+
123
+ # ── _project_constraint ──────────────────────────────────────────────────
124
+
125
+ def test_project_constraint_stays_normalized(pxp):
126
+ sv = _random_state(pxp["dim"], seed=3)
127
+ projected = qs._project_constraint(pxp, sv)
128
+ assert np.linalg.norm(projected) == pytest.approx(1.0, abs=1e-9)
129
+
130
+
131
+ def test_project_constraint_zero_weight_on_invalid_states(pxp):
132
+ sv = _random_state(pxp["dim"], seed=4)
133
+ projected = qs._project_constraint(pxp, sv)
134
+ assert np.allclose(projected[~pxp["valid_mask"]], 0.0)
135
+
136
+
137
+ # ── _invalid_weight ───────────────────────────────────────────────────────
138
+
139
+ def test_invalid_weight_neel_state_is_zero(pxp):
140
+ neel = np.zeros(pxp["dim"], dtype=np.complex128)
141
+ neel[pxp["neel_idx"]] = 1.0
142
+ assert qs._invalid_weight(pxp, neel) == pytest.approx(0.0, abs=1e-12)
143
+
144
+
145
+ def test_invalid_weight_uniform_state_matches_complement_of_valid_dim(pxp):
146
+ uniform = np.ones(pxp["dim"], dtype=np.complex128) / np.sqrt(pxp["dim"])
147
+ expected = 1.0 - pxp["valid_dim"] / pxp["dim"]
148
+ assert qs._invalid_weight(pxp, uniform) == pytest.approx(expected, abs=1e-9)