diff --git a/src/common.h b/src/common.h index 0d3fc722..a3c5b5bb 100644 --- a/src/common.h +++ b/src/common.h @@ -62,11 +62,12 @@ struct ErrorChainNode { size_t error_index; size_t min_detector; int64_t parent_idx = -1; + uint64_t det_hash = 0; }; // Represents an error / weighted hyperedge struct Error { - double likelihood_cost; + double likelihood_cost = 0.0; Symptom symptom; Error() = default; Error(double likelihood_cost, std::vector& detectors, std::vector observables) diff --git a/src/py/tesseract_test.py b/src/py/tesseract_test.py index 5e98959b..fc244684 100644 --- a/src/py/tesseract_test.py +++ b/src/py/tesseract_test.py @@ -595,5 +595,84 @@ def test_decoder_compilation_validation(): config.compile_decoder() +def test_duplicate_detections_cancel_mod_2(): + dem = stim.DetectorErrorModel(""" + error(0.1) D0 L0 + error(0.1) D1 D2 L1 + error(0.1) D2 + error(0.1) D3 + error(0.1) D4 + error(0.05) D0 D1 D2 + error(0.01) D2 D3 D4 L1 + """) + for sparsify in (False, True): + cfg = tesseract_decoder.tesseract.TesseractConfig( + dem, + merge_errors=False, + sparsify_errors=sparsify, + sparsify_base_degree=2, + sparsify_max_degree=4, + sparsify_reactivate_limit=1, + ) + dec = cfg.compile_decoder() + + # Duplicate pair [0, 0] cancels mod 2 to the trivial syndrome. + obs_mask = dec.decode_from_detection_events([0, 0]) + assert not dec.low_confidence_flag + assert not np.any(obs_mask) + assert list(dec.predicted_errors_buffer) == [] + + # [1, 1, 2] cancels D1 mod 2, matching [2] rather than misactivating D1. + obs_dup = dec.decode_from_detection_events([1, 1, 2]) + assert not dec.low_confidence_flag + errs_dup = list(dec.predicted_errors_buffer) + obs_single = dec.decode_from_detection_events([2]) + assert np.array_equal(obs_dup, obs_single) + assert not np.any(obs_dup) + assert errs_dup == list(dec.predicted_errors_buffer) == [2] + + # Under sparsify_errors=True with reactivate_limit=1, cancelling [0, 0, 1, 1] in + # [0, 0, 1, 1, 2, 3, 4] must rank optional degree-3 errors against {2, 3, 4} (reactivating + # error 6: D2 D3 D4 L1 with overlap 3), not against {0, 1, 2, 3, 4}. + obs_sparse = dec.decode_from_detection_events([0, 0, 1, 1, 2, 3, 4]) + assert not dec.low_confidence_flag + assert list(dec.predicted_errors_buffer) == [6] + assert list(obs_sparse) == [False, True] + + +def test_post_compile_det_orders_mutation_rebuilds_ranks(): + dem = stim.DetectorErrorModel(""" + error(0.1) D0 D1 D3 L0 + error(0.1) D2 D3 + error(0.1) D1 + detector(0, 0, 0) D0 + detector(1, 0, 0) D1 + detector(2, 0, 0) D2 + detector(3, 0, 0) D3 + """) + dec = tesseract_decoder.tesseract.TesseractConfig( + dem, det_beam=0, merge_errors=False, det_orders=[[0, 1, 2, 3]] + ).compile_decoder() + shot = np.array([True, False, True, False], dtype=bool) + assert dec.decode_to_errors(shot) == [] + assert dec.low_confidence_flag + + # Grow det_orders after compilation (previously caused out-of-bounds rank access). + dec.config.det_orders = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 2, 0, 1]] + assert sorted(dec.decode_to_errors(shot)) == [0, 1, 2] + assert not dec.low_confidence_flag + + # Replace in-place with same order count and verify invalid permutations raise cleanly. + dec.config.det_orders = [[0, 1, 2, 3]] + assert dec.decode_to_errors(shot) == [] + assert dec.low_confidence_flag + dec.config.det_orders = [[2, 0, 1, 3]] + assert sorted(dec.decode_to_errors(shot)) == [0, 1, 2] + + dec.config.det_orders = [[0, 1, 2]] + with pytest.raises(ValueError, match="size"): + dec.decode_to_errors(shot) + + if __name__ == "__main__": raise SystemExit(pytest.main([__file__])) diff --git a/src/tesseract.cc b/src/tesseract.cc index 8749867d..a15b1748 100644 --- a/src/tesseract.cc +++ b/src/tesseract.cc @@ -15,7 +15,6 @@ #include "tesseract.h" #include -#include // For boost::hash_range #include #include #include @@ -80,18 +79,14 @@ int suggest_sparsify_reactivate_limit_capped(size_t num_detectors, int sparsify_ return static_cast(rounded); } -}; // namespace +uint64_t splitmix64(uint64_t x) { + x += 0x9e3779b97f4a7c15ULL; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL; + x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL; + return x ^ (x >> 31); +} -namespace std { -template <> -struct hash> { - size_t operator()(const boost::dynamic_bitset<>& bs) const { - // Delegate to Boost's internal hash_value for dynamic_bitset - // This is the correct and most efficient way. - return boost::hash_value(bs); - } -}; -} // namespace std +} // namespace namespace tesseract_decoder { @@ -196,9 +191,9 @@ TesseractDecoder::TesseractDecoder(TesseractConfig config_) : config(std::move(c std::cout << std::flush; } num_detectors = config.dem.count_detectors(); - num_errors = config.dem.count_errors(); + num_errors = errors.size(); num_observables = config.dem.count_observables(); - initialize_structures(config.dem.count_detectors()); + initialize_structures(num_detectors); if (config.create_visualization) { auto detectors = get_detector_coords(config.dem); visualizer.add_detector_coords(detectors); @@ -318,9 +313,39 @@ void TesseractDecoder::restore_error_probabilities(const ErrorProbabilityRollbac update_internal_costs(rollback.modified_error_indices, rollback.affected_detectors); } +void TesseractDecoder::ensure_eneighbors(size_t ei) { + if (eneighbors_ready_[ei]) return; + if (++eneighbor_gen_ == 0) { + std::fill(eneighbor_seen_.begin(), eneighbor_seen_.end(), 0); + eneighbor_gen_ = 1; + } + const uint32_t mark_self = eneighbor_gen_; + if (++eneighbor_gen_ == 0) { + std::fill(eneighbor_seen_.begin(), eneighbor_seen_.end(), 0); + eneighbor_gen_ = 2; + } + const uint32_t mark_neighbor = eneighbor_gen_; + for (int d : edets[ei]) { + eneighbor_seen_[d] = mark_self; + } + auto& out = eneighbors[ei]; + for (int d : edets[ei]) { + for (int oei : d2e[d]) { + for (int od : edets[oei]) { + if (eneighbor_seen_[od] != mark_self && eneighbor_seen_[od] != mark_neighbor) { + eneighbor_seen_[od] = mark_neighbor; + out.push_back(od); + } + } + } + } + std::sort(out.begin(), out.end()); + eneighbors_ready_[ei] = 1; +} + void TesseractDecoder::initialize_structures(size_t num_detectors) { - d2e.resize(num_detectors); - edets.resize(num_errors); + d2e.assign(num_detectors, {}); + edets.assign(num_errors, {}); for (size_t ei = 0; ei < num_errors; ++ei) { edets[ei] = errors[ei].symptom.detectors; @@ -330,6 +355,7 @@ void TesseractDecoder::initialize_structures(size_t num_detectors) { } // Initial fill of error_costs and sorting of d2e for all errors + error_costs.clear(); error_costs.reserve(errors.size()); for (size_t i = 0; i < errors.size(); ++i) { double min_cost = errors[i].symptom.detectors.empty() @@ -345,32 +371,39 @@ void TesseractDecoder::initialize_structures(size_t num_detectors) { }); } - eneighbors.resize(num_errors); + // Lazy eneighbors state: computed on first expansion of each error ei. + eneighbors.assign(num_errors, {}); + eneighbors_ready_.assign(num_errors, 0); + eneighbor_seen_.assign(num_detectors, 0); + eneighbor_gen_ = 1; - std::vector> edets_bitsets(num_errors, - boost::dynamic_bitset<>(num_detectors)); - for (size_t ei = 0; ei < num_errors; ++ei) { - for (int d : edets[ei]) { - edets_bitsets[ei][d] = 1; - } + // Precompute inverse detector order ranks: det_order_ranks_[order_idx][d] = position. + cached_detector_orders_.clear(); + det_order_ranks_.clear(); + cached_detector_orders_.resize(config.detector_orders.size()); + det_order_ranks_.resize(config.detector_orders.size()); + for (size_t oi = 0; oi < config.detector_orders.size(); ++oi) { + ensure_det_order_ranks(oi); } - for (size_t ei = 0; ei < num_errors; ++ei) { - boost::dynamic_bitset<> neighbor_set(num_detectors, false); - for (int d : edets[ei]) { - for (int oei : d2e[d]) { - // Unify detectors from neighboring errors - neighbor_set |= edets_bitsets[oei]; - } - } - // Remove detectors from error's own set - neighbor_set &= ~edets_bitsets[ei]; - - for (size_t d = neighbor_set.find_first(); d != boost::dynamic_bitset<>::npos; - d = neighbor_set.find_next(d)) { - eneighbors[ei].push_back(d); - } + // Zobrist hash constants for residual detector vectors. + zobrist_.resize(num_detectors); + for (size_t d = 0; d < num_detectors; ++d) { + zobrist_[d] = splitmix64(d + 1); } + if (config.no_revisit_dets) { + visited_.init(1024); + } + + // Reusable per-search scratch buffers to eliminate per-pop heap allocations. + initial_detectors_.resize(num_detectors, false); + scratch_detectors_.resize(num_detectors, false); + initial_dct_.assign(num_errors, {0, 0}); + scratch_dct_.assign(num_errors, {0, 0}); + scratch_next_dct_.assign(num_errors, {0, 0}); + scratch_det_cost_cache_.assign(num_detectors, 0.0); + scratch_det_cost_stamp_.assign(num_detectors, 0); + scratch_det_cost_gen_ = 1; if (config.sparsify_errors) { if (config.sparsify_base_degree <= 0) { @@ -392,7 +425,7 @@ void TesseractDecoder::initialize_structures(size_t num_detectors) { int error_count_limit = static_cast( std::min(num_errors, static_cast(std::numeric_limits::max()))); config.sparsify_reactivate_limit = suggest_sparsify_reactivate_limit_capped( - config.dem.count_detectors(), config.sparsify_base_degree, error_count_limit); + num_detectors, config.sparsify_base_degree, error_count_limit); } sparsify_mandatory_errors.clear(); @@ -501,43 +534,97 @@ void TesseractDecoder::decode_to_errors(const std::vector& detections, decode_to_errors_with_graph(detections, detector_order_index, detector_beam, active_d2e); } +const std::vector& TesseractDecoder::ensure_det_order_ranks(size_t detector_order_index) { + const std::vector& order = config.detector_orders.at(detector_order_index).get_order(); + if (detector_order_index < det_order_ranks_.size() && + det_order_ranks_[detector_order_index].size() == num_detectors && + cached_detector_orders_[detector_order_index] == order) { + return det_order_ranks_[detector_order_index]; + } + if (order.size() != num_detectors) { + throw std::invalid_argument("Detector order has size " + std::to_string(order.size()) + + ", but the decoder has " + std::to_string(num_detectors) + + " detectors."); + } + std::vector ranks(num_detectors, std::numeric_limits::max()); + for (size_t pos = 0; pos < order.size(); ++pos) { + const size_t d = order[pos]; + if (d >= num_detectors) { + throw std::invalid_argument("Detector order contains out-of-range detector ID " + + std::to_string(d) + " at position " + std::to_string(pos) + "."); + } + if (ranks[d] != std::numeric_limits::max()) { + throw std::invalid_argument("Detector order contains detector ID " + std::to_string(d) + + " more than once."); + } + ranks[d] = pos; + } + if (det_order_ranks_.size() != config.detector_orders.size()) { + cached_detector_orders_.resize(config.detector_orders.size()); + det_order_ranks_.resize(config.detector_orders.size()); + } + cached_detector_orders_[detector_order_index] = order; + det_order_ranks_[detector_order_index] = std::move(ranks); + return det_order_ranks_[detector_order_index]; +} + void TesseractDecoder::decode_to_errors_with_graph( const std::vector& detections, size_t detector_order_index, size_t detector_beam, const std::vector>& active_d2e) { - const std::vector& detector_at_position = - config.detector_orders.at(detector_order_index).get_order(); + const std::vector& rank_of_detector = ensure_det_order_ranks(detector_order_index); predicted_errors_buffer.clear(); low_confidence_flag = false; error_chain_arena.clear(); - // Can technically be larger than pqlimit, but we need an initial guess on how many nodes we - // will process from the queue. Only reserve if pqlimit is a reasonable (finite) value; - // reserving SIZE_MAX bytes would throw std::length_error. - if (config.pqlimit != std::numeric_limits::max()) { - error_chain_arena.reserve(config.pqlimit); + if (error_chain_arena.capacity() == 0 && config.pqlimit > 0) { + error_chain_arena.reserve(std::min(config.pqlimit, 1024)); } - std::priority_queue, std::greater> pq; - std::unordered_map>> visited_detectors; + pq_container_.clear(); + auto pq_push = [&](const Node& n) { + pq_container_.push_back(n); + std::push_heap(pq_container_.begin(), pq_container_.end(), std::greater{}); + }; + auto pq_pop = [&]() -> Node { + std::pop_heap(pq_container_.begin(), pq_container_.end(), std::greater{}); + Node top = pq_container_.back(); + pq_container_.pop_back(); + return top; + }; + + if (config.no_revisit_dets) { + if (visited_.empty()) visited_.init(1024); + visited_.clear(); + } - boost::dynamic_bitset<> initial_detectors(num_detectors, false); - std::vector initial_detector_cost_tuples(num_errors); + initial_detectors_.reset(); + std::fill(initial_dct_.begin(), initial_dct_.end(), DetectorCostTuple{0, 0}); + uint64_t initial_det_hash = 0ULL; + size_t active_initial_dets = 0; for (size_t d : detections) { if (d >= num_detectors) { throw std::runtime_error( "Symptom " + std::to_string(d) + " references a detector >= num_detectors (= " + std::to_string(num_detectors) + ")."); } - initial_detectors[d] = true; + const bool now_set = !initial_detectors_[d]; + initial_detectors_[d] = now_set; + initial_det_hash ^= zobrist_[d]; + const int delta = now_set ? 1 : -1; + active_initial_dets += delta; for (int ei : active_d2e[d]) { - ++initial_detector_cost_tuples[ei].detectors_count; + initial_dct_[ei].detectors_count += delta; } } + if (active_initial_dets == 0) { + return; + } double initial_cost = 0; - for (size_t d : detections) { - initial_cost += get_detcost(d, initial_detector_cost_tuples, active_d2e); + for (size_t d = initial_detectors_.find_first(); d != boost::dynamic_bitset<>::npos; + d = initial_detectors_.find_next(d)) { + initial_cost += get_detcost(d, initial_dct_, active_d2e); } if (initial_cost == INF) { @@ -545,30 +632,35 @@ void TesseractDecoder::decode_to_errors_with_graph( return; } - size_t min_num_dets = detections.size(); + size_t min_num_dets = active_initial_dets; size_t max_num_dets = min_num_dets + detector_beam; - boost::dynamic_bitset<> next_detectors; - std::vector next_detector_cost_tuples; - - pq.push({initial_cost, min_num_dets, 0, -1}); + pq_push({initial_cost, min_num_dets, 0, -1}); size_t num_pq_pushed = 1; - while (!pq.empty()) { - const Node node = pq.top(); - pq.pop(); + auto cached_detcost = [&](size_t d, uint32_t stamp) -> double { + if (scratch_det_cost_stamp_[d] != stamp) { + scratch_det_cost_stamp_[d] = stamp; + scratch_det_cost_cache_[d] = get_detcost(d, scratch_dct_, active_d2e); + } + return scratch_det_cost_cache_[d]; + }; - if (node.num_dets > max_num_dets) continue; + while (!pq_container_.empty()) { + const Node node = pq_pop(); - boost::dynamic_bitset<> detectors = initial_detectors; - std::vector detector_cost_tuples(num_errors); - flip_detectors_and_block_errors(detector_order_index, node.error_chain_idx, detectors, - detector_cost_tuples, active_d2e); + if (node.num_dets > max_num_dets) continue; if (node.num_dets == 0) { + if (config.create_visualization || config.verbose) { + scratch_detectors_ = initial_detectors_; + std::fill(scratch_dct_.begin(), scratch_dct_.end(), DetectorCostTuple{0, 0}); + flip_detectors_and_block_errors(detector_order_index, node.error_chain_idx, + scratch_detectors_, scratch_dct_, active_d2e); + } if (config.create_visualization) { visualizer.add_activated_errors(node.error_chain_idx, error_chain_arena); - visualizer.add_activated_detectors(detectors, num_detectors); + visualizer.add_activated_detectors(scratch_detectors_, num_detectors); } if (config.verbose) { std::cout << "activated_errors = "; @@ -580,7 +672,7 @@ void TesseractDecoder::decode_to_errors_with_graph( std::cout << std::endl; std::cout << "activated_detectors = "; for (size_t d = 0; d < num_detectors; ++d) { - if (detectors[d]) { + if (scratch_detectors_[d]) { std::cout << d << ", "; } } @@ -599,16 +691,24 @@ void TesseractDecoder::decode_to_errors_with_graph( return; } - if (config.no_revisit_dets && !visited_detectors[node.num_dets].insert(detectors).second) - continue; + const uint64_t det_hash = (node.error_chain_idx == -1) + ? initial_det_hash + : error_chain_arena[node.error_chain_idx].det_hash; + if (config.no_revisit_dets && !visited_.insert(det_hash)) continue; + + scratch_detectors_ = initial_detectors_; + std::fill(scratch_dct_.begin(), scratch_dct_.end(), DetectorCostTuple{0, 0}); + flip_detectors_and_block_errors(detector_order_index, node.error_chain_idx, scratch_detectors_, + scratch_dct_, active_d2e); if (config.create_visualization) { visualizer.add_activated_errors(node.error_chain_idx, error_chain_arena); - visualizer.add_activated_detectors(detectors, num_detectors); + visualizer.add_activated_detectors(scratch_detectors_, num_detectors); } if (config.verbose) { std::cout.precision(13); - std::cout << "len(pq) = " << pq.size() << " num_pq_pushed = " << num_pq_pushed << std::endl; + std::cout << "len(pq) = " << pq_container_.size() << " num_pq_pushed = " << num_pq_pushed + << std::endl; std::cout << "num_dets = " << node.num_dets << " max_num_dets = " << max_num_dets << " cost = " << node.cost << std::endl; std::cout << "activated_errors = "; @@ -620,7 +720,7 @@ void TesseractDecoder::decode_to_errors_with_graph( std::cout << std::endl; std::cout << "activated_detectors = "; for (size_t d = 0; d < num_detectors; ++d) { - if (detectors[d]) { + if (scratch_detectors_[d]) { std::cout << d << ", "; } } @@ -629,99 +729,83 @@ void TesseractDecoder::decode_to_errors_with_graph( if (node.num_dets < min_num_dets) { min_num_dets = node.num_dets; - if (config.no_revisit_dets) { - for (size_t i = min_num_dets + detector_beam + 1; i <= max_num_dets; ++i) { - visited_detectors[i].clear(); - } - } max_num_dets = std::min(max_num_dets, min_num_dets + detector_beam); } - for (size_t d = 0; d < num_detectors; ++d) { - if (!detectors[d]) continue; + size_t min_detector = std::numeric_limits::max(); + size_t best_rank = std::numeric_limits::max(); + for (size_t d = scratch_detectors_.find_first(); d != boost::dynamic_bitset<>::npos; + d = scratch_detectors_.find_next(d)) { + if (rank_of_detector[d] < best_rank) { + best_rank = rank_of_detector[d]; + min_detector = d; + } for (int ei : active_d2e[d]) { - ++detector_cost_tuples[ei].detectors_count; + ++scratch_dct_[ei].detectors_count; } } - next_detector_cost_tuples = detector_cost_tuples; - - size_t min_detector = std::numeric_limits::max(); - for (size_t position = 0; position < num_detectors; ++position) { - const size_t detector = detector_at_position[position]; - if (detectors[detector]) { - min_detector = detector; - break; - } + scratch_next_dct_ = scratch_dct_; + if (++scratch_det_cost_gen_ == 0) { + std::fill(scratch_det_cost_stamp_.begin(), scratch_det_cost_stamp_.end(), 0); + scratch_det_cost_gen_ = 1; } - + const uint32_t pop_stamp = scratch_det_cost_gen_; size_t prev_ei = std::numeric_limits::max(); - std::vector detector_cost_cache(num_detectors, -1); for (int ei : active_d2e[min_detector]) { - if (detector_cost_tuples[ei].error_blocked) continue; + if (scratch_dct_[ei].error_blocked) continue; if (prev_ei != std::numeric_limits::max()) { for (int d : edets[prev_ei]) { - int fired = detectors[d] ? 1 : -1; + int fired = scratch_detectors_[d] ? 1 : -1; for (int oei : active_d2e[d]) { - next_detector_cost_tuples[oei].detectors_count += fired; + scratch_next_dct_[oei].detectors_count += fired; } } } prev_ei = ei; + scratch_next_dct_[ei].error_blocked = 1; - next_detectors = detectors; - next_detector_cost_tuples[ei].error_blocked = 1; - + uint64_t next_det_hash = det_hash; double next_cost = node.cost + errors[ei].likelihood_cost; size_t next_num_dets = node.num_dets; for (int d : edets[ei]) { - next_detectors[d] = !next_detectors[d]; - int fired = next_detectors[d] ? 1 : -1; + int fired = scratch_detectors_[d] ? -1 : 1; next_num_dets += fired; + next_det_hash ^= zobrist_[d]; for (int oei : active_d2e[d]) { - next_detector_cost_tuples[oei].detectors_count += fired; + scratch_next_dct_[oei].detectors_count += fired; } } if (next_num_dets > max_num_dets) continue; - if (config.no_revisit_dets && visited_detectors[next_num_dets].find(next_detectors) != - visited_detectors[next_num_dets].end()) - continue; + if (config.no_revisit_dets && next_num_dets > 0 && visited_.contains(next_det_hash)) continue; for (int d : edets[ei]) { - if (detectors[d]) { - if (detector_cost_cache[d] == -1) { - detector_cost_cache[d] = get_detcost(d, detector_cost_tuples, active_d2e); - } - next_cost -= detector_cost_cache[d]; + if (scratch_detectors_[d]) { + next_cost -= cached_detcost(d, pop_stamp); } else { - next_cost += get_detcost(d, next_detector_cost_tuples, active_d2e); + next_cost += get_detcost(d, scratch_next_dct_, active_d2e); } } + ensure_eneighbors(ei); for (int od : eneighbors[ei]) { - if (!detectors[od] || !next_detectors[od]) continue; - if (detector_cost_cache[od] == -1) { - detector_cost_cache[od] = get_detcost(od, detector_cost_tuples, active_d2e); - } - next_cost -= detector_cost_cache[od]; - next_cost += get_detcost(od, next_detector_cost_tuples, active_d2e); + if (!scratch_detectors_[od]) continue; + next_cost -= cached_detcost(od, pop_stamp); + next_cost += get_detcost(od, scratch_next_dct_, active_d2e); } if (next_cost == INF) continue; - // Create the error chain node for this candidate. - error_chain_arena.emplace_back(); - auto& next_node = error_chain_arena.back(); - next_node.error_index = ei; - next_node.min_detector = min_detector; - next_node.parent_idx = node.error_chain_idx; + error_chain_arena.push_back( + {static_cast(ei), min_detector, node.error_chain_idx, next_det_hash}); + const int64_t child_idx = static_cast(error_chain_arena.size() - 1); - pq.push({next_cost, next_num_dets, node.depth + 1, (int64_t)(error_chain_arena.size() - 1)}); + pq_push({next_cost, next_num_dets, node.depth + 1, child_idx}); ++num_pq_pushed; if (num_pq_pushed > config.pqlimit) { @@ -734,7 +818,7 @@ void TesseractDecoder::decode_to_errors_with_graph( } } - if (!pq.empty()) { + if (!pq_container_.empty()) { throw std::runtime_error("Priority queue should be empty after decoding failure."); } if (config.verbose) { @@ -757,31 +841,36 @@ double TesseractDecoder::cost_from_errors(const std::vector& predicted_e std::vector TesseractDecoder::get_flipped_observables( const std::vector& predicted_errors) const { - std::unordered_set flipped_observables_set; - - // Iterate over all errors and compute the mask. - // We use a set to perform an XOR-like sum. - // If an observable is already in the set, we remove it (XORing with itself). - // If it's not, we add it. + if (predicted_errors.empty()) return {}; + std::vector parity(num_observables, 0); + std::vector extra; for (size_t dem_error_index : predicted_errors) { size_t error_index = dem_error_to_error.at(dem_error_index); if (error_index == std::numeric_limits::max()) { throw std::invalid_argument("error index does not map to a retained decoder error"); } for (int obs_index : errors[error_index].symptom.observables) { - if (flipped_observables_set.count(obs_index)) { - flipped_observables_set.erase(obs_index); + if (obs_index >= 0 && static_cast(obs_index) < parity.size()) { + parity[obs_index] ^= 1; } else { - flipped_observables_set.insert(obs_index); + extra.push_back(obs_index); } } } - - // Convert the set to a vector and return it. - std::vector flipped_observables(flipped_observables_set.begin(), - flipped_observables_set.end()); - // Sort observables - std::sort(flipped_observables.begin(), flipped_observables.end()); + std::vector flipped_observables; + for (size_t o = 0; o < parity.size(); ++o) { + if (parity[o]) flipped_observables.push_back(static_cast(o)); + } + if (!extra.empty()) { + std::sort(extra.begin(), extra.end()); + for (size_t i = 0; i < extra.size();) { + size_t j = i + 1; + while (j < extra.size() && extra[j] == extra[i]) ++j; + if ((j - i) & 1) flipped_observables.push_back(extra[i]); + i = j; + } + std::sort(flipped_observables.begin(), flipped_observables.end()); + } return flipped_observables; } @@ -825,7 +914,7 @@ void TesseractDecoder::build_sparse_d2e(const std::vector& detections) std::vector shot_dets(num_detectors, 0); for (uint64_t d : detections) { if (d < num_detectors) { - shot_dets[d] = 1; + shot_dets[d] ^= 1; } } diff --git a/src/tesseract.h b/src/tesseract.h index 41962190..a1cd012b 100644 --- a/src/tesseract.h +++ b/src/tesseract.h @@ -15,12 +15,12 @@ #ifndef TESSERACT_DECODER_H #define TESSERACT_DECODER_H +#include #include #include #include #include #include -#include #include #include @@ -62,6 +62,84 @@ struct TesseractConfig { std::string str(); }; +// Flat open-addressed Zobrist hash set storing 64-bit syndrome hashes paired with 16-bit +// generation tags, supporting sentinel-free lookup (including hash == 0) and O(1) resets between +// detector-ordering trials via generation increment. Note: under no_revisit_dets=true, a 64-bit +// hash collision (~choose(N, 2) / 2^64 <= 1e-9 for N <= 2e5 visited states) acts as a false +// visited hit for that trial. +struct VisitedTable { + std::vector hashes; + std::vector gens; + uint64_t mask = 0; + uint16_t gen = 1; + size_t count = 0; + + void init(size_t min_cap = 1024) { + size_t cap = 16; + while (cap < min_cap) cap <<= 1; + hashes.assign(cap, 0ULL); + gens.assign(cap, 0); + mask = cap - 1; + gen = 1; + count = 0; + } + + bool empty() const { + return hashes.empty(); + } + + void clear() { + count = 0; + if (++gen == 0) { + std::fill(gens.begin(), gens.end(), 0); + gen = 1; + } + } + + bool contains(uint64_t hash) const { + if (hashes.empty()) return false; + size_t idx = static_cast(hash) & mask; + while (gens[idx] == gen) { + if (hashes[idx] == hash) return true; + idx = (idx + 1) & mask; + } + return false; + } + + bool insert(uint64_t hash) { + if (hashes.empty()) init(1024); + if ((count + 1) * 10 >= hashes.size() * 7) grow(); + size_t idx = static_cast(hash) & mask; + while (gens[idx] == gen) { + if (hashes[idx] == hash) return false; + idx = (idx + 1) & mask; + } + hashes[idx] = hash; + gens[idx] = gen; + ++count; + return true; + } + + void grow() { + const size_t new_cap = hashes.size() << 1; + std::vector new_hashes(new_cap, 0ULL); + std::vector new_gens(new_cap, 0); + const uint64_t new_mask = new_cap - 1; + for (size_t i = 0; i < hashes.size(); ++i) { + if (gens[i] == gen) { + const uint64_t h = hashes[i]; + size_t idx = static_cast(h) & new_mask; + while (new_gens[idx] == gen) idx = (idx + 1) & new_mask; + new_hashes[idx] = h; + new_gens[idx] = gen; + } + } + hashes = std::move(new_hashes); + gens = std::move(new_gens); + mask = new_mask; + } +}; + class Node { public: double cost; @@ -141,8 +219,12 @@ struct TesseractDecoder : public Decoder { size_t num_detectors; std::vector>& get_eneighbors() { + for (size_t ei = 0; ei < num_errors; ++ei) { + ensure_eneighbors(ei); + } return eneighbors; } + void ensure_eneighbors(size_t ei); std::vector> d2e; std::vector> sparse_d2e; @@ -181,9 +263,28 @@ struct TesseractDecoder : public Decoder { void update_internal_costs(const std::vector& modified_error_indices, const std::vector& affected_detectors); void build_sparse_d2e(const std::vector& detections); + const std::vector& ensure_det_order_ranks(size_t detector_order_index); void decode_to_errors_with_graph(const std::vector& detections, size_t detector_order_index, size_t detector_beam, const std::vector>& active_d2e); + + std::vector> cached_detector_orders_; + std::vector> det_order_ranks_; + std::vector zobrist_; + VisitedTable visited_; + std::vector eneighbors_ready_; + std::vector eneighbor_seen_; + uint32_t eneighbor_gen_ = 1; + + boost::dynamic_bitset<> initial_detectors_; + boost::dynamic_bitset<> scratch_detectors_; + std::vector initial_dct_; + std::vector scratch_dct_; + std::vector scratch_next_dct_; + std::vector scratch_det_cost_cache_; + std::vector scratch_det_cost_stamp_; + uint32_t scratch_det_cost_gen_ = 1; + std::vector pq_container_; }; } // namespace tesseract_decoder diff --git a/src/tesseract.test.cc b/src/tesseract.test.cc index 11549588..7a05ab2b 100644 --- a/src/tesseract.test.cc +++ b/src/tesseract.test.cc @@ -1172,5 +1172,136 @@ TEST(utils, GariSourcePrefixB8Decodes) { EXPECT_EQ(simplex.decode(shots[1].hits), (std::vector{1})); } +TEST(tesseract, VisitedTableFull64BitSentinelFreeAndGenerationWraparound) { + VisitedTable uninit_table; + EXPECT_TRUE(uninit_table.empty()); + EXPECT_FALSE(uninit_table.contains(0ULL)); + EXPECT_FALSE(uninit_table.contains(42ULL)); + EXPECT_TRUE(uninit_table.insert(42ULL)); + EXPECT_FALSE(uninit_table.empty()); + EXPECT_TRUE(uninit_table.contains(42ULL)); + + VisitedTable table; + EXPECT_TRUE(table.empty()); + table.init(16); + EXPECT_FALSE(table.empty()); + EXPECT_EQ(table.hashes.size(), 16u); + + // Zero hash works natively without any reserved sentinel value. + EXPECT_FALSE(table.contains(0ULL)); + EXPECT_TRUE(table.insert(0ULL)); + EXPECT_TRUE(table.contains(0ULL)); + EXPECT_FALSE(table.insert(0ULL)); + + // Keys identical in the upper 48 bits and differing only in the low 16 bits must not collide. + const uint64_t h_a = 0xCAFEBABE12340000ULL; + const uint64_t h_b = 0xCAFEBABE12340001ULL; + const uint64_t h_c = 0xCAFEBABE1234FFFFULL; + EXPECT_TRUE(table.insert(h_a)); + EXPECT_TRUE(table.insert(h_b)); + EXPECT_TRUE(table.insert(h_c)); + EXPECT_TRUE(table.contains(h_a)); + EXPECT_TRUE(table.contains(h_b)); + EXPECT_TRUE(table.contains(h_c)); + EXPECT_FALSE(table.insert(h_b)); + + // Trigger rehash/grow and verify full 64-bit hashes (including 0ULL) remain intact. + for (uint64_t k = 1; k <= 40; ++k) { + EXPECT_TRUE(table.insert(0x9e3779b97f4a7c15ULL * k)); + } + EXPECT_GT(table.hashes.size(), 16u); + EXPECT_TRUE(table.contains(0ULL)); + EXPECT_TRUE(table.contains(h_a)); + EXPECT_TRUE(table.contains(h_b)); + EXPECT_TRUE(table.contains(h_c)); + for (uint64_t k = 1; k <= 40; ++k) { + EXPECT_TRUE(table.contains(0x9e3779b97f4a7c15ULL * k)); + } + + // O(1) clear resets logical membership immediately and handles 16-bit generation wraparound + // without resurrecting stale entries from 65,535 clears ago. + table.clear(); + EXPECT_FALSE(table.contains(0ULL)); + EXPECT_FALSE(table.contains(h_a)); + EXPECT_TRUE(table.insert(h_a)); + for (uint32_t step = 0; step < 65540; ++step) { + table.clear(); + EXPECT_FALSE(table.contains(h_a)); + EXPECT_FALSE(table.contains(0ULL)); + } + EXPECT_EQ(table.count, 0u); + EXPECT_TRUE(table.insert(0ULL)); + EXPECT_TRUE(table.insert(h_a)); + EXPECT_TRUE(table.contains(0ULL)); + EXPECT_TRUE(table.contains(h_a)); +} + +TEST(tesseract, LazyEneighborsMatchesOnDemandExpansion) { + stim::DetectorErrorModel dem(R"DEM( + error(0.1) D0 D1 L0 + error(0.1) D1 D2 + error(0.1) D2 D3 L0 + error(0.1) D4 D5 + )DEM"); + TesseractConfig config{dem}; + config.merge_errors = false; + TesseractDecoder decoder(config); + // Before any decode or get_eneighbors() call, neighbor lists have not been materialized yet. + for (const auto& nbrs : decoder.eneighbors) { + EXPECT_TRUE(nbrs.empty()); + } + // Decoding D0^D1 expands error 0 on demand without needing disconnected component {D4, D5}. + EXPECT_EQ(decoder.decode({0, 1}), (std::vector{0})); + EXPECT_EQ(decoder.eneighbors[0], (std::vector{2})); + EXPECT_TRUE(decoder.eneighbors[3].empty()); + // Calling get_eneighbors() materializes any remaining unexpanded entries. + const auto& all = decoder.get_eneighbors(); + EXPECT_EQ(all[0], (std::vector{2})); + EXPECT_EQ(all[1], (std::vector{0, 3})); + EXPECT_EQ(all[2], (std::vector{1})); + EXPECT_TRUE(all[3].empty()); +} + +TEST(tesseract, PostCompileDetectorOrderMutationAndDuplicateDetections) { + // On shot {D0, D2} at beam 0, order [0, 1, 2, 3] expands D0 first via {D0, D1, D3} -> {D1, D2, + // D3} (3 > 2 + 0, pruned) and fails, whereas order [2, 0, 1, 3] expands D2 first via {D2, D3} -> + // {D0, D3} -> {D1} -> {} and succeeds. Mutating config.detector_orders after construction must + // refresh cached inverse ranks without crashing or reading stale ranks. + stim::DetectorErrorModel dem(R"DEM( + error(0.1) D0 D1 D3 L0 + error(0.1) D2 D3 + error(0.1) D1 + detector(0, 0, 0) D0 + detector(1, 0, 0) D1 + detector(2, 0, 0) D2 + detector(3, 0, 0) D3 + )DEM"); + TesseractConfig cfg{dem}; + cfg.merge_errors = false; + cfg.det_beam = 0; + cfg.detector_orders = make_literal_detector_orders({{0, 1, 2, 3}}); + TesseractDecoder dec(cfg); + + dec.decode_to_errors({0, 2}); + EXPECT_TRUE(dec.low_confidence_flag); + EXPECT_TRUE(dec.predicted_errors_buffer.empty()); + + // Grow detector_orders after construction from 1 order to 2 orders. + dec.config.detector_orders = make_literal_detector_orders({{0, 1, 2, 3}, {2, 0, 1, 3}}); + EXPECT_EQ(dec.decode({0, 2}), (std::vector{0})); + EXPECT_FALSE(dec.low_confidence_flag); + + // Replace single order in-place with same count, and verify shrinking rejects out-of-range index. + dec.config.detector_orders = make_literal_detector_orders({{2, 0, 1, 3}}); + EXPECT_EQ(dec.decode({0, 2}), (std::vector{0})); + EXPECT_THROW(dec.decode_to_errors({0, 2}, /*detector_order_index=*/1, /*detector_beam=*/0), + std::out_of_range); + + // Duplicate detections cancel mod 2 cleanly instead of underflowing or crashing. + EXPECT_TRUE(dec.decode({0, 0}).empty()); + EXPECT_FALSE(dec.low_confidence_flag); + EXPECT_EQ(dec.decode({0, 0, 0, 2}), (std::vector{0})); +} + } // namespace } // namespace tesseract_decoder