Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
NVIDIA
GitHub Repository: NVIDIA/cuda-q-academic
Path: blob/main/qec101/Images/toric/solution2_button.py
1125 views
1
# reveal_cudaq.py
2
import ipywidgets as widgets
3
from IPython.display import display, Markdown
4
import runpy, tempfile, os
5
6
_NV_GREEN = "#76b900"
7
8
_CODE_pre = """\
9
L=3
10
11
stabilizers_z = [] # Plaquette stabilizers that flag Z errors
12
for row in range(L):
13
for col in range(L):
14
stabilizers_z.append(L*row + col+L**2) #left
15
stabilizers_z.append(L*row + col ) #top
16
stabilizers_z.append((L*row + (col+1)%L)+L**2) #right
17
stabilizers_z.append(L*((row + 1)%L) +col) #bottom
18
19
20
stabilizers_x = [] # vertex stabilizers that flag X errors
21
for row in range(L):
22
for col in range(L):
23
stabilizers_x.append( (L*row + (col -1 )%L)) #left
24
stabilizers_x.append(L*((row-1)%L) + col + L**2) #top
25
stabilizers_x.append(L*row +col) #right
26
stabilizers_x.append(L*row +col + L**2 ) #bottom
27
"""
28
29
# your example as a raw‐string
30
_CODE = """\
31
import cudaq
32
@cudaq.kernel
33
def toric(plaq: list[int], vert: list[int]):
34
data = cudaq.qvector(18) #first horizontal then vertical
35
anc = cudaq.qvector(18)
36
37
38
for x in range(9): #loops over 9 plaquette stabilizers
39
h(anc[x])
40
for i in range(4): # loops over data qubit index in stabilizer
41
x.ctrl(anc[x], data[plaq[4*x+i]]) #
42
h(anc[x])
43
44
for x in range(9):#loops over 9 vertex stabilizers
45
for i in range(4):
46
x.ctrl( data[vert[4*x+i]],anc[x+9])
47
48
49
anc0 = mz(anc) # saves round 0 measurements
50
reset(anc) # resets only ancilla measurements
51
52
for x in range(9):
53
h(anc[x])
54
for i in range(4):
55
x.ctrl(anc[x], data[plaq[4*x+i]])
56
h(anc[x])
57
58
for x in range(9):
59
for i in range(4):
60
x.ctrl( data[vert[4*x+i]],anc[x+9])
61
62
63
64
anc1 = mz(anc)
65
reset(anc)
66
67
for x in range(9):
68
h(anc[x])
69
for i in range(4):
70
x.ctrl(anc[x], data[plaq[4*x+i]])
71
h(anc[x])
72
73
for x in range(9):
74
for i in range(4):
75
x.ctrl( data[vert[4*x+i]],anc[x+9])
76
77
78
anc2 = mz(anc)
79
reset(anc)
80
81
82
for x in range(9):
83
h(anc[x])
84
for i in range(4):
85
x.ctrl(anc[x], data[plaq[4*x+i]])
86
h(anc[x])
87
88
for x in range(9):
89
for i in range(4):
90
x.ctrl( data[vert[4*x+i]],anc[x+9])
91
92
93
anc3 = mz(anc)
94
reset(anc)
95
96
97
data_qubits=mz(data)
98
99
100
101
cudaq.set_target('stim')
102
103
results = cudaq.sample(toric,stabilizers_z, stabilizers_x,shots_count=1)
104
105
print(results.get_register_counts("anc0"))
106
print(results.get_register_counts("anc1"))
107
print(results.get_register_counts("anc2"))
108
print(results.get_register_counts("anc3"))
109
print(results.get_register_counts("data_qubits"))
110
"""
111
112
def show_cudaq_solution():
113
"""
114
Returns a Reveal‐Answer widget that displays the CUDA‑Q code
115
and then runs it, showing its printed output.
116
"""
117
btn = widgets.Button(
118
description="Reveal Answer",
119
style={"button_color": _NV_GREEN, "font_weight": "bold"},
120
layout=widgets.Layout(width="180px")
121
)
122
out = widgets.Output()
123
124
def _on_click(_):
125
out.clear_output()
126
with out:
127
# 1) show the source
128
display(Markdown("```python\n" + _CODE + "\n```"))
129
# 2) write to a temp .py file so inspect.getsource will succeed
130
fd, path = tempfile.mkstemp(suffix=".py")
131
with os.fdopen(fd, "w") as f:
132
f.write(_CODE_pre)
133
f.write(_CODE)
134
try:
135
# 3) execute that file as __main__
136
runpy.run_path(path, run_name="__main__")
137
finally:
138
os.remove(path)
139
140
btn.on_click(_on_click)
141
return widgets.VBox([btn, out])
142
143