-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathplayground.html
439 lines (326 loc) · 13.1 KB
/
playground.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html>
<head>
<title>Q ⟩ Playground</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta name="description" content="Quantum computing in your browser.">
<meta name="copyright" content="Stewart Smith 2019–2020">
<meta name="keywords" content="
Q, Q.js, Q-js, Qjs, quantum JavaScript,
quantum, quantum physics, quantum mechanics, superposition,
quantum computer, quantum computer programming, quantum computing, QC,
quantum simulator, quantum computer simulator,
qubit, qbit, gate, Hadamard, Bloch, Bloch Sphere,
Web, Web site, website, Web browser, browser, HTML, HTML5, JavaScript, ES6, CSS,
Chrome, Firefox, Safari, Opera, Brave, Edge, WebKit, Blink, Gecko, Mozilla,
Stewart Smith, Stewart, Stew, Stuart, Steven, Steve, Stewdio, stewartsmith, stew_rtsmith, @stew_rtsmith,
Moar, Moar Technologies Corp, MTC,
Google, IBM, Microsoft, Amazon, NASA, DWave, D-Wave,
Quil, OpenQASM,
ProjectQ, Qiskit,
Quantum Development Kit, Cirq, Strawberry Fields, t|ket>,
QCL, Quantum pseudocode, Q#, Q|SI>, Q language, qGCL, QFC, QML, LIQUi|>, Quipper,
Stanford CS 269 Q: Quantum Computer Programming">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@stew_rtsmith">
<meta name="twitter:creator" content="@stew_rtsmith">
<meta name="twitter:title" content="Q ⟩ Playground">
<meta name="twitter:description" content="Quantum computing in your browser.">
<meta name="twitter:image" content="https://quantumjavascript.app/assets/Q-website-preview.jpg">
<meta property="og:type" content="website">
<meta property="og:title" content="Q ⟩ Playground">
<meta property="og:description" content="Quantum computing in your browser.">
<meta property="og:image" content="https://quantumjavascript.app/assets/Q-website-preview.jpg">
<meta property="og:url" content="https://quantumjavascript.app/playground.html">
<link rel="canonical" href="https://quantumjavascript.app/playground.html">
<link href="assets/Q-favicon-064.png" rel="icon" type="image/png">
<link href="assets/Q-favicon-144.png" rel="apple-touch-icon">
<link rel="stylesheet" href="Q/Q.css">
<link rel="stylesheet" href="Q/Q-Circuit-Editor.css">
<link rel="stylesheet" href="assets/documentation.css">
<script src="https://www.googletagmanager.com/gtag/js" async></script>
<script src="assets/ga.js"></script>
<script src="Q/Q.js"></script>
<script src="Q/Q-ComplexNumber.js"></script>
<script src="Q/Q-Matrix.js"></script>
<script src="Q/Q-Qubit.js"></script>
<script src="Q/Q-Gate.js"></script>
<script src="Q/Q-History.js"></script>
<script src="Q/Q-Circuit.js"></script>
<script src="Q/Q-Circuit-Editor.js"></script>
<script src="assets/navigation.js"></script>
</head>
<body>
<main>
<h3>Playground</h3>
<p>
Type, paste, or edit your circuit here.
Then tap “Apply” to
generate an interactive circuit diagram
and other output formats below.
You can learn more about Q’s plain text format
in <a href="Q-Circuit.html#Writing_quantum_circuits">“Writing quantum circuits.”</a>
</p>
<!-- textarea class="Q-circuit-text-input" id="playground-input">I-I-I---I-I
I-H-X#0-I-I
I-I-X#1-I-I
I-I-I---I-I</textarea -->
<!--
H#0-Z#0-I-H-I-X#0-I---I-H
I---Z#1-I-H-I-I---X#0-I-H
H#1-Z#2-I-X-H-X#1-X#1-H-X
H#2-I---I-I-I-I---I---I-I
I---I---I
H#0-Z#0-I
I---Z#1-I
H#1-Z#2-I
H#2-I---I
I---I---I
I---I---I
I---I---I
--->
<textarea class="Q-circuit-text-input" id="playground-input">
H-I-X#0-I---I-H
H-I-I---X#0-I-H
X-H-X#1-X#1-H-X
I-I-I---I---I-I
</textarea>
<p class="right">
<button class="Q-button" id="playground-apply-button" type="button" onclick="updatePlaygroundFromText()">
Apply
</button>
<br><br>
</p>
<!-- <div id="palette-1"></div>
<div id="tester"></div>
<div id="tester2"></div> -->
<div class='Q-circuit-palette'></div>
<div id="playground"></div>
<h4>Result probabilities</h4>
<p>
</p>
<pre><samp id="playground-report"></samp></pre>
<h4>Circuit as ASCII diagram</h4>
<p>
This can be particularly helpful within the JavaScript console
when you need to double check the composition of a circuit
in a way that’s more illustrative than simple plain text.
</p>
<pre><samp id="playground-diagram"></samp></pre>
<!-- h4>Circuit as Amazon Braket code</h4>
<p>
This code can be copied and then pasted in to an
<a href="https://aws.amazon.com/braket/" target="_blank">Amazon Braket</a> Jupyter notebook
to be run on a cloud-based simulator
or actual quantum hardware.
(Requires an
<a href="https://aws.amazon.com/" target="_blank">AWS account</a> and fees may apply.)
</p>
<pre><samp id="playground-braket"></samp></pre -->
<h4>Circuit as LaTeX code</h4>
<p>
This code can be used within
<a href="https://www.latex-project.org/" target="_blank">LaTeX</a> with the
<a href="https://ctan.org/pkg/qcircuit?lang=en" target="_blank">QCircuit</a>
library to create circuit diagrams.
Note that <code>ctrl{}</code> commands do not yet contain valid index values.
<span class="coming-soon">Check back in May 2020.</span>
</p>
<pre><samp id="playground-latex"></samp></pre>
<h4>Circuit as plain text</h4>
<p>
If all’s gone well
then copying this and pasting this code
in to the original text input box
should have no effect on the maths.
</p>
<pre><samp id="playground-text"></samp></pre>
<hr>
<h3>Sample circuits</h3>
<p>
This page previously contained the following pre-loaded sample circuits for experimenting with.
Their plain text representations are included here for reference.
(You can copy and paste their text in to the input field at the top of the page to load them.)
</p>
<h4>Bell state</h4>
<p>
Here’s a <a href="https://en.wikipedia.org/wiki/Bell_state" target="_blank">Bell state</a>
(which produces <a href="https://en.wikipedia.org/wiki/Quantum_entanglement" target="_blank">quantum entanglement</a>),
but with
<a href="Q-Gate.html#.PAULI_X">Pauli X</a>
operations added to both registers as a finale—so
the only two possible results are
<span class="complex-vector ket">00</span>
and
<span class="complex-vector ket">11</span>; all or nothing!
</p>
<pre><code>
H X#0 X
I X#1 X</code></pre>
<h4>Deutsch oracle</h4>
<p>
which
The <a href="https://en.wikipedia.org/wiki/Deutsch%E2%80%93Jozsa_algorithm" target="_blank">Deutsch–Jozsa algorithm</a> is a deterministic quantum algorithm proposed by David Deutsch and Richard Jozsa in 1992 with improvements by Richard Cleve, Artur Ekert, Chiara Macchiavello, and Michele Mosca in 1998.[1][2] Although of little practical use, it is one of the first examples of a quantum algorithm that is exponentially faster than any possible deterministic classical algorithm and is the inspiration for Simon's Algorithm, which is, in turn, the inspiration for Shor's Algorithm.
[Wikipedia]
</p>
<pre><code>
H-I-X#0-I---I-H
H-I-I---X#0-I-H
X-H-X#1-X#1-H-X</code></pre>
<h4>Whiplash</h4>
<p>
This monster demonstrates some
overlapping multi-register operations
and an “upside-down CNOT” finale.
Notice too how in the diagram version
the boxes within each column (moment) are equal widths
and there’s an attempt to center the text labels within their boxes.
Also note how the overlapping connectors are dealt with, eh?
Of course this circuit would make little sense in the real world.
It’s here purely to test different aspects of rendering a circuit visually.
</p>
<!-- p>
Also—have you noticed that you can drag operations from one circuit to another?
You’ll have to get creative with scrolling,
but you can drag operations between here and the Bell state above ;)
</p -->
<pre><code>
H X.0#0 Y Z X#1
H X.1#0 X#0 I X#0
H X.0#1 Y X#0 S
H X X#1 X#1 I
H X.1#1 Y Z S</code></pre>
<br><br><br>
<div class="color-swatch cos" id="color-swatch-1">1</div
><div class="color-swatch cos" id="color-swatch-2">2</div
><div class="color-swatch cos" id="color-swatch-3">3</div
><div class="color-swatch cos" id="color-swatch-4">4</div
><div class="color-swatch cos" id="color-swatch-5">5</div>
</main>
<script>
// Let’s place circuit palettes
// inside anything with the class 'Q-circuit-palette'.
Array
.from( document.querySelectorAll( '.Q-circuit-palette' ))
.forEach( function( el ){
Q.Circuit.Editor.createPalette( el )
})
// Hook up our input text field and “Apply” button.
function applyButtonActivated(){
const button = document.getElementById( 'playground-apply-button' )
if( button && button.getAttribute( 'disabled' ) === null ) updatePlaygroundFromText()
}
let playgroundInputText = ''
function hasTextInputUpdated(){
const
input = document.getElementById( 'playground-input' ).value
button = document.getElementById( 'playground-apply-button' )
if( input === playgroundInputText ){
button.setAttribute( 'disabled', 'disabled' )
}
else button.removeAttribute( 'disabled' )
}
// When our user edits the plain text input
// we want to update our interactive circuit
// and exported code accordingly.
function updatePlaygroundFromText(){
const text = document.getElementById( 'playground-input' ).value
if( text !== playgroundInputText ){
playgroundInputText = text
document.getElementById( 'playground-apply-button' ).setAttribute( 'disabled', 'disabled' )
const circuit = Q( text )
if( circuit instanceof Q.Circuit ){//+++++ This validation appears broken!
circuit.name = 'playground'
const domEl = document.getElementById( 'playground' )
if( domEl ){
while( domEl.lastChild ){
domEl.removeChild( domEl.lastChild )
}
circuit.sort$()// Is this still necessary??
circuit.toDom( domEl )
}
circuit.evaluate$()
}
else {
updateCircuitParts( Q`I` )
console.log( 'There’s an error in your circuit!!' )
}
}
}
// When our user edits the interactive circuit
// we want to update
function updatePlaygroundFromDom( circuit ){
const inputEl = document.getElementById( circuit.name +'-input' )
if( inputEl ){
const text = circuit.toText().substr( 1 )
inputEl.value = text
circuit.evaluate$()
// updateCircuitParts( circuit )
playgroundInputText = text
}
}
// Update all the export code and probabilities report.
function updateCircuitParts( circuit ){
// const braketEl = document.getElementById( circuit.name +'-braket' )
// if( braketEl ) braketEl.innerText = '\n'+ circuit.toAmazonBraket( true )
const latexEl = document.getElementById( circuit.name +'-latex' )
if( latexEl ) latexEl.innerText = '\n'+ circuit.toLatex( true )
const diagramEl = document.getElementById( circuit.name +'-diagram' )
if( diagramEl ) diagramEl.innerText = circuit.toDiagram( true )
const textEl = document.getElementById( circuit.name +'-text' )
if( textEl ) textEl.innerText = circuit.toText()
const reportEl = document.getElementById( circuit.name +'-report' )
if( reportEl ) reportEl.innerText = circuit.report$()
}
window.addEventListener( 'DOMContentLoaded', function(){
updatePlaygroundFromText()
setInterval( hasTextInputUpdated, 100 )
})
// EVALUATION.
window.addEventListener( 'Q.Circuit.evaluate began', function( event ){
console.log(
'\n\nBeginning evaluation for “'+ event.detail.circuit.name +'”\n'+
event.detail.circuit.toDiagram() +'\n\n'
)
})
window.addEventListener( 'Q.Circuit.evaluate progressed', function( event ){
const
length = 20,
percent = Math.round( event.detail.progress * 100 ),
percentText = ( ''+ percent ).padStart( 3 ) +'%',
barLength = Math.round( length * event.detail.progress ),
barComplete = ''.padStart( barLength, '█' ),
barRemaining = ''.padStart( length - barLength, '░' ),
opsTotal = event.detail.operationsTotal,
opsCompleted = ( ''+ event.detail.operationsCompleted ).padStart( Math.log( opsTotal ) * Math.LOG10E + 1 | 0, ' ' )
console.log(
barComplete +
barRemaining +' '+
percentText +' '+
opsCompleted +' of '+
opsTotal
)
// What’s the actual state of the circuit at this moment?
// We should come back and do a cool animated version of this data :)
const state = event.detail.state
// console.log( 'state width', state.getWidth(), 'state height', state.getHeight() )
// console.log( 'state', state.toTsv() )
})
window.addEventListener( 'Q.Circuit.evaluate completed', function( event ){
console.log(
'\nEvaluation completed for “'+ event.detail.circuit.name +'”'+
'\nwith these results:\n'+ event.detail.circuit.report$() +'\n\n\n'
)
const
circuit = event.detail.circuit,
reportEl = document.getElementById( circuit.name +'-report' )
//if( reportEl ) reportEl.innerText = circuit.report$()
updateCircuitParts( circuit )
})
window.addEventListener( 'Q gui altered circuit', function( event ){
updatePlaygroundFromDom( event.detail.circuit )
})
</script>
</body>
</html>