Path: blob/main/qec101/Images/toric/solution2_button.py
1125 views
# reveal_cudaq.py1import ipywidgets as widgets2from IPython.display import display, Markdown3import runpy, tempfile, os45_NV_GREEN = "#76b900"67_CODE_pre = """\8L=3910stabilizers_z = [] # Plaquette stabilizers that flag Z errors11for row in range(L):12for col in range(L):13stabilizers_z.append(L*row + col+L**2) #left14stabilizers_z.append(L*row + col ) #top15stabilizers_z.append((L*row + (col+1)%L)+L**2) #right16stabilizers_z.append(L*((row + 1)%L) +col) #bottom171819stabilizers_x = [] # vertex stabilizers that flag X errors20for row in range(L):21for col in range(L):22stabilizers_x.append( (L*row + (col -1 )%L)) #left23stabilizers_x.append(L*((row-1)%L) + col + L**2) #top24stabilizers_x.append(L*row +col) #right25stabilizers_x.append(L*row +col + L**2 ) #bottom26"""2728# your example as a raw‐string29_CODE = """\30import cudaq31@cudaq.kernel32def toric(plaq: list[int], vert: list[int]):33data = cudaq.qvector(18) #first horizontal then vertical34anc = cudaq.qvector(18)353637for x in range(9): #loops over 9 plaquette stabilizers38h(anc[x])39for i in range(4): # loops over data qubit index in stabilizer40x.ctrl(anc[x], data[plaq[4*x+i]]) #41h(anc[x])4243for x in range(9):#loops over 9 vertex stabilizers44for i in range(4):45x.ctrl( data[vert[4*x+i]],anc[x+9])464748anc0 = mz(anc) # saves round 0 measurements49reset(anc) # resets only ancilla measurements5051for x in range(9):52h(anc[x])53for i in range(4):54x.ctrl(anc[x], data[plaq[4*x+i]])55h(anc[x])5657for x in range(9):58for i in range(4):59x.ctrl( data[vert[4*x+i]],anc[x+9])60616263anc1 = mz(anc)64reset(anc)6566for x in range(9):67h(anc[x])68for i in range(4):69x.ctrl(anc[x], data[plaq[4*x+i]])70h(anc[x])7172for x in range(9):73for i in range(4):74x.ctrl( data[vert[4*x+i]],anc[x+9])757677anc2 = mz(anc)78reset(anc)798081for x in range(9):82h(anc[x])83for i in range(4):84x.ctrl(anc[x], data[plaq[4*x+i]])85h(anc[x])8687for x in range(9):88for i in range(4):89x.ctrl( data[vert[4*x+i]],anc[x+9])909192anc3 = mz(anc)93reset(anc)949596data_qubits=mz(data)979899100cudaq.set_target('stim')101102results = cudaq.sample(toric,stabilizers_z, stabilizers_x,shots_count=1)103104print(results.get_register_counts("anc0"))105print(results.get_register_counts("anc1"))106print(results.get_register_counts("anc2"))107print(results.get_register_counts("anc3"))108print(results.get_register_counts("data_qubits"))109"""110111def show_cudaq_solution():112"""113Returns a Reveal‐Answer widget that displays the CUDA‑Q code114and then runs it, showing its printed output.115"""116btn = widgets.Button(117description="Reveal Answer",118style={"button_color": _NV_GREEN, "font_weight": "bold"},119layout=widgets.Layout(width="180px")120)121out = widgets.Output()122123def _on_click(_):124out.clear_output()125with out:126# 1) show the source127display(Markdown("```python\n" + _CODE + "\n```"))128# 2) write to a temp .py file so inspect.getsource will succeed129fd, path = tempfile.mkstemp(suffix=".py")130with os.fdopen(fd, "w") as f:131f.write(_CODE_pre)132f.write(_CODE)133try:134# 3) execute that file as __main__135runpy.run_path(path, run_name="__main__")136finally:137os.remove(path)138139btn.on_click(_on_click)140return widgets.VBox([btn, out])141142143