Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
RandomOp.h
Go to the documentation of this file.
1
12
13#pragma once
14
15#ifndef _RANDOM_OP_H_
16#define _RANDOM_OP_H_
17
18#include "Operations.h"
19#include <random>
20
21namespace Circuits {
22
32template <typename Time = Types::time_type>
33class Random : public IOperation<Time> {
34public:
43 Random(const std::vector<size_t> &ind = {}, size_t seed = 0, Time delay = 0)
44 : IOperation<Time>(delay), indices(ind), s(seed) {
45 Seed(seed);
46 }
47
55 void Execute(const std::shared_ptr<Simulators::ISimulator> &sim,
56 OperationState &state) const override {
57 for (size_t i : indices)
58 state.SetBit(i, dist_bool(rng));
59 }
60
67 OperationType GetType() const override { return OperationType::kRandomGen; }
68
75 const std::vector<size_t> &GetBitsIndices() const { return indices; }
76
83 void SetBitsIndices(const std::vector<size_t> &ind) { indices = ind; }
84
91 void Seed(size_t sd) {
92 s = sd;
93 std::seed_seq seed{uint32_t(s & 0xffffffff), uint32_t(s >> 32)};
94 rng.seed(seed);
95 }
96
103 std::shared_ptr<IOperation<Time>> Clone() const override {
104 return std::make_shared<Random<Time>>(GetBitsIndices(), s,
106 }
107
114 std::vector<size_t> AffectedBits() const override { return GetBitsIndices(); }
115
126 std::shared_ptr<IOperation<Time>>
127 Remap(const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
128 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
129 const override {
130 auto newOp = Clone();
131
132 for (size_t i = 0; i < indices.size(); ++i) {
133 const auto bitit = bitsMap.find(indices[i]);
134 if (bitit != bitsMap.end())
135 std::static_pointer_cast<Random<Time>>(newOp)->SetBit(i, bitit->second);
136 }
137
138 return newOp;
139 }
140
150 bool IsClifford() const override { return true; }
151
152protected:
160 void SetBit(size_t index, Types::qubit_t bit) {
161 if (index < indices.size())
162 indices[index] = bit;
163 }
164
165private:
166 std::vector<size_t> indices;
168 mutable std::mt19937_64 rng;
169 mutable std::bernoulli_distribution
170 dist_bool;
172 size_t s;
173};
174
175} // namespace Circuits
176
177#endif // !_RANDOM_OP_H_
The operation interface.
Definition Operations.h:361
Time GetDelay() const
Get the delay of the operation.
Definition Operations.h:500
IOperation(Types::time_type delay=0)
Definition Operations.h:369
The state class that stores the classical state of a quantum circuit execution.
Definition Operations.h:62
void SetBit(size_t index, bool value=true)
Set the classical bit at the specified index.
Definition Operations.h:232
void Seed(size_t sd)
Seeds the random generator.
Definition RandomOp.h:91
std::vector< size_t > AffectedBits() const override
Get the affected bits.
Definition RandomOp.h:114
void SetBit(size_t index, Types::qubit_t bit)
Set the classical bit to index.
Definition RandomOp.h:160
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:127
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
Definition RandomOp.h:103
const std::vector< size_t > & GetBitsIndices() const
Get the indices of the classical bits affected by this operation.
Definition RandomOp.h:75
void Execute(const std::shared_ptr< Simulators::ISimulator > &sim, OperationState &state) const override
Execute the operation.
Definition RandomOp.h:55
OperationType GetType() const override
Get the type of the operation.
Definition RandomOp.h:67
Random(const std::vector< size_t > &ind={}, size_t seed=0, Time delay=0)
Construct a new Random object.
Definition RandomOp.h:43
void SetBitsIndices(const std::vector< size_t > &ind)
Set the indices of the classical bits affected by this operation.
Definition RandomOp.h:83
bool IsClifford() const override
Checks if the operation is a Clifford one.
Definition RandomOp.h:150
OperationType
The type of operations.
Definition Operations.h:26
@ kRandomGen
random classical bit generator, result in 'OperationState'
Definition Operations.h:29
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:20