Skip to content

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

Description

@spital

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions