18#ifndef _MEASUREMENTS_H_
19#define _MEASUREMENTS_H_
111 void SetQubits(
const std::vector<std::pair<Types::qubit_t, size_t>> &qs)
113 qubits.resize(qs.size());
114 bits.resize(qs.size());
115 for (
size_t i = 0; i < qs.size(); ++i)
117 qubits[i] = qs[i].first;
118 bits[i] = qs[i].second;
159 auto res = sim->MeasureMany(qubits);
180 const auto res = sim->SampleCountsMany(qubits, 1).begin()->first;
201 for (
size_t index = 0; index < qubits.size(); ++index)
203 bool value = index < measurements.size() && measurements[index];
205 if (rng && index < readout.size())
207 const auto &rates = readout[index];
208 if (!value && rates.p_meas1_prep0 > 0.0 && rng->RandomUniform() < rates.p_meas1_prep0)
210 else if (value && rates.p_meas0_prep1 > 0.0 && rng->RandomUniform() < rates.p_meas0_prep1)
214 state.
SetBit(bits[index], value);
224 std::shared_ptr<IOperation<Time>>
Clone()
const override
226 std::vector<std::pair<Types::qubit_t, size_t>> qs(qubits.size());
227 for (
size_t i = 0; i < qubits.size(); ++i)
228 qs[i] = std::make_pair(qubits[i], bits[i]);
231 copy->readout = readout;
268 std::shared_ptr<IOperation<Time>>
Remap(
const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
269 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
const override
271 auto newOp = this->
Clone();
273 for (
size_t i = 0; i < qubits.size(); ++i)
275 const auto qubitit = qubitsMap.find(qubits[i]);
276 if (qubitit != qubitsMap.end())
277 std::static_pointer_cast<MeasurementOperation<Time>>(newOp)->
SetQubit(i, qubitit->second);
282 for (
size_t i = 0; i < bits.size(); ++i)
284 const auto bitit = bitsMap.find(bits[i]);
285 if (bitit != bitsMap.end())
286 std::static_pointer_cast<MeasurementOperation<Time>>(newOp)->
SetBit(i, bitit->second);
317 if (index < qubits.size())
318 qubits[index] = qubit;
330 if (index < bits.size())
343 rates.resize(rates.empty() ? 0 : qubits.size());
344 readout = std::move(rates);
356 if (index >= qubits.size())
358 if (readout.size() != qubits.size())
359 readout.resize(qubits.size());
360 readout[index] = rates;
372 return !readout.empty();
377 std::vector<size_t> bits;
378 std::vector<ReadoutRates> readout;
Time GetDelay() const
Get the delay of the operation.
IOperation(Types::time_type delay=0)
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
void SetReadout(std::vector< ReadoutRates > rates)
Attach readout-error rates, one entry per measured index.
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.
void SetReadoutAt(size_t index, const ReadoutRates &rates)
Attach readout-error rates for a single measured index.
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 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.
const std::vector< ReadoutRates > & GetReadout() const
Per-index readout rates, empty when none are attached.
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.
void SetStateFromSample(const std::vector< bool > &measurements, OperationState &state, Simulators::ISimulator *rng=nullptr) const
Writes sampled outcomes into the classical state, applying readout error per measured qubit.
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.
bool HasReadout() const
True when readout rates are attached to this 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.
Interface class for a quantum computing simulator.
OperationType
The type of operations.
@ kMeasurement
measurement, result in 'OperationState'
double p_meas0_prep1
P(report 0 | measured 1).
double p_meas1_prep0
P(report 1 | measured 0).
Classical readout-error rates for one measured qubit.
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
uint_fast64_t qubit_t
The type of a qubit.