Q ⟩

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 of extractDocumentation, passing f as the argument. If no argument is supplied, the Q function is passed.
  • extractDocumentation
    Function( f: Function ) ⇒ String Extracts the first Template literal found within the passed Function and returns it as a String.
  • verbosity
    Number Expected to be a number from 0 to 1, inclusive. Throttles the output of built-in console logging.
  • log
    Function( verbosityThreshold: Number, … ) Passes remaining arguments to console.log if the current value of verbosity is greater than or equal to the verbosityThreshold argument.
  • warn
    Function( … ) Wraps console.warn.
  • error
    Function( … ) Wraps console.error.

Constants and constant creation

  • constants
    Object Constants are appended directly to the Q object. For convenience they are also appended to this Q.constants object to make looking up constants in the JavaScript console trivial, and to make iterating across all constants convenient via functions like Object.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 by key with a value of value to both the Q object and its constants property.
  • createConstants
    Function( … ) Expects an even number of arguments. Will use each pair in the sequence of arguments to call createConstant.
  • REVISION
    Number The code revision number. The version of Q currently loaded on this web page is revision #NOT FOUND.
  • EPSILON
    Number Initialized equal to Number.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 to 180 / Math.PI.
  • COLORS
    Array Arriving soon. Intended for future use in assigning human-readable, unique-ish identification labels to various objects; in combination with the ANIMALS 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 the COLORS 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 lengths x and y.
  • logHypotenuse
    Function( x: Number, y: Number ) ⇒ Number Returns half of the logarithm of x2 + y2.
  • hyperbolicSine
    Function( n: Number ) ⇒ Number Returns the hyperbolic sine of n.
  • hyperbolicCosine
    Function( n: Number ) ⇒ Number Returns the hyperbolic cosine of n.
  • round
    Function( n: Number[, d: Number ]) ⇒ Number Returns a number, n, rounded to d decimal places. If d is not provided then n is rounded to the nearest whole number.

Miscellaneous

  • toTitleCase
    Function( text: String ) ⇒ String For the supplied String replaces underscores with spaces, breaks apart text in to words, capitalizes the first letter of each word, then returns a new String with these changes.
  • centerText
    Function( text: String, length: Number[, filler: String ]) ⇒ String Returns a String at least length characters long containing the original text and padded on either side with filler such that the text is more or less centered within the returned String.