MCPcopy Create free account
hub / github.com/QuEST-Kit/QuEST / rand_getRandomMultiQubitOutcome

Function rand_getRandomMultiQubitOutcome

quest/src/core/randomiser.cpp:140–166  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

138
139
140qindex rand_getRandomMultiQubitOutcome(vector<qreal> probs) {
141
142 // assumes sum(probs) = 1, and produces a multinomial variate
143 // which is always consistent between distributed nodes
144
145 std::uniform_real_distribution<qreal> distrib(0, 1); // ~[0,1]
146
147 // advances generator on every node, retaining consensus
148 qreal sample = distrib(mainGenerator);
149
150 // map sample to an element of probs
151 qreal cumProb = 0;
152 for (size_t i=0; i<probs.size(); i++) {
153 cumProb += probs[i];
154
155 if (sample < cumProb)
156 return i;
157 }
158
159 // it is principally possible that cumProb == 1 - eps, and that
160 // sample lies within [1-eps, 1], and should take final elem
161 if (1 - cumProb <= validateconfig_getEpsilon())
162 return probs.size() - 1;
163
164 error_randomiserGivenNonNormalisedProbList();
165 return probs.size();
166}
167
168
169

Callers 1

Tested by

no test coverage detected