quantumlib / Stim

A fast stabilizer circuit library.
Apache License 2.0
305 stars 88 forks source link

Add `stim::ReferenceSampleTree` to support loop folded reference sampling #772

Closed Strilanc closed 1 month ago

Strilanc commented 1 month ago

Get a compressed tree representation of the reference sample by using stim::ReferenceSampleTree::from_circuit_reference_sample(circuit).

The reason for a tree, instead of raw run length encoding, is to support nested loops (e.g. physical surface code rounds repeating within repeated logical operations).

Example test:

    CircuitGenParameters params(10000, 3, "rotated_memory_x");
    auto circuit = generate_surface_code_circuit(params).circuit;
    circuit.blocks[0].append_from_text("X 10 11 12 13");
    auto ref = ReferenceSampleTree::from_circuit_reference_sample(circuit);
    ASSERT_EQ(ref.str(), "1*(''+2*('00000000')+4999*('0110000000110000')+1*('000000000'))");

Example benchmark (25 milliseconds to do a distance 31 surface code with a billion rounds):

    CircuitGenParameters params(1000000000, 31, "rotated_memory_x");
    auto circuit = generate_surface_code_circuit(params).circuit;
    simd_bits<MAX_BITWORD_WIDTH> ref(0);
    auto total = 0;
    benchmark_go([&]() {
        auto result = ReferenceSampleTree::from_circuit_reference_sample(circuit);
        total += result.empty();
    })
        .goal_millis(25);

Part of https://github.com/quantumlib/Stim/issues/768