Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở
#163 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
64/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
cpp, json

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C++
Star
41
Fork
26
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Qiskit/qiskit-cpp

Tất cả issue của Qiskit/qiskit-cpp

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.