Maestro 0.2.5
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Measurements.h
Go to the documentation of this file.
1
16#pragma once
17
18#ifndef _MEASUREMENTS_H_
19#define _MEASUREMENTS_H_
20
21#include "Operations.h"
22#include <vector>
23
24namespace Circuits {
25
36template <typename Time = Types::time_type>
37class MeasurementOperation : public IOperation<Time> {
38 public:
50 const std::vector<std::pair<Types::qubit_t, size_t>> &qs = {},
51 Time delay = 0)
52 : IOperation<Time>(delay) {
53 SetQubits(qs);
54 }
55
64
71 virtual size_t GetNumQubits() const { return qubits.size(); }
72
78 void Clear() {
79 qubits.clear();
80 bits.clear();
81 }
82
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;
97 }
98 }
99
106 const Types::qubits_vector &GetQubits() const { return qubits; }
107
114 const std::vector<size_t> &GetBitsIndices() const { return bits; }
115
126 void Execute(const std::shared_ptr<Simulators::ISimulator> &sim,
127 OperationState &state) const override {
128 if (qubits.empty()) return;
129
130 auto res = sim->MeasureMany(qubits);
131
132 SetStateFromSample(res, state);
133 }
134
146 void Sample(const std::shared_ptr<Simulators::ISimulator> &sim,
147 OperationState &state) const {
148 if (qubits.empty()) return;
149
150 const auto res = sim->SampleCountsMany(qubits, 1).begin()->first;
151
152 SetStateFromSample(res, state);
153 }
154
155 void SetStateFromSample(const std::vector<bool> &measurements,
156 OperationState &state) const {
157 if (qubits.empty()) return;
158
159 for (size_t index = 0; index < qubits.size(); ++index)
160 state.SetBit(bits[index],
161 index < measurements.size() && measurements[index]);
162 }
163
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]);
174
175 return std::make_shared<MeasurementOperation<Time>>(
177 }
178
185 Types::qubits_vector AffectedQubits() const override { return qubits; }
186
193 std::vector<size_t> AffectedBits() const override { return GetBitsIndices(); }
194
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 = {})
208 const override {
209 auto newOp = this->Clone();
210
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(
215 i, qubitit->second);
216 // else throw std::runtime_error("MeasurementOperation::Remap: qubit not
217 // found in map.");
218 }
219
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(
224 i, bitit->second);
225 // else throw std::runtime_error("MeasurementOperation::Remap: bit not
226 // found in map.");
227 }
228
229 return newOp;
230 }
231
241 bool IsClifford() const override { return true; }
242
250 void SetQubit(size_t index, Types::qubit_t qubit) {
251 if (index < qubits.size()) qubits[index] = qubit;
252 }
253
261 void SetBit(size_t index, Types::qubit_t bit) {
262 if (index < bits.size()) bits[index] = bit;
263 }
264
265 private:
266 Types::qubits_vector qubits;
267 std::vector<size_t>
268 bits;
269};
270
271} // namespace Circuits
272
273#endif // !_MEASUREMENTS_H_
The operation interface.
Definition Operations.h:358
Time GetDelay() const
Get the delay of the operation.
Definition Operations.h:497
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.
Definition Operations.h:63
void SetBit(size_t index, bool value=true)
Set the classical bit at the specified index.
Definition Operations.h:231
OperationType
The type of operations.
Definition Operations.h:27
@ kMeasurement
measurement, result in 'OperationState'
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:21
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
Definition Types.h:23