Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
RandomStream.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <cstdint>
5#include <random>
6
8{
9// A versioned stream identity is separate from the engine's current position.
10// For a fixed seed this mixer maps distinct 64-bit stream ids to distinct seeds.
11inline uint64_t Derive(uint64_t seed, uint64_t stream)
12{
13 uint64_t value = seed + 0x9e3779b97f4a7c15ULL * (stream + 1);
14 value = (value ^ (value >> 30)) * 0xbf58476d1ce4e5b9ULL;
15 value = (value ^ (value >> 27)) * 0x94d049bb133111ebULL;
16 return value ^ (value >> 31);
17}
18
19inline uint64_t FreshSeed()
20{
21 std::random_device entropy;
22 static std::atomic<uint64_t> next{0};
23 const uint64_t random = (uint64_t(entropy()) << 32) ^ uint64_t(entropy());
24 return Derive(random, next.fetch_add(1, std::memory_order_relaxed));
25}
26
27// Exactly one engine word, with a specified [0,1) conversion on every standard
28// library. Do not use thread ids or worker-local engines to generate a batch.
29inline double Uniform(std::mt19937_64 &engine)
30{
31 return static_cast<double>(engine() >> 11) * (1.0 / 9007199254740992.0);
32}
33} // namespace Utils::RandomStream
uint64_t Derive(uint64_t seed, uint64_t stream)
double Uniform(std::mt19937_64 &engine)