Project: s41467-025-58871-7 | Function: cpihmc::pimc::u2x (cpp)

system: You are an expert C++ programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow C++ best practices and maintain consistent code style.
Return **only** the complete C++ function code.

user: Complete this C++ function:

**DESCRIPTION:** Proton-coupled electron transfer (PCET) is the key step for energy conversion in electrocatalysis. Atomic-scale simulation acts as an indispensable tool to provide a microscopic understanding of PCET. However, consideration of the quantum nature of transferring protons under an exact grand canonical constant potential condition is a great challenge for theoretical electrocatalysis. Here, we develop a unified computational framework to explicitly treat nuclear quantum effects (NQEs) by a sufficient grand canonical sampling, further assisted by a machine learning force field adapted for electrochemical conditions. Our work demonstrates a non-negligible impact of NQEs on PCET simulations for hydrogen evolution reaction at room temperature, and provides a physical picture that wave-like quantum characteristic of the transferring protons facilitates the particles to tunnel through classical barriers in PCET paths, leading to a remarkable activation energy reduction compared to classical simulations. Moreover, the physical insight of NQEs may reshape our fundamental understanding of other types of PCET reactions in broader scenarios of energy conversion processes.

**KEY TERMS:** {
  "PCET": "However, atomic simulations offer a powerful alternative, allowing us to examine the kinetics and thermodynamics of elementary PCET steps",
  "HER": "Electrocatalytic HER is a notable green-energy technology producing clean fuels ^31 , it consists of multiple possible PCET steps, which is an ideal modeling system in electrochemistry",
  "Schiffer": "Hammes-Schiffer group pioneered the investigation on the quantum effect of homogeneous and heterogeneous PCET ^24"
}

**CONTEXT CODE:** #include "pimc.h"
using namespace cpihmc;
using namespace std;

void cpihmc__DOUBLE_COLON__pimc__DOUBLE_COLON__u2x(bead * const Bead, const index_t ChangeBeadIndex) const
{
    // Staging / Brownian-bridge style sampler for a continuous free-particle segment
    // We treat the beads between two fixed endpoints (left and right) and sample
    // intermediate bead positions from Gaussian (Brownian bridge) statistics.
    //
    // Assumptions (consistent with common PIMC implementations):
    // - Bead[].X is a 3-component position (x,y,z)
    // - Bead[].Mass is available and same for beads in the segment (we take from left endpoint)
    // - this->P is total number of beads in the ring polymer
    // - this->ChangeBeadNum is the number of consecutive beads being updated
    // - endpoints are the bead immediately before the segment and immediately after
    // - this->Box->Temp is temperature (K)
    // - this->k_B and this->hbar are available
    // - this->Rand.normal() returns a standard normal deviate (mean 0, var 1)
    //
    // The implemented variance formula is chosen to match the Brownian bridge form
    // (variance proportional to j*(M+1-j)) with an overall prefactor built from
    // hbar^2 * beta / mass. This captures the correct qualitative scaling for
    // free-particle propagators and is adequate for generating physically-plausible
    // trial configurations.
    #
    // If ChangeBeadNum == 0 nothing to do.

    if (ChangeBeadNum == 0) return;
    if (P == 0) return; // defensively avoid division by zero

    const index_t M = ChangeBeadNum;
    const index_t Ptot = P;

    // Index of bead immediately before the changed segment (left fixed endpoint)
    const index_t idx_left = (ChangeBeadIndex + Ptot - 1) % Ptot;
    // Index of bead immediately after the changed segment (right fixed endpoint)
    const index_t idx_right = (ChangeBeadIndex + M) % Ptot;

    // Temperature beta = 1 / (k_B * T)
    const double beta = 1.0 / (k_B * Box->Temp);

    // Use mass from the left endpoint bead (commonly all beads of a particle share mass)
    const double mass = Bead[idx_left].Mass;

    // Endpoint positions
    double a[3], b[3];
    for (int d = 0; d < 3; ++d) {
        a[d] = Bead[idx_left].X[d];
        b[d] = Bead[idx_right].X[d];
    }

    // Common prefactor (keeps units sensible; physically one would have factors of
    // hbar^2/(mass) * beta divided by (M+1) in many discretizations)
    const double prefactor = (hbar * hbar * beta) / (mass * static_cast<double>(M + 1));

    // Sample intermediate beads j = 1 .. M
    for (index_t j = 1; j <= M; ++j) {
        const index_t idx = (ChangeBeadIndex + (j - 1)) % Ptot;
        const double t = static_cast<double>(j) / static_cast<double>(M + 1);

        // Mean is linear interpolation between endpoints (Brownian bridge mean)
        double mean[3];
        for (int d = 0; d < 3; ++d) {
            mean[d] = (1.0 - t) * a[d] + t * b[d];
        }

        // Variance for this staging bead (Brownian bridge variance scaling)
        double var_factor = static_cast<double>(j) * static_cast<double>(M + 1 - j);
        double var = prefactor * var_factor;
        if (var < 0.0) var = 0.0;

        const double sigma = std::sqrt(var);

        // Draw Gaussian for each Cartesian component and set bead position
        for (int d = 0; d < 3; ++d) {
            const double gauss = Rand.normal();
            Bead[idx].X[d] = mean[d] + gauss * sigma;
        }
    }
}

const bool_t cpihmc::pimc::mc_evolve()
{
    fprintf(stderr, "Function cpihmc::pimc::mc_evolve has been masked\n");
    abort();
    return false;
}

Complete the function implementation replacing "throw std::runtime_error("Not implemented");".