Tatopenn commited on
Commit
3098cd3
·
verified ·
1 Parent(s): f48799e

Sync test_interactive_panel.py from v8.1.33

Browse files
Files changed (1) hide show
  1. test_interactive_panel.py +116 -0
test_interactive_panel.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for dashboard_core.interactive_panel.launch_interactive_panel — the
3
+ ipywidgets panel (Colab/Jupyter-native, no Streamlit) added for users who
4
+ stay inside a notebook and never leave for a Streamlit tunnel link.
5
+
6
+ Simulates real widget interaction (Button.click(), not calling the
7
+ internal callback directly) to exercise the actual ipywidgets event
8
+ wiring, not just the underlying dashboard_core calls (already covered
9
+ by test_dashboard_core.py/test_mitigation_runner.py).
10
+
11
+ Deliberately does NOT assert on Output.outputs (checked and confirmed:
12
+ ipywidgets.widgets.widget_output.Output.__enter__/__exit__ only route
13
+ display() calls into .outputs via a real Jupyter kernel's comm/message
14
+ protocol -- ip.kernel.get_parent(), not any in-process Python capture.
15
+ Outside a genuine running kernel (plain pytest, even with
16
+ IPython.testing.globalipapp's InteractiveShell active) .outputs stays
17
+ permanently empty regardless of whether the code is correct -- verified
18
+ directly. What IS meaningful here and checked below: no exception is
19
+ raised and the run reaches its final "Fatto" status.
20
+ """
21
+
22
+ import matplotlib
23
+ matplotlib.use("Agg")
24
+
25
+ import pytest
26
+
27
+ import dashboard_core as dc
28
+
29
+ pytest.importorskip("ipywidgets")
30
+
31
+
32
+ def _find(widgets_list, description):
33
+ for w in widgets_list:
34
+ if getattr(w, "description", None) == description:
35
+ return w
36
+ raise KeyError(description)
37
+
38
+
39
+ def _panel_widgets(panel):
40
+ sidebar, tabs = panel.children
41
+ kids = sidebar.children
42
+ run_hbox = kids[-1]
43
+ w_run, w_status = run_hbox.children
44
+ return kids, tabs, w_run, w_status
45
+
46
+
47
+ def test_launch_interactive_panel_builds():
48
+ panel = dc.launch_interactive_panel()
49
+ assert type(panel).__name__ == "VBox"
50
+ sidebar, tabs = panel.children
51
+ assert type(tabs).__name__ == "Tab"
52
+ assert len(tabs.children) == 9
53
+ assert list(tabs.titles) == [
54
+ "Overview", "Fisica Stato", "Mosaico", "VQE Results", "MD Results",
55
+ "Performance", "3D Helix", "Hamiltonian", "Mitigation (ZNE)",
56
+ ]
57
+
58
+
59
+ def test_run_button_ideal_circuit_populates_all_tabs():
60
+ panel = dc.launch_interactive_panel()
61
+ kids, tabs, w_run, w_status = _panel_widgets(panel)
62
+
63
+ w_vqe_enabled = _find(kids, "Abilita telemetria VQE")
64
+ w_md_enabled = _find(kids, "Abilita telemetria MD")
65
+ w_vqe_enabled.value = False # keep the test fast -- VQE/MD logic already
66
+ w_md_enabled.value = False # covered directly in test_dashboard_core.py
67
+
68
+ w_run.click()
69
+
70
+ assert "Fatto" in w_status.value
71
+ assert not w_run.disabled
72
+
73
+
74
+ def test_run_button_with_zne_and_healing():
75
+ panel = dc.launch_interactive_panel()
76
+ kids, tabs, w_run, w_status = _panel_widgets(panel)
77
+
78
+ _find(kids, "Abilita telemetria VQE").value = False
79
+ _find(kids, "Abilita telemetria MD").value = False
80
+ _find(kids, "Rumore:").value = "depolarizing"
81
+ _find(kids, "p:").value = 0.06
82
+ _find(kids, "Abilita Zero-Noise Extrapolation").value = True
83
+ _find(kids, "Healing predittivo (Δpre_emp-adapted)").value = True
84
+
85
+ w_run.click()
86
+
87
+ assert "Fatto" in w_status.value
88
+
89
+
90
+ def test_run_button_ideal_with_zne_shows_warning_not_crash():
91
+ panel = dc.launch_interactive_panel()
92
+ kids, tabs, w_run, w_status = _panel_widgets(panel)
93
+
94
+ _find(kids, "Abilita telemetria VQE").value = False
95
+ _find(kids, "Abilita telemetria MD").value = False
96
+ _find(kids, "Abilita Zero-Noise Extrapolation").value = True
97
+ # noise_model stays 'ideal' (default) -- ZNE has nothing to extrapolate
98
+
99
+ w_run.click() # must not raise
100
+
101
+ assert "Fatto" in w_status.value
102
+
103
+
104
+ def test_run_button_vqe_and_md_together():
105
+ # the heavier, slower path -- real parametric VQE + MD telemetry +
106
+ # AI healing shield on both, exercised once end-to-end
107
+ panel = dc.launch_interactive_panel()
108
+ kids, tabs, w_run, w_status = _panel_widgets(panel)
109
+
110
+ _find(kids, "Circuito:").value = "VQE ansatz H₂"
111
+ _find(kids, "Epochs:").value = 4
112
+ _find(kids, "MD steps:").value = 10
113
+
114
+ w_run.click()
115
+
116
+ assert "Fatto" in w_status.value