Stabilizer simulator¶
-
class
StabilizerSimulator
¶ Stabilizer simulator class.
Simulator class implemented using the stabilizer formalism.
Public Functions
-
StabilizerSimulator
(unsigned num_qubits, unsigned seed)¶ Constructor.
- Parameters
num_qubits
: Maximum number of allocated qubitsseed
: Seed for pseudo-random number generator
-
void
allocate_qubit
(unsigned qubit_id)¶ Allocate one qubit with a given id.
- Parameters
qubit_id
: ID of the qubit to allocate
- Exceptions
std::runtime_error
: if the qubit de-allocation fails either due to the qubit being already allocated or if the maximum number of allocated qubit has been reached.
-
void
allocate_all_qubits
()¶ Convenience function to allocate all qubits.
-
void
deallocate_qubit
(unsigned qubit_id)¶ De-allocate one qubit with a given id.
- Parameters
qubit_id
: ID of the qubit to de-allocate
- Exceptions
std::runtime_error
: if the qubit de-allocation fails either due to the qubit not being allocated or not in a classical state.
-
void
H
(unsigned qubit_id)¶ Apply a Hadamard gate.
- Parameters
qubit_id
: ID of a qubit
- Exceptions
std::runtime_error
: if the desired qubit is not already allocated
-
void
S
(unsigned qubit_id)¶ Apply an S gate.
- Parameters
qubit_id
: ID of a qubit
- Exceptions
std::runtime_error
: if the desired qubit is not already allocated
-
void
X
(unsigned qubit_id)¶ Apply a X (or NOT) gate.
- Parameters
qubit_id
: ID of a qubit
- Exceptions
std::runtime_error
: if the desired qubit is not already allocated
-
void
CNOT
(unsigned control_qubit_id, unsigned target_qubit_id)¶ Apply a controlled-NOT gate.
- Parameters
control_qubit_id
: ID of the control qubittarget_qubit_id
: ID of the target qubit
- Exceptions
std::runtime_error
: if the desired qubit is not already allocated
-
std::vector<bool>
measure_qubits_return
(std::vector<unsigned> const &qubit_ids)¶ Apply measurement gates to a set of qubits.
- Return
Array of the measured values
- Parameters
qubit_ids
: Array of qubit IDs
- Exceptions
std::runtime_error
: if any of the qubit IDs are invalid
-
bool
measure
(unsigned qubit_id)¶ Apply a measurement gate to a single qubit.
- Return
Measured value
- Parameters
qubit_id
: ID of a qubit
- Exceptions
std::runtime_error
: if the desired qubit is not already allocated
-
bool
get_classical_value
(unsigned qubit_id, calc_type tol = 1.e-12)¶ Get the classical value of a qubit.
- Return
Classical value of the qubit
- Parameters
qubit_id
: ID of a qubittol
: Tolerance for floating point comparison
- Exceptions
std::runtime_error
: if the desired qubit is not already allocated
-
bool
is_classical
(unsigned qubit_id, calc_type tol = 1.e-12)¶ Test whether a qubit is in a classical state or not.
- Return
true/ depending
on whether the qubit is in a classical state or not- Parameters
qubit_id
: ID of a qubittol
: Tolerance for floating point comparison
- Exceptions
std::runtime_error
: if the desired qubit is not already allocated
-
double
get_probability
(const std::vector<bool> &bitstring, const std::vector<unsigned> &qubit_ids)¶ Return the probability of the outcome bit_string when measuring the quantum register qureg.
- Return
The required probability
- Parameters
bitstring
: Measurement outcome.qubit_ids
: Array of qubit IDs
- Exceptions
std::runtime_error
: if any of the desired qubits are not already allocated
-
void
collapse_wavefunction
(const std::vector<unsigned> &qubit_ids, const std::vector<bool> &bitstring)¶ Collapse a quantum register onto a classical basis state.
- Parameters
qubit_ids
: Array of qubit IDsbitstring
: Measurement outcome for each of the qubits
- Exceptions
std::runtime_error
: if any of the desired qubits are not already allocated
-
void
sync
()¶ Flush gate cache.
-
Implementation details¶
-
namespace
details
¶ Functions
-
template<typename
func_t
, typenamearray_t
>
constexpr voidapply_array_func
(const array_t &array, typename array_traits<array_t>::type &val)¶ Apply a function to each element of an array while accumulating the result.
func_t
must be a functor class which implements the ()-operator:T func_t::operator()( const T& lhs, const T& rhs ) const;
- Parameters
array
: Array of value to apply the function toval
: Value used to accumulate the result
- Template Parameters
func_t
: Class implementing a function via the ()-operatorarray_t
: Array class
-
template<typename
func_t
, typenamearray_t
>
constexpr voidapply_array_func
(const array_t &array)¶ Apply a function to each element of an array while accumulating the result.
func_t
must be a functor class which implements the ()-operator:T func_t::operator()( const T& lhs, const T& rhs ) const;
- Parameters
array
: Array of value to apply the function to
- Template Parameters
func_t
: Class implementing a function via the ()-operatorarray_t
: Array class
-
template<typename
T
>
structarray_traits
¶ - #include <helper_functions.hpp>
Trait class to identify array types.
-
template<typename
T
, std::size_tN
>
structarray_traits
<std::array<T, N>>¶ - #include <helper_functions.hpp>
Specialisation to identify std::array.
-
template<typename
T
, std::size_tN
>
structarray_traits
<T[N]>¶ - #include <helper_functions.hpp>
Specialisation to identify C-style arrays.
-
struct
Gates
¶ - #include <StabilizerSimulator.hpp>
Gate storage.
Class used to cache gate operations
-
template<typename
-
uint32_t
popcnt32
(uint32_t a)¶ Generic implementation of population count for 32-bit unsigned integers.
Only used if if no-intrinsics or compiler builtin can be found
- Return
Number of of bit set to
1
in integer- Parameters
a
: Unsigned integer