Template Class MeasurementsBase

Class Documentation

template<class StateVectorT, class Derived>
class MeasurementsBase

Observable’s Measurement Class.

This class performs measurements in the state vector provided to its constructor. Observables are defined by its operator(matrix), the observable class, or through a string-based function dispatch.

Template Parameters
  • StateVectorT

  • Derived

Public Functions

inline explicit MeasurementsBase(const StateVectorT &statevector)
inline void setSeed(const std::size_t seed)

Set the seed of the internal random generator.

Parameters

seed – Seed

inline void setRandomSeed()

Randomly set the seed of the internal random generator.

inline auto expval(const Observable<StateVectorT> &obs) -> PrecisionT

Calculate the expectation value for a general Observable.

Parameters

obs – Observable.

Returns

Expectation value with respect to the given observable.

inline auto var(const Observable<StateVectorT> &obs) -> PrecisionT

Calculate the variance for a general Observable.

Parameters

obs – Observable.

Returns

Variance with respect to the given observable.

inline auto probs() -> std::vector<PrecisionT>

Probabilities of each computational basis state.

Returns

Floating point std::vector with probabilities in lexicographic order.

inline auto probs(const std::vector<std::size_t> &wires) -> std::vector<PrecisionT>

Probabilities for a subset of the full system.

Parameters

wires – Wires will restrict probabilities to a subset of the full system.

Returns

Floating point std::vector with probabilities. The basis columns are rearranged according to wires.

inline auto generate_samples(std::size_t num_samples) -> std::vector<std::size_t>

Generate samples.

Parameters

num_samples – Number of samples

Returns

1-D vector of samples in binary with each sample separated by a stride equal to the number of qubits.

inline auto expval(const Observable<StateVectorT> &obs, const std::size_t &num_shots, const std::vector<std::size_t> &shot_range = {}) -> PrecisionT

Calculate the expectation value for a general Observable.

Parameters
  • obs – Observable.

  • num_shots – Number of shots used to generate samples

  • shot_range – The range of samples to use. All samples are used by default.

Returns

Expectation value with respect to the given observable.

inline auto measure_with_samples(const Observable<StateVectorT> &obs, const std::size_t &num_shots, const std::vector<std::size_t> &shot_range) -> std::vector<PrecisionT>

Calculate the expectation value for a general Observable.

Parameters
  • obs – Observable.

  • num_shots – Number of shots used to generate samples

  • shot_range – The range of samples to use. All samples are used by default.

Returns

Expectation value with respect to the given observable.

inline auto var(const Observable<StateVectorT> &obs, const std::size_t &num_shots) -> PrecisionT

Calculate the variance for an observable with the number of shots.

Parameters
  • obs – An observable object.

  • num_shots – Number of shots used to generate samples

Returns

Variance of the given observable.

inline auto probs(const Observable<StateVectorT> &obs, std::size_t num_shots = 0) -> std::vector<PrecisionT>

Probabilities to measure rotated basis states.

Parameters
  • obs – An observable object.

  • num_shots – Number of shots (Optional). If specified with a non-zero number, shot-noise will be added to return probabilities

Returns

Floating point std::vector with probabilities. The basis columns are rearranged according to wires.

inline auto probs(const std::vector<std::size_t> &wires, std::size_t num_shots) -> std::vector<PrecisionT>

Probabilities with shot-noise for a subset of the full system.

Parameters
  • wires – Wires will restrict probabilities to a subset of the full system.

  • num_shots – Number of shots.

Returns

Floating point std::vector with probabilities.

inline auto probs(std::size_t num_shots) -> std::vector<PrecisionT>

Probabilities with shot-noise.

Parameters

num_shots – Number of shots.

Returns

Floating point std::vector with probabilities.

inline auto sample(const Observable<StateVectorT> &obs, const std::size_t &num_shots) -> std::vector<PrecisionT>

Return samples drawn from eigenvalues of the observable.

Parameters
  • obs – The observable object to sample

  • num_shots – Number of shots used to generate samples

Returns

Samples of eigenvalues of the observable

inline auto sample(const std::size_t &num_shots) -> std::vector<std::size_t>

Return the raw basis state samples.

Parameters

num_shots – Number of shots used to generate samples

Returns

Raw basis state samples

inline auto counts(const Observable<StateVectorT> &obs, const std::size_t &num_shots) -> std::unordered_map<PrecisionT, std::size_t>

Groups the eigenvalues of samples into a dictionary showing number of occurences for each possible outcome with the number of shots.

Parameters
  • obs – The observable to sample

  • num_shots – Number of wires the sampled observable was performed on

Returns

std::unordered_map<PrecisionT, std::size_t> with format {'EigenValue': num_occurences}

inline auto counts(const std::size_t &num_shots) -> std::unordered_map<std::size_t, std::size_t>

Groups the samples into a dictionary showing number of occurences for each possible outcome with the number of shots.

Parameters

num_shots – Number of wires the sampled observable was performed on

Returns

std::unordered_map<std::size_t, std::size_t> with format {'outcome': num_occurences}

Protected Attributes

const StateVectorT &_statevector
std::mt19937 rng