18#ifndef _MEASUREMENTS_H_
19#define _MEASUREMENTS_H_
36template <
typename Time = Types::time_type>
50 const std::vector<std::pair<Types::qubit_t, size_t>> &qs = {},
91 void SetQubits(
const std::vector<std::pair<Types::qubit_t, size_t>> &qs) {
92 qubits.resize(qs.size());
93 bits.resize(qs.size());
94 for (
size_t i = 0; i < qs.size(); ++i) {
95 qubits[i] = qs[i].first;
96 bits[i] = qs[i].second;
126 void Execute(
const std::shared_ptr<Simulators::ISimulator> &sim,
128 if (qubits.empty())
return;
130 auto res = sim->MeasureMany(qubits);
146 void Sample(
const std::shared_ptr<Simulators::ISimulator> &sim,
148 if (qubits.empty())
return;
150 const auto res = sim->SampleCountsMany(qubits, 1).begin()->first;
157 if (qubits.empty())
return;
159 for (
size_t index = 0; index < qubits.size(); ++index)
161 index < measurements.size() && measurements[index]);
170 std::shared_ptr<IOperation<Time>>
Clone()
const override {
171 std::vector<std::pair<Types::qubit_t, size_t>> qs(qubits.size());
172 for (
size_t i = 0; i < qubits.size(); ++i)
173 qs[i] = std::make_pair(qubits[i], bits[i]);
175 return std::make_shared<MeasurementOperation<Time>>(
205 std::shared_ptr<IOperation<Time>>
Remap(
206 const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
207 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
209 auto newOp = this->
Clone();
211 for (
size_t i = 0; i < qubits.size(); ++i) {
212 const auto qubitit = qubitsMap.find(qubits[i]);
213 if (qubitit != qubitsMap.end())
214 std::static_pointer_cast<MeasurementOperation<Time>>(newOp)->
SetQubit(
220 for (
size_t i = 0; i < bits.size(); ++i) {
221 const auto bitit = bitsMap.find(bits[i]);
222 if (bitit != bitsMap.end())
223 std::static_pointer_cast<MeasurementOperation<Time>>(newOp)->
SetBit(
251 if (index < qubits.size()) qubits[index] = qubit;
262 if (index < bits.size()) bits[index] = bit;
Time GetDelay() const
Get the delay of the operation.
Measurement operation class.
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
void SetBit(size_t index, Types::qubit_t bit)
Set the classical bit to index.
void Clear()
Clears the qubits and classical bits involved in the measurement.
void SetQubits(const std::vector< std::pair< Types::qubit_t, size_t > > &qs)
Sets the qubits and classical bits involved in the measurement.
virtual size_t GetNumQubits() const
Get the number of qubits affected by the operation.
const std::vector< size_t > & GetBitsIndices() const
Returns the classical bits where the measurement results are stored.
const Types::qubits_vector & GetQubits() const
Returns the qubits that are to be measured.
MeasurementOperation(const std::vector< std::pair< Types::qubit_t, size_t > > &qs={}, Time delay=0)
Constructor.
void SetStateFromSample(const std::vector< bool > &measurements, OperationState &state) const
void Sample(const std::shared_ptr< Simulators::ISimulator > &sim, OperationState &state) const
Samples the measurement on the given simulator.
OperationType GetType() const override
Get the type of the operation.
void SetQubit(size_t index, Types::qubit_t qubit)
Set the qubit to measure.
std::vector< size_t > AffectedBits() const override
Returns the classical bits affected by the measurement.
bool IsClifford() const override
Checks if the operation is a Clifford one.
void Execute(const std::shared_ptr< Simulators::ISimulator > &sim, OperationState &state) const override
Executes the measurement on the given simulator.
std::shared_ptr< IOperation< Time > > Remap(const std::unordered_map< Types::qubit_t, Types::qubit_t > &qubitsMap, const std::unordered_map< Types::qubit_t, Types::qubit_t > &bitsMap={}) const override
Get a shared pointer to a remapped operation.
Types::qubits_vector AffectedQubits() const override
Returns the qubits affected by the measurement.
The state class that stores the classical state of a quantum circuit execution.
void SetBit(size_t index, bool value=true)
Set the classical bit at the specified index.
OperationType
The type of operations.
@ kMeasurement
measurement, result in 'OperationState'
uint_fast64_t qubit_t
The type of a qubit.
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.