Home Reference Source
public class | source

Simulator

NodeJS implementation of a quantum computer simulator.

This Simulator can be used as a backup if compiling the c++ simulator is not an option (for some reason). It has the same features but is much slower, so please consider building the c++ version for larger experiments.

Constructor Summary

Public Constructor
public

Member Summary

Private Members
private

_map: {}

private
private

_state: *

Method Summary

Public Methods
public

Allocate a qubit.

public

applyControlledGate(m: Array<Array<number>>, ids: number[], ctrlids: number[])

Applies the k-qubit gate matrix m to the qubits with indices ids, using ctrlids as control qubits.

public

applyQubitOperator(termsArray: Array<Array>, IDs: number[])

Apply a (possibly non-unitary) qubit operator to qubits.

public

applyTerm(term: Array, ids: number[], controlIDs: number[])

Applies a QubitOperator term to the state vector.

public

Return the qubit index to bit location map and the corresponding state vector.

public

collapseWavefunction(ids: number[], values: boolean[])

Collapse a quantum register onto a classical basis state.

public

Deallocate a qubit (if it has been measured / uncomputed).

public

emulateMath(f: function, qubitIDs: Array<number[]>, ctrlQubitIDs: number[])

Emulate a math function (e.g., BasicMathGate).

public

emulateTimeEvolution(terms_dict: Array<Array>, time: number, ids: number[], ctrlids: number[])

Applies exp(-itimeH) to the wave function, i.e., evolves under the Hamiltonian H for a given time.

public

getAmplitude(bitString: boolean[] | number[], IDs: number[]): *

Return the probability amplitude of the supplied bit_string.

public

getClassicalValue(ID: number, tolerance: number): *

Return the classical value of a classical bit (i.e., a qubit which has been measured / uncomputed).

public

getControlMask(ctrlids: *): number

Get control mask from list of control qubit IDs.

public

getExpectationValue(termsArray: Array<Array>, IDs: number[]): *

Return the expectation value of a qubit operator w.r.t.

public

getProbability(bitString: boolean[] | number[], IDs: number[]): *

Return the probability of the outcome bit_string when measuring the qubits given by the list of ids.

public

Measure the qubits with IDs ids and return a list of measurement outcomes (true/false).

public

run()

leave it empty to keep same API with cpp simulator

public

setWavefunction(wavefunction: Complex[], ordering: Array)

Set wavefunction and qubit ordering.

Private Methods
private

_multiQubitGate(m: Array<number[]>, pos: number[], mask: number)

Applies the k-qubit gate matrix m to the qubits at pos using mask to identify control qubits.

private

Applies the single qubit gate matrix m to the qubit at position pos using mask to identify control qubits.

Public Constructors

public constructor() source

Private Members

private _map: {} source

private _numQubits: number source

private _state: * source

Public Methods

public allocateQubit(ID: number) source

Allocate a qubit.

Params:

NameTypeAttributeDescription
ID number

ID of the qubit which is being allocated.

public applyControlledGate(m: Array<Array<number>>, ids: number[], ctrlids: number[]) source

Applies the k-qubit gate matrix m to the qubits with indices ids, using ctrlids as control qubits.

Params:

NameTypeAttributeDescription
m Array<Array<number>>

2^k x 2^k complex matrix describing the k-qubit gate.

ids number[]

A list containing the qubit IDs to which to apply the gate.

ctrlids number[]

A list of control qubit IDs (i.e., the gate is only applied where these qubits are 1).

public applyQubitOperator(termsArray: Array<Array>, IDs: number[]) source

Apply a (possibly non-unitary) qubit operator to qubits.

Params:

NameTypeAttributeDescription
termsArray Array<Array>

Operator array (see QubitOperator.terms)

IDs number[]

List of qubit ids upon which the operator acts.

public applyTerm(term: Array, ids: number[], controlIDs: number[]) source

Applies a QubitOperator term to the state vector. (Helper function for time evolution & expectation)

Params:

NameTypeAttributeDescription
term Array

One term of QubitOperator.terms

ids number[]

Term index to Qubit ID mapping

controlIDs number[]

Control qubit IDs

public cheat(): Array source

Return the qubit index to bit location map and the corresponding state vector.

This function can be used to measure expectation values more efficiently (emulation).

Return:

Array

A tuple where the first entry is a dictionary mapping qubit indices to bit-locations and the second entry is the corresponding state vector

public collapseWavefunction(ids: number[], values: boolean[]) source

Collapse a quantum register onto a classical basis state.

Params:

NameTypeAttributeDescription
ids number[]

Qubit IDs to collapse.

values boolean[]

Measurement outcome for each of the qubit IDs in ids.

Throw:

Error

If probability of outcome is ~0 or unknown qubits are provided.

public deallocateQubit(ID: number) source

Deallocate a qubit (if it has been measured / uncomputed).

Params:

NameTypeAttributeDescription
ID number

ID of the qubit to deallocate.

Throw:

Error

If the qubit is in a superposition, i.e., has not been measured / uncomputed.

public emulateMath(f: function, qubitIDs: Array<number[]>, ctrlQubitIDs: number[]) source

Emulate a math function (e.g., BasicMathGate).

Params:

NameTypeAttributeDescription
f function

Function executing the operation to emulate.

qubitIDs Array<number[]>

List of lists of qubit IDs to which the gate is being applied. Every gate is applied to a tuple of quantum registers, which corresponds to this 'list of lists'.

ctrlQubitIDs number[]

List of control qubit ids.

public emulateTimeEvolution(terms_dict: Array<Array>, time: number, ids: number[], ctrlids: number[]) source

Applies exp(-itimeH) to the wave function, i.e., evolves under the Hamiltonian H for a given time. The terms in the Hamiltonian are not required to commute.

This function computes the action of the matrix exponential using ideas from Al-Mohy and Higham, 2011. TODO: Implement better estimates for s.

Params:

NameTypeAttributeDescription
terms_dict Array<Array>

Operator dictionary (see QubitOperator.terms) defining the Hamiltonian.

time number

Time to evolve for

ids number[]

A list of qubit IDs to which to apply the evolution.

ctrlids number[]

A list of control qubit IDs.

public getAmplitude(bitString: boolean[] | number[], IDs: number[]): * source

Return the probability amplitude of the supplied bit_string. The ordering is given by the list of qubit ids.

Params:

NameTypeAttributeDescription
bitString boolean[] | number[]

Computational basis state

IDs number[]

List of qubit ids determining the ordering. Must contain all allocated qubits.

Return:

*

Probability amplitude of the provided bit string.

Throw:

Error

if the second argument is not a permutation of all allocated qubits.

public getClassicalValue(ID: number, tolerance: number): * source

Return the classical value of a classical bit (i.e., a qubit which has been measured / uncomputed).

Params:

NameTypeAttributeDescription
ID number

ID of the qubit of which to get the classical value.

tolerance number

Tolerance for numerical errors when determining whether the qubit is indeed classical.

Return:

*

Throw:

Error

If the qubit is in a superposition, i.e., has not been measured / uncomputed.

public getControlMask(ctrlids: *): number source

Get control mask from list of control qubit IDs.

Params:

NameTypeAttributeDescription
ctrlids *

Return:

number

A mask which represents the control qubits in binary.

public getExpectationValue(termsArray: Array<Array>, IDs: number[]): * source

Return the expectation value of a qubit operator w.r.t. qubit ids.

Params:

NameTypeAttributeDescription
termsArray Array<Array>

Operator Array (see QubitOperator.terms)

IDs number[]

List of qubit ids upon which the operator acts.

Return:

*

Expectation value

public getProbability(bitString: boolean[] | number[], IDs: number[]): * source

Return the probability of the outcome bit_string when measuring the qubits given by the list of ids.

Params:

NameTypeAttributeDescription
bitString boolean[] | number[]

Measurement outcome.

IDs number[]

List of qubit ids determining the ordering.

Return:

*

Probability of measuring the provided bit string.

Throw:

Error

if an unknown qubit id was provided.

public measureQubits(ids: number[]): boolean[] source

Measure the qubits with IDs ids and return a list of measurement outcomes (true/false).

Params:

NameTypeAttributeDescription
ids number[]

List of qubit IDs to measure.

Return:

boolean[]

List of measurement results (containing either true or false).

public run() source

leave it empty to keep same API with cpp simulator

public setWavefunction(wavefunction: Complex[], ordering: Array) source

Set wavefunction and qubit ordering.

Params:

NameTypeAttributeDescription
wavefunction Complex[]

Array of complex amplitudes describing the wavefunction (must be normalized).

ordering Array

List of ids describing the new ordering of qubits (i.e., the ordering of the provided wavefunction).

Private Methods

private _multiQubitGate(m: Array<number[]>, pos: number[], mask: number) source

Applies the k-qubit gate matrix m to the qubits at pos using mask to identify control qubits.

Params:

NameTypeAttributeDescription
m Array<number[]>

2^k x 2^k complex matrix describing the k-qubit gate.

pos number[]

List of bit-positions of the qubits.

mask number

Bit-mask where set bits indicate control qubits. see follows the description in https://arxiv.org/abs/1704.01127

private _singleQubitGate(m: Array<Array<number>>, pos: number, mask: number) source

Applies the single qubit gate matrix m to the qubit at position pos using mask to identify control qubits.

Params:

NameTypeAttributeDescription
m Array<Array<number>>

2x2 complex matrix describing the single-qubit gate.

pos number

Bit-position of the qubit.

mask number

Bit-mask where set bits indicate control qubits.