Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
NVIDIA
GitHub Repository: NVIDIA/cuda-q-academic
Path: blob/main/qec101/Images/toric/solution3_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)
35
anc = cudaq.qvector(18)
36
37
38
for x in range(9):
39
h(anc[x])
40
for i in range(4):
41
x.ctrl(anc[x], data[plaq[4*x+i]])
42
h(anc[x])
43
44
for x in range(9):
45
for i in range(4):
46
x.ctrl(data[vert[4*x+i]],anc[x+9])
47
48
49
mz(anc)
50
reset(anc)
51
52
#x(data[6],data[7],data[8]) # X1 L
53
#x(data[10],data[13],data[16]) # X2 L
54
55
#z(data[12],data[13],data[14]) # Z1 L
56
#z(data[2],data[5],data[8]) # Z2 L
57
58
d = mz(data)
59
60
61
62
cudaq.set_target('stim')
63
64
results = cudaq.sample(toric,stabilizers_z, stabilizers_x,shots_count=1000)
65
66
67
def count_summed_bits_at_indices(bit_dict, indices):
68
69
zero_count = 0
70
one_count = 0
71
72
for bitstring, count in bit_dict.items():
73
# Compute sum of bits at specified indices
74
bit_sum = sum(int(bitstring[i]) for i in indices) % 2
75
# If bit_sum is 1, increment one_count; else increment zero_count
76
if bit_sum == 1:
77
one_count += count
78
else:
79
zero_count += count
80
81
return zero_count, one_count
82
83
84
85
logical_x1 = [0,1,2] # L to R horizontal qubits
86
logical_x2 = [11,14,17] # T to B vertical qubits
87
88
89
logical_z1 = [9,10,11] # L to R vertical qubits
90
logical_z2 = [1,4,7] # T to B horizontal qubits
91
92
print("Result of measuring X1:")
93
print(count_summed_bits_at_indices(results, logical_x1))
94
print("Result of measuring X2:")
95
print(count_summed_bits_at_indices(results, logical_x2))
96
97
print("Result of measuring Z1:")
98
print(count_summed_bits_at_indices(results, logical_z1))
99
print("Result of measuring Z2:")
100
print(count_summed_bits_at_indices(results, logical_z2))
101
"""
102
103
def show_cudaq_solution():
104
"""
105
Returns a Reveal‐Answer widget that displays the CUDA‑Q code
106
and then runs it, showing its printed output.
107
"""
108
btn = widgets.Button(
109
description="Reveal Answer",
110
style={"button_color": _NV_GREEN, "font_weight": "bold"},
111
layout=widgets.Layout(width="180px")
112
)
113
out = widgets.Output()
114
115
def _on_click(_):
116
out.clear_output()
117
with out:
118
# 1) show the source
119
display(Markdown("```python\n" + _CODE + "\n```"))
120
# 2) write to a temp .py file so inspect.getsource will succeed
121
fd, path = tempfile.mkstemp(suffix=".py")
122
with os.fdopen(fd, "w") as f:
123
f.write(_CODE_pre)
124
f.write(_CODE)
125
try:
126
# 3) execute that file as __main__
127
runpy.run_path(path, run_name="__main__")
128
finally:
129
os.remove(path)
130
131
btn.on_click(_on_click)
132
return widgets.VBox([btn, out])
133
134