Simulator
Extends:
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 |
_gate_fusion: * |
|
private |
_simulator: * |
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 |
convertLogicalToMappedQureg(qureg: Array<Qubit> | Qureg): * Converts a qureg from logical to mapped qubits if there is a mapper. |
|
public |
Return the probability amplitude of the supplied |
|
public |
getExpectationValue(qubitOperator: QubitOperator, qureg: Array<Qubit> | Qureg): * Get the expectation value of qubit_operator w.r.t. |
|
public |
Return the probability of the outcome |
|
public |
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 |
isAvailable(cmd: Command): boolean 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 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 |
allocateQubit(dirty: boolean): Qureg Return a new qubit as a list containing 1 qubit object (quantum register of size 1). |
|
public |
allocateQureg(n: number): Qureg Allocate n qubits and return them as a quantum register, which is a list of qubit objects. |
|
public |
deallocateQubit(qubit: BasicQubit) Deallocate a qubit (and sends the deallocation command down the pipeline). |
|
public |
isAvailable(cmd: Command): boolean 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 |
isMetaTagSupported(metaTag: function): boolean Check if there is a compiler engine handling the meta tag |
|
public |
receive() |
|
public |
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#constructorParams:
Name | Type | Attribute | Description |
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. |
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:
Name | Type | Attribute | Description |
qubitOperator | QubitOperator | Operator to apply. |
|
qureg | Array<Qubit> | Qureg | Quantum bits to which to apply the operator. |
Return:
* |
Throw:
If 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.
Return:
* |
Throw:
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.
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.
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:
Name | Type | Attribute | Description |
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:
If |
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
.
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:
Name | Type | Attribute | Description |
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#isAvailableParams:
Name | Type | Attribute | Description |
cmd | Command | Command for which to check availability (single-qubit gate, arbitrary controls) |
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#receiveParams:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
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. |