Skip to content

Add glue/stimflow directory#1062

Merged
Strilanc merged 10 commits into
mainfrom
addstimflow
May 13, 2026
Merged

Add glue/stimflow directory#1062
Strilanc merged 10 commits into
mainfrom
addstimflow

Conversation

@Strilanc
Copy link
Copy Markdown
Collaborator

@Strilanc Strilanc commented May 11, 2026

stimflow is a library for creating quantum error correction circuits. stimflow's design philosophy is to be a tool box, not a black box. For example, stimflow does not include a make_surface_code method. Instead it provides tools that can be used to more easily create a surface code circuit from scratch. The hope is that these tools make it easier to create as-yet-unknown constructions in the future.

stimflow decomposes the circuit creation problem into making and combining chunks. A Chunk is a circuit combined with stabilizer flow assertions that the circuit is supposed to satisfy. stimflow provides tools for making chunks, verifying chunks, debugging chunks, and compiling sequences of chunks into a complete final circuit.

An example of using stimflow to make a surface code memory experiment:

import stimflow as sf


def make_surface_code(d: int) -> sf.StabilizerCode:
    """Defines the stabilizers and observables of a surface code."""
    tiles = []
    ds = [0, 1, 1j, 1 + 1j]
    for x in range(-1, d):
        for y in range(-1, d):
            m = x + 1j * y
            qs = [m + d for d in ds]
            qs = [q for q in qs
                  if 0 <= q.real < d and 0 <= q.imag < d]
            b = 'XZ'[(x + y) % 2]
            if b == 'X' and x in [-1, d - 1]:
                continue
            if b == 'Z' and y in [-1, d - 1]:
                continue
            tiles.append(sf.Tile(
                data_qubits=qs,
                bases=b,
                measure_qubit=m + 0.5 + 0.5j,
            ))

    patch = sf.Patch(tiles)
    obs_x = sf.PauliMap.from_xs([q for q in patch.data_set if q.real == 0]).with_obs_name('X')
    obs_z = sf.PauliMap.from_zs([q for q in patch.data_set if q.imag == 0]).with_obs_name('Z')
    return sf.StabilizerCode(patch, logicals=[(obs_x, obs_z)])


def make_idle_round(d: int) -> sf.Chunk:
    """Creates a circuit that performs one round of surface code stabilizer measurement."""
    code = make_surface_code(d=d)
    builder = sf.ChunkBuilder(allowed_qubits=code.used_set)
    mxs = [tile.measure_qubit for tile in code.tiles if tile.basis == 'X']
    mzs = [tile.measure_qubit for tile in code.tiles if tile.basis == 'Z']

    # Prepare measure qubits.
    builder.append("RX", mxs)
    builder.append("RZ", mzs)
    builder.append("TICK")

    # Perform entangling gates.
    dxs = [-0.5 - 0.5j, 0.5 - 0.5j, -0.5 + 0.5j, 0.5 + 0.5j]
    dzs = [dxs[0], dxs[2], dxs[1], dxs[3]]
    for k in range(4):
        builder.append(
            'CX',
            [(m, m + dxs[k]) for m in mxs] + [(m + dzs[k], m) for m in mzs],
            unknown_qubit_append_mode='skip',
        )
        builder.append("TICK")

    # Measure the measure qubits.
    builder.append("MX", mxs)
    builder.append("MZ", mzs)

    # Assert the circuit should be preparing and measuring the stabilizers.
    for tile in code.tiles:
        builder.add_flow(start=tile, measurements=[tile.measure_qubit])
        builder.add_flow(end=tile, measurements=[tile.measure_qubit])
    # Assert the circuit should be preserving the logical operators.
    for obs in code.flat_logicals:
        builder.add_flow(start=obs, end=obs)

    return builder.finish_chunk()


def main():
    # Create the code, verify its commutation relationships, and save a picture of it.
    code = make_surface_code(d=7)
    code.verify()
    code.to_svg().write_to('tmp.svg')

    # Create the circuit cycle, verify its operation, and create an interactive viewer.
    chunk = make_idle_round(d=7)
    chunk.to_html_viewer(background=code).write_to('tmp.html')
    chunk.verify()

    # Compile a physical memory experiment with alternating cycle orderings.
    compiler = sf.ChunkCompiler()
    compiler.append(code.transversal_init_chunk(basis='X'))
    compiler.append(sf.ChunkLoop(
        [chunk, chunk.time_reversed()],
        repetitions=5,
    ))
    compiler.append(code.transversal_measure_chunk(basis='X'))
    circuit = compiler.finish_circuit()

    # Add noise to the circuit, check its distance, and make another viewer.
    noisy_circuit = sf.NoiseModel.uniform_depolarizing(1e-3).noisy_circuit(circuit)
    distance = len(noisy_circuit.shortest_graphlike_error())
    assert distance == 7
    sf.stim_circuit_html_viewer(noisy_circuit, background=code).write_to('tmp2.html')


if __name__ == "__main__":
    main()

stimflow is a library for creating quantum error correction circuits
@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@Strilanc Strilanc enabled auto-merge (squash) May 13, 2026 02:50
@Strilanc Strilanc merged commit 6db111d into main May 13, 2026
114 of 115 checks passed
@Strilanc Strilanc deleted the addstimflow branch May 13, 2026 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant