Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
RandomOp.h
Go to the documentation of this file.
1
13
14#pragma once
15
16#ifndef _RANDOM_OP_H_
17#define _RANDOM_OP_H_
18
19#include "Operations.h"
20#include <random>
21
22namespace Circuits
23{
24
34template <typename Time = Types::time_type> class Random : public IOperation<Time>
35{
36 public:
42 Random(const std::vector<size_t> &ind = {}) : Random(ind, GenerateSeed())
43 {
44 explicitlySeeded = false;
45 }
46
55 Random(const std::vector<size_t> &ind, size_t seed, Time delay = 0) : IOperation<Time>(delay), indices(ind), s(seed)
56 {
57 Seed(seed);
58 }
59
67 void Execute(const std::shared_ptr<Simulators::ISimulator> &sim, OperationState &state) const override
68 {
69 for (size_t i : indices)
70 state.SetBit(i, dist_bool(rng));
71 }
72
79 OperationType GetType() const override
80 {
82 }
83
90 const std::vector<size_t> &GetBitsIndices() const
91 {
92 return indices;
93 }
94
101 void SetBitsIndices(const std::vector<size_t> &ind)
102 {
103 indices = ind;
104 }
105
112 void Seed(size_t sd)
113 {
114 s = sd;
115 explicitlySeeded = true;
116 std::seed_seq seed{uint32_t(s & 0xffffffff), uint32_t(s >> 32)};
117 rng.seed(seed);
118 }
119
120 bool HasExplicitSeed() const
121 {
122 return explicitlySeeded;
123 }
124
131 std::shared_ptr<IOperation<Time>> Clone() const override
132 {
133 auto copy = std::make_shared<Random<Time>>(GetBitsIndices(), s, IOperation<Time>::GetDelay());
134 copy->explicitlySeeded = explicitlySeeded;
135 return copy;
136 }
137
146 std::shared_ptr<Random<Time>> CloneForExecution(uint64_t stream, uint64_t defaultSeed = 0) const
147 {
148 auto copy = std::static_pointer_cast<Random<Time>>(Clone());
149 copy->Seed(Simulators::IState::DeriveSeed(explicitlySeeded ? s : defaultSeed, stream));
150 return copy;
151 }
152
159 std::vector<size_t> AffectedBits() const override
160 {
161 return GetBitsIndices();
162 }
163
174 std::shared_ptr<IOperation<Time>> Remap(const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
175 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {}) const override
176 {
177 auto newOp = Clone();
178
179 for (size_t i = 0; i < indices.size(); ++i)
180 {
181 const auto bitit = bitsMap.find(indices[i]);
182 if (bitit != bitsMap.end())
183 std::static_pointer_cast<Random<Time>>(newOp)->SetBit(i, bitit->second);
184 }
185
186 return newOp;
187 }
188
198 bool IsClifford() const override
199 {
200 return true;
201 }
202
203 protected:
211 void SetBit(size_t index, Types::qubit_t bit)
212 {
213 if (index < indices.size())
214 indices[index] = bit;
215 }
216
217 private:
218 static size_t GenerateSeed()
219 {
220 std::random_device entropy;
221 return std::uniform_int_distribution<size_t>{}(entropy);
222 }
223
224 std::vector<size_t> indices;
226 mutable std::mt19937_64 rng;
227 mutable std::bernoulli_distribution dist_bool;
229 size_t s;
230 bool explicitlySeeded = false;
231};
232
233} // namespace Circuits
234
235#endif // !_RANDOM_OP_H_
Time GetDelay() const
Get the delay of the operation.
Definition Operations.h:546
IOperation(Types::time_type delay=0)
Definition Operations.h:410
The state class that stores the classical state of a quantum circuit execution.
Definition Operations.h:68
void SetBit(size_t index, bool value=true)
Set the classical bit at the specified index.
Definition Operations.h:267
Random operation.
Definition RandomOp.h:35
void Seed(size_t sd)
Seeds the random generator.
Definition RandomOp.h:112
std::vector< size_t > AffectedBits() const override
Get the affected bits.
Definition RandomOp.h:159
void SetBit(size_t index, Types::qubit_t bit)
Set the classical bit to index.
Definition RandomOp.h:211
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.
Definition RandomOp.h:174
Random(const std::vector< size_t > &ind, size_t seed, Time delay=0)
Construct a new Random object.
Definition RandomOp.h:55
bool HasExplicitSeed() const
Definition RandomOp.h:120
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
Definition RandomOp.h:131
const std::vector< size_t > & GetBitsIndices() const
Get the indices of the classical bits affected by this operation.
Definition RandomOp.h:90
void Execute(const std::shared_ptr< Simulators::ISimulator > &sim, OperationState &state) const override
Execute the operation.
Definition RandomOp.h:67
Random(const std::vector< size_t > &ind={})
Construct a generator with a fresh random seed.
Definition RandomOp.h:42
OperationType GetType() const override
Get the type of the operation.
Definition RandomOp.h:79
void SetBitsIndices(const std::vector< size_t > &ind)
Set the indices of the classical bits affected by this operation.
Definition RandomOp.h:101
std::shared_ptr< Random< Time > > CloneForExecution(uint64_t stream, uint64_t defaultSeed=0) const
Clone the generator for an independent execution stream.
Definition RandomOp.h:146
bool IsClifford() const override
Checks if the operation is a Clifford one.
Definition RandomOp.h:198
static uint64_t DeriveSeed(uint64_t seed, uint64_t stream)
Definition State.h:162
OperationType
The type of operations.
Definition Operations.h:29
@ kRandomGen
random classical bit generator, result in 'OperationState'
Definition Operations.h:32
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:23