17#ifndef _MEASUREMENTS_H_
18#define _MEASUREMENTS_H_
35template <
typename Time = Types::time_type>
49 const std::vector<std::pair<Types::qubit_t, size_t>> &qs = {},
90 void SetQubits(
const std::vector<std::pair<Types::qubit_t, size_t>> &qs) {
91 qubits.resize(qs.size());
92 bits.resize(qs.size());
93 for (
size_t i = 0; i < qs.size(); ++i) {
94 qubits[i] = qs[i].first;
95 bits[i] = qs[i].second;
124 void Execute(
const std::shared_ptr<Simulators::ISimulator> &sim,
129 size_t res = sim->Measure(qubits);
131 for (
size_t i = 0; i < qubits.size(); ++i) {
132 state.
SetBit(bits[i], (res & 1) != 0);
148 void Sample(
const std::shared_ptr<Simulators::ISimulator> &sim,
153 size_t res = sim->SampleCounts(qubits, 1).begin()->first;
155 for (
auto bit : bits) {
156 state.
SetBit(bit, (res & 1) != 0);
165 for (
size_t index = 0; index < qubits.size(); ++index) {
166 const auto cbit = bits[index];
167 const size_t qubitMask = 1ULL << index;
168 state.
SetBit(cbit, (measurements & qubitMask) != 0);
177 for (
size_t index = 0; index < qubits.size(); ++index) {
178 const auto q = qubits[index];
179 const auto cbit = bits[index];
181 const size_t qubitMask = 1ULL << q;
183 state.
SetBit(cbit, (allMeasurements & qubitMask) != 0);
193 std::shared_ptr<IOperation<Time>>
Clone()
const override {
194 std::vector<std::pair<Types::qubit_t, size_t>> qs(qubits.size());
195 for (
size_t i = 0; i < qubits.size(); ++i)
196 qs[i] = std::make_pair(qubits[i], bits[i]);
198 return std::make_shared<MeasurementOperation<Time>>(
228 std::shared_ptr<IOperation<Time>>
229 Remap(
const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
230 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
232 auto newOp = this->
Clone();
234 for (
size_t i = 0; i < qubits.size(); ++i) {
235 const auto qubitit = qubitsMap.find(qubits[i]);
236 if (qubitit != qubitsMap.end())
237 std::static_pointer_cast<MeasurementOperation<Time>>(newOp)->
SetQubit(
243 for (
size_t i = 0; i < bits.size(); ++i) {
244 const auto bitit = bitsMap.find(bits[i]);
245 if (bitit != bitsMap.end())
246 std::static_pointer_cast<MeasurementOperation<Time>>(newOp)->
SetBit(
274 if (index < qubits.size())
275 qubits[index] = qubit;
286 if (index < bits.size())
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 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.
void SetStateFromSample(size_t measurements, OperationState &state) const
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.
void SetStateFromAllMeasurements(size_t allMeasurements, OperationState &state) const
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'
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
uint_fast64_t qubit_t
The type of a qubit.