-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q.html
358 lines (341 loc) · 13.2 KB
/
Q.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
<!DOCTYPE html>
<html>
<head>
<title>Q ⟩</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 ⟩">
<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 ⟩">
<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/Q.html">
<link rel="canonical" href="https://quantumjavascript.app/Q.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 class="api">
<p>
Source code:
<a href="https://github.com/stewdio/q.js/blob/master/source/Q.js?ts=4" target="_blank">
<code>Q.js</code>
</a>
</p>
<hr>
<h3>The Q object</h3>
<p>
The root of Q.js is the <code>Q</code> object.
It provides a single point for attaching all other Q.js functionality to,
thereby avoiding pollution of the global namespace
while keeping everything accessible to us.
It also provides some convenience properties and methods for use across the library.
Most conveniently,
the <code>Q</code> object is actually a function
that wraps the
<code>Q.<a href="Q-Circuit.html">Circuit</a>.<a href="Q-Circuit.html#fromText">fromText</a></code> function.
This makes defining quantum circuits incredibly brief.
</p>
<pre><code>
var circuit = Q`
<strong>H</strong>-<strong>X#0</strong>
<strong>I</strong>-<strong>X#1</strong>
`
</code></pre>
<p>
See <a href="Q-Circuit.html#Writing_quantum_circuits">“Writing quantum circuits”</a> for more details.
</p>
<hr>
<h3>Properties</h3>
<h4>Help and inspection</h4>
<ul class="properties">
<li>
<dt id=".help">help</dt>
<dd>
<code class="value-type">Function([ f: Function ]) ⇒ String</code>
Calls and returns the value of
<code><a href="#.extractDocumentation">extractDocumentation</a></code>,
passing <code>f</code> as the argument.
If no argument is supplied,
the <code>Q</code> function is passed.
</dd>
</li>
<li>
<dt id=".extractDocumentation">extractDocumentation</dt>
<dd>
<code class="value-type">Function( f: Function ) ⇒ String</code>
Extracts the first
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">Template literal</a>
found within the passed <code>Function</code>
and returns it as a <code>String</code>.
</dd>
</li>
<li>
<dt id=".verbosity">verbosity</dt>
<dd>
<code class="value-type">Number</code>
Expected to be a number from <code>0</code> to <code>1</code>, inclusive.
Throttles the output of built-in console logging.
</dd>
</li>
<li>
<dt id=".log">log</dt>
<dd>
<code class="value-type">Function( verbosityThreshold: Number, … )</code>
Passes remaining arguments to <code>console.log</code>
if the current value of <code><a href="#.verbosity">verbosity</a></code>
is greater than or equal to the <code>verbosityThreshold</code> argument.
</dd>
</li>
<li>
<dt id=".warn">warn</dt>
<dd>
<code class="value-type">Function( … )</code>
Wraps <code>console.warn</code>.
</dd>
</li>
<li>
<dt id=".error">error</dt>
<dd>
<code class="value-type">Function( … )</code>
Wraps <code>console.error</code>.
</dd>
</li>
</ul>
<h4>Constants and constant creation</h4>
<ul class="properties">
<li>
<dt id=".constants">constants</dt>
<dd>
<code class="value-type">Object</code>
Constants are appended <em>directly</em> to the
<code>Q</code> object.
For convenience they are also appended to this
<code>Q.constants</code> object
to make looking up constants in the JavaScript console trivial,
and to make iterating across all constants convenient via functions like
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries" target="_blank">Object.entries</a></code>,
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys" target="_blank">Object.keys</a></code>,
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values" target="_blank">Object.values</a></code>,
and so on.
<!-- Configured to be unwritable once appended via
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty" target="_blank">Object.defineProperty</a></code>
and they are labeled in uppercase to signal to us that this is so. -->
The intention that a property act as a constant is signaled
by its labelling in all-uppercase.
</dd>
</li>
<li>
<dt id=".createConstant">createConstant</dt>
<dd>
<code class="value-type">Function( key: String, value: * )<!-- → undefined --></code>
Appends a property named by <code>key</code>
with a value of <code>value</code>
to both the
<code>Q</code> object
and its <code><a href="#.constants">constants</a></code> property.
</dd>
</li>
<li>
<dt id=".createConstants">createConstants</dt>
<dd>
<code class="value-type">Function( … )</code>
Expects an even number of arguments.
Will use each pair in the sequence of arguments to call
<code><a href="#.createConstant">createConstant</a></code>.
</dd>
</li>
<li>
<dt id=".REVISION">REVISION</dt>
<dd>
<code class="value-type">Number</code>
The code revision number.
The version of Q currently loaded on this web page is revision #<strong id="q-revision">NOT FOUND</strong>.
</dd>
</li>
<li>
<dt id=".EPSILON">EPSILON</dt>
<dd>
<code class="value-type">Number</code>
Initialized equal to <code>Number.EPSILON * 6</code>.
In the future we may wish to incorporate something like
<a href="https://github.com/MikeMcl/bignumber.js" target="_blank">bignumber.js</a> in to Q,
but for now this higher epsilon value is necessary
for the following equality to be true
when using JavaScript’s floating point implementation:
<pre><code>
var a = new Q.<a href="Q-ComplexNumber.html">ComplexNumber</a>( 1, 2 )
a.<a href="Q-ComplexNumber.html#.prototype.multiply">multiply</a>( a ).<a href="Q-ComplexNumber.html#.prototype.multiply">isEqualTo</a>( a.<a href="Q-ComplexNumber.html#.prototype.power">power</a>( 2 ))
</code>
<samp>true</samp></pre>
</dd>
</li>
<li>
<dt id=".RADIANS_TO_DEGREES">RADIANS_TO_DEGREES</dt>
<dd>
<code class="value-type">Number</code>
Initialized equal to <code>180 / Math.PI</code>.
</dd>
</li>
<li>
<dt id=".COLORS">COLORS</dt>
<dd>
<code class="value-type">Array</code>
Arriving soon.
Intended for future use in assigning human-readable,
unique-ish identification labels
to various objects;
in combination with the <code><a href="#.ANIMALS">ANIMALS</a></code> property.
Some examples:
Blue Panda,
Orange Ocelot,
Purple Reindeer.
</dd>
</li>
<li>
<dt id=".ANIMALS">ANIMALS</dt>
<dd>
<code class="value-type">Array</code>
Arriving soon.
Intended for future use in assigning human-readable,
unique-ish identification labels
to various objects;
in combination with the <code><a href="#.COLORS">COLORS</a></code> property.
Some examples:
Pink Camel,
Silver Badger,
Red Seahorse.
</dd>
</li>
<li>
<dt id=".ANIMALS3">ANIMALS3</dt>
<dd>
<code class="value-type">Array</code>
An alphabetical list of animal names,
each exactly three letters long,
one per letter of the alphabet.
(The current list falls short of running the full alphabet by L, M, Q, U, V, W, X and Z.)
Many of these names are currently used as variable names in these documentation code examples.
</dd>
</li>
</ul>
<h4>Maths</h4>
<ul class="properties">
<li>
<dt id=".hypotenuse">hypotenuse</dt>
<dd>
<code class="value-type">Function( x: Number, y: Number ) ⇒ Number</code>
Returns the
<a href="https://en.wikipedia.org/wiki/Hypotenuse" target="_blank">hypotenuse</a>
length of a right-angled triangle whose other two (shorter) sides are lengths
<code>x</code> and <code>y</code>.
</dd>
</li>
<li>
<dt id=".logHypotenuse">logHypotenuse</dt>
<dd>
<code class="value-type">Function( x: Number, y: Number ) ⇒ Number</code>
Returns half of the
<a href="https://en.wikipedia.org/wiki/Logarithm" target="_blank">logarithm</a> of
<code>x</code><sup>2</sup> + <code>y</code><sup>2</sup>.
</dd>
</li>
<li>
<dt id=".hyperbolicSine">hyperbolicSine</dt>
<dd>
<code class="value-type">Function( n: Number ) ⇒ Number</code>
Returns the
<a href="https://en.wikipedia.org/wiki/Hyperbolic_function" target="_blank">hyperbolic sine</a>
of <code>n</code>.
</dd>
</li>
<li>
<dt id=".hyperbolicCosine">hyperbolicCosine</dt>
<dd>
<code class="value-type">Function( n: Number ) ⇒ Number</code>
Returns the
<a href="https://en.wikipedia.org/wiki/Hyperbolic_function" target="_blank">hyperbolic cosine</a>
of <code>n</code>.
</dd>
</li>
<li>
<dt id=".round">round</dt>
<dd>
<code class="value-type">Function( n: Number[, d: Number ]) ⇒ Number</code>
Returns a number, <code>n</code>,
rounded to <code>d</code> decimal places.
If <code>d</code> is not provided
then <code>n</code> is rounded to the nearest whole number.
</dd>
</li>
</ul>
<h4>Miscellaneous</h4>
<ul class="properties">
<li>
<dt id=".toTitleCase">toTitleCase</dt>
<dd>
<code class="value-type">Function( text: String ) ⇒ String</code>
For the supplied <code>String</code>
replaces underscores with spaces,
breaks apart text in to words,
capitalizes the first letter of each word,
then returns a new <code>String</code> with these changes.
</dd>
</li>
<li>
<dt id=".centerText">centerText</dt>
<dd>
<code class="value-type">Function( text: String, length: Number[, filler: String ]) ⇒ String</code>
Returns a <code>String</code>
at least <code>length</code> characters long
containing the original <code>text</code>
and padded on either side with <code>filler</code>
such that the <code>text</code> is more or less centered
within the returned <code>String</code>.
</dd>
</li>
</ul>
</main>
<script>
// Insert the current Q.js revision number in to the description of the REVISION property.
document.getElementById( 'q-revision' ).innerText = Q.REVISION
</script>
</body>
</html>