errorcorrectionzoo / eczoo_data

Content of the Error Correction Zoo, stored in structured YAML format
https://errorcorrectionzoo.org/
Other
54 stars 90 forks source link

[partial] Attempt to add encoding circuits to [[5,1,3]] page #334

Closed Strilanc closed 8 months ago

Strilanc commented 1 year ago

This PR adds circuit diagrams, parseable stim circuits, and quirk links for the encoders on the [[5,1,3]] page. It also adds a 6-two-qubit gate encoder.

I ran into several problems trying to make these edits that mean the PR is incomplete and needs to pushed through be someone more knowledgeable about the workings of the site.

First, I wasn't able to get yarn working. So I have no way to preview the page. I have no idea if this looks right or is getting completely garbled. (This has always been my experience with node. And all the help about it that I find online is asking me to do dangerous things like pipe curl directly into bash.)

Second, I don't know what syntax you use to define a code block. The circuit diagrams and the computer-parsable stim circuits should be in code blocks.

Third, I'm not sure how to add the circuits in a verified way where anything actually checks that they work? Any kind of big repository of circuits like this needs some way to make sure its contents are actually accurate, or it will be full of mistakes. Circuits are incredibly easy to typo, and very hard to debug when there is a typo. (As an example... I am extremely suspicious that the "four CCZ" decoder circuit claim on the [[5,1,3]] page is totally wrong.) I wrote code to verify the circuits I'm adding:

from typing import Iterable, Optional

import stim

def verify_flow(
        circuit: stim.Circuit,
        *,
        start: Optional[stim.PauliString] = None,
        end: Optional[stim.PauliString] = None,
        measurements: Iterable[int] = (),
        allow_negated: bool = False):

    def do_controlled_pauli_string(
            sim: stim.TableauSimulator,
            control: int,
            paulis: Optional[stim.PauliString]):
        if paulis is None:
            return
        if paulis.sign == -1:
            sim.z(control)
        elif paulis.sign != +1:
            raise NotImplementedError(f'{paulis=}')
        for q, p in enumerate(paulis):
            if p == 1:
                sim.cx(control, q)
            elif p == 2:
                sim.cy(control, q)
            elif p == 3:
                sim.cz(control, q)

    q = circuit.num_qubits
    t = stim.TableauSimulator()
    t.reset_x(q)
    do_controlled_pauli_string(sim=t, control=q, paulis=start)
    t.do(circuit)
    do_controlled_pauli_string(sim=t, control=q, paulis=end)
    ms = t.current_measurement_record()
    for m in measurements:
        if ms[m]:
            t.z(q)
    p = t.peek_x(q)
    if p == 0:
        raise ValueError(f"Flow {start!r}->{end!r} was random.")
    if p == -1 and not allow_negated:
        raise ValueError(f"Flow {start!r}->{end!r} was deterministic, but negated.")

circuit1 = stim.Circuit("""
    R 1 2 3 4

    YCX 0 3
    XCX 0 4
    ZCX 0 2
    XCX 1 2
    YCX 1 3
    XCX 1 4

    SQRT_X 0
    H
    S 3
    C_XYZ 2
    C_ZYX 4

    X 2
    Y 1 3
""")
circuit2 = stim.Circuit("""
            R 1 2 3 4
            TICK

            Z 0
            H 1 2 3 4
            TICK

            CX 1 0
            TICK
            CX 2 0
            TICK
            CX 3 0
            TICK
            CX 4 0
            TICK
            CZ 0 1
            TICK
            CZ 1 2
            TICK
            CZ 2 3
            TICK
            CZ 3 4
            TICK
            CZ 4 0
""")

for circuit in [circuit1, circuit2]:
    print(circuit.diagram())

    verify_flow(circuit, end=stim.PauliString("XZZX_"))
    verify_flow(circuit, end=stim.PauliString("ZZX_X"))
    verify_flow(circuit, end=stim.PauliString("ZX_XZ"))
    verify_flow(circuit, end=stim.PauliString("X_XZZ"))
    verify_flow(circuit, start=stim.PauliString("X____"), end=stim.PauliString("XXXXX"))
    verify_flow(circuit, start=stim.PauliString("Z____"), end=stim.PauliString("ZZZZZ"))

Probably there should actually be special functionality for adding circuits, where the diagrams and source code and external URLs and verification all source from a common syntactical block unique to the site.

valbert4 commented 8 months ago

Thanks, Craig! @phfaist how should we include code blocks? Should we just include the pictures of the circuits?

You're right, the encoder you think is wrong is not "four CCZ" but four more general control gates: image

phfaist commented 8 months ago

Hi both,

Thanks for the input! Sure, the circuit can go as a figure.

I'm thinking about how best to include code blocks on a code page. I'm not sure they'd appear well in the current layout and they might be a bit too detailed for what we're currently aiming for, and typing them directly in the YAML doesn't seem like an optimal solution. I think it would be great to be able to include pieces of code or other documents (e.g. pdf notes) as "attachments" that can be viewed and/or downloaded interactively. That's on the roadmap :)

valbert4 commented 8 months ago

Craig, would it be possible for you to make the figures for these circuits? You'd be acknowledged of course by being a contributor, but also, if you want, as a private comm citation. Thanks!

On Fri, Mar 15, 2024, 17:13 Philippe Faist @.***> wrote:

Hi both,

Thanks for the input! Sure, the circuit can go as a figure.

I'm thinking about how best to include code blocks on a code page. I'm not sure they'd appear well in the current layout and they might be a bit too detailed for what we're currently aiming for, and typing them directly in the YAML doesn't seem like an optimal solution. I think it would be great to be able to include pieces of code or other documents (e.g. pdf notes) as "attachments" that can be viewed and/or downloaded interactively. That's on the roadmap :)

— Reply to this email directly, view it on GitHub https://github.com/errorcorrectionzoo/eczoo_data/pull/334#issuecomment-2000447071, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACCCKUXGCIOJDIJMVUVBQ3LYYNQALAVCNFSM6AAAAAA6HYDMH2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBQGQ2DOMBXGE . You are receiving this because you commented.Message ID: @.***>

valbert4 commented 8 months ago

Closing so someone can realize these circuits as pictures.