Quantum Circuit Inspector

Dec 6, 2014

HTML

<canvas id="drawCanvas" width="800" height="550" style="border:1px solid #000000;"></canvas>

JavaScript

/**
 * A Complex number of the form a + bi, where i is the square root of -1.
 * @param real The real part of the Complex number. The 'a' in a + bi.
 * @param imag The imaginary part of the Complex number. The 'b' in a + bi.
 * @class
 */
function Complex(real, imag) {
    this.real = real;
    this.imag = imag;
}

/**
 * Wraps the given number into a Complex value (unless it's already a Complex value).
 * @param {number|Complex} v
 * @returns {Complex)
 */
Complex.from = function (v) {
    if (v instanceof Complex) {
        return v;
    }
    if (typeof v == "number") {
        return new Complex(v, 0);
    }
    throw "Don't know how create a Complex equal to: " + v;
};

/**
 * Returns the real component of a Complex, integer, or float value.
 * @param {number|Complex} v
 * @returns {number)
 */
Complex.real = function (v) {
    if (v instanceof Complex) {
        return v.real;
    }
    if (typeof v == "number") {
        return v;
    }
    throw "Don't know how to get real part of: " + v;
};

/**
 * Returns the imaginary component of a Complex value, or else 0 for integer and float values.
 * @param {number|Complex} v
 * @returns {number)
 */
Complex.imag = function (v) {
    if (v instanceof Complex) {
        return v.imag;
    }
    if (typeof v == "number") {
        return 0;
    }
    throw "Don't know how to get imaginary part of: " + v;
};

/**
 * Determines if the receiving complex value is equal to the given complex, integer, or float value.
 * This method returns false, instead of throwing, when given badly typed arguments.
 * @param {number|Complex|object} other
 * @returns {boolean}
 */
Complex.prototype.isEqualTo = function (other) {
    if (other instanceof Complex) {
        return this.real == other.real && this.imag == other.imag;
    }
    if (typeof other == "number") {
        return this.real == other;
    }
    return false;
};

/**
 * Returns a compact text representation of the receiving complex value.
 * @returns {String}
...