Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

BackendEstimatorV2: duplicate Pauli labels in EstimatorPub term-vector observables are silently overwritten instead of summed

オープン
#163 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
64/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
cpp, json

調査の方向性

Start in src/primitives/containers/estimator_pub.hpp and run the reported test_estimator target with the duplicate-label reproducer. Check the term-vector and SparseObservable construction paths, then add regression coverage showing duplicate Pauli coefficients are handled consistently. Separately inspect src/quantum_info/sparse_observable.hpp and preserve the reported behavior for inconsistent widths as a distinct decision.

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

説明

Hi maintainers, thanks for the recent BackendEstimatorV2 work in #160. This report comes from local work I did while attempting issue #145 during UnitaryHack 2026. I was not able to submit that #145 branch because of the UnitaryHack 2026 per-contributor PR limit; my submitted PR went to the sibling QASM3 issue #146 and was merged as #159.

I continued testing current main locally and found two observable-construction behaviors that seem worth tracking. I searched the current open/closed issues and PRs for pauli_terms_to_json, SparseObservable::from_list, duplicate Pauli, and related EstimatorPub terms, and I did not find an existing report for this.

Environment

  • Qiskit/qiskit-cpp main at 6d90e8cda5202012a9e80d4360414cbffa87a1b4 (fetched 2026-06-27)
  • Qiskit C extension built from source at 2.5.0rc1
  • g++ 13.3.0, -std=c++11
  • nlohmann/json 3.12.0

Note for reproduction: current main uses qk_circuit_global_phase, so I used a Qiskit C extension build new enough to provide that symbol.

Finding 1: duplicate Pauli labels are dropped in term-vector EstimatorPub constructors

EstimatorPub accepts observables as Pauli term vectors, for example std::vector<std::pair<std::string, double>>. If the same Pauli label appears more than once, the term-vector path keeps only the last coefficient. The mathematically equivalent SparseObservable path through the same EstimatorPub class sums those terms.

This makes two supported ways of expressing the same observable disagree. For example:

  • input observable: 0.5 * Z + 0.25 * Z
  • expected simplified observable: 0.75 * Z
  • actual term-vector payload: 0.25 * Z

Qiskit's Python SparsePauliOp.from_list([("Z", 0.5), ("Z", 0.25)]).simplify() also simplifies this to one Z term with coefficient 0.75+0.j.

Reproducer

From a checkout of current main, set:

export QISKIT_ROOT=/path/to/qiskit
export JSON_INC=/path/to/nlohmann/json/single_include

Then run:

cat > estimator_dup_label_repro.cpp <<'CPP'
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#include "circuit/quantumcircuit.hpp"
#include "primitives/containers/estimator_pub.hpp"
#include "quantum_info/sparse_observable.hpp"

using namespace Qiskit;
using namespace Qiskit::circuit;
using namespace Qiskit::primitives;
using namespace Qiskit::quantum_info;

int main()
{
    QuantumCircuit circ(1, 0);

    std::vector<std::pair<std::string, double>> terms;
    terms.push_back(std::make_pair(std::string("Z"), 0.5));
    terms.push_back(std::make_pair(std::string("Z"), 0.25));

    EstimatorPub pub_pairs(circ, terms, 0.02);

    SparseObservable obs = SparseObservable::from_list({
        {"Z", {0.5, 0.0}},
        {"Z", {0.25, 0.0}},
    });
    EstimatorPub pub_sparse(circ, obs, 0.02);

    std::cout << "observable submitted by vector<pair>      : " << pub_pairs.observables().dump() << "\n";
    std::cout << "observable submitted by SparseObservable  : " << pub_sparse.observables().dump() << "\n\n";
    std::cout << "Runtime PUB payload from vector<pair>:\n" << pub_pairs.to_json().dump(2) << "\n\n";

    double c_pairs = pub_pairs.observables().value("Z", 0.0);
    double c_sparse = pub_sparse.observables().value("Z", 0.0);

    std::cout << "Expected coefficient of Z : 0.75  (0.5 + 0.25)\n";
    std::cout << "  vector<pair>   gives    : " << c_pairs
              << (c_pairs == 0.75 ? "" : "   <-- one duplicate term was silently dropped") << "\n";
    std::cout << "  SparseObservable gives  : " << c_sparse << "\n";

    return c_pairs == 0.75 ? 0 : 1;
}
CPP

g++ -std=c++11 estimator_dup_label_repro.cpp \
    -I src \
    -I "$QISKIT_ROOT/dist/c/include" \
    -I "$JSON_INC" \
    -L "$QISKIT_ROOT/dist/c/lib" -lqiskit -Wl,-rpath,"$QISKIT_ROOT/dist/c/lib" \
    -o estimator_dup_label_repro

./estimator_dup_label_repro
Expected

Both constructors represent 0.75 * Z, so both should submit coefficient 0.75:

observable submitted by vector<pair>      : {"Z":0.75}
observable submitted by SparseObservable  : {"Z":0.75}

The Runtime PUB should also contain "Z": 0.75.

Actual

On main at 6d90e8c:

observable submitted by vector<pair>      : {"Z":0.25}
observable submitted by SparseObservable  : {"Z":0.75}

Runtime PUB payload from vector<pair>:
[
  "OPENQASM 3.0;\ninclude \"stdgates.inc\";\nqubit[1] q;\n",
  {
    "Z": 0.25
  },
  null,
  0.02
]

Expected coefficient of Z : 0.75  (0.5 + 0.25)
  vector<pair>   gives    : 0.25   <-- one duplicate term was silently dropped
  SparseObservable gives  : 0.75

The process exits with code 1 because the observed vector<pair> coefficient is 0.25, not 0.75.

Source path

The term-vector path in src/primitives/containers/estimator_pub.hpp assigns into a JSON object:

ret[observable[i].first] = coeff;

That overwrites any previous coefficient for the same Pauli label.

The SparseObservable path in the same file accumulates duplicate labels:

if (ret.contains(label)) {
    ret[label] = ret[label].get<double>() + coeff.real();
} else {
    ret[label] = coeff.real();
}

The complex term-vector overload also funnels through the real pauli_terms_to_json path, so it appears to have the same last-wins behavior.

Finding 2: SparseObservable::from_list aborts the process on inconsistent term widths

While preparing the reproducer through the recommended SparseObservable observable path, I also noticed that SparseObservable::from_list terminates the whole process on inconsistent-width input. This is in src/quantum_info/sparse_observable.hpp, so it may deserve a separate issue, but it is on the observable-construction path used by estimator examples.

Reproducer
cat > sparse_observable_abort_repro.cpp <<'CPP'
#include <complex>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#include "quantum_info/sparse_observable.hpp"

using namespace Qiskit::quantum_info;

int main()
{
    try {
        std::vector<std::pair<std::string, std::complex<double>>> terms;
        terms.push_back(std::make_pair(std::string("ZZ"), std::complex<double>(1.0, 0.0)));
        terms.push_back(std::make_pair(std::string("Z"), std::complex<double>(1.0, 0.0)));

        SparseObservable obs = SparseObservable::from_list(terms);
        std::cout << "constructed, num_terms=" << obs.num_terms() << "\n";
    } catch (const std::exception& exc) {
        std::cout << "caught (recoverable): " << exc.what() << "\n";
    }
    return 0;
}
CPP

g++ -std=c++11 sparse_observable_abort_repro.cpp \
    -I src \
    -I "$QISKIT_ROOT/dist/c/include" \
    -L "$QISKIT_ROOT/dist/c/lib" -lqiskit -Wl,-rpath,"$QISKIT_ROOT/dist/c/lib" \
    -o sparse_observable_abort_repro

./sparse_observable_abort_repro
Expected

A library caller should be able to catch a normal C++ error, for example an std::invalid_argument, or the behavior should be otherwise documented as unrecoverable.

Actual

The process is terminated by abort() before the catch block can run:

 SparseObservable : Error label with length 1cannot be added to a 2-qubits operator

The exit code is 134 (SIGABRT) in my local run.

Test-suite check

I also built and ran the current test suite against the same C extension:

cmake -S test -B test/build-codex
cmake --build test/build-codex -j2
./test/build-codex/test_driver test_estimator
ctest --test-dir test/build-codex --output-on-failure

Results:

  • test_estimator passed all 26 subtests.
  • Full CTest passed 4/4 tests.

So the duplicate-label behavior above appears to be an uncovered case rather than an existing failing test.

No patch is attached here; I wanted to report the reproduced behaviors clearly so they can be tracked.

主要言語
C++
スター
41
フォーク
26
PR マージ指標
30日以内にマージされた PR はありません

環境構築

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートあり
  • コントリビューションガイドなし

はじめの一歩

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

Qiskit/qiskit-cpp のほかの issue

Qiskit/qiskit-cpp の issue をすべて見る

似ている issue

C++ の issue をもっと見る

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

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