File size: 1,663 Bytes
9f8cf99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Hexagonal dynamic surface code circuit builder."""
from __future__ import annotations

from typing import Dict, Optional

from surface_code_in_stem.noise_models import NoiseModel

from .base import DynamicLayout, StimStringBuilder, stabilizer_cycle


def hexagonal_surface_code(distance: int, rounds: int, p: float, noise_model: Optional[NoiseModel] = None) -> str:
    """Return a Stim circuit string for the hexagonal dynamic surface code.

    The implementation alternates three-edge stabilizer footprints forward and
    reverse in time to model the degree-3 connectivity of Morvan et al.'s hex
    code while keeping detectors stitched between consecutive measurements of
    each ancilla.
    """

    layout = DynamicLayout.build(distance)
    index_to_coord = {idx: coord for coord, idx in layout.coord_to_index.items()}
    builder = StimStringBuilder(coord_lookup=index_to_coord)
    builder.qubit_coords()

    orientations_fwd = (0, 1, 2)
    orientations_rev = tuple(reversed(orientations_fwd))

    prev_meas: Dict[tuple[float, float], int] = {}
    for cycle in range(rounds):
        orient = orientations_fwd if cycle % 2 == 0 else orientations_rev
        prev_meas = stabilizer_cycle(
            builder,
            layout,
            p=p,
            orientations=orient,
            gate="CX",
            noise_model=noise_model,
            reset_data=False,
            measure_data=False,
            prev_meas=prev_meas,
        )

    logical_targets = [layout.coord_to_index[c] for c in layout.datas[:distance]]
    logical_meas = builder.measure(logical_targets)
    builder.observable(logical_meas)
    return builder.build()