`MPSStateSpace::Sample` accesses results out of bounds when the output vector is empty

オープン 初心者向け
#1,126 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@aadityarshah がすでに取り組んでいます。

2026年10月12日 から。

  • #1127 @aadityarshah による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
84/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp
領域
backend

調査の方向性

lib/mps_statespace.hのMPSStateSpace::Sample()から始め、サンプリングループでresultsをインデックスする前にどのように準備しているかを確認してください。この報告では、出力ベクトルが空でサンプルが1つの場合に問題が再現されます。そのベクトルを事前にサイズ指定している既存のサンプリングテストと比較してください。初期状態で空のベクトルを使ったサンプリングで範囲外アクセスが発生せず、関連するテストがそのケースをカバーすれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Describe the issue

While investigating the implementation of lib/mps_statespace.h, I found that MPSStateSpace::Sample() may access elements beyond the size of the output vector.

The current implementation calls

results->reserve(num_samples);
for (unsigned i = 0; i < num_samples; i++) {
  SampleOnce(state, scratch, scratch2, &rand_source, &(*results)[i]);
}

std::vector::reserve() increases capacity without changing the vector's size. If the caller provides an initially empty results vector, the indexed access is out of bounds resulting in undefined behavior.

Tell us how to reproduce the issue

To reproduce the issue, following steps should be followed:

  1. Create an MPS state and the two required scratch states.
  2. Initialize the state to |000>.
  3. Create an empty std::vector<std::vector<bool>>.
  4. Call Sample() with num_samples = 1

A minimal example would be as follows:

auto ss = qsim::mps::MPSStateSpace<qsim::For, float>(1);
auto state = ss.Create(3, 4);
auto scratch = ss.Create(3, 4);
auto scratch2 = ss.Create(3, 4);

ss.SetStateZero(state);

std::vector<std::vector<bool>> results;
ss.Sample(state, scratch, scratch2, 1, 1234, &results);

When compiled with GCC 15.2.0 using the flag -D_GLIBCXX_DEBUG , the program aborts giving the following diagnostic

Error: attempt to subscript container with out-of-bounds index 0, but 
container only holds 0 elements.

The existing sampling tests pre-size the outer results vector before calling Sample() so they do not cover this case.

Tell us the version of qsim or qsimcirq (if relevant)

No response

Tell us the computing environment (if relevant)

Ubuntu 26.04.1 LTS, GCC 15.2.0, C++17.

主要言語
C++
スター
702
フォーク
219
平均マージ
1日 2時間
マージ済み PR(30日)
8

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

quantumlib/qsim のほかの issue

quantumlib/qsim の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。