Source code:
Q.js
The Q object
The root of Q.js is the Q
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 Q
object is actually a function
that wraps the
Q.Circuit.fromText
function.
This makes defining quantum circuits incredibly brief.
var circuit = Q`
H-X#0
I-X#1
`
See “Writing quantum circuits” for more details.
Properties
Help and inspection
-
- help
Function([ f: Function ]) ⇒ String
Calls and returns the value ofextractDocumentation
, passingf
as the argument. If no argument is supplied, theQ
function is passed. -
- extractDocumentation
Function( f: Function ) ⇒ String
Extracts the first Template literal found within the passedFunction
and returns it as aString
. -
- verbosity
Number
Expected to be a number from0
to1
, inclusive. Throttles the output of built-in console logging. -
- log
Function( verbosityThreshold: Number, … )
Passes remaining arguments toconsole.log
if the current value ofverbosity
is greater than or equal to theverbosityThreshold
argument. -
- warn
Function( … )
Wrapsconsole.warn
. -
- error
Function( … )
Wrapsconsole.error
.
Constants and constant creation
-
- constants
Object
Constants are appended directly to theQ
object. For convenience they are also appended to thisQ.constants
object to make looking up constants in the JavaScript console trivial, and to make iterating across all constants convenient via functions likeObject.entries
,Object.keys
,Object.values
, and so on. The intention that a property act as a constant is signaled by its labelling in all-uppercase. -
- createConstant
Function( key: String, value: * )
Appends a property named bykey
with a value ofvalue
to both theQ
object and itsconstants
property. -
- createConstants
Function( … )
Expects an even number of arguments. Will use each pair in the sequence of arguments to callcreateConstant
. -
- REVISION
Number
The code revision number. The version of Q currently loaded on this web page is revision #NOT FOUND. -
- EPSILON
Number
Initialized equal toNumber.EPSILON * 6
. In the future we may wish to incorporate something like bignumber.js 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:var a = new Q.ComplexNumber( 1, 2 ) a.multiply( a ).isEqualTo( a.power( 2 ))
true -
- RADIANS_TO_DEGREES
Number
Initialized equal to180 / Math.PI
. -
- COLORS
Array
Arriving soon. Intended for future use in assigning human-readable, unique-ish identification labels to various objects; in combination with theANIMALS
property. Some examples: Blue Panda, Orange Ocelot, Purple Reindeer. -
- ANIMALS
Array
Arriving soon. Intended for future use in assigning human-readable, unique-ish identification labels to various objects; in combination with theCOLORS
property. Some examples: Pink Camel, Silver Badger, Red Seahorse. -
- ANIMALS3
Array
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.
Maths
-
- hypotenuse
Function( x: Number, y: Number ) ⇒ Number
Returns the hypotenuse length of a right-angled triangle whose other two (shorter) sides are lengthsx
andy
. -
- logHypotenuse
Function( x: Number, y: Number ) ⇒ Number
Returns half of the logarithm ofx
2 +y
2. -
- hyperbolicSine
Function( n: Number ) ⇒ Number
Returns the hyperbolic sine ofn
. -
- hyperbolicCosine
Function( n: Number ) ⇒ Number
Returns the hyperbolic cosine ofn
. -
- round
Function( n: Number[, d: Number ]) ⇒ Number
Returns a number,n
, rounded tod
decimal places. Ifd
is not provided thenn
is rounded to the nearest whole number.
Miscellaneous
-
- toTitleCase
Function( text: String ) ⇒ String
For the suppliedString
replaces underscores with spaces, breaks apart text in to words, capitalizes the first letter of each word, then returns a newString
with these changes. -
- centerText
Function( text: String, length: Number[, filler: String ]) ⇒ String
Returns aString
at leastlength
characters long containing the originaltext
and padded on either side withfiller
such that thetext
is more or less centered within the returnedString
.