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