`MPSStateSpace::Sample` accesses results out of bounds when the output vector is empty
メンテナーはふだん 2 日以内に返信
評価
調査の方向性
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:
- Create an MPS state and the two required scratch states.
- Initialize the state to
|000>. - Create an empty
std::vector<std::vector<bool>>. - Call
Sample()withnum_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
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
quantumlib/qsim のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 57/100
quantumlib/qsim#1146 ·
メンテナーはふだん 2 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 56/100
quantumlib/qsim#1145 ·
メンテナーはふだん 2 日以内に返信
-
`MPSStateSpace::SampleOnce` crashes for two-qubit states対応中かも @aadityarshah が 40 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 58/100
quantumlib/qsim#1129 ·
メンテナーはふだん 2 日以内に返信
-
Implement expectation values for the MPS Simulator対応中かも @rupasri118 が 36 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
quantumlib/qsim#1122 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 38/100
quantumlib/qsim#1121 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
quantumlib/qsim の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
ai_p1 comp-sql-syntax
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
ClickHouse/ClickHouse#133888 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
documentation good first issue
難易度 2/5 半日 初心者へのやさしさ 66/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
jbcoe/cc-protocol#507 ·
メンテナーはふだん 1 日以内に返信