Home Reference Source
public class | source

Simulator

Extends:

BasicEngine → Simulator

Simulator is a compiler engine which simulates a quantum computer using C++-based kernels.

OpenMP is enabled and the number of threads can be controlled using the OMP_NUM_THREADS environment variable, i.e.

Example:


export OMP_NUM_THREADS=4 # use 4 threads
export OMP_PROC_BIND=spread # bind threads to processors by spreading

Constructor Summary

Public Constructor
public

constructor(gate_fusion: boolean, rnd_seed: number, forceSimulation: boolean)

Member Summary

Private Members
private
private

Method Summary

Public Methods
public

applyQubitOperator(qubitOperator: QubitOperator, qureg: Array<Qubit> | Qureg): *

Apply a (possibly non-unitary) qubit_operator to the current wave function represented by the supplied quantum register.

public

Access the ordering of the qubits and the state vector directly.

public

collapseWavefunction(qureg: Qureg | Array<Qubit>, values: boolean[]): *

Collapse a quantum register onto a classical basis state.

public

Converts a qureg from logical to mapped qubits if there is a mapper.

public

getAmplitude(bitString: number[] | string, qureg: Qureg | Array<Qubit>): number

Return the probability amplitude of the supplied bit_string.

public

getExpectationValue(qubitOperator: QubitOperator, qureg: Array<Qubit> | Qureg): *

Get the expectation value of qubit_operator w.r.t.

public

getProbability(bitString: number[] | string, qureg: Qureg | Array<Qubit>): number

Return the probability of the outcome bit_string when measuring the quantum register qureg.

public

handle(cmd: Command)

Handle all commands, i.e., call the member functions of the C++- simulator object corresponding to measurement, allocation/ deallocation, and (controlled) single-qubit gate.

public

Specialized implementation of isAvailable: The simulator can deal with all arbitrarily-controlled gates which provide a gate-matrix (via gate.matrix) and acts on 5 or less qubits (not counting the control qubits).

public

receive(commandList: Command[])

Receive a list of commands from the previous engine and handle them (simulate them classically) prior to sending them on to the next engine.

public

setWavefunction(wavefunction: Complex[], qureg: Qureg | Array<Qubit>)

Set the wavefunction and the qubit ordering of the simulator.

Inherited Summary

From class BasicEngine
public
public

Return a new qubit as a list containing 1 qubit object (quantum register of size 1).

public

Allocate n qubits and return them as a quantum register, which is a list of qubit objects.

public

Deallocate a qubit (and sends the deallocation command down the pipeline).

public

Default implementation of isAvailable: Ask the next engine whether a command is available, i.e., whether it can be executed by the next engine(s).

public

Check if there is a compiler engine handling the meta tag

public
public

send(commandList: Command[])

Forward the list of commands to the next engine in the pipeline.

Public Constructors

public constructor(gate_fusion: boolean, rnd_seed: number, forceSimulation: boolean) source

Override:

BasicEngine#constructor

Params:

NameTypeAttributeDescription
gate_fusion boolean

If true, gates are cached and only executed once a certain gate-size has been reached (only has an effect for the c++ simulator).

rnd_seed number

Random seed (uses random.randint(0, 4294967295) by default). Ignored currently!!!

forceSimulation boolean

if true, will force use cpp simulator

Example of gate_fusion Instead of applying a Hadamard gate to 5 qubits, the simulator calculates the kronecker product of the 1-qubit gate matrices and then applies one 5-qubit gate. This increases operational intensity and keeps the simulator from having to iterate through the state vector multiple times. Depending on the system (and, especially, number of threads), this may or may not be beneficial.

Note: If the C++ Simulator extension was not built or cannot be found, the Simulator defaults to a Javascript implementation of the kernels. While this is much slower, it is still good enough to run basic quantum algorithms.

If you need to run large simulations, check out the tutorial in the docs which gives futher hints on how to build the C++ extension.

Private Members

private _gate_fusion: * source

private _simulator: * source

Public Methods

public applyQubitOperator(qubitOperator: QubitOperator, qureg: Array<Qubit> | Qureg): * source

Apply a (possibly non-unitary) qubit_operator to the current wave function represented by the supplied quantum register.

Params:

NameTypeAttributeDescription
qubitOperator QubitOperator

Operator to apply.

qureg Array<Qubit> | Qureg

Quantum bits to which to apply the operator.

Return:

*

Throw:

Error

If qubit_operator acts on more qubits than present in the qureg argument.

Warning: This function allows applying non-unitary gates and it will not re-normalize the wave function! It is for numerical experiments only and should not be used for other purposes.

Note: Make sure all previous commands (especially allocations) have passed through the compilation chain (call main.flush() to make sure).

Note: If there is a mapper present in the compiler, this function automatically converts from logical qubits to mapped qubits for the qureg argument.

public cheat(): Array source

Access the ordering of the qubits and the state vector directly.

This is a cheat function which enables, e.g., more efficient evaluation of expectation values and debugging.

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.

Note: Make sure all previous commands have passed through the compilation chain (call main.flush() to make sure).

Note: If there is a mapper present in the compiler, this function DOES NOT automatically convert from logical qubits to mapped qubits.

public collapseWavefunction(qureg: Qureg | Array<Qubit>, values: boolean[]): * source

Collapse a quantum register onto a classical basis state.

Params:

NameTypeAttributeDescription
qureg Qureg | Array<Qubit>

Qubits to collapse.

values boolean[]

Measurement outcome for each of the qubits in qureg.

Return:

*

Throw:

Error

If an outcome has probability (approximately) 0 or if unknown qubits are provided (see note).

Note: Make sure all previous commands have passed through the compilation chain (call main.flush() to make sure).

Note: If there is a mapper present in the compiler, this function automatically converts from logical qubits to mapped qubits for the qureg argument.

public convertLogicalToMappedQureg(qureg: Array<Qubit> | Qureg): * source

Converts a qureg from logical to mapped qubits if there is a mapper.

Params:

NameTypeAttributeDescription
qureg Array<Qubit> | Qureg

Logical quantum bits

Return:

*

public getAmplitude(bitString: number[] | string, qureg: Qureg | Array<Qubit>): number source

Return the probability amplitude of the supplied bit_string. The ordering is given by the quantum register qureg, which must contain all allocated qubits.

Params:

NameTypeAttributeDescription
bitString number[] | string

Computational basis state

qureg Qureg | Array<Qubit>

Quantum register determining the ordering. Must contain all allocated qubits.

Return:

number

Probability amplitude of the provided bit string.

Note: Make sure all previous commands (especially allocations) have passed through the compilation chain (call main.flush() to make sure).

Note: If there is a mapper present in the compiler, this function automatically converts from logical qubits to mapped qubits for the qureg argument.

public getExpectationValue(qubitOperator: QubitOperator, qureg: Array<Qubit> | Qureg): * source

Get the expectation value of qubit_operator w.r.t. the current wave function represented by the supplied quantum register.

Params:

NameTypeAttributeDescription
qubitOperator QubitOperator

Operator to measure.

qureg Array<Qubit> | Qureg

Quantum bits to measure.

Return:

*

Expectation value

Note: Make sure all previous commands (especially allocations) have passed through the compilation chain (call main.flush() to make sure).

Note: If there is a mapper present in the compiler, this function automatically converts from logical qubits to mapped qubits for the qureg argument.

Throw:

Error

If qubit_operator acts on more qubits than present in the qureg argument.

public getProbability(bitString: number[] | string, qureg: Qureg | Array<Qubit>): number source

Return the probability of the outcome bit_string when measuring the quantum register qureg.

Params:

NameTypeAttributeDescription
bitString number[] | string

Measurement outcome.

qureg Qureg | Array<Qubit>

Quantum register.

Return:

number

Probability of measuring the provided bit string.

Note: Make sure all previous commands (especially allocations) have passed through the compilation chain (call main.flush() to make sure).

Note: If there is a mapper present in the compiler, this function automatically converts from logical qubits to mapped qubits for the qureg argument.

public handle(cmd: Command) source

Handle all commands, i.e., call the member functions of the C++- simulator object corresponding to measurement, allocation/ deallocation, and (controlled) single-qubit gate.

Params:

NameTypeAttributeDescription
cmd Command

Command to handle.

Throw:

*

Error If a non-single-qubit gate needs to be processed (which should never happen due to isAvailable).

public isAvailable(cmd: Command): boolean source

Specialized implementation of isAvailable: The simulator can deal with all arbitrarily-controlled gates which provide a gate-matrix (via gate.matrix) and acts on 5 or less qubits (not counting the control qubits).

Override:

BasicEngine#isAvailable

Params:

NameTypeAttributeDescription
cmd Command

Command for which to check availability (single-qubit gate, arbitrary controls)

Return:

boolean

true if it can be simulated and false otherwise.

public receive(commandList: Command[]) source

Receive a list of commands from the previous engine and handle them (simulate them classically) prior to sending them on to the next engine.

Override:

BasicEngine#receive

Params:

NameTypeAttributeDescription
commandList Command[]

List of commands to execute on the simulator.

public setWavefunction(wavefunction: Complex[], qureg: Qureg | Array<Qubit>) source

Set the wavefunction and the qubit ordering of the simulator.

The simulator will adopt the ordering of qureg (instead of reordering the wavefunction).

Params:

NameTypeAttributeDescription
wavefunction Complex[]

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

qureg Qureg | Array<Qubit>

Quantum register determining the ordering. Must contain all allocated qubits.

Note: Make sure all previous commands (especially allocations) have passed through the compilation chain (call main.flush() to make sure).

Note: If there is a mapper present in the compiler, this function automatically converts from logical qubits to mapped qubits for the qureg argument.