Skip to content

Commit

Permalink
Fix extended stabilizer thread safety in apply_ops_parallel (#1993)
Browse files Browse the repository at this point in the history
* Extended stabilizer simulator no longer shares RngEngine amongst states when ops are applied in parallel

* Added release note

* Fixed ugly cast

---------

Co-authored-by: Jun Doi <doichan@jp.ibm.com>
  • Loading branch information
eliotheinrich and doichanj authored Nov 24, 2023
1 parent 7adacaa commit 0de759e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fixes:
- |
Extended stabilizer simulation was sharing a single copy of RngEngine amongst
parallelized states in ``ExtendedStabilizer::State::apply_ops_parallel``,
leading to thread safety issue. Now, a new RngEngine is seeded for each parallel
state.
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,24 @@ template <typename InputIterator>
void State::apply_ops_parallel(InputIterator first, InputIterator last,
ExperimentResult &result, RngEngine &rng) {
const int_t NUM_STATES = BaseState::qreg_.get_num_states();

std::vector<size_t> rng_seeds(NUM_STATES);
for (int_t i = 0; i < NUM_STATES; i++) {
rng_seeds[i] = rng.rand_int<size_t>(0, SIZE_MAX);
}

#pragma omp parallel for if (BaseState::qreg_.check_omp_threshold() && \
BaseState::threads_ > 1) \
num_threads(BaseState::threads_)
for (int_t i = 0; i < NUM_STATES; i++) {
if (!BaseState::qreg_.check_eps(i)) {
continue;
}
RngEngine local_rng(rng_seeds[i]);
for (auto it = first; it != last; it++) {
switch (it->type) {
case Operations::OpType::gate:
apply_gate(*it, rng, i);
apply_gate(*it, local_rng, i);
break;
case Operations::OpType::barrier:
case Operations::OpType::qerror_loc:
Expand Down

0 comments on commit 0de759e

Please sign in to comment.