Path: blob/main/qec101/Images/noisy/trajectory_widget.py
1128 views
import ipywidgets as widgets1from IPython.display import display23# NVIDIA green4_NV_GREEN = "#76b900"56TREE_STYLE = f"""7<style>8.tree-wrapper {{9width: 100%; /* full page width */10overflow: hidden; /* no scrollbar */11}}12.tree {{13transform: scale(0.75); /* shrink to 75% */14transform-origin: top left;15}}16.tree ul {{ position: relative; padding-top: 20px; white-space: nowrap; }}17.tree li {{18display: inline-block; vertical-align: top; text-align: center;19list-style: none; position: relative; padding: 15px 3px 0;20}}21.tree li::before, .tree li::after {{22content: ''; position: absolute; top: 0; right:50%;23border-top:1px solid #ccc; width:50%; height:20px;24}}25.tree li::after {{26left:50%; right:auto; border-left:1px solid #ccc;27}}28.tree li:only-child::before, .tree li:only-child::after {{ display:none; }}29.tree li:first-child::before, .tree li:last-child::after {{ border:none; }}30.tree li:last-child::before {{31border-right:1px solid #ccc; border-radius:0 5px 0 0;32}}33.tree li:first-child::after {{ border-radius:5px 0 0 0; }}34.tree ul ul::before {{35content:''; position:absolute; top:0; left:50%;36border-left:1px solid #ccc; height:20px;37}}38.node {{39border:1px solid #ccc; background:#f5f5f5;40padding:8px 12px; border-radius:5px;41display:inline-block; transition:all 0.3s;42font-size: 14px; /* main text bigger */43}}44.node small {{45display:block; /* force prob onto its own line */46margin-top:4px;47font-size:12px; /* prob text */48color: #333;49}}50.active > .node {{51background:{_NV_GREEN};52border-color:{_NV_GREEN};53color:#fff;54}}55</style>56"""5758def show_error_tree_widget():59p_slider = widgets.FloatSlider(60description="P(error):", min=0.0, max=0.20, step=0.01, value=0.1,61continuous_update=False62)63q1 = widgets.ToggleButtons(description="Qubit 1:", options=[("No Error",0),("Error",1)])64q2 = widgets.ToggleButtons(description="Qubit 2:", options=[("No Error",0),("Error",1)])65q3 = widgets.ToggleButtons(description="Qubit 3:", options=[("No Error",0),("Error",1)])66tree_out = widgets.HTML()6768def _redraw(change=None):69pp = p_slider.value70html = TREE_STYLE71html += "<div class='tree-wrapper'><div class='tree'><ul>"72for b1 in (0,1):73a1 = (b1 == q1.value)74html += f"<li class='{'active' if a1 else ''}'>"75html += f"<div class='node'>Q1={b1}</div><ul>"76for b2 in (0,1):77a2 = a1 and (b2 == q2.value)78html += f"<li class='{'active' if a2 else ''}'>"79html += f"<div class='node'>Q2={b2}</div><ul>"80for b3 in (0,1):81a3 = a2 and (b3 == q3.value)82n = b1 + b2 + b383k = 3 - n84prob = (pp**n) * ((1-pp)**k)85leaf_html = (86f"{b1}{b2}{b3}"87f"<small>P(state) = {prob:.3f}</small>"88)89html += f"<li class='{'active' if a3 else ''}'>"90html += f"<div class='node'>{leaf_html}</div></li>"91html += "</ul></li>"92html += "</ul></li>"93html += "</ul></div></div>"94tree_out.value = html9596for w in (p_slider, q1, q2, q3):97w.observe(_redraw, names="value")98_redraw()99100return widgets.VBox([p_slider, q1, q2, q3, tree_out])101102103