backends

The HiQ backends package.

hiq.projectq.backends.SimulatorMPI([…])

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

hiq.projectq.backends.StabilizerSimulator(…)

StabilizerSimulator is a compiler engine which simulates a quantum computer using the stabilizer formalism written in C++.

Module contents

Contains back-ends for HiQ.

This includes:

  • a full-amplitude quantum simulator

  • a single amplitude simulator

  • a stabilizer circuit simulator

class hiq.projectq.backends.SimulatorMPI(gate_fusion=False, rnd_seed=None, num_local_qubits=33, max_fused_qubits=4)[source]

SimulatorMPI 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.

export OMP_NUM_THREADS=4 # use 4 threads
export OMP_PROC_BIND=spread # bind threads to processors by spreading
__init__(gate_fusion=False, rnd_seed=None, num_local_qubits=33, max_fused_qubits=4)[source]

Construct the C++/Python-simulator object and initialize it with a random seed.

Parameters
  • gate_fusion (bool) – 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 (int) – Random seed (uses random.randint(0, 4294967295) by default).

  • num_local_qubits (int) – maximum number of qubits the MPI node can allocate by itself

  • max_fused_qubits (int) – the maximum number of qubits the fused gate can act on

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.

apply_qubit_operator(qubit_operator, qureg)[source]

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

Parameters
  • qubit_operator (projectq.ops.QubitOperator) – Operator to apply.

  • qureg (list[Qubit],Qureg) – Quantum bits to which to apply the operator.

Raises

Exception – 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_engine.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.

cheat()[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.

Returns

A tuple (id2pos, tot_vec) where the first entry is a dictionary mapping qubit indices to bit-locations and the second entry is the corresponding state vector.

Note

The function performs MPI_Allgather() and returns concatenated full state vector. It is always concatenated from np parts, so it is presumed that there are log(np) global qubits. One should check qubit ordering returned to find out which bit positions are valid. For example: np = 4, id2pos = {0:0, 1:2} then state vector size is 8 and valid data at indices 0, 1, 4, 5.

Note

Make sure all previous commands have passed through the compilation chain (call main_engine.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.

cheat_local()[source]

Access the ordering of the qubits and this MPI process’s part of of state vector directly.

Returns

A tuple where the first entry is a dictionary mapping qubit indices to bit-locations (all qubits) and the second entry is the local part of state vector.

collapse_wavefunction(qureg, values)[source]

Collapse a quantum register onto a classical basis state.

Parameters
  • qureg (Qureg|list[Qubit]) – Qubits to collapse.

  • values (list[bool|int]|string[0|1]) – Measurement outcome for each of the qubits in qureg.

Raises

RuntimeError – 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_engine.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.

get_amplitude(bit_string, qureg)[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.

Parameters
  • bit_string (list[bool|int]|string[0|1]) – Computational basis state

  • qureg (Qureg|list[Qubit]) – Quantum register determining the ordering. Must contain all allocated qubits.

Returns

Probability amplitude of the provided bit string.

Note

Make sure all previous commands (especially allocations) have passed through the compilation chain (call main_engine.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.

get_expectation_value(qubit_operator, qureg)[source]

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

Parameters
  • qubit_operator (projectq.ops.QubitOperator) – Operator to measure.

  • qureg (list[Qubit],Qureg) – Quantum bits to measure.

Returns

Expectation value

Note

Make sure all previous commands (especially allocations) have passed through the compilation chain (call main_engine.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.

Raises

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

get_global_qubits_ids()[source]
Returns

A list of global qubits ids

get_local_qubits_ids()[source]
Returns

A list of local qubits ids

get_probability(bit_string, qureg)[source]

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

Parameters
  • bit_string (list[bool|int]|string[0|1]) – Measurement outcome.

  • qureg (Qureg|list[Qubit]) – Quantum register.

Returns

Probability of measuring the provided bit string.

Note

Make sure all previous commands (especially allocations) have passed through the compilation chain (call main_engine.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.

get_qubits_ids()[source]
Returns

A list of all qubits ids allocated in simulator

is_available(cmd)[source]

Specialized implementation of is_available: 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).

Parameters

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

Returns

True if it can be simulated and False otherwise.

receive(command_list)[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.

Parameters

command_list (list<Command>) – List of commands to execute on the simulator.

set_qubits_perm(ids)[source]

Sets the initial permutation of qubits in simulator just after a first Qureg has been allocated

Parameters

ids (list[int]) – list of all qubits ids

set_wavefunction(wavefunction, qureg)[source]

Set the wavefunction and the qubit ordering of the simulator.

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

Parameters
  • wavefunction (list[complex]) – Array of complex amplitudes describing the wavefunction (must be normalized).

  • qureg (Qureg|list[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_engine.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.

class hiq.projectq.backends.StabilizerSimulator(num_qubits, rnd_seed=None)[source]

StabilizerSimulator is a compiler engine which simulates a quantum computer using the stabilizer formalism written in C++.

__init__(num_qubits, rnd_seed=None)[source]

Construct the C++/Python-simulator object and initialize it with a random seed.

Parameters
  • num_qubits (int) – Number of qubits

  • rnd_seed (int) – Random seed (uses random.randint(0, 4294967295) by default).

apply_qubit_operator(qubit_operator, qureg)[source]

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

Parameters
  • qubit_operator (projectq.ops.QubitOperator) – Operator to apply.

  • qureg (list[Qubit],Qureg) – Quantum bits to which to apply the operator.

Raises

Exception – 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_engine.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.

cheat()[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.

Returns

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_engine.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.

collapse_wavefunction(qureg, values)[source]

Collapse a quantum register onto a classical basis state.

Parameters
  • qureg (Qureg|list[Qubit]) – Qubits to collapse.

  • values (list[bool|int]|string[0|1]) – Measurement outcome for each of the qubits in qureg.

Raises

RuntimeError – 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_engine.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.

get_amplitude(bit_string, qureg)[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.

Parameters
  • bit_string (list[bool|int]|string[0|1]) – Computational basis state

  • qureg (Qureg|list[Qubit]) – Quantum register determining the ordering. Must contain all allocated qubits.

Returns

Probability amplitude of the provided bit string.

Note

Make sure all previous commands (especially allocations) have passed through the compilation chain (call main_engine.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.

get_expectation_value(qubit_operator, qureg)[source]

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

Parameters
  • qubit_operator (projectq.ops.QubitOperator) – Operator to measure.

  • qureg (list[Qubit],Qureg) – Quantum bits to measure.

Returns

Expectation value

Note

Make sure all previous commands (especially allocations) have passed through the compilation chain (call main_engine.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.

Raises

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

get_probability(bit_string, qureg)[source]

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

Parameters
  • bit_string (list[bool|int]|string[0|1]) – Measurement outcome.

  • qureg (Qureg|list[Qubit]) – Quantum register.

Returns

Probability of measuring the provided bit string.

Note

Make sure all previous commands (especially allocations) have passed through the compilation chain (call main_engine.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.

is_available(cmd)[source]

Specialized implementation of is_available: 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).

Parameters

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

Returns

True if it can be simulated and False otherwise.

receive(command_list)[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.

Parameters

command_list (list<Command>) – List of commands to execute on the simulator.

set_qubits(qureg, values)[source]

Collapse a quantum register onto a classical basis state.

Parameters
  • qureg (Qureg|list[Qubit]) – Qubits to collapse.

  • values (list[bool|int]|string[0|1]) – Measurement outcome for each of the qubits in qureg.

Note

Make sure all previous commands have passed through the compilation chain (call main_engine.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.

set_wavefunction(wavefunction, qureg)[source]

Set the wavefunction and the qubit ordering of the simulator.

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

Parameters
  • wavefunction (list[complex]) – Array of complex amplitudes describing the wavefunction (must be normalized).

  • qureg (Qureg|list[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_engine.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.