<commit_before>#include <iostream>
#include "lemon_mwpm.h"
#include <lemon/list_graph.h>
#include <lemon/matching.h>
#include <vector>
#include <string>
#include "weighted_stabiliser_graph.h"
#include "stabiliser_graph.h"
#include <stdexcept>
#include <set>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <boost/graph/adjacency_list.hpp>

typedef lemon::ListGraph UGraph;
typedef UGraph::EdgeMap<double> LengthMap;

py::array_t<std::uint8_t> LemonDecode(IStabiliserGraph& sg, const py::array_t<int>& defects){
    if (!sg.HasComputedAllPairsShortestPaths()){
        sg.ComputeAllPairsShortestPaths();
    }
    auto d = defects.unchecked<1>();
    int num_nodes = d.shape(0);

    UGraph g;
    LengthMap length(g);
    UGraph::NodeMap<int> node_map(g);
    std::vector<UGraph::Node> node_list;
    for (int i=0; i<num_nodes; i++){
        UGraph::Node x;
        x = g.addNode();
        node_map[x] = i;
        node_list.push_back(x);
    }

    for (py::size_t i = 0; i<num_nodes; i++){
        for (py::size_t j=i+1; j<num_nodes; j++){
            UGraph::Edge e = g.addEdge(node_list[i], node_list[j]);
            length[e] = 1.0/(1.0+sg.SpaceTimeDistance(d(i), d(j)));
        }
    };
    typedef lemon::MaxWeightedPerfectMatching<UGraph,LengthMap> MWPM;
    MWPM pm(g, length);
    pm.run();
    int N = sg.GetNumQubits();
    auto correction = new std::vector<int>(N, 0);
    std::set<int> qids;
    for (py::size_t i = 0; i<num_nodes; i++){
        int j = node_map[pm.mate(node_list[i])];
        if (i<j){
            std::vector<int> path = sg.SpaceTimeShortestPath(d(i), d(j));
            for (std::vector<int>::size_type k=0; k<path.size()-1; k++){
                qids = sg.QubitIDs(path[k], path[k+1]);
                for (auto qid : qids){
                    if ((qid != -1) && (qid >= 0) && (qid < N)){
                        (*correction)[qid] = ((*correction)[qid] + 1) % 2;
                    }
                }
            }
        }
    }

    auto capsule = py::capsule(correction, [](void *correction) { delete reinterpret_cast<std::vector<int>*>(correction); });
    return py::array_t<int>(correction->size(), correction->data(), capsule);
}


py::array_t<std::uint8_t> LemonDecodeMatchNeighbourhood(WeightedStabiliserGraph& sg, const py::array_t<int>& defects, int num_neighbours){
    auto d = defects.unchecked<1>();
    int num_defects = d.shape(0);

    int num_nodes = boost::num_vertices(sg.stabiliser_graph);

    std::vector<int> defect_id(num_nodes, -1);
    for (int i=0; i<num_defects; i++){
        defect_id[d(i)] = i;
    }

    num_neighbours = std::min(num_neighbours, num_defects-1) + 1;

    UGraph g;
    LengthMap length(g);
    UGraph::NodeMap<int> node_map(g);
    std::vector<UGraph::Node> node_list;
    for (int i=0; i<num_defects; i++){
        UGraph::Node x;
        x = g.addNode();
        node_map[x] = i;
        node_list.push_back(x);
    }

    std::vector<std::pair<int, double>> neighbours;
    std::vector<std::set<int>> adj_list(num_defects);
    int j;
    bool is_in;
    for (int i=0; i<num_defects; i++){
        neighbours = sg.GetNearestNeighbours(d(i), num_neighbours, defect_id);
        for (const auto &neighbour : neighbours){
            j = defect_id[neighbour.first];
            is_in = adj_list[i].find(j) != adj_list[i].end();
            if (!is_in && i!=j){
                UGraph::Edge e = g.addEdge(node_list[i], node_list[j]);
                length[e] = 1.0/(1.0+neighbour.second);
                adj_list[i].insert(j);
                adj_list[j].insert(i);
            }
        }
    }

    typedef lemon::MaxWeightedPerfectMatching<UGraph,LengthMap> MWPM;
    MWPM pm(g, length);
    pm.run();

    int N = sg.GetNumQubits();
    auto correction = new std::vector<int>(N, 0);
<commit_msg>Check for connected graph when matching<commit_after>#include <iostream>
#include "lemon_mwpm.h"
#include <lemon/list_graph.h>
#include <lemon/matching.h>
#include <lemon/connectivity.h>
#include <vector>
#include <string>
#include "weighted_stabiliser_graph.h"
#include "stabiliser_graph.h"
#include <stdexcept>
#include <set>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <boost/graph/adjacency_list.hpp>

typedef lemon::ListGraph UGraph;
typedef UGraph::EdgeMap<double> LengthMap;

py::array_t<std::uint8_t> LemonDecode(IStabiliserGraph& sg, const py::array_t<int>& defects){
    if (!sg.HasComputedAllPairsShortestPaths()){
        sg.ComputeAllPairsShortestPaths();
    }
    auto d = defects.unchecked<1>();
    int num_nodes = d.shape(0);

    UGraph g;
    LengthMap length(g);
    UGraph::NodeMap<int> node_map(g);
    std::vector<UGraph::Node> node_list;
    for (int i=0; i<num_nodes; i++){
        UGraph::Node x;
        x = g.addNode();
        node_map[x] = i;
        node_list.push_back(x);
    }

    for (py::size_t i = 0; i<num_nodes; i++){
        for (py::size_t j=i+1; j<num_nodes; j++){
            UGraph::Edge e = g.addEdge(node_list[i], node_list[j]);
            length[e] = 1.0/(1.0+sg.SpaceTimeDistance(d(i), d(j)));
        }
    };
    typedef lemon::MaxWeightedPerfectMatching<UGraph,LengthMap> MWPM;
    MWPM pm(g, length);
    pm.run();
    int N = sg.GetNumQubits();
    auto correction = new std::vector<int>(N, 0);
    std::set<int> qids;
    for (py::size_t i = 0; i<num_nodes; i++){
        int j = node_map[pm.mate(node_list[i])];
        if (i<j){
            std::vector<int> path = sg.SpaceTimeShortestPath(d(i), d(j));
            for (std::vector<int>::size_type k=0; k<path.size()-1; k++){
                qids = sg.QubitIDs(path[k], path[k+1]);
                for (auto qid : qids){
                    if ((qid != -1) && (qid >= 0) && (qid < N)){
                        (*correction)[qid] = ((*correction)[qid] + 1) % 2;
                    }
                }
            }
        }
    }

    auto capsule = py::capsule(correction, [](void *correction) { delete reinterpret_cast<std::vector<int>*>(correction); });
    return py::array_t<int>(correction->size(), correction->data(), capsule);
}


py::array_t<std::uint8_t> LemonDecodeMatchNeighbourhood(WeightedStabiliserGraph& sg, const py::array_t<int>& defects, int num_neighbours){
    auto d = defects.unchecked<1>();
    int num_defects = d.shape(0);

    int num_nodes = boost::num_vertices(sg.stabiliser_graph);

    std::vector<int> defect_id(num_nodes, -1);
    for (int i=0; i<num_defects; i++){
        defect_id[d(i)] = i;
    }

    num_neighbours = std::min(num_neighbours, num_defects-1) + 1;

    UGraph g;
    LengthMap length(g);
    UGraph::NodeMap<int> node_map(g);
    std::vector<UGraph::Node> node_list;
    for (int i=0; i<num_defects; i++){
        UGraph::Node x;
        x = g.addNode();
        node_map[x] = i;
        node_list.push_back(x);
    }

    std::vector<std::pair<int, double>> neighbours;
    std::vector<std::set<int>> adj_list(num_defects);
    int j;
    bool is_in;
    for (int i=0; i<num_defects; i++){
        neighbours = sg.GetNearestNeighbours(d(i), num_neighbours, defect_id);
        for (const auto &neighbour : neighbours){
            j = defect_id[neighbour.first];
            is_in = adj_list[i].find(j) != adj_list[i].end();
            if (!is_in && i!=j){
                UGraph::Edge e = g.addEdge(node_list[i], node_list[j]);
                length[e] = 1.0/(1.0+neighbour.second);
                adj_list[i].insert(j);
                adj_list[j].insert(i);
            }
        }
    }

    if (!lemon::connected(g)){
        throw std::runtime_error("Graph must have only one connected component");
    }
    typedef lemon::MaxWeightedPerfectMatching<UGraph,LengthMap> MWPM;
    MWPM pm(g, length);
    pm.run();

    int N = sg.GetNumQubits();
    auto correction = new std::vector<int>(N, 0);

{'fraction_non_alphanumeric': 0.1676700397079544, 'fraction_numerical': 0.009991033687716153, 'mean_word_length': 2.8519980266403553, 'pattern_counts': {'":': 0, '<': 96, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2242768', 'n_tokens_mistral': 2999, 'n_tokens_neox': 2862, 'n_words': 529}