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 ]) ⇒ StringCalls and returns the value ofextractDocumentation, passingfas the argument. If no argument is supplied, theQfunction is passed. -
- extractDocumentation
Function( f: Function ) ⇒ StringExtracts the first Template literal found within the passedFunctionand returns it as aString. -
- verbosity
NumberExpected to be a number from0to1, inclusive. Throttles the output of built-in console logging. -
- log
Function( verbosityThreshold: Number, … )Passes remaining arguments toconsole.logif the current value ofverbosityis greater than or equal to theverbosityThresholdargument. -
- warn
Function( … )Wrapsconsole.warn. -
- error
Function( … )Wrapsconsole.error.
Constants and constant creation
-
- constants
ObjectConstants are appended directly to theQobject. For convenience they are also appended to thisQ.constantsobject 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 bykeywith a value ofvalueto both theQobject and itsconstantsproperty. -
- createConstants
Function( … )Expects an even number of arguments. Will use each pair in the sequence of arguments to callcreateConstant. -
- REVISION
NumberThe code revision number. The version of Q currently loaded on this web page is revision #NOT FOUND. -
- EPSILON
NumberInitialized 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
NumberInitialized equal to180 / Math.PI. -
- COLORS
ArrayArriving soon. Intended for future use in assigning human-readable, unique-ish identification labels to various objects; in combination with theANIMALSproperty. Some examples: Blue Panda, Orange Ocelot, Purple Reindeer. -
- ANIMALS
ArrayArriving soon. Intended for future use in assigning human-readable, unique-ish identification labels to various objects; in combination with theCOLORSproperty. Some examples: Pink Camel, Silver Badger, Red Seahorse. -
- ANIMALS3
ArrayAn 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 ) ⇒ NumberReturns the hypotenuse length of a right-angled triangle whose other two (shorter) sides are lengthsxandy. -
- logHypotenuse
Function( x: Number, y: Number ) ⇒ NumberReturns half of the logarithm ofx2 +y2. -
- hyperbolicSine
Function( n: Number ) ⇒ NumberReturns the hyperbolic sine ofn. -
- hyperbolicCosine
Function( n: Number ) ⇒ NumberReturns the hyperbolic cosine ofn. -
- round
Function( n: Number[, d: Number ]) ⇒ NumberReturns a number,n, rounded toddecimal places. Ifdis not provided thennis rounded to the nearest whole number.
Miscellaneous
-
- toTitleCase
Function( text: String ) ⇒ StringFor the suppliedStringreplaces underscores with spaces, breaks apart text in to words, capitalizes the first letter of each word, then returns a newStringwith these changes. -
- centerText
Function( text: String, length: Number[, filler: String ]) ⇒ StringReturns aStringat leastlengthcharacters long containing the originaltextand padded on either side withfillersuch that thetextis more or less centered within the returnedString.