Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
RandomOp.h
Go to the documentation of this file.
1
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> {
34 public:
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) state.SetBit(i, dist_bool(rng));
58 }
59
66 OperationType GetType() const override { return OperationType::kRandomGen; }
67
74 const std::vector<size_t> &GetBitsIndices() const { return indices; }
75
82 void SetBitsIndices(const std::vector<size_t> &ind) { indices = ind; }
83
90 void Seed(size_t sd) {
91 s = sd;
92 std::seed_seq seed{uint32_t(s & 0xffffffff), uint32_t(s >> 32)};
93 rng.seed(seed);
94 }
95
102 std::shared_ptr<IOperation<Time>> Clone() const override {
103 return std::make_shared<Random<Time>>(GetBitsIndices(), s,
105 }
106
113 std::vector<size_t> AffectedBits() const override { return GetBitsIndices(); }
114
125 std::shared_ptr<IOperation<Time>> Remap(
126 const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
127 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
128 const override {
129 auto newOp = Clone();
130
131 for (size_t i = 0; i < indices.size(); ++i) {
132 const auto bitit = bitsMap.find(indices[i]);
133 if (bitit != bitsMap.end())
134 std::static_pointer_cast<Random<Time>>(newOp)->SetBit(i, bitit->second);
135 }
136
137 return newOp;
138 }
139
149 bool IsClifford() const override { return true; }
150
151 protected:
159 void SetBit(size_t index, Types::qubit_t bit) {
160 if (index < indices.size()) indices[index] = bit;
161 }
162
163 private:
164 std::vector<size_t> indices;
166 mutable std::mt19937_64 rng;
167 mutable std::bernoulli_distribution
168 dist_bool;
170 size_t s;
171};
172
173} // namespace Circuits
174
175#endif // !_RANDOM_OP_H_
The operation interface.
Definition Operations.h:357
Time GetDelay() const
Get the delay of the operation.
Definition Operations.h:496
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:230
Random operation.
Definition RandomOp.h:33
void Seed(size_t sd)
Seeds the random generator.
Definition RandomOp.h:90
std::vector< size_t > AffectedBits() const override
Get the affected bits.
Definition RandomOp.h:113
void SetBit(size_t index, Types::qubit_t bit)
Set the classical bit to index.
Definition RandomOp.h:159
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:125
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
Definition RandomOp.h:102
const std::vector< size_t > & GetBitsIndices() const
Get the indices of the classical bits affected by this operation.
Definition RandomOp.h:74
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:66
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:82
bool IsClifford() const override
Checks if the operation is a Clifford one.
Definition RandomOp.h:149
OperationType
The type of operations.
Definition Operations.h:26
@ kRandomGen
random classical bit generator, result in 'OperationState'