Show More
@@ -0,0 +1,1 b'' | |||
|
1 | {"cells":[{"cell_type":"text","text":"<h1>SymPy: Open Source Symbolic Mathematics</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from __future__ import division\nfrom sympy import *\nx, y, z = symbols(\"x y z\")\nk, m, n = symbols(\"k m n\", integer=True)\nf, g, h = map(Function, 'fgh')","cell_type":"code","prompt_number":2},{"cell_type":"text","text":"<h2>Elementary operations</h2>"},{"code":"Rational(3,2)*pi + exp(I*x) / (x**2 + y)\n","cell_type":"code","prompt_number":3},{"code":"exp(I*x).subs(x,pi).evalf()\n","cell_type":"code","prompt_number":4},{"code":"e = x + 2*y","cell_type":"code","prompt_number":5},{"code":"srepr(e)","cell_type":"code","prompt_number":6},{"code":"exp(pi * sqrt(163)).evalf(50)","cell_type":"code","prompt_number":7},{"cell_type":"text","text":"<h2>Algebra<h2>"},{"code":"((x+y)**2 * (x+1)).expand()","cell_type":"code","prompt_number":8},{"code":"a = 1/x + (x*sin(x) - 1)/x\ndisplay(a)\nsimplify(a)","cell_type":"code","prompt_number":9},{"code":"solve(Eq(x**3 + 2*x**2 + 4*x + 8, 0), x)","cell_type":"code","prompt_number":10},{"code":"a, b = symbols('a b')\nSum(6*n**2 + 2**n, (n, a, b))","cell_type":"code","prompt_number":11},{"cell_type":"text","text":"<h2>Calculus</h2>"},{"code":"limit((sin(x)-x)/x**3, x, 0)","cell_type":"code","prompt_number":12},{"code":"(1/cos(x)).series(x, 0, 6)","cell_type":"code","prompt_number":13},{"code":"diff(cos(x**2)**2 / (1+x), x)","cell_type":"code","prompt_number":14},{"code":"integrate(x**2 * cos(x), (x, 0, pi/2))","cell_type":"code","prompt_number":16},{"code":"eqn = Eq(Derivative(f(x),x,x) + 9*f(x), 1)\ndisplay(eqn)\ndsolve(eqn, f(x))","cell_type":"code","prompt_number":17},{"code":"%notebook save sympy.ipynb","cell_type":"code","prompt_number":40},{"code":"%notebook load basic_quantum.ipynb","cell_type":"code","prompt_number":41}]} No newline at end of file |
@@ -1,1053 +1,1059 b'' | |||
|
1 | 1 | var IPYTHON = {}; |
|
2 | 2 | |
|
3 | 3 | |
|
4 | 4 | //============================================================================ |
|
5 | 5 | // Utilities |
|
6 | 6 | //============================================================================ |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | var uuid = function () { |
|
10 | 10 | // http://www.ietf.org/rfc/rfc4122.txt |
|
11 | 11 | var s = []; |
|
12 | 12 | var hexDigits = "0123456789ABCDEF"; |
|
13 | 13 | for (var i = 0; i < 32; i++) { |
|
14 | 14 | s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); |
|
15 | 15 | } |
|
16 | 16 | s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 |
|
17 | 17 | s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 |
|
18 | 18 | |
|
19 | 19 | var uuid = s.join(""); |
|
20 | 20 | return uuid; |
|
21 | 21 | }; |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | //Fix raw text to parse correctly in crazy XML |
|
25 | 25 | function xmlencode(string) { |
|
26 | 26 | return string.replace(/\&/g,'&'+'amp;') |
|
27 | 27 | .replace(/</g,'&'+'lt;') |
|
28 | 28 | .replace(/>/g,'&'+'gt;') |
|
29 | 29 | .replace(/\'/g,'&'+'apos;') |
|
30 | 30 | .replace(/\"/g,'&'+'quot;') |
|
31 | 31 | .replace(/`/g,'&'+'#96;') |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | //Map from terminal commands to CSS classes |
|
35 | 35 | attrib = { |
|
36 | 36 | "30":"cblack", "31":"cred", |
|
37 | 37 | "32":"cgreen", "33":"cyellow", |
|
38 | 38 | "34":"cblue", "36":"ccyan", |
|
39 | 39 | "37":"cwhite", "01":"cbold"} |
|
40 | 40 | |
|
41 | 41 | //Fixes escaped console commands, IE colors. Turns them into HTML |
|
42 | 42 | function fixConsole(txt) { |
|
43 | 43 | txt = xmlencode(txt) |
|
44 | 44 | var re = /\033\[([\d;]*?)m/ |
|
45 | 45 | var opened = false |
|
46 | 46 | var cmds = [] |
|
47 | 47 | var opener = "" |
|
48 | 48 | var closer = "" |
|
49 | 49 | |
|
50 | 50 | while (re.test(txt)) { |
|
51 | 51 | var cmds = txt.match(re)[1].split(";") |
|
52 | 52 | closer = opened?"</span>":"" |
|
53 | 53 | opened = cmds.length > 1 || cmds[0] != 0 |
|
54 | 54 | var rep = [] |
|
55 | 55 | for (var i in cmds) |
|
56 | 56 | if (typeof(attrib[cmds[i]]) != "undefined") |
|
57 | 57 | rep.push(attrib[cmds[i]]) |
|
58 | 58 | opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":"" |
|
59 | 59 | txt = txt.replace(re, closer + opener) |
|
60 | 60 | } |
|
61 | 61 | if (opened) txt += "</span>" |
|
62 | 62 | return txt.trim() |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | //============================================================================ |
|
67 | 67 | // Notebook |
|
68 | 68 | //============================================================================ |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | var Notebook = function (selector) { |
|
72 | 72 | this.element = $(selector); |
|
73 | 73 | this.element.scroll(); |
|
74 | 74 | this.element.data("notebook", this); |
|
75 | 75 | this.next_prompt_number = 1; |
|
76 | 76 | this.kernel = null; |
|
77 | 77 | this.msg_cell_map = {}; |
|
78 | 78 | this.filename = null; |
|
79 | 79 | this.notebook_load_re = /%notebook load/ |
|
80 | 80 | this.notebook_save_re = /%notebook save/ |
|
81 | 81 | this.notebook_filename_re = /(\w)+.ipynb/ |
|
82 | 82 | this.bind_events(); |
|
83 | 83 | this.start_kernel(); |
|
84 | 84 | }; |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | Notebook.prototype.bind_events = function () { |
|
88 | 88 | var that = this; |
|
89 | 89 | $(document).keydown(function (event) { |
|
90 | 90 | // console.log(event); |
|
91 | 91 | if (event.which == 38 && event.shiftKey) { |
|
92 | 92 | event.preventDefault(); |
|
93 | 93 | that.select_prev(); |
|
94 | 94 | } else if (event.which == 40 && event.shiftKey) { |
|
95 | 95 | event.preventDefault(); |
|
96 | 96 | that.select_next(); |
|
97 | 97 | } else if (event.which == 13 && event.shiftKey) { |
|
98 | 98 | // The focus is not quite working here. |
|
99 | 99 | var cell = that.selected_cell(); |
|
100 | 100 | var cell_index = that.find_cell_index(cell); |
|
101 | 101 | // TODO: the logic here needs to be moved into appropriate |
|
102 | 102 | // methods of Notebook. |
|
103 | 103 | if (cell instanceof CodeCell) { |
|
104 | 104 | event.preventDefault(); |
|
105 | 105 | cell.clear_output(); |
|
106 | 106 | var code = cell.get_code(); |
|
107 | 107 | if (that.notebook_load_re.test(code)) { |
|
108 | 108 | var code_parts = code.split(' '); |
|
109 | 109 | if (code_parts.length === 3) { |
|
110 | 110 | that.load_notebook(code_parts[2]); |
|
111 | 111 | }; |
|
112 | 112 | } else if (that.notebook_save_re.test(code)) { |
|
113 | 113 | var code_parts = code.split(' '); |
|
114 | 114 | if (code_parts.length === 3) { |
|
115 | 115 | that.save_notebook(code_parts[2]); |
|
116 | 116 | } else { |
|
117 | 117 | that.save_notebook() |
|
118 | 118 | }; |
|
119 | 119 | } else { |
|
120 | 120 | var msg_id = that.kernel.execute(cell.get_code()); |
|
121 | 121 | that.msg_cell_map[msg_id] = cell.cell_id; |
|
122 | 122 | }; |
|
123 | 123 | if (cell_index === (that.ncells()-1)) { |
|
124 | 124 | that.insert_code_cell_after(); |
|
125 | 125 | } else { |
|
126 | 126 | // Select the next cell if it is a CodeCell, but not |
|
127 | 127 | // if it is a TextCell. |
|
128 | 128 | var next_cell = that.cells()[cell_index+1]; |
|
129 | 129 | if (!(next_cell instanceof TextCell)) { |
|
130 | 130 | that.select(cell_index+1); |
|
131 | 131 | }; |
|
132 | 132 | }; |
|
133 | 133 | } |
|
134 | 134 | } else if (event.which == 9) { |
|
135 | 135 | event.preventDefault(); |
|
136 | 136 | var cell = that.selected_cell(); |
|
137 | 137 | if (cell instanceof CodeCell) { |
|
138 | 138 | var ta = cell.element.find("textarea.input_textarea"); |
|
139 | 139 | ta.val(ta.val() + " "); |
|
140 | 140 | }; |
|
141 | 141 | }; |
|
142 | 142 | }); |
|
143 | 143 | }; |
|
144 | 144 | |
|
145 | 145 | |
|
146 | 146 | // Cell indexing, retrieval, etc. |
|
147 | 147 | |
|
148 | 148 | |
|
149 | 149 | Notebook.prototype.cell_elements = function () { |
|
150 | 150 | return this.element.children("div.cell"); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | Notebook.prototype.ncells = function (cell) { |
|
155 | 155 | return this.cell_elements().length; |
|
156 | 156 | } |
|
157 | 157 | |
|
158 | 158 | |
|
159 | 159 | // TODO: we are often calling cells as cells()[i], which we should optimize |
|
160 | 160 | // to cells(i) or a new method. |
|
161 | 161 | Notebook.prototype.cells = function () { |
|
162 | 162 | return this.cell_elements().toArray().map(function (e) { |
|
163 | 163 | return $(e).data("cell"); |
|
164 | 164 | }); |
|
165 | 165 | } |
|
166 | 166 | |
|
167 | 167 | |
|
168 | 168 | Notebook.prototype.find_cell_index = function (cell) { |
|
169 | 169 | var result = null; |
|
170 | 170 | this.cell_elements().filter(function (index) { |
|
171 | 171 | if ($(this).data("cell") === cell) { |
|
172 | 172 | result = index; |
|
173 | 173 | }; |
|
174 | 174 | }); |
|
175 | 175 | return result; |
|
176 | 176 | }; |
|
177 | 177 | |
|
178 | 178 | |
|
179 | 179 | Notebook.prototype.index_or_selected = function (index) { |
|
180 | 180 | return index || this.selected_index() || 0; |
|
181 | 181 | } |
|
182 | 182 | |
|
183 | 183 | |
|
184 | 184 | Notebook.prototype.select = function (index) { |
|
185 | 185 | if (index !== undefined && index >= 0 && index < this.ncells()) { |
|
186 | 186 | if (this.selected_index() !== null) { |
|
187 | 187 | this.selected_cell().unselect(); |
|
188 | 188 | }; |
|
189 | 189 | this.cells()[index].select(); |
|
190 | 190 | }; |
|
191 | 191 | return this; |
|
192 | 192 | }; |
|
193 | 193 | |
|
194 | 194 | |
|
195 | 195 | Notebook.prototype.select_next = function () { |
|
196 | 196 | var index = this.selected_index(); |
|
197 | 197 | if (index !== null && index >= 0 && (index+1) < this.ncells()) { |
|
198 | 198 | this.select(index+1); |
|
199 | 199 | }; |
|
200 | 200 | return this; |
|
201 | 201 | }; |
|
202 | 202 | |
|
203 | 203 | |
|
204 | 204 | Notebook.prototype.select_prev = function () { |
|
205 | 205 | var index = this.selected_index(); |
|
206 | 206 | if (index !== null && index >= 0 && (index-1) < this.ncells()) { |
|
207 | 207 | this.select(index-1); |
|
208 | 208 | }; |
|
209 | 209 | return this; |
|
210 | 210 | }; |
|
211 | 211 | |
|
212 | 212 | |
|
213 | 213 | Notebook.prototype.selected_index = function () { |
|
214 | 214 | var result = null; |
|
215 | 215 | this.cell_elements().filter(function (index) { |
|
216 | 216 | if ($(this).data("cell").selected === true) { |
|
217 | 217 | result = index; |
|
218 | 218 | }; |
|
219 | 219 | }); |
|
220 | 220 | return result; |
|
221 | 221 | }; |
|
222 | 222 | |
|
223 | 223 | |
|
224 | 224 | Notebook.prototype.cell_for_msg = function (msg_id) { |
|
225 | 225 | var cell_id = this.msg_cell_map[msg_id]; |
|
226 | 226 | var result = null; |
|
227 | 227 | this.cell_elements().filter(function (index) { |
|
228 | 228 | cell = $(this).data("cell"); |
|
229 | 229 | if (cell.cell_id === cell_id) { |
|
230 | 230 | result = cell; |
|
231 | 231 | }; |
|
232 | 232 | }); |
|
233 | 233 | return result; |
|
234 | 234 | }; |
|
235 | 235 | |
|
236 | 236 | |
|
237 | 237 | Notebook.prototype.selected_cell = function () { |
|
238 | 238 | return this.cell_elements().eq(this.selected_index()).data("cell"); |
|
239 | 239 | } |
|
240 | 240 | |
|
241 | 241 | |
|
242 | 242 | // Cell insertion, deletion and moving. |
|
243 | 243 | |
|
244 | 244 | |
|
245 | 245 | Notebook.prototype.delete_cell = function (index) { |
|
246 | 246 | var i = index || this.selected_index(); |
|
247 | 247 | if (i !== null && i >= 0 && i < this.ncells()) { |
|
248 | 248 | this.cell_elements().eq(i).remove(); |
|
249 | 249 | if (i === (this.ncells())) { |
|
250 | 250 | this.select(i-1); |
|
251 | 251 | } else { |
|
252 | 252 | this.select(i); |
|
253 | 253 | }; |
|
254 | 254 | }; |
|
255 | 255 | return this; |
|
256 | 256 | }; |
|
257 | 257 | |
|
258 | 258 | |
|
259 | 259 | Notebook.prototype.append_cell = function (cell) { |
|
260 | 260 | this.element.append(cell.element); |
|
261 | 261 | return this; |
|
262 | 262 | }; |
|
263 | 263 | |
|
264 | 264 | |
|
265 | 265 | Notebook.prototype.insert_cell_after = function (cell, index) { |
|
266 | 266 | var ncells = this.ncells(); |
|
267 | 267 | if (ncells === 0) { |
|
268 | 268 | this.append_cell(cell); |
|
269 | 269 | return this; |
|
270 | 270 | }; |
|
271 | 271 | if (index >= 0 && index < ncells) { |
|
272 | 272 | this.cell_elements().eq(index).after(cell.element); |
|
273 | 273 | }; |
|
274 | 274 | return this |
|
275 | 275 | }; |
|
276 | 276 | |
|
277 | 277 | |
|
278 | 278 | Notebook.prototype.insert_cell_before = function (cell, index) { |
|
279 | 279 | var ncells = this.ncells(); |
|
280 | 280 | if (ncells === 0) { |
|
281 | 281 | this.append_cell(cell); |
|
282 | 282 | return this; |
|
283 | 283 | }; |
|
284 | 284 | if (index >= 0 && index < ncells) { |
|
285 | 285 | this.cell_elements().eq(index).before(cell.element); |
|
286 | 286 | }; |
|
287 | 287 | return this; |
|
288 | 288 | }; |
|
289 | 289 | |
|
290 | 290 | |
|
291 | 291 | Notebook.prototype.move_cell_up = function (index) { |
|
292 | 292 | var i = index || this.selected_index(); |
|
293 | 293 | if (i !== null && i < this.ncells() && i > 0) { |
|
294 | 294 | var pivot = this.cell_elements().eq(i-1); |
|
295 | 295 | var tomove = this.cell_elements().eq(i); |
|
296 | 296 | if (pivot !== null && tomove !== null) { |
|
297 | 297 | tomove.detach(); |
|
298 | 298 | pivot.before(tomove); |
|
299 | 299 | this.select(i-1); |
|
300 | 300 | }; |
|
301 | 301 | }; |
|
302 | 302 | return this; |
|
303 | 303 | } |
|
304 | 304 | |
|
305 | 305 | |
|
306 | 306 | Notebook.prototype.move_cell_down = function (index) { |
|
307 | 307 | var i = index || this.selected_index(); |
|
308 | 308 | if (i !== null && i < (this.ncells()-1) && i >= 0) { |
|
309 | 309 | var pivot = this.cell_elements().eq(i+1) |
|
310 | 310 | var tomove = this.cell_elements().eq(i) |
|
311 | 311 | if (pivot !== null && tomove !== null) { |
|
312 | 312 | tomove.detach(); |
|
313 | 313 | pivot.after(tomove); |
|
314 | 314 | this.select(i+1); |
|
315 | 315 | }; |
|
316 | 316 | }; |
|
317 | 317 | return this; |
|
318 | 318 | } |
|
319 | 319 | |
|
320 | 320 | |
|
321 | 321 | Notebook.prototype.sort_cells = function () { |
|
322 | 322 | var ncells = this.ncells(); |
|
323 | 323 | var sindex = this.selected_index(); |
|
324 | 324 | var swapped; |
|
325 | 325 | do { |
|
326 | 326 | swapped = false |
|
327 | 327 | for (var i=1; i<ncells; i++) { |
|
328 | 328 | current = this.cell_elements().eq(i).data("cell"); |
|
329 | 329 | previous = this.cell_elements().eq(i-1).data("cell"); |
|
330 | 330 | if (previous.input_prompt_number > current.input_prompt_number) { |
|
331 | 331 | this.move_cell_up(i); |
|
332 | 332 | swapped = true; |
|
333 | 333 | }; |
|
334 | 334 | }; |
|
335 | 335 | } while (swapped); |
|
336 | 336 | this.select(sindex); |
|
337 | 337 | return this; |
|
338 | 338 | }; |
|
339 | 339 | |
|
340 | 340 | |
|
341 | 341 | Notebook.prototype.insert_code_cell_before = function (index) { |
|
342 | 342 | // TODO: Bounds check for i |
|
343 | 343 | var i = this.index_or_selected(index); |
|
344 | 344 | var cell = new CodeCell(this); |
|
345 | 345 | cell.set_input_prompt(this.next_prompt_number); |
|
346 | 346 | this.next_prompt_number = this.next_prompt_number + 1; |
|
347 | 347 | this.insert_cell_before(cell, i); |
|
348 | 348 | this.select(this.find_cell_index(cell)); |
|
349 | 349 | return this; |
|
350 | 350 | } |
|
351 | 351 | |
|
352 | 352 | |
|
353 | 353 | Notebook.prototype.insert_code_cell_after = function (index) { |
|
354 | 354 | // TODO: Bounds check for i |
|
355 | 355 | var i = this.index_or_selected(index); |
|
356 | 356 | var cell = new CodeCell(this); |
|
357 | 357 | cell.set_input_prompt(this.next_prompt_number); |
|
358 | 358 | this.next_prompt_number = this.next_prompt_number + 1; |
|
359 | 359 | this.insert_cell_after(cell, i); |
|
360 | 360 | this.select(this.find_cell_index(cell)); |
|
361 | 361 | return this; |
|
362 | 362 | } |
|
363 | 363 | |
|
364 | 364 | |
|
365 | 365 | Notebook.prototype.insert_text_cell_before = function (index) { |
|
366 | 366 | // TODO: Bounds check for i |
|
367 | 367 | var i = this.index_or_selected(index); |
|
368 | 368 | var cell = new TextCell(this); |
|
369 | 369 | cell.config_mathjax(); |
|
370 | 370 | this.insert_cell_before(cell, i); |
|
371 | 371 | this.select(this.find_cell_index(cell)); |
|
372 | 372 | return this; |
|
373 | 373 | } |
|
374 | 374 | |
|
375 | 375 | |
|
376 | 376 | Notebook.prototype.insert_text_cell_after = function (index) { |
|
377 | 377 | // TODO: Bounds check for i |
|
378 | 378 | var i = this.index_or_selected(index); |
|
379 | 379 | var cell = new TextCell(this); |
|
380 | 380 | cell.config_mathjax(); |
|
381 | 381 | this.insert_cell_after(cell, i); |
|
382 | 382 | this.select(this.find_cell_index(cell)); |
|
383 | 383 | return this; |
|
384 | 384 | } |
|
385 | 385 | |
|
386 | 386 | |
|
387 | 387 | Notebook.prototype.text_to_code = function (index) { |
|
388 | 388 | // TODO: Bounds check for i |
|
389 | 389 | var i = this.index_or_selected(index); |
|
390 | 390 | var source_element = this.cell_elements().eq(i); |
|
391 | 391 | var source_cell = source_element.data("cell"); |
|
392 | 392 | if (source_cell instanceof TextCell) { |
|
393 | 393 | this.insert_code_cell_after(i); |
|
394 | 394 | var target_cell = this.cells()[i+1]; |
|
395 | 395 | target_cell.set_code(source_cell.get_text()); |
|
396 | 396 | source_element.remove(); |
|
397 | 397 | }; |
|
398 | 398 | }; |
|
399 | 399 | |
|
400 | 400 | |
|
401 | 401 | Notebook.prototype.code_to_text = function (index) { |
|
402 | 402 | // TODO: Bounds check for i |
|
403 | 403 | var i = this.index_or_selected(index); |
|
404 | 404 | var source_element = this.cell_elements().eq(i); |
|
405 | 405 | var source_cell = source_element.data("cell"); |
|
406 | 406 | if (source_cell instanceof CodeCell) { |
|
407 | 407 | this.insert_text_cell_after(i); |
|
408 | 408 | var target_cell = this.cells()[i+1]; |
|
409 | 409 | var text = source_cell.get_code(); |
|
410 | 410 | if (text === "") {text = target_cell.placeholder;}; |
|
411 | 411 | target_cell.set_text(text); |
|
412 | 412 | source_element.remove(); |
|
413 | 413 | }; |
|
414 | 414 | }; |
|
415 | 415 | |
|
416 | 416 | |
|
417 | 417 | // Cell collapsing |
|
418 | 418 | |
|
419 | 419 | Notebook.prototype.collapse = function (index) { |
|
420 | 420 | var i = this.index_or_selected(index); |
|
421 | 421 | this.cells()[i].collapse(); |
|
422 | 422 | }; |
|
423 | 423 | |
|
424 | 424 | |
|
425 | 425 | Notebook.prototype.expand = function (index) { |
|
426 | 426 | var i = this.index_or_selected(index); |
|
427 | 427 | this.cells()[i].expand(); |
|
428 | 428 | }; |
|
429 | 429 | |
|
430 | 430 | |
|
431 | 431 | // Kernel related things |
|
432 | 432 | |
|
433 | 433 | Notebook.prototype.start_kernel = function () { |
|
434 | 434 | this.kernel = new Kernel(); |
|
435 | 435 | this.kernel.start_kernel(this._kernel_started, this); |
|
436 | 436 | }; |
|
437 | 437 | |
|
438 | 438 | |
|
439 | 439 | Notebook.prototype._kernel_started = function () { |
|
440 | 440 | console.log("Kernel started: ", this.kernel.kernel_id); |
|
441 | 441 | var that = this; |
|
442 | 442 | |
|
443 | 443 | this.kernel.shell_channel.onmessage = function (e) { |
|
444 | 444 | reply = $.parseJSON(e.data); |
|
445 | 445 | console.log(reply); |
|
446 | 446 | var msg_type = reply.msg_type; |
|
447 | 447 | var cell = that.cell_for_msg(reply.parent_header.msg_id); |
|
448 | 448 | if (msg_type === "execute_reply") { |
|
449 | 449 | cell.set_input_prompt(reply.content.execution_count); |
|
450 | 450 | }; |
|
451 | 451 | }; |
|
452 | 452 | |
|
453 | 453 | this.kernel.iopub_channel.onmessage = function (e) { |
|
454 | 454 | reply = $.parseJSON(e.data); |
|
455 | 455 | var content = reply.content; |
|
456 | 456 | console.log(reply); |
|
457 | 457 | var msg_type = reply.msg_type; |
|
458 | 458 | var cell = that.cell_for_msg(reply.parent_header.msg_id); |
|
459 | 459 | if (msg_type === "stream") { |
|
460 | 460 | cell.expand(); |
|
461 | 461 | cell.append_stream(content.data + "\n"); |
|
462 | 462 | } else if (msg_type === "display_data") { |
|
463 | 463 | cell.expand(); |
|
464 | 464 | cell.append_display_data(content.data); |
|
465 | 465 | } else if (msg_type === "pyout") { |
|
466 | 466 | cell.expand(); |
|
467 | 467 | cell.append_pyout(content.data, content.execution_count) |
|
468 | 468 | } else if (msg_type === "pyerr") { |
|
469 | 469 | cell.expand(); |
|
470 | 470 | cell.append_pyerr(content.ename, content.evalue, content.traceback); |
|
471 | 471 | } else if (msg_type === "status") { |
|
472 | 472 | if (content.execution_state === "busy") { |
|
473 | 473 | that.kernel.status_busy(); |
|
474 | 474 | } else if (content.execution_state === "idle") { |
|
475 | 475 | that.kernel.status_idle(); |
|
476 | 476 | }; |
|
477 | 477 | } |
|
478 | 478 | }; |
|
479 | 479 | }; |
|
480 | 480 | |
|
481 | 481 | |
|
482 | 482 | // Persistance and loading |
|
483 | 483 | |
|
484 | 484 | |
|
485 | 485 | Notebook.prototype.fromJSON = function (data) { |
|
486 | 486 | var ncells = this.ncells(); |
|
487 | 487 | for (var i=0; i<ncells; i++) { |
|
488 | 488 | // Always delete cell 0 as they get renumbered as they are deleted. |
|
489 | 489 | this.delete_cell(0); |
|
490 | 490 | }; |
|
491 | 491 | var new_cells = data.cells; |
|
492 | 492 | ncells = new_cells.length; |
|
493 | 493 | var cell_data = null; |
|
494 | 494 | for (var i=0; i<ncells; i++) { |
|
495 | 495 | cell_data = new_cells[i]; |
|
496 | 496 | if (cell_data.cell_type == 'code') { |
|
497 | 497 | this.insert_code_cell_after(); |
|
498 | 498 | this.selected_cell().fromJSON(cell_data); |
|
499 | 499 | } else if (cell_data.cell_type === 'text') { |
|
500 | 500 | this.insert_text_cell_after(); |
|
501 | 501 | this.selected_cell().fromJSON(cell_data); |
|
502 | 502 | }; |
|
503 | 503 | }; |
|
504 | 504 | }; |
|
505 | 505 | |
|
506 | 506 | |
|
507 | 507 | Notebook.prototype.toJSON = function () { |
|
508 | 508 | var cells = this.cells(); |
|
509 | 509 | var ncells = cells.length; |
|
510 | 510 | cell_array = new Array(ncells); |
|
511 | 511 | for (var i=0; i<ncells; i++) { |
|
512 | 512 | cell_array[i] = cells[i].toJSON(); |
|
513 | 513 | }; |
|
514 | 514 | json = { |
|
515 | 515 | cells : cell_array |
|
516 | 516 | }; |
|
517 | 517 | return json |
|
518 | 518 | }; |
|
519 | 519 | |
|
520 | 520 | |
|
521 | 521 | Notebook.prototype.test_filename = function (filename) { |
|
522 | 522 | if (this.notebook_filename_re.test(filename)) { |
|
523 | 523 | return true; |
|
524 | 524 | } else { |
|
525 | 525 | var bad_filename = $('<div/>'); |
|
526 | 526 | bad_filename.html( |
|
527 | 527 | "The filename you entered (" + filename + ") is not valid. Notebook filenames must have the following form: foo.ipynb" |
|
528 | 528 | ); |
|
529 | 529 | bad_filename.dialog({title: 'Invalid filename', modal: true}); |
|
530 | 530 | return false; |
|
531 | 531 | }; |
|
532 | 532 | }; |
|
533 | 533 | |
|
534 | 534 | Notebook.prototype.save_notebook = function (filename) { |
|
535 | 535 | this.filename = filename || this.filename || ''; |
|
536 | 536 | if (this.filename === '') { |
|
537 | 537 | var no_filename = $('<div/>'); |
|
538 | 538 | no_filename.html( |
|
539 | 539 | "This notebook has no filename, please specify a filename of the form: foo.ipynb" |
|
540 | 540 | ); |
|
541 | 541 | no_filename.dialog({title: 'Missing filename', modal: true}); |
|
542 | 542 | return; |
|
543 | 543 | } |
|
544 | 544 | if (!this.test_filename(this.filename)) {return;} |
|
545 | 545 | var thedata = this.toJSON(); |
|
546 | 546 | var settings = { |
|
547 | 547 | processData : false, |
|
548 | 548 | cache : false, |
|
549 | 549 | type : "PUT", |
|
550 | 550 | data : JSON.stringify(thedata), |
|
551 | 551 | success : function (data, status, xhr) {console.log(data);} |
|
552 | 552 | }; |
|
553 | 553 | $.ajax("/notebooks/" + this.filename, settings); |
|
554 | 554 | }; |
|
555 | 555 | |
|
556 | 556 | |
|
557 | 557 | Notebook.prototype.load_notebook = function (filename) { |
|
558 | 558 | if (!this.test_filename(filename)) {return;} |
|
559 | 559 | var that = this; |
|
560 | $.getJSON("/notebooks/" + filename, | |
|
561 | function (data, status, xhr) { | |
|
562 | that.fromJSON(data); | |
|
563 | that.filename = filename; | |
|
564 | that.kernel.restart(); | |
|
565 | } | |
|
566 | ); | |
|
560 | // We do the call with settings so we can set cache to false. | |
|
561 | var settings = { | |
|
562 | processData : false, | |
|
563 | cache : false, | |
|
564 | type : "GET", | |
|
565 | dataType : "json", | |
|
566 | success : function (data, status, xhr) { | |
|
567 | that.fromJSON(data); | |
|
568 | that.filename = filename; | |
|
569 | that.kernel.restart(); | |
|
570 | } | |
|
571 | }; | |
|
572 | $.ajax("/notebooks/" + filename, settings); | |
|
567 | 573 | } |
|
568 | 574 | |
|
569 | 575 | |
|
570 | 576 | //============================================================================ |
|
571 | 577 | // Cell |
|
572 | 578 | //============================================================================ |
|
573 | 579 | |
|
574 | 580 | |
|
575 | 581 | var Cell = function (notebook) { |
|
576 | 582 | this.notebook = notebook; |
|
577 | 583 | this.selected = false; |
|
578 | 584 | this.element; |
|
579 | 585 | this.create_element(); |
|
580 | 586 | if (this.element !== undefined) { |
|
581 | 587 | this.element.data("cell", this); |
|
582 | 588 | this.bind_events(); |
|
583 | 589 | } |
|
584 | 590 | this.cell_id = uuid(); |
|
585 | 591 | }; |
|
586 | 592 | |
|
587 | 593 | |
|
588 | 594 | Cell.prototype.grow = function(element) { |
|
589 | 595 | // Grow the cell by hand. This is used upon reloading from JSON, when the |
|
590 | 596 | // autogrow handler is not called. |
|
591 | 597 | var dom = element.get(0); |
|
592 | 598 | var lines_count = 0; |
|
593 | 599 | // modified split rule from |
|
594 | 600 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 |
|
595 | 601 | var lines = dom.value.split(/\r|\r\n|\n/); |
|
596 | 602 | lines_count = lines.length; |
|
597 | 603 | if (lines_count >= 1) { |
|
598 | 604 | dom.rows = lines_count; |
|
599 | 605 | } else { |
|
600 | 606 | dom.rows = 1; |
|
601 | 607 | } |
|
602 | 608 | }; |
|
603 | 609 | |
|
604 | 610 | |
|
605 | 611 | Cell.prototype.select = function () { |
|
606 | 612 | this.element.addClass('ui-widget-content ui-corner-all'); |
|
607 | 613 | this.selected = true; |
|
608 | 614 | // TODO: we need t test across browsers to see if both of these are needed. |
|
609 | 615 | // In the meantime, there should not be any harm in having them both. |
|
610 | 616 | this.element.find('textarea').trigger('focusin'); |
|
611 | 617 | this.element.find('textarea').trigger('focus'); |
|
612 | 618 | }; |
|
613 | 619 | |
|
614 | 620 | |
|
615 | 621 | Cell.prototype.unselect = function () { |
|
616 | 622 | this.element.removeClass('ui-widget-content ui-corner-all'); |
|
617 | 623 | this.selected = false; |
|
618 | 624 | }; |
|
619 | 625 | |
|
620 | 626 | |
|
621 | 627 | Cell.prototype.bind_events = function () { |
|
622 | 628 | var that = this; |
|
623 | 629 | var nb = that.notebook |
|
624 | 630 | that.element.click(function (event) { |
|
625 | 631 | if (that.selected === false) { |
|
626 | 632 | nb.select(nb.find_cell_index(that)); |
|
627 | 633 | }; |
|
628 | 634 | }); |
|
629 | 635 | that.element.focusin(function (event) { |
|
630 | 636 | if (that.selected === false) { |
|
631 | 637 | nb.select(nb.find_cell_index(that)); |
|
632 | 638 | }; |
|
633 | 639 | }); |
|
634 | 640 | }; |
|
635 | 641 | |
|
636 | 642 | |
|
637 | 643 | // Subclasses must implement create_element. |
|
638 | 644 | Cell.prototype.create_element = function () {}; |
|
639 | 645 | |
|
640 | 646 | |
|
641 | 647 | //============================================================================ |
|
642 | 648 | // CodeCell |
|
643 | 649 | //============================================================================ |
|
644 | 650 | |
|
645 | 651 | |
|
646 | 652 | var CodeCell = function (notebook) { |
|
647 | 653 | Cell.apply(this, arguments); |
|
648 | 654 | this.input_prompt_number = ' '; |
|
649 | 655 | }; |
|
650 | 656 | |
|
651 | 657 | |
|
652 | 658 | CodeCell.prototype = new Cell(); |
|
653 | 659 | |
|
654 | 660 | |
|
655 | 661 | CodeCell.prototype.create_element = function () { |
|
656 | 662 | var cell = $('<div></div>').addClass('cell code_cell'); |
|
657 | 663 | var input = $('<div></div>').addClass('input'); |
|
658 | 664 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
659 | 665 | var input_textarea = $('<textarea/>').addClass('input_textarea').attr('rows',1).attr('wrap','hard').autogrow(); |
|
660 | 666 | input.append($('<div/>').addClass('input_area').append(input_textarea)); |
|
661 | 667 | var output = $('<div></div>').addClass('output'); |
|
662 | 668 | cell.append(input).append(output); |
|
663 | 669 | this.element = cell; |
|
664 | 670 | this.collapse() |
|
665 | 671 | }; |
|
666 | 672 | |
|
667 | 673 | |
|
668 | 674 | CodeCell.prototype.append_pyout = function (data, n) { |
|
669 | 675 | var toinsert = $("<div/>").addClass("output_area output_pyout"); |
|
670 | 676 | toinsert.append($('<div/>'). |
|
671 | 677 | addClass('prompt output_prompt'). |
|
672 | 678 | html('Out[' + n + ']:') |
|
673 | 679 | ); |
|
674 | 680 | this.append_display_data(data, toinsert); |
|
675 | 681 | toinsert.children().last().addClass("box_flex1"); |
|
676 | 682 | this.element.find("div.output").append(toinsert); |
|
677 | 683 | // If we just output latex, typeset it. |
|
678 | 684 | if (data["text/latex"] !== undefined) { |
|
679 | 685 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
680 | 686 | }; |
|
681 | 687 | }; |
|
682 | 688 | |
|
683 | 689 | |
|
684 | 690 | CodeCell.prototype.append_pyerr = function (ename, evalue, tb) { |
|
685 | 691 | var s = ''; |
|
686 | 692 | var len = tb.length; |
|
687 | 693 | for (var i=0; i<len; i++) { |
|
688 | 694 | s = s + tb[i] + '\n'; |
|
689 | 695 | } |
|
690 | 696 | s = s + '\n'; |
|
691 | 697 | this.append_stream(s); |
|
692 | 698 | }; |
|
693 | 699 | |
|
694 | 700 | |
|
695 | 701 | CodeCell.prototype.append_display_data = function (data, element) { |
|
696 | 702 | if (data["text/latex"] !== undefined) { |
|
697 | 703 | this.append_latex(data["text/latex"], element); |
|
698 | 704 | // If it is undefined, then we just appended to div.output, which |
|
699 | 705 | // makes the latex visible and we can typeset it. The typesetting |
|
700 | 706 | // has to be done after the latex is on the page. |
|
701 | 707 | if (element === undefined) { |
|
702 | 708 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
703 | 709 | }; |
|
704 | 710 | } else if (data["image/svg+xml"] !== undefined) { |
|
705 | 711 | this.append_svg(data["image/svg+xml"], element); |
|
706 | 712 | } else if (data["text/plain"] !== undefined) { |
|
707 | 713 | this.append_stream(data["text/plain"], element); |
|
708 | 714 | }; |
|
709 | 715 | return element; |
|
710 | 716 | }; |
|
711 | 717 | |
|
712 | 718 | |
|
713 | 719 | CodeCell.prototype.append_stream = function (data, element) { |
|
714 | 720 | element = element || this.element.find("div.output"); |
|
715 | 721 | var toinsert = $("<div/>").addClass("output_area output_stream"); |
|
716 | 722 | toinsert.append($("<pre/>").html(fixConsole(data))); |
|
717 | 723 | element.append(toinsert); |
|
718 | 724 | return element; |
|
719 | 725 | }; |
|
720 | 726 | |
|
721 | 727 | |
|
722 | 728 | CodeCell.prototype.append_svg = function (svg, element) { |
|
723 | 729 | element = element || this.element.find("div.output"); |
|
724 | 730 | var toinsert = $("<div/>").addClass("output_area output_svg"); |
|
725 | 731 | toinsert.append(svg); |
|
726 | 732 | element.append(toinsert); |
|
727 | 733 | return element; |
|
728 | 734 | }; |
|
729 | 735 | |
|
730 | 736 | |
|
731 | 737 | CodeCell.prototype.append_latex = function (latex, element) { |
|
732 | 738 | // This method cannot do the typesetting because the latex first has to |
|
733 | 739 | // be on the page. |
|
734 | 740 | element = element || this.element.find("div.output"); |
|
735 | 741 | var toinsert = $("<div/>").addClass("output_area output_latex"); |
|
736 | 742 | toinsert.append(latex); |
|
737 | 743 | element.append(toinsert); |
|
738 | 744 | return element; |
|
739 | 745 | } |
|
740 | 746 | |
|
741 | 747 | |
|
742 | 748 | CodeCell.prototype.clear_output = function () { |
|
743 | 749 | this.element.find("div.output").html(""); |
|
744 | 750 | }; |
|
745 | 751 | |
|
746 | 752 | |
|
747 | 753 | CodeCell.prototype.collapse = function () { |
|
748 | 754 | this.element.find('div.output').hide(); |
|
749 | 755 | }; |
|
750 | 756 | |
|
751 | 757 | |
|
752 | 758 | CodeCell.prototype.expand = function () { |
|
753 | 759 | this.element.find('div.output').show(); |
|
754 | 760 | }; |
|
755 | 761 | |
|
756 | 762 | |
|
757 | 763 | CodeCell.prototype.set_input_prompt = function (number) { |
|
758 | 764 | var n = number || ' '; |
|
759 | 765 | this.input_prompt_number = n |
|
760 | 766 | this.element.find('div.input_prompt').html('In [' + n + ']:'); |
|
761 | 767 | }; |
|
762 | 768 | |
|
763 | 769 | |
|
764 | 770 | CodeCell.prototype.get_code = function () { |
|
765 | 771 | return this.element.find("textarea.input_textarea").val(); |
|
766 | 772 | }; |
|
767 | 773 | |
|
768 | 774 | |
|
769 | 775 | CodeCell.prototype.set_code = function (code) { |
|
770 | 776 | return this.element.find("textarea.input_textarea").val(code); |
|
771 | 777 | }; |
|
772 | 778 | |
|
773 | 779 | |
|
774 | 780 | CodeCell.prototype.fromJSON = function (data) { |
|
775 | 781 | if (data.cell_type === 'code') { |
|
776 | 782 | this.set_code(data.code); |
|
777 | 783 | this.set_input_prompt(data.prompt_number); |
|
778 | 784 | this.grow(this.element.find("textarea.input_textarea")); |
|
779 | 785 | }; |
|
780 | 786 | }; |
|
781 | 787 | |
|
782 | 788 | |
|
783 | 789 | CodeCell.prototype.toJSON = function () { |
|
784 | 790 | return { |
|
785 | 791 | code : this.get_code(), |
|
786 | 792 | cell_type : 'code', |
|
787 | 793 | prompt_number : this.input_prompt_number |
|
788 | 794 | }; |
|
789 | 795 | }; |
|
790 | 796 | |
|
791 | 797 | //============================================================================ |
|
792 | 798 | // TextCell |
|
793 | 799 | //============================================================================ |
|
794 | 800 | |
|
795 | 801 | |
|
796 | 802 | var TextCell = function (notebook) { |
|
797 | 803 | Cell.apply(this, arguments); |
|
798 | 804 | this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$" |
|
799 | 805 | }; |
|
800 | 806 | |
|
801 | 807 | |
|
802 | 808 | TextCell.prototype = new Cell(); |
|
803 | 809 | |
|
804 | 810 | |
|
805 | 811 | TextCell.prototype.create_element = function () { |
|
806 | 812 | var cell = $("<div>").addClass('cell text_cell'). |
|
807 | 813 | append( |
|
808 | 814 | $("<textarea>" + this.placeholder + "</textarea>"). |
|
809 | 815 | addClass('text_cell_input'). |
|
810 | 816 | attr('rows',1). |
|
811 | 817 | attr('cols',80). |
|
812 | 818 | autogrow() |
|
813 | 819 | ).append( |
|
814 | 820 | $('<div></div>').addClass('text_cell_render') |
|
815 | 821 | ) |
|
816 | 822 | this.element = cell; |
|
817 | 823 | }; |
|
818 | 824 | |
|
819 | 825 | |
|
820 | 826 | TextCell.prototype.select = function () { |
|
821 | 827 | this.edit(); |
|
822 | 828 | Cell.prototype.select.apply(this); |
|
823 | 829 | }; |
|
824 | 830 | |
|
825 | 831 | |
|
826 | 832 | TextCell.prototype.edit = function () { |
|
827 | 833 | var text_cell = this.element; |
|
828 | 834 | var input = text_cell.find("textarea.text_cell_input"); |
|
829 | 835 | var output = text_cell.find("div.text_cell_render"); |
|
830 | 836 | output.hide(); |
|
831 | 837 | input.show().trigger('focus'); |
|
832 | 838 | }; |
|
833 | 839 | |
|
834 | 840 | |
|
835 | 841 | TextCell.prototype.render = function () { |
|
836 | 842 | var text_cell = this.element; |
|
837 | 843 | var input = text_cell.find("textarea.text_cell_input"); |
|
838 | 844 | var output = text_cell.find("div.text_cell_render"); |
|
839 | 845 | var text = input.val(); |
|
840 | 846 | if (text === "") { |
|
841 | 847 | text = this.placeholder; |
|
842 | 848 | input.val(text); |
|
843 | 849 | }; |
|
844 | 850 | output.html(text) |
|
845 | 851 | input.html(text); |
|
846 | 852 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
847 | 853 | input.hide(); |
|
848 | 854 | output.show(); |
|
849 | 855 | }; |
|
850 | 856 | |
|
851 | 857 | |
|
852 | 858 | TextCell.prototype.config_mathjax = function () { |
|
853 | 859 | var text_cell = this.element; |
|
854 | 860 | var that = this; |
|
855 | 861 | text_cell.click(function () { |
|
856 | 862 | that.edit(); |
|
857 | 863 | }).focusout(function () { |
|
858 | 864 | that.render(); |
|
859 | 865 | }); |
|
860 | 866 | |
|
861 | 867 | text_cell.trigger("focusout"); |
|
862 | 868 | }; |
|
863 | 869 | |
|
864 | 870 | |
|
865 | 871 | TextCell.prototype.get_text = function() { |
|
866 | 872 | return this.element.find("textarea.text_cell_input").val(); |
|
867 | 873 | }; |
|
868 | 874 | |
|
869 | 875 | |
|
870 | 876 | TextCell.prototype.set_text = function(text) { |
|
871 | 877 | this.element.find("textarea.text_cell_input").val(text); |
|
872 | 878 | this.element.find("textarea.text_cell_input").html(text); |
|
873 | 879 | this.element.find("div.text_cell_render").html(text); |
|
874 | 880 | }; |
|
875 | 881 | |
|
876 | 882 | |
|
877 | 883 | TextCell.prototype.fromJSON = function (data) { |
|
878 | 884 | if (data.cell_type === 'text') { |
|
879 | 885 | this.set_text(data.text); |
|
880 | 886 | this.grow(this.element.find("textarea.text_cell_input")); |
|
881 | 887 | }; |
|
882 | 888 | } |
|
883 | 889 | |
|
884 | 890 | |
|
885 | 891 | TextCell.prototype.toJSON = function () { |
|
886 | 892 | return { |
|
887 | 893 | cell_type : 'text', |
|
888 | 894 | text : this.get_text(), |
|
889 | 895 | }; |
|
890 | 896 | }; |
|
891 | 897 | |
|
892 | 898 | //============================================================================ |
|
893 | 899 | // On document ready |
|
894 | 900 | //============================================================================ |
|
895 | 901 | |
|
896 | 902 | |
|
897 | 903 | var Kernel = function () { |
|
898 | 904 | this.kernel_id = null; |
|
899 | 905 | this.base_url = "/kernels"; |
|
900 | 906 | this.kernel_url = null; |
|
901 | 907 | }; |
|
902 | 908 | |
|
903 | 909 | |
|
904 | 910 | Kernel.prototype.get_msg = function (msg_type, content) { |
|
905 | 911 | var msg = { |
|
906 | 912 | header : { |
|
907 | 913 | msg_id : uuid(), |
|
908 | 914 | username : "bgranger", |
|
909 | 915 | session: this.session_id |
|
910 | 916 | }, |
|
911 | 917 | msg_type : msg_type, |
|
912 | 918 | content : content, |
|
913 | 919 | parent_header : {} |
|
914 | 920 | }; |
|
915 | 921 | return msg; |
|
916 | 922 | } |
|
917 | 923 | |
|
918 | 924 | Kernel.prototype.start_kernel = function (callback, context) { |
|
919 | 925 | var that = this; |
|
920 | 926 | $.post(this.base_url, |
|
921 | 927 | function (kernel_id) { |
|
922 | 928 | that._handle_start_kernel(kernel_id, callback, context); |
|
923 | 929 | }, |
|
924 | 930 | 'json' |
|
925 | 931 | ); |
|
926 | 932 | }; |
|
927 | 933 | |
|
928 | 934 | |
|
929 | 935 | Kernel.prototype._handle_start_kernel = function (kernel_id, callback, context) { |
|
930 | 936 | this.kernel_id = kernel_id; |
|
931 | 937 | this.kernel_url = this.base_url + "/" + this.kernel_id; |
|
932 | 938 | this._start_channels(); |
|
933 | 939 | callback.call(context); |
|
934 | 940 | }; |
|
935 | 941 | |
|
936 | 942 | |
|
937 | 943 | Kernel.prototype._start_channels = function () { |
|
938 | 944 | var ws_url = "ws://127.0.0.1:8888" + this.kernel_url; |
|
939 | 945 | this.shell_channel = new WebSocket(ws_url + "/shell"); |
|
940 | 946 | this.iopub_channel = new WebSocket(ws_url + "/iopub"); |
|
941 | 947 | } |
|
942 | 948 | |
|
943 | 949 | |
|
944 | 950 | Kernel.prototype.execute = function (code) { |
|
945 | 951 | var content = { |
|
946 | 952 | code : code, |
|
947 | 953 | silent : false, |
|
948 | 954 | user_variables : [], |
|
949 | 955 | user_expressions : {} |
|
950 | 956 | }; |
|
951 | 957 | var msg = this.get_msg("execute_request", content); |
|
952 | 958 | this.shell_channel.send(JSON.stringify(msg)); |
|
953 | 959 | return msg.header.msg_id; |
|
954 | 960 | } |
|
955 | 961 | |
|
956 | 962 | |
|
957 | 963 | Kernel.prototype.interrupt = function () { |
|
958 | 964 | $.post(this.kernel_url + "/interrupt"); |
|
959 | 965 | }; |
|
960 | 966 | |
|
961 | 967 | |
|
962 | 968 | Kernel.prototype.restart = function () { |
|
963 | 969 | this.status_restarting(); |
|
964 | 970 | url = this.kernel_url + "/restart" |
|
965 | 971 | var that = this; |
|
966 | 972 | $.post(url, function (kernel_id) { |
|
967 | 973 | console.log("Kernel restarted: " + kernel_id); |
|
968 | 974 | that.kernel_id = kernel_id; |
|
969 | 975 | that.kernel_url = that.base_url + "/" + that.kernel_id; |
|
970 | 976 | that.status_idle(); |
|
971 | 977 | }, 'json'); |
|
972 | 978 | }; |
|
973 | 979 | |
|
974 | 980 | |
|
975 | 981 | Kernel.prototype.status_busy = function () { |
|
976 | 982 | $("#kernel_status").removeClass("status_idle"); |
|
977 | 983 | $("#kernel_status").removeClass("status_restarting"); |
|
978 | 984 | $("#kernel_status").addClass("status_busy"); |
|
979 | 985 | $("#kernel_status").text("Busy"); |
|
980 | 986 | }; |
|
981 | 987 | |
|
982 | 988 | |
|
983 | 989 | Kernel.prototype.status_idle = function () { |
|
984 | 990 | $("#kernel_status").removeClass("status_busy"); |
|
985 | 991 | $("#kernel_status").removeClass("status_restarting"); |
|
986 | 992 | $("#kernel_status").addClass("status_idle"); |
|
987 | 993 | $("#kernel_status").text("Idle"); |
|
988 | 994 | }; |
|
989 | 995 | |
|
990 | 996 | Kernel.prototype.status_restarting = function () { |
|
991 | 997 | $("#kernel_status").removeClass("status_busy"); |
|
992 | 998 | $("#kernel_status").removeClass("status_idle"); |
|
993 | 999 | $("#kernel_status").addClass("status_restarting"); |
|
994 | 1000 | $("#kernel_status").text("Restarting"); |
|
995 | 1001 | }; |
|
996 | 1002 | |
|
997 | 1003 | //============================================================================ |
|
998 | 1004 | // On document ready |
|
999 | 1005 | //============================================================================ |
|
1000 | 1006 | |
|
1001 | 1007 | |
|
1002 | 1008 | $(document).ready(function () { |
|
1003 | 1009 | |
|
1004 | 1010 | MathJax.Hub.Config({ |
|
1005 | 1011 | tex2jax: { |
|
1006 | 1012 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], |
|
1007 | 1013 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ], |
|
1008 | 1014 | }, |
|
1009 | 1015 | displayAlign: 'left', // Change this to 'center' to center equations. |
|
1010 | 1016 | "HTML-CSS": { |
|
1011 | 1017 | styles: {'.MathJax_Display': {"margin": 0}} |
|
1012 | 1018 | } |
|
1013 | 1019 | }); |
|
1014 | 1020 | |
|
1015 | 1021 | IPYTHON.notebook = new Notebook('div.notebook'); |
|
1016 | 1022 | IPYTHON.notebook.insert_code_cell_after(); |
|
1017 | 1023 | |
|
1018 | 1024 | $("#menu_tabs").tabs(); |
|
1019 | 1025 | |
|
1020 | 1026 | $("#help_toolbar").buttonset(); |
|
1021 | 1027 | |
|
1022 | 1028 | $("#kernel_toolbar").buttonset(); |
|
1023 | 1029 | $("#interrupt_kernel").click(function () {IPYTHON.notebook.kernel.interrupt();}); |
|
1024 | 1030 | $("#restart_kernel").click(function () {IPYTHON.notebook.kernel.restart();}); |
|
1025 | 1031 | $("#kernel_status").addClass("status_idle"); |
|
1026 | 1032 | |
|
1027 | 1033 | $("#move_cell").buttonset(); |
|
1028 | 1034 | $("#move_up").button("option", "icons", {primary:"ui-icon-arrowthick-1-n"}); |
|
1029 | 1035 | $("#move_up").button("option", "text", false); |
|
1030 | 1036 | $("#move_up").click(function () {IPYTHON.notebook.move_cell_up();}); |
|
1031 | 1037 | $("#move_down").button("option", "icons", {primary:"ui-icon-arrowthick-1-s"}); |
|
1032 | 1038 | $("#move_down").button("option", "text", false); |
|
1033 | 1039 | $("#move_down").click(function () {IPYTHON.notebook.move_cell_down();}); |
|
1034 | 1040 | |
|
1035 | 1041 | $("#insert_delete").buttonset(); |
|
1036 | 1042 | $("#insert_cell_before").click(function () {IPYTHON.notebook.insert_code_cell_before();}); |
|
1037 | 1043 | $("#insert_cell_after").click(function () {IPYTHON.notebook.insert_code_cell_after();}); |
|
1038 | 1044 | $("#delete_cell").button("option", "icons", {primary:"ui-icon-closethick"}); |
|
1039 | 1045 | $("#delete_cell").button("option", "text", false); |
|
1040 | 1046 | $("#delete_cell").click(function () {IPYTHON.notebook.delete_cell();}); |
|
1041 | 1047 | |
|
1042 | 1048 | $("#cell_type").buttonset(); |
|
1043 | 1049 | $("#to_code").click(function () {IPYTHON.notebook.text_to_code();}); |
|
1044 | 1050 | $("#to_text").click(function () {IPYTHON.notebook.code_to_text();}); |
|
1045 | 1051 | |
|
1046 | 1052 | $("#sort").buttonset(); |
|
1047 | 1053 | $("#sort_cells").click(function () {IPYTHON.notebook.sort_cells();}); |
|
1048 | 1054 | |
|
1049 | 1055 | $("#toggle").buttonset(); |
|
1050 | 1056 | $("#collapse").click(function () {IPYTHON.notebook.collapse();}); |
|
1051 | 1057 | $("#expand").click(function () {IPYTHON.notebook.expand();}); |
|
1052 | 1058 | |
|
1053 | 1059 | }); No newline at end of file |
@@ -1,98 +1,99 b'' | |||
|
1 | 1 | <!DOCTYPE HTML> |
|
2 | 2 | <html> |
|
3 | 3 | |
|
4 | 4 | <head> |
|
5 | 5 | <meta charset="utf-8"> |
|
6 | 6 | |
|
7 | 7 | <title>IPython Notebook</title> |
|
8 | 8 | |
|
9 | 9 | <link rel="stylesheet" href="static/css/notebook.css" type="text/css" /> |
|
10 | 10 | |
|
11 | 11 | <link rel="stylesheet" href="static/jquery/css/themes/aristo/jquery-wijmo.css" type="text/css" /> |
|
12 | 12 | <!-- <link rel="stylesheet" href="static/jquery/css/themes/rocket/jquery-wijmo.css" type="text/css" /> --> |
|
13 | 13 | <!-- <link rel="stylesheet" href="static/jquery/css/themes/smoothness/jquery-ui-1.8.11.custom.css" type="text/css" /> --> |
|
14 | 14 | |
|
15 | 15 | <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" charset="utf-8"></script> |
|
16 | <!-- <script type='text/javascript' src='static/mathjax/MathJax.js?config=TeX-AMS_HTML' charset='utf-8'></script> --> | |
|
16 | 17 | <script type="text/javascript"> |
|
17 | 18 | if (typeof MathJax == 'undefined') { |
|
18 | 19 | console.log("Trying to load local copy of MathJax"); |
|
19 | 20 | document.write(unescape("%3Cscript type='text/javascript' src='static/mathjax/MathJax.js%3Fconfig=TeX-AMS_HTML' charset='utf-8'%3E%3C/script%3E")); |
|
20 | 21 | } |
|
21 | 22 | </script> |
|
22 | 23 | |
|
23 | 24 | </head> |
|
24 | 25 | |
|
25 | 26 | <body> |
|
26 | 27 | |
|
27 | 28 | <div id="wrapper"> |
|
28 | 29 | |
|
29 | 30 | <div id="header"> |
|
30 | 31 | <span id="ipython_notebook"><h1>[I]:Python Notebook</h1></span> |
|
31 | 32 | </div> |
|
32 | 33 | |
|
33 | 34 | |
|
34 | 35 | <div id="tools"> |
|
35 | 36 | |
|
36 | 37 | <div id="menu_tabs"> |
|
37 | 38 | <span id="kernel_status">Idle</span> |
|
38 | 39 | <ul> |
|
39 | 40 | <li><a href="#cell_tab">Cell</a></li> |
|
40 | 41 | <li><a href="#kernel_tab">Kernel</a></li> |
|
41 | 42 | <li><a href="#help_tab">Help</a></li> |
|
42 | 43 | </ul> |
|
43 | 44 | <div id="cell_tab"> |
|
44 | 45 | <span id="cell_toolbar"> |
|
45 | 46 | <span id="move_cell"> |
|
46 | 47 | <button id="move_up">Move up</button> |
|
47 | 48 | <button id="move_down">Move down</button> |
|
48 | 49 | </span> |
|
49 | 50 | <span id="insert_delete"> |
|
50 | 51 | <button id="insert_cell_before">Before</button> |
|
51 | 52 | <button id="insert_cell_after">After</button> |
|
52 | 53 | <button id="delete_cell">Delete</button> |
|
53 | 54 | </span> |
|
54 | 55 | <span id="cell_type"> |
|
55 | 56 | <button id="to_code">Code</button> |
|
56 | 57 | <button id="to_text">Text</button> |
|
57 | 58 | </span> |
|
58 | 59 | <span id="sort"> |
|
59 | 60 | <button id="sort_cells">Sort</button> |
|
60 | 61 | </span> |
|
61 | 62 | <span id="toggle"> |
|
62 | 63 | <button id="collapse">Collapse</button> |
|
63 | 64 | <button id="expand">Expand</button> |
|
64 | 65 | </span> |
|
65 | 66 | </span> |
|
66 | 67 | </div> |
|
67 | 68 | <div id="kernel_tab"> |
|
68 | 69 | <span id="kernel_toolbar"> |
|
69 | 70 | <button id="interrupt_kernel">Interrupt</button> |
|
70 | 71 | <button id="restart_kernel">Restart</button> |
|
71 | 72 | </span> |
|
72 | 73 | </div> |
|
73 | 74 | <div id="help_tab"> |
|
74 | 75 | <span id="help_toolbar"> |
|
75 | 76 | <button><a href="http://docs.python.org" target="_blank">Python</a></button> |
|
76 | 77 | <button><a href="http://ipython.github.com/ipython-doc/dev/index.html" target="_blank">IPython</a></button> |
|
77 | 78 | <button><a href="http://matplotlib.sourceforge.net/" target="_blank">Matplotlib</a></button> |
|
78 | 79 | <button><a href="http://docs.scipy.org/doc/numpy/reference/" target="_blank">NumPy</a></button> |
|
79 | 80 | <button><a href="http://docs.scipy.org/doc/scipy/reference/" target="_blank">SciPy</a></button> |
|
80 | 81 | <button><a href="http://docs.sympy.org/dev/index.html" target="_blank">SymPy</a></button> |
|
81 | 82 | </span> |
|
82 | 83 | </div> |
|
83 | 84 | </div> |
|
84 | 85 | |
|
85 | 86 | </div> |
|
86 | 87 | |
|
87 | 88 | <div class="notebook"></div> |
|
88 | 89 | |
|
89 | 90 | </div> |
|
90 | 91 | |
|
91 | 92 | <script src="static/jquery/js/jquery-1.5.1.min.js" type="text/javascript" charset="utf-8"></script> |
|
92 | 93 | <script src="static/jquery/js/jquery-ui-1.8.11.custom.min.js" type="text/javascript" charset="utf-8"></script> |
|
93 | 94 | <script src="static/jquery/js/jquery.autogrow.js" type="text/javascript" charset="utf-8"></script> |
|
94 | 95 | <script src="static/js/notebook.js" type="text/javascript" charset="utf-8"></script> |
|
95 | 96 | |
|
96 | 97 | </body> |
|
97 | 98 | |
|
98 | 99 | </html> No newline at end of file |
@@ -1,1 +1,1 b'' | |||
|
1 |
{"cells":[{"cell_type":"text","text":"<h1>Basic Symbolic Quantum Mechanics</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number": |
|
|
1 | {"cells":[{"cell_type":"text","text":"<h1>Basic Symbolic Quantum Mechanics</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":2},{"cell_type":"text","text":"<h2>Bras and Kets</h2>"},{"code":"phi, psi = Ket('phi'), Ket('psi')\nalpha, beta = symbols('alpha beta', complex=True)","cell_type":"code","prompt_number":3},{"code":"state = alpha*psi + beta*phi; state\n","cell_type":"code","prompt_number":4},{"code":"ip = Dagger(state)*state; ip\n","cell_type":"code","prompt_number":5},{"code":"qapply(expand(ip))\n","cell_type":"code","prompt_number":6},{"cell_type":"text","text":"<h2>Operators</h2>"},{"code":"A = Operator('A')\nB = Operator('B')\nC = Operator('C')","cell_type":"code","prompt_number":7},{"code":"A*B == B*A\n","cell_type":"code","prompt_number":8},{"code":"expand((A+B)**2)","cell_type":"code","prompt_number":9},{"code":"comm = Commutator(A,B); comm\n","cell_type":"code","prompt_number":10},{"code":"comm.doit()","cell_type":"code","prompt_number":11},{"code":"comm = Commutator(A*B,B+C); comm","cell_type":"code","prompt_number":12},{"code":"comm.expand(commutator=True)","cell_type":"code","prompt_number":13},{"code":"_.doit().expand()\n","cell_type":"code","prompt_number":14},{"code":"Dagger(_)","cell_type":"code","prompt_number":15},{"code":"%notebook save basic_quantum.ipynb","cell_type":"code","prompt_number":16},{"code":"%notebook load quantum_computing.ipynb","cell_type":"code","prompt_number":19}]} No newline at end of file |
@@ -1,1 +1,1 b'' | |||
|
1 |
{"cells":[{"cell_type":"text","text":"<h1>Gate Decomposition</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number": |
|
|
1 | {"cells":[{"cell_type":"text","text":"<h1>Gate Decomposition</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":2},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":3},{"cell_type":"text","text":"<h2>Example 1</h2>"},{"code":"CY10 = CGate(1, Y(0)); CY10\n","cell_type":"code","prompt_number":4},{"code":"CY10.decompose()\n","cell_type":"code","prompt_number":5},{"code":"circuit_plot(CY10.decompose(), nqubits=2)","cell_type":"code","prompt_number":6},{"cell_type":"text","text":"<h2>Example 2</h2>"},{"code":"CZ01 = CGate(0, Z(1)); CZ01\n","cell_type":"code","prompt_number":7},{"code":"CZ01.decompose()\n","cell_type":"code","prompt_number":8},{"code":"circuit_plot(CZ01.decompose(), nqubits=2)","cell_type":"code","prompt_number":9},{"cell_type":"text","text":"<h2>Example 3</h2>"},{"code":"SWAP10 = SWAP(1, 0); SWAP10\n","cell_type":"code","prompt_number":10},{"code":"SWAP10.decompose()","cell_type":"code","prompt_number":11},{"code":"circuit_plot(SWAP10.decompose(), nqubits=2)","cell_type":"code","prompt_number":12},{"cell_type":"text","text":"<h2>All together now</h2>"},{"code":"gates = [CGate(1,Y(0)), CGate(0,Z(1)), SWAP(1, 0)]","cell_type":"code","prompt_number":13},{"code":"for g in gates:\n dg = g.decompose()\n display(Eq(g, dg))\n circuit_plot(g, nqubits=2)\n circuit_plot(dg, nqubits=2) ","cell_type":"code","prompt_number":14},{"code":"%notebook save decompose.ipynb","cell_type":"code","prompt_number":30}]} No newline at end of file |
@@ -1,1 +1,1 b'' | |||
|
1 |
{"cells":[{"cell_type":"text","text":"<h1>Dense Coding\n</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number": |
|
|
1 | {"cells":[{"cell_type":"text","text":"<h1>Dense Coding\n</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":2},{"code":"psi = Qubit('00')/sqrt(2) + Qubit('11')/sqrt(2); psi\n","cell_type":"code","prompt_number":3},{"code":"circuits = [H(1)*CNOT(1,0), H(1)*CNOT(1,0)*X(1), H(1)*CNOT(1,0)*Z(1), H(1)*CNOT(1,0)*Z(1)*X(1)]","cell_type":"code","prompt_number":4},{"code":"for circuit in circuits:\n circuit_plot(circuit, nqubits=2)\n display(Eq(circuit*psi,qapply(circuit*psi)))","cell_type":"code","prompt_number":5},{"code":"%notebook save dense_coding.ipynb","cell_type":"code","prompt_number":28},{"code":"%notebook load teleportation.ipynb","cell_type":"code","prompt_number":181}]} No newline at end of file |
@@ -1,1 +1,1 b'' | |||
|
1 |
{"cells":[{"cell_type":"text","text":"<h1>Grover's Algorithm</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":2},{"code":"nqubits = 3\n","cell_type":"code","prompt_number": |
|
|
1 | {"cells":[{"cell_type":"text","text":"<h1>Grover's Algorithm</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":2},{"code":"nqubits = 3\n","cell_type":"code","prompt_number":3},{"cell_type":"text","text":"Define a black box function that returns True if it is passed the state we are searching for."},{"code":"def black_box(qubits):\n return True if qubits == IntQubit(1, qubits.nqubits) else False\n","cell_type":"code","prompt_number":4},{"cell_type":"text","text":"Build a uniform superposition state to start the search."},{"code":"psi = superposition_basis(nqubits); psi\n","cell_type":"code","prompt_number":5},{"cell_type":"text","text":"Wrap that black box function in an oracle gate."},{"code":"v = OracleGate(nqubits, black_box)\n","cell_type":"code","prompt_number":6},{"cell_type":"text","text":"Perform two iterations of Grover's algorithm. Each iteration, the amplitude of the target state increases."},{"code":"iter1 = qapply(grover_iteration(psi, v)); iter1\n","cell_type":"code","prompt_number":7},{"code":"iter2 = qapply(grover_iteration(iter1, v)); iter2\n","cell_type":"code","prompt_number":8},{"cell_type":"text","text":"A single shot measurement is performed to retrieve the target state."},{"code":"measure_all_oneshot(iter2)\n","cell_type":"code","prompt_number":9},{"code":"%notebook save grovers.ipynb","cell_type":"code","prompt_number":28},{"code":"%notebook load qft.ipynb","cell_type":"code","prompt_number":63}]} No newline at end of file |
@@ -1,1 +1,1 b'' | |||
|
1 |
{"cells":[{"cell_type":"text","text":"<h1>Quantum Error Correction</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number": |
|
|
1 | {"cells":[{"cell_type":"text","text":"<h1>Quantum Error Correction</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":2},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":3},{"cell_type":"text","text":"<h2>5 qubit code</h2>"},{"code":"M0 = Z(1)*X(2)*X(3)*Z(4); M0\n","cell_type":"code","prompt_number":4},{"code":"M1 = Z(2)*X(3)*X(4)*Z(0); M1\n","cell_type":"code","prompt_number":5},{"code":"M2 = Z(3)*X(4)*X(0)*Z(1); M2\n","cell_type":"code","prompt_number":6},{"code":"M3 = Z(4)*X(0)*X(1)*Z(2); M3\n","cell_type":"code","prompt_number":7},{"cell_type":"text","text":"These operators should mutually commute."},{"code":"gate_simp(Commutator(M0,M1).doit())\n","cell_type":"code","prompt_number":7},{"cell_type":"text","text":"And square to the identity."},{"code":"for o in [M0,M1,M2,M3]:\n display(gate_simp(o*o))\n","cell_type":"code","prompt_number":8},{"cell_type":"text","text":"<h2>Codewords</h2>"},{"code":"zero = Rational(1,4)*(1+M0)*(1+M1)*(1+M2)*(1+M3)*IntQubit(0, 5); zero\n","cell_type":"code","prompt_number":8},{"code":"qapply(4*zero)\n","cell_type":"code","prompt_number":9},{"code":"one = Rational(1,4)*(1+M0)*(1+M1)*(1+M2)*(1+M3)*IntQubit(2**5-1, 5); one\n","cell_type":"code","prompt_number":10},{"code":"qapply(4*one)\n","cell_type":"code","prompt_number":11},{"cell_type":"text","text":"<h2>The encoding circuit</h2>"},{"code":"encoding_circuit = H(3)*H(4)*CNOT(2,0)*CNOT(3,0)*CNOT(4,0)*H(1)*H(4)*\\\n CNOT(2,1)*CNOT(4,1)*H(2)*CNOT(3,2)*CNOT(4,2)*H(3)*\\\n H(4)*CNOT(4, 3)*Z(4)*H(4)*Z(4)\n","cell_type":"code","prompt_number":12},{"code":"circuit_plot(encoding_circuit, nqubits=5, scale=0.5)","cell_type":"code","prompt_number":13},{"code":"represent(4*encoding_circuit, nqubits=5)","cell_type":"code","prompt_number":14},{"code":"%notebook save qerror.ipynb","cell_type":"code","prompt_number":23},{"code":"%notebook load decompose.ipynb","cell_type":"code","prompt_number":23}]} No newline at end of file |
@@ -1,1 +1,1 b'' | |||
|
1 |
{"cells":[{"cell_type":"text","text":"<h1> |
|
|
1 | {"cells":[{"cell_type":"text","text":"<h1>Quantum Fourier Transform</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":2},{"cell_type":"text","text":"<h2>QFT Gate and Circuit</h2>"},{"cell_type":"text","text":"Build a 3 qubit QFT and decompose it into primitive gates."},{"code":"fourier = QFT(0,3).decompose(); fourier\n","cell_type":"code","prompt_number":3},{"code":"circuit_plot(fourier, nqubits=3)","cell_type":"code","prompt_number":4},{"cell_type":"text","text":"The QFT circuit can be represented in various symbolic forms."},{"code":"m = represent(fourier, nqubits=3)","cell_type":"code","prompt_number":5},{"code":"m","cell_type":"code","prompt_number":6},{"code":"represent(Fourier(0,3), nqubits=3)*4/sqrt(2)\n","cell_type":"code","prompt_number":7},{"cell_type":"text","text":"<h2>QFT in Action</h2>"},{"cell_type":"text","text":"Build a 3 qubit state to take the QFT of."},{"code":"state = (Qubit('000') + Qubit('010') + Qubit('100') + Qubit('110'))/sqrt(4); state\n","cell_type":"code","prompt_number":8},{"cell_type":"text","text":"Perform the QFT."},{"code":"qapply(fourier*state)\n","cell_type":"code","prompt_number":9},{"code":"%notebook save qft.ipynb","cell_type":"code","prompt_number":23},{"code":"%notebook load qerror.ipynb","cell_type":"code","prompt_number":207}]} No newline at end of file |
@@ -1,1 +1,1 b'' | |||
|
1 |
{"cells":[{"cell_type":"text","text":"<h1>Symbolic Quantum Computing</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":2},{"code":"alpha, beta = symbols('alpha beta',real=True)","cell_type":"code","prompt_number":3},{"code":"psi = alpha*Qubit('00') + beta*Qubit('11'); psi\n","cell_type":"code","prompt_number":4},{"code":"Dagger(psi)\n","cell_type":"code","prompt_number":5},{"code":"qapply(Dagger(Qubit('00'))*psi)\n","cell_type":"code","prompt_number":6},{"code":"for state, prob in measure_all(psi):\n display(state)\n display(prob)\n","cell_type":"code","prompt_number":7},{"c |
|
|
1 | {"cells":[{"cell_type":"text","text":"<h1>Symbolic Quantum Computing</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":2},{"cell_type":"text","text":"<h2>Qubits</h2>"},{"code":"alpha, beta = symbols('alpha beta',real=True)","cell_type":"code","prompt_number":3},{"code":"psi = alpha*Qubit('00') + beta*Qubit('11'); psi\n","cell_type":"code","prompt_number":4},{"code":"Dagger(psi)\n","cell_type":"code","prompt_number":5},{"code":"qapply(Dagger(Qubit('00'))*psi)\n","cell_type":"code","prompt_number":6},{"cell_type":"text","text":"SymPy supports many different types of measurements."},{"code":"for state, prob in measure_all(psi):\n display(state)\n display(prob)\n","cell_type":"code","prompt_number":7},{"cell_type":"text","text":"Qubits can be represented in the computational basis."},{"code":"represent(psi)\n","cell_type":"code","prompt_number":8},{"cell_type":"text","text":"<h2>Gates</h2>"},{"code":"g = X(0); g\n","cell_type":"code","prompt_number":9},{"code":"represent(g, nqubits=2)\n","cell_type":"code","prompt_number":10},{"code":"c = H(0)*Qubit('00'); c\n","cell_type":"code","prompt_number":11},{"code":"qapply(c)\n","cell_type":"code","prompt_number":12},{"cell_type":"text","text":"<h2>Symbolic gate rules and circuit simplification</h2>"},{"code":"for g1 in (Y,Z,H):\n for g2 in (Y,Z,H):\n e = Commutator(g1(0),g2(0))\n if g1 != g2:\n display(Eq(e,e.doit()))\n","cell_type":"code","prompt_number":13},{"code":"c = H(0)*X(1)*H(0)**2*CNOT(0,1)*X(1)**3*X(0)*Z(1)**2; c\n","cell_type":"code","prompt_number":15},{"code":"circuit_plot(c, nqubits=2)","cell_type":"code","prompt_number":16},{"cell_type":"text","text":"This performs a commutator/anticommutator aware bubble sort algorithm to simplify a circuit."},{"code":"gate_simp(c)\n","cell_type":"code","prompt_number":17},{"code":"circuit_plot(gate_simp(c),nqubits=2)","cell_type":"code","prompt_number":18},{"code":"%notebook save quantum_computing.ipynb","cell_type":"code","prompt_number":35},{"code":"%notebook load grovers.ipynb","cell_type":"code","prompt_number":90}]} No newline at end of file |
@@ -1,1 +1,1 b'' | |||
|
1 | {"cells":[{"cell_type":"text","text":"<h1>Teleportation</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":2},{"code":"a,b = symbols('ab', real=True)\nstate = Qubit('000')*a + Qubit('001')*b; state","cell_type":"code","prompt_number":3},{"code":"entangle1_2 = CNOT(1,2)*HadamardGate(1); entangle1_2\n","cell_type":"code","prompt_number":4},{"code":"state = qapply(entangle1_2*state); state\n","cell_type":"code","prompt_number":5},{"code":"entangle0_1 = HadamardGate(0)*CNOT(0,1); entangle0_1\n","cell_type":"code","prompt_number":6},{"code":"circuit_plot(entangle0_1*entangle1_2, nqubits=3)\n","cell_type":"code","prompt_number":7},{"code":"state = qapply(entangle0_1*state); state\n","cell_type":"code","prompt_number":8},{"code":"result = measure_partial(state, (0,1))\n","cell_type":"code","prompt_number":10},{"code":"state = (result[2][0]*2).expand(); state","cell_type":"code","prompt_number":11},{"code":"state = qapply(XGate(2)*state); state\n","cell_type":"code","prompt_number":12},{"code":"%notebook save teleportation.ipynb","cell_type":"code","prompt_number":13},{"code":"","cell_type":"code","prompt_number":18}]} No newline at end of file | |
|
1 | {"cells":[{"cell_type":"text","text":"<h1>Teleportation</h1>"},{"code":"%load_ext sympy_printing","cell_type":"code","prompt_number":1},{"code":"from sympy import sqrt, symbols, Rational\nfrom sympy import expand, Eq, Symbol, simplify, exp, sin\nfrom sympy.physics.quantum import *\nfrom sympy.physics.quantum.qubit import *\nfrom sympy.physics.quantum.gate import *\nfrom sympy.physics.quantum.grover import *\nfrom sympy.physics.quantum.qft import QFT, IQFT, Fourier\nfrom sympy.physics.quantum.circuitplot import circuit_plot","cell_type":"code","prompt_number":2},{"code":"a,b = symbols('ab', real=True)\nstate = Qubit('000')*a + Qubit('001')*b; state","cell_type":"code","prompt_number":3},{"code":"entangle1_2 = CNOT(1,2)*HadamardGate(1); entangle1_2\n","cell_type":"code","prompt_number":4},{"code":"state = qapply(entangle1_2*state); state\n","cell_type":"code","prompt_number":5},{"code":"entangle0_1 = HadamardGate(0)*CNOT(0,1); entangle0_1\n","cell_type":"code","prompt_number":6},{"code":"circuit_plot(entangle0_1*entangle1_2, nqubits=3)\n","cell_type":"code","prompt_number":7},{"code":"state = qapply(entangle0_1*state); state\n","cell_type":"code","prompt_number":8},{"code":"result = measure_partial(state, (0,1))\n","cell_type":"code","prompt_number":10},{"code":"state = (result[2][0]*2).expand(); state","cell_type":"code","prompt_number":11},{"code":"state = qapply(XGate(2)*state); state\n","cell_type":"code","prompt_number":12},{"code":"%notebook save teleportation.ipynb","cell_type":"code","prompt_number":13},{"code":"%notebook load qft.ipynb","cell_type":"code","prompt_number":18}]} No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now