Skip to content

encoder

Transversal encoder utilities for QEC code experiments.

ColorEncoder5

ColorEncoder5()

Bases: TransversalEncoder


              flowchart TD
              tsim.utils.encoder.ColorEncoder5[ColorEncoder5]
              tsim.utils.encoder.TransversalEncoder[TransversalEncoder]

                              tsim.utils.encoder.TransversalEncoder --> tsim.utils.encoder.ColorEncoder5
                


              click tsim.utils.encoder.ColorEncoder5 href "" "tsim.utils.encoder.ColorEncoder5"
              click tsim.utils.encoder.TransversalEncoder href "" "tsim.utils.encoder.TransversalEncoder"
            

Transversal encoder for the [[17,1,5]] 2D color code.

Source code in src/tsim/utils/encoder.py
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
def __init__(self):
    """Initialize the color code encoder."""
    encoding_program = """
    R 0 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16
    SQRT_Y 0 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16
    TICK
    CZ 1 3 7 10 12 14 13 16
    TICK
    SQRT_Y_DAG 7 16
    TICK
    CZ 4 7 8 10 11 14 15 16
    TICK
    SQRT_Y_DAG 4 10 14 16
    TICK
    CZ 2 4 6 8 7 9 10 13
    CZ 14 16
    TICK
    SQRT_Y 3 6 9 10 12 13
    TICK
    CZ 0 2 3 6 5 8 10 12 11 13
    TICK
    SQRT_Y 1 2 3 4 6 7 8 9 11 12 14
    TICK
    CZ 0 1 2 3 4 5 6 7 8 9 12 15
    TICK
    SQRT_Y_DAG 0 2 5 6 8 10 12
    X 14 7 5 2 1 4
    Z 11 6 4 2
    """
    stabs = [
        [0, 1, 2, 3],
        [0, 2, 4, 5],
        [4, 5, 6, 7],
        [6, 7, 8, 9],
        [11, 13, 14, 16],
        [10, 11, 12, 14],
        [12, 14, 15, 16],
        [2, 3, 5, 6, 8, 10, 11, 13],
    ]
    obs = [[1, 3, 10, 12, 15]]
    super().__init__(
        n=17,
        encoding_qubit=7,
        encoding_program_text=encoding_program,
        stabilizer_generators=stabs,
        observables=obs,
    )

SteaneEncoder

SteaneEncoder()

Bases: TransversalEncoder


              flowchart TD
              tsim.utils.encoder.SteaneEncoder[SteaneEncoder]
              tsim.utils.encoder.TransversalEncoder[TransversalEncoder]

                              tsim.utils.encoder.TransversalEncoder --> tsim.utils.encoder.SteaneEncoder
                


              click tsim.utils.encoder.SteaneEncoder href "" "tsim.utils.encoder.SteaneEncoder"
              click tsim.utils.encoder.TransversalEncoder href "" "tsim.utils.encoder.TransversalEncoder"
            

Transversal encoder for the [[7,1,3]] Steane code.

Source code in src/tsim/utils/encoder.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def __init__(self):
    """Initialize the Steane code encoder."""
    encoding_program = """
    R 0 1 2 3 4 5
    TICK
    SQRT_Y_DAG 0 1 2 3 4 5
    TICK
    CZ 1 2 3 4 5 6
    TICK
    SQRT_Y 6
    TICK
    CZ 0 3 2 5 4 6
    TICK
    SQRT_Y 2 3 4 5 6
    TICK
    CZ 0 1 2 3 4 5
    TICK
    SQRT_Y 1 2 4
    TICK
    X 3
    Z 5 1
    TICK
    """
    super().__init__(
        n=7,
        encoding_qubit=6,
        encoding_program_text=encoding_program,
        logical_gate_expansions={
            "SQRT_X": ["SQRT_X", "X"],
            "SQRT_X_DAG": ["SQRT_X_DAG", "X"],
            "S": ["S", "Z"],
            "S_DAG": ["S_DAG", "Z"],
        },
        stabilizer_generators=[[0, 1, 2, 3], [1, 2, 4, 5], [2, 3, 4, 6]],
        observables=[[0, 1, 5]],
    )

TransversalEncoder

TransversalEncoder(
    n: int,
    encoding_qubit: int,
    encoding_program_text: str | None,
    stabilizer_generators: list[list[int]],
    observables: list[list[int]],
    logical_gate_expansions: (
        dict[str, list[str]] | None
    ) = None,
)

Base class for transversal quantum error correction encoders.

Source code in src/tsim/utils/encoder.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def __init__(
    self,
    n: int,
    encoding_qubit: int,
    encoding_program_text: str | None,
    stabilizer_generators: list[list[int]],
    observables: list[list[int]],
    logical_gate_expansions: dict[str, list[str]] | None = None,
):
    """Initialize the transversal encoder with code parameters."""
    self.n = n
    self.encoding_qubit = encoding_qubit
    self.circuit = tsim.Circuit()
    self.used_qubits: set[int] = set()
    self.encoding_program_text = encoding_program_text
    self.logical_gate_expansions = logical_gate_expansions or {}
    self.stabilizer_generators = stabilizer_generators
    self.observables = observables

diagram

diagram(**kwargs)

Return a timeline-svg diagram of the encoded circuit.

Source code in src/tsim/utils/encoder.py
179
180
181
def diagram(self, **kwargs):
    """Return a timeline-svg diagram of the encoded circuit."""
    return self.circuit.diagram("timeline-svg", **kwargs)

encode_transversally

encode_transversally(program_text: str) -> None

Encode a program transversally by replacing physicalgates with transversal gates.

Transform a program on m qubits into a program on n * m qubits (consisting of n code blocks).

Parameters:

Name Type Description Default
program_text str

The program to encode transversally.

required
Source code in src/tsim/utils/encoder.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def encode_transversally(self, program_text: str) -> None:
    """Encode a program transversally by replacing  physicalgates with transversal gates.

    Transform a program on m qubits into a program on n * m qubits (consisting of n code blocks).

    Args:
        program_text: The program to encode transversally.

    """
    mod_circ = _transform_circuit(
        program_text,
        stride=self.n,
        offsets=list(range(self.n)),
        gate_expansions=self.logical_gate_expansions,
        stabilizer_generators=self.stabilizer_generators,
        observables=self.observables,
    )
    self.circuit.append_from_stim_program_text(str(mod_circ))

encoding_flow_generators

encoding_flow_generators()

Return the Pauli flow generators for the encoding circuit.

Source code in src/tsim/utils/encoder.py
183
184
185
186
def encoding_flow_generators(self):
    """Return the Pauli flow generators for the encoding circuit."""
    assert self.encoding_program_text is not None
    return stim.Circuit(self.encoding_program_text).flow_generators()

initialize

initialize(
    program_text: str,
    encoding_program_text: str | None = None,
) -> None

Initialize state preparation and apply encoding circuit.

Apply the state preparation program for k qubits, then apply an encoding circuit to encode the state into n qubits.

Parameters:

Name Type Description Default
program_text str

The state preparation program for k qubits. Generally, this should be a simple program that prepares each of the k qubits in a single-qubit state.

required
encoding_program_text optional

An encoding circuit for a single logical qubit. This should encode a single logical qubit at input self.encoding_qubit into a state of n qubits. If not provided, the encoder will use a noiseless default encoding.

None
Source code in src/tsim/utils/encoder.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def initialize(
    self, program_text: str, encoding_program_text: str | None = None
) -> None:
    """Initialize state preparation and apply encoding circuit.

    Apply the state preparation program for k qubits, then apply an encoding
    circuit to encode the state into n qubits.

    Args:
        program_text: The state preparation program for k qubits. Generally, this
            should be a simple program that prepares each of the k qubits in a
            single-qubit state.
        encoding_program_text (optional): An encoding circuit for a single logical
            qubit. This should encode a single logical qubit at input
            `self.encoding_qubit` into a state of n qubits.
            If not provided, the encoder will use a noiseless default encoding.

    """
    encoding = encoding_program_text or self.encoding_program_text
    if not encoding:
        raise ValueError("Encoding program text is required")

    mod_circ = _transform_circuit(
        program_text,
        stride=self.n,
        offsets=[self.encoding_qubit],
        used_qubits=self.used_qubits,
        stabilizer_generators=self.stabilizer_generators,
        observables=self.observables,
    )

    self.circuit.append_from_stim_program_text(str(mod_circ))
    self.circuit.append_from_stim_program_text(
        str(
            _transform_circuit(
                encoding,
                stride=1,
                offsets=[self.n * off for off in sorted(self.used_qubits)],
                stabilizer_generators=self.stabilizer_generators,
                observables=self.observables,
            )
        )
    )

broadcast_targets

broadcast_targets(
    groups: list[list[GateTarget]],
    *,
    stride: int,
    offsets: list[int]
) -> list[int]

Broadcast gate target groups with a stride and set of offsets.

Source code in src/tsim/utils/encoder.py
 8
 9
10
11
12
13
14
15
16
def broadcast_targets(
    groups: list[list[stim.GateTarget]], *, stride: int, offsets: list[int]
) -> list[int]:
    """Broadcast gate target groups with a stride and set of offsets."""
    out: list[int] = []
    for g in groups:
        for off in offsets:
            out.extend([t.value * stride + off for t in g])
    return out