Template Class Measurements

Inheritance Relationships

Base Type

  • public MeasurementsBase< StateVectorT, Measurements< StateVectorT > >

Class Documentation

template<class StateVectorT>
class Measurements : public MeasurementsBase<StateVectorT, Measurements<StateVectorT>>

Observable’s Measurement Class.

This class couples with a statevector to performs measurements. Observables are defined by its operator(matrix), the observable class, or through a string-based function dispatch.

Template Parameters

StateVectorT – type of the statevector to be measured.

Public Functions

inline explicit Measurements(const StateVectorT &statevector)
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, [[maybe_unused]] const std::vector<std::size_t> &device_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 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 in lexicographic order.

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

  • num_shots – Number of shots. of the full system.

Returns

Floating point std::vector with probabilities.

inline auto expval(const std::vector<ComplexT> &matrix, const std::vector<std::size_t> &wires) -> PrecisionT

Expected value of an observable.

Parameters
  • matrix – Square matrix in row-major order.

  • wires – Wires where to apply the operator.

Returns

Floating point expected value of the observable.

inline auto expval(const std::string &operation, const std::vector<std::size_t> &wires) -> PrecisionT

Expected value of an observable.

Parameters
  • operation – String with the operator name.

  • wires – Wires where to apply the operator.

Returns

Floating point expected value of the observable.

template<class index_type>
inline auto expval(const index_type *row_map_ptr, const index_type row_map_size, const index_type *entries_ptr, const ComplexT *values_ptr, const index_type numNNZ) -> PrecisionT

Expected value of a Sparse Hamiltonian.

Template Parameters

index_type – integer type used as indices of the sparse matrix.

Parameters
  • row_map_ptr – row_map array pointer. The j element encodes the number of non-zeros above row j.

  • row_map_size – row_map array size.

  • entries_ptr – pointer to an array with column indices of the non-zero elements.

  • values_ptr – pointer to an array with the non-zero elements.

  • numNNZ – number of non-zero elements.

Returns

Floating point expected value of the observable.

template<typename op_type>
inline auto expval(const std::vector<op_type> &operations_list, const std::vector<std::vector<std::size_t>> &wires_list) -> std::vector<PrecisionT>

Expected value for a list of observables.

Template Parameters

op_type – Operation type.

Parameters
  • operations_list – List of operations to measure.

  • wires_list – List of wires where to apply the operators.

Returns

Floating point std::vector with expected values for the observables.

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

Expectation value for a general Observable.

Parameters

obs – An observable object.

Returns

Floating point expected value of the observable.

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

Expectation value for a Observable with shots.

Parameters
  • obs – An observable object.

  • num_shots – Number of shots.

  • shot_range – Vector of shot number to measurement

Returns

Floating point expected value of the 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.

Returns

Variance of the given observable.

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

Variance value for a general Observable.

Parameters

obs – An observable object.

Returns

Floating point with the variance of the observable.

inline auto var(const std::string &operation, const std::vector<std::size_t> &wires) -> PrecisionT

Variance of an observable.

Parameters
  • operation – String with the operator name.

  • wires – Wires where to apply the operator.

Returns

Floating point with the variance of the observable.

inline auto var(const std::vector<ComplexT> &matrix, const std::vector<std::size_t> &wires) -> PrecisionT

Variance of a Hermitian matrix.

Parameters
  • matrix – Square matrix in row-major order.

  • wires – Wires where to apply the operator.

Returns

Floating point with the variance of the observable.

template<typename op_type>
inline auto var(const std::vector<op_type> &operations_list, const std::vector<std::vector<std::size_t>> &wires_list) -> std::vector<PrecisionT>

Variance for a list of observables.

Template Parameters

op_type – Operation type.

Parameters
  • operations_list – List of operations to measure. Square matrix in row-major order or string with the operator name.

  • wires_list – List of wires where to apply the operators.

Returns

Floating point std::vector with the variance of the observables.

inline std::vector<std::size_t> generate_samples_metropolis(const std::string &kernelname, std::size_t num_burnin, std::size_t num_samples)

Generate samples using the Metropolis-Hastings method. Reference: Numerical Recipes, NetKet paper.

Parameters
  • transition_kernel – User-defined functor for producing transitions between metropolis states.

  • num_burnin – Number of Metropolis burn-in steps.

  • num_samples – The number of samples to generate.

Returns

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

template<class index_type>
inline PrecisionT var(const index_type *row_map_ptr, const index_type row_map_size, const index_type *entries_ptr, const ComplexT *values_ptr, const index_type numNNZ)

Variance of a sparse Hamiltonian.

Template Parameters

index_type – integer type used as indices of the sparse matrix.

Parameters
  • row_map_ptr – row_map array pointer. The j element encodes the number of non-zeros above row j.

  • row_map_size – row_map array size.

  • entries_ptr – pointer to an array with column indices of the non-zero elements.

  • values_ptr – pointer to an array with the non-zero elements.

  • numNNZ – number of non-zero elements.

Returns

Floating point with the variance of the sparse Hamiltonian.

inline std::vector<std::size_t> generate_samples(const std::size_t num_samples, const std::optional<std::size_t> &seed = std::nullopt)

Generate samples using the alias method. Reference: https://en.wikipedia.org/wiki/Alias_method.

Parameters
  • num_samples – The number of samples to generate.

  • seed – Seed to generate the samples from

Returns

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

inline std::vector<std::size_t> generate_samples(const std::vector<std::size_t> &wires, const std::size_t num_samples, const std::optional<std::size_t> &seed = std::nullopt)

Generate samples.

Parameters
  • wires – Sample are generated for the specified wires.

  • num_samples – The number of samples to generate.

  • seed – Seed to generate the samples from

Returns

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