Skip to content

instructions

ZX graph representations of quantum gates and instructions.

GraphRepresentation dataclass

GraphRepresentation(
    graph: GraphS = GraphS(),
    rec: list[int] = list(),
    silent_rec: list[int] = list(),
    detectors: list[int] = list(),
    observables_dict: dict[int, int] = dict(),
    first_vertex: dict[int, int] = dict(),
    last_vertex: dict[int, int] = dict(),
    channel_probs: list[ndarray] = list(),
    correlated_error_probs: list[float] = list(),
    num_error_bits: int = 0,
    num_correlated_error_bits: int = 0,
    track_classical_wires: bool = False,
)

ZX graph built from a stim circuit.

Contains the graph and all auxiliary data needed for sampling.

edge_type property

edge_type

Edge type enum.

observables property

observables: list[int]

Get list of observable vertices sorted by index.

vertex_type property

vertex_type

Vertex type enum.

add_dummy

add_dummy(
    b: GraphRepresentation,
    qubit: int,
    row: float | int | None = None,
) -> int

Add a dummy boundary vertex for a qubit.

Source code in src/tsim/core/instructions.py
81
82
83
84
85
86
87
88
89
def add_dummy(
    b: GraphRepresentation, qubit: int, row: float | int | None = None
) -> int:
    """Add a dummy boundary vertex for a qubit."""
    if row is None:
        row = last_row(b, qubit) + 1
    v1 = b.graph.add_vertex(VertexType.BOUNDARY, qubit=qubit, row=row)
    b.last_vertex[qubit] = v1
    return v1

add_lane

add_lane(b: GraphRepresentation, qubit: int) -> int

Initialize a qubit lane if it doesn't exist.

Source code in src/tsim/core/instructions.py
92
93
94
95
96
97
98
99
def add_lane(b: GraphRepresentation, qubit: int) -> int:
    """Initialize a qubit lane if it doesn't exist."""
    v1 = b.graph.add_vertex(VertexType.BOUNDARY, qubit=qubit, row=0)
    v2 = b.graph.add_vertex(VertexType.BOUNDARY, qubit=qubit, row=1)
    b.graph.add_edge((v1, v2), b.edge_type.SIMPLE)
    b.first_vertex[qubit] = v1
    b.last_vertex[qubit] = v2
    return v1

c_nxyz

c_nxyz(b: GraphRepresentation, qubit: int) -> None

Period 3 axis cycling gate, sending -X -> Y -> Z -> -X.

Source code in src/tsim/core/instructions.py
223
224
225
226
227
def c_nxyz(b: GraphRepresentation, qubit: int) -> None:
    """Period 3 axis cycling gate, sending -X -> Y -> Z -> -X."""
    s_dag(b, qubit)
    sqrt_y_dag(b, qubit)
    b.graph.scalar.add_phase(Fraction(1, 4))

c_nzyx

c_nzyx(b: GraphRepresentation, qubit: int) -> None

Period 3 axis cycling gate, sending -Z -> Y -> X -> -Z.

Source code in src/tsim/core/instructions.py
250
251
252
253
254
def c_nzyx(b: GraphRepresentation, qubit: int) -> None:
    """Period 3 axis cycling gate, sending -Z -> Y -> X -> -Z."""
    s_dag(b, qubit)
    sqrt_x(b, qubit)
    b.graph.scalar.add_phase(Fraction(-1, 4))

c_xnyz

c_xnyz(b: GraphRepresentation, qubit: int) -> None

Period 3 axis cycling gate, sending X -> -Y -> Z -> X.

Source code in src/tsim/core/instructions.py
230
231
232
233
def c_xnyz(b: GraphRepresentation, qubit: int) -> None:
    """Period 3 axis cycling gate, sending X -> -Y -> Z -> X."""
    s(b, qubit)
    h(b, qubit)

c_xynz

c_xynz(b: GraphRepresentation, qubit: int) -> None

Period 3 axis cycling gate, sending X -> Y -> -Z -> X.

Source code in src/tsim/core/instructions.py
236
237
238
239
240
def c_xynz(b: GraphRepresentation, qubit: int) -> None:
    """Period 3 axis cycling gate, sending X -> Y -> -Z -> X."""
    s(b, qubit)
    sqrt_y_dag(b, qubit)
    b.graph.scalar.add_phase(Fraction(1, 4))

c_xyz

c_xyz(b: GraphRepresentation, qubit: int) -> None

Right handed period 3 axis cycling gate, sending X -> Y -> Z -> X.

Source code in src/tsim/core/instructions.py
216
217
218
219
220
def c_xyz(b: GraphRepresentation, qubit: int) -> None:
    """Right handed period 3 axis cycling gate, sending X -> Y -> Z -> X."""
    s_dag(b, qubit)
    h(b, qubit)
    b.graph.scalar.add_phase(Fraction(-1, 4))

c_znyx

c_znyx(b: GraphRepresentation, qubit: int) -> None

Period 3 axis cycling gate, sending Z -> -Y -> X -> Z.

Source code in src/tsim/core/instructions.py
257
258
259
260
261
def c_znyx(b: GraphRepresentation, qubit: int) -> None:
    """Period 3 axis cycling gate, sending Z -> -Y -> X -> Z."""
    s(b, qubit)
    sqrt_x(b, qubit)
    b.graph.scalar.add_phase(Fraction(-1, 4))

c_zynx

c_zynx(b: GraphRepresentation, qubit: int) -> None

Period 3 axis cycling gate, sending Z -> Y -> -X -> Z.

Source code in src/tsim/core/instructions.py
264
265
266
267
268
def c_zynx(b: GraphRepresentation, qubit: int) -> None:
    """Period 3 axis cycling gate, sending Z -> Y -> -X -> Z."""
    s(b, qubit)
    sqrt_x_dag(b, qubit)
    b.graph.scalar.add_phase(Fraction(1, 4))

c_zyx

c_zyx(b: GraphRepresentation, qubit: int) -> None

Left handed period 3 axis cycling gate, sending Z -> Y -> X -> Z.

Source code in src/tsim/core/instructions.py
243
244
245
246
247
def c_zyx(b: GraphRepresentation, qubit: int) -> None:
    """Left handed period 3 axis cycling gate, sending Z -> Y -> X -> Z."""
    h(b, qubit)
    s(b, qubit)
    b.graph.scalar.add_phase(Fraction(1, 4))

cnot

cnot(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None

Apply CNOT (controlled-X) gate.

Source code in src/tsim/core/instructions.py
419
420
421
422
423
424
425
426
def cnot(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None:
    """Apply CNOT (controlled-X) gate."""
    _cx_cz(b, True, control, target, classically_controlled)

correlated_error

correlated_error(
    b: GraphRepresentation,
    qubits: list[int],
    types: list[Literal["X", "Y", "Z"]],
    p: float,
) -> None

Add a correlated error term affecting multiple qubits with given Pauli types.

Source code in src/tsim/core/instructions.py
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
def correlated_error(
    b: GraphRepresentation,
    qubits: list[int],
    types: list[Literal["X", "Y", "Z"]],
    p: float,
) -> None:
    """Add a correlated error term affecting multiple qubits with given Pauli types."""
    for qubit, type_ in zip(qubits, types, strict=True):
        if type_ == "X" or type_ == "Y":
            _error(b, qubit, b.vertex_type.X, f"c{b.num_correlated_error_bits}")
        if type_ == "Z" or type_ == "Y":
            _error(b, qubit, b.vertex_type.Z, f"c{b.num_correlated_error_bits}")

    b.correlated_error_probs.append(p)
    b.num_correlated_error_bits += 1

cxswap

cxswap(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Apply CX then SWAP.

Source code in src/tsim/core/instructions.py
465
466
467
468
def cxswap(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Apply CX then SWAP."""
    cnot(b, qubit1, qubit2)
    swap(b, qubit1, qubit2)

cy

cy(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None

Apply controlled-Y gate.

Source code in src/tsim/core/instructions.py
429
430
431
432
433
434
435
436
437
438
def cy(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None:
    """Apply controlled-Y gate."""
    s_dag(b, target)
    cnot(b, control, target, classically_controlled)
    s(b, target)

cz

cz(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None

Apply controlled-Z gate.

Source code in src/tsim/core/instructions.py
441
442
443
444
445
446
447
448
def cz(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None:
    """Apply controlled-Z gate."""
    _cx_cz(b, False, control, target, classically_controlled)

czswap

czswap(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Apply CZ then SWAP.

Source code in src/tsim/core/instructions.py
471
472
473
474
def czswap(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Apply CZ then SWAP."""
    cz(b, qubit1, qubit2)
    swap(b, qubit1, qubit2)

depolarize1

depolarize1(
    b: GraphRepresentation, qubit: int, p: float
) -> None

Apply single-qubit depolarizing channel with total error probability p.

Source code in src/tsim/core/instructions.py
674
675
676
def depolarize1(b: GraphRepresentation, qubit: int, p: float) -> None:
    """Apply single-qubit depolarizing channel with total error probability p."""
    pauli_channel_1(b, qubit, p / 3, p / 3, p / 3)

depolarize2

depolarize2(
    b: GraphRepresentation,
    qubit_i: int,
    qubit_j: int,
    p: float,
) -> None

Apply two-qubit depolarizing channel with total error probability p.

Source code in src/tsim/core/instructions.py
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
def depolarize2(b: GraphRepresentation, qubit_i: int, qubit_j: int, p: float) -> None:
    """Apply two-qubit depolarizing channel with total error probability p."""
    pauli_channel_2(
        b,
        qubit_i,
        qubit_j,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
        p / 15,
    )

detector

detector(
    b: GraphRepresentation, rec: list[int], *args
) -> None

Add detector annotation that XORs the given measurement record bits.

Source code in src/tsim/core/instructions.py
1144
1145
1146
1147
1148
1149
1150
1151
1152
def detector(b: GraphRepresentation, rec: list[int], *args) -> None:
    """Add detector annotation that XORs the given measurement record bits."""
    row = _annotation_row(b, rec)
    v0 = b.graph.add_vertex(
        VertexType.X, qubit=-1, row=row, phase=f"det[{len(b.detectors)}]"
    )
    for rec_ in rec:
        b.graph.add_edge((v0, b.rec[rec_]))
    b.detectors.append(v0)

ensure_lane

ensure_lane(b: GraphRepresentation, qubit: int) -> None

Ensure qubit lane exists.

Source code in src/tsim/core/instructions.py
102
103
104
105
def ensure_lane(b: GraphRepresentation, qubit: int) -> None:
    """Ensure qubit lane exists."""
    if qubit not in b.last_vertex:
        add_lane(b, qubit)

finalize_correlated_error

finalize_correlated_error(b: GraphRepresentation) -> None

Finalize the current correlated error channel.

  1. Rename all "c{i}" phases to "e{num_error_bits + i}" in the graph
  2. Compute and append the 2^k probability array to channel_probs
  3. Increment num_error_bits by k
  4. Reset num_correlated_error_bits to 0 and correlated_error_probs to []
Source code in src/tsim/core/instructions.py
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
def finalize_correlated_error(b: GraphRepresentation) -> None:
    """Finalize the current correlated error channel.

    1. Rename all "c{i}" phases to "e{num_error_bits + i}" in the graph
    2. Compute and append the 2^k probability array to channel_probs
    3. Increment num_error_bits by k
    4. Reset num_correlated_error_bits to 0 and correlated_error_probs to []
    """
    k = b.num_correlated_error_bits

    if k == 0:
        return

    # Rename "c{i}" phases to "e{num_error_bits + i}" in the graph
    for v in b.graph.vertices():
        phase_vars = b.graph._phaseVars.get(v, set())
        new_phase_vars = set()
        for var in phase_vars:
            if isinstance(var, str) and var.startswith("c"):
                # Extract the bit index from "c{i}"
                bit_idx = int(var[1:])
                new_phase_vars.add(f"e{b.num_error_bits + bit_idx}")
            else:
                new_phase_vars.add(var)
        b.graph._phaseVars[v] = new_phase_vars

    # Compute probability array from conditional probabilities
    probs = correlated_error_probs(b.correlated_error_probs)
    b.channel_probs.append(probs)

    b.num_error_bits += k

    # Reset correlated error state
    b.num_correlated_error_bits = 0
    b.correlated_error_probs = []

h

h(b: GraphRepresentation, qubit: int) -> None

Apply Hadamard gate.

Source code in src/tsim/core/instructions.py
271
272
273
274
275
276
277
278
279
280
281
282
def h(b: GraphRepresentation, qubit: int) -> None:
    """Apply Hadamard gate."""
    ensure_lane(b, qubit)
    e = last_edge(b, qubit)
    b.graph.set_edge_type(
        e,
        (
            b.edge_type.HADAMARD
            if b.graph.edge_type(e) == b.edge_type.SIMPLE
            else b.edge_type.SIMPLE
        ),
    )

h_nxy

h_nxy(b: GraphRepresentation, qubit: int) -> None

Apply Hadamard-like gate that sends -X <-> Y, Z -> -Z.

Source code in src/tsim/core/instructions.py
292
293
294
295
def h_nxy(b: GraphRepresentation, qubit: int) -> None:
    """Apply Hadamard-like gate that sends -X <-> Y, Z -> -Z."""
    x(b, qubit)
    s_dag(b, qubit)

h_nxz

h_nxz(b: GraphRepresentation, qubit: int) -> None

Apply Hadamard-like gate that sends -X <-> Z.

Source code in src/tsim/core/instructions.py
298
299
300
301
302
def h_nxz(b: GraphRepresentation, qubit: int) -> None:
    """Apply Hadamard-like gate that sends -X <-> Z."""
    z(b, qubit)
    sqrt_y_dag(b, qubit)
    b.graph.scalar.add_phase(Fraction(1, 4))

h_nyz

h_nyz(b: GraphRepresentation, qubit: int) -> None

Apply Hadamard-like gate that sends -Y <-> Z, X -> -X.

Source code in src/tsim/core/instructions.py
312
313
314
315
316
def h_nyz(b: GraphRepresentation, qubit: int) -> None:
    """Apply Hadamard-like gate that sends -Y <-> Z, X -> -X."""
    z(b, qubit)
    sqrt_x(b, qubit)
    b.graph.scalar.add_phase(Fraction(-1, 4))

h_xy

h_xy(b: GraphRepresentation, qubit: int) -> None

Apply variant of Hadamard gate that swaps the X and Y axes (instead of X and Z).

Source code in src/tsim/core/instructions.py
285
286
287
288
289
def h_xy(b: GraphRepresentation, qubit: int) -> None:
    """Apply variant of Hadamard gate that swaps the X and Y axes (instead of X and Z)."""
    x(b, qubit)
    s(b, qubit)
    b.graph.scalar.add_phase(Fraction(-1, 4))

h_yz

h_yz(b: GraphRepresentation, qubit: int) -> None

Apply variant of Hadamard gate that swaps the Y and Z axes (instead of X and Z).

Source code in src/tsim/core/instructions.py
305
306
307
308
309
def h_yz(b: GraphRepresentation, qubit: int) -> None:
    """Apply variant of Hadamard gate that swaps the Y and Z axes (instead of X and Z)."""
    sqrt_x(b, qubit)
    z(b, qubit)
    b.graph.scalar.add_phase(Fraction(-1, 4))

heralded_erase

heralded_erase(
    b: GraphRepresentation, qubit: int, p: float
) -> None

Apply heralded erasure channel.

Special case of heralded_pauli_channel_1 with equal probabilities p/4 for each of I, X, Y, Z when the channel fires.

Source code in src/tsim/core/instructions.py
750
751
752
753
754
755
756
def heralded_erase(b: GraphRepresentation, qubit: int, p: float) -> None:
    """Apply heralded erasure channel.

    Special case of heralded_pauli_channel_1 with equal probabilities p/4
    for each of I, X, Y, Z when the channel fires.
    """
    heralded_pauli_channel_1(b, qubit, p / 4, p / 4, p / 4, p / 4)

heralded_pauli_channel_1

heralded_pauli_channel_1(
    b: GraphRepresentation,
    qubit: int,
    pi: float,
    px: float,
    py: float,
    pz: float,
) -> None

Apply heralded single-qubit Pauli channel.

Records a herald bit into the measurement record. When the channel fires (with total probability pi+px+py+pz), the herald is 1 and one of I/X/Y/Z is applied. When it doesn't fire, the herald is 0 and nothing happens.

Source code in src/tsim/core/instructions.py
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
def heralded_pauli_channel_1(
    b: GraphRepresentation,
    qubit: int,
    pi: float,
    px: float,
    py: float,
    pz: float,
) -> None:
    """Apply heralded single-qubit Pauli channel.

    Records a herald bit into the measurement record. When the channel fires
    (with total probability pi+px+py+pz), the herald is 1 and one of I/X/Y/Z
    is applied. When it doesn't fire, the herald is 0 and nothing happens.
    """
    b.channel_probs.append(heralded_pauli_channel_1_probs(pi, px, py, pz))
    aux = -2
    r(b, aux)
    _error(b, aux, b.vertex_type.X, f"e{b.num_error_bits}")  # herald bit flip
    m(b, aux)
    _error(b, qubit, b.vertex_type.Z, f"e{b.num_error_bits + 1}")  # Z error bit
    _error(b, qubit, b.vertex_type.X, f"e{b.num_error_bits + 2}")  # X error bit
    b.num_error_bits += 3

i

i(
    b: GraphRepresentation, qubit: int, *_args: float
) -> None

Apply identity (advances the row).

Source code in src/tsim/core/instructions.py
181
182
183
184
185
def i(b: GraphRepresentation, qubit: int, *_args: float) -> None:
    """Apply identity (advances the row)."""
    ensure_lane(b, qubit)
    v = b.last_vertex[qubit]
    b.graph.set_row(v, last_row(b, qubit) + 1)

ii

ii(
    b: GraphRepresentation,
    qubit1: int,
    qubit2: int,
    *_args: float
) -> None

Apply two-qubit identity (advances the row on both qubits).

Source code in src/tsim/core/instructions.py
188
189
190
191
def ii(b: GraphRepresentation, qubit1: int, qubit2: int, *_args: float) -> None:
    """Apply two-qubit identity (advances the row on both qubits)."""
    i(b, qubit1)
    i(b, qubit2)

iswap

iswap(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Swap two qubits and phase the -1 eigenspace of the ZZ observable by i.

Source code in src/tsim/core/instructions.py
489
490
491
492
493
494
def iswap(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Swap two qubits and phase the -1 eigenspace of the ZZ observable by i."""
    cnot(b, qubit1, qubit2)
    s(b, qubit2)
    cnot(b, qubit1, qubit2)
    swap(b, qubit1, qubit2)

iswap_dag

iswap_dag(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Swap two qubits and phase the -1 eigenspace of the ZZ observable by -i.

Source code in src/tsim/core/instructions.py
497
498
499
500
501
502
def iswap_dag(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Swap two qubits and phase the -1 eigenspace of the ZZ observable by -i."""
    cnot(b, qubit1, qubit2)
    s_dag(b, qubit2)
    cnot(b, qubit1, qubit2)
    swap(b, qubit1, qubit2)

last_edge

last_edge(b: GraphRepresentation, qubit: int)

Get the last edge for a qubit.

Source code in src/tsim/core/instructions.py
74
75
76
77
78
def last_edge(b: GraphRepresentation, qubit: int):
    """Get the last edge for a qubit."""
    edges = b.graph.incident_edges(b.last_vertex[qubit])
    assert len(edges) == 1
    return edges[0]

last_row

last_row(b: GraphRepresentation, qubit: int) -> float

Get the row of the last vertex for a qubit.

Source code in src/tsim/core/instructions.py
69
70
71
def last_row(b: GraphRepresentation, qubit: int) -> float:
    """Get the row of the last vertex for a qubit."""
    return b.graph.row(b.last_vertex[qubit])

m

m(
    b: GraphRepresentation,
    qubit: int,
    p: float = 0,
    invert: bool = False,
) -> None

Measure qubit in Z basis with optional bit-flip error probability p.

Source code in src/tsim/core/instructions.py
868
869
870
871
872
873
874
def m(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) -> None:
    """Measure qubit in Z basis with optional bit-flip error probability p."""
    if invert:
        x(b, qubit)
    _m(b, qubit, p, silent=False)
    if invert:
        x(b, qubit)

mpad

mpad(
    b: GraphRepresentation, value: int, p: float = 0
) -> None

Pad measurement record with a fixed bit value.

Parameters:

Name Type Description Default
b GraphRepresentation

The graph representation to modify.

required
value int

The bit value to record (0 or 1).

required
p float

Error probability for the recorded bit.

0
Source code in src/tsim/core/instructions.py
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
def mpad(b: GraphRepresentation, value: int, p: float = 0) -> None:
    """Pad measurement record with a fixed bit value.

    Args:
        b: The graph representation to modify.
        value: The bit value to record (0 or 1).
        p: Error probability for the recorded bit.

    """
    aux = -2
    r(b, aux)
    if value == 1:
        x(b, aux)
    m(b, aux, p=p)

mpp

mpp(
    b: GraphRepresentation,
    paulis: list[tuple[Literal["X", "Y", "Z"], int]],
    invert: bool = False,
    p: float = 0,
) -> None

Measure a single Pauli product.

Parameters:

Name Type Description Default
b GraphRepresentation

The graph representation to modify.

required
paulis list[tuple[Literal['X', 'Y', 'Z'], int]]

List of (pauli_type, qubit) pairs defining the Pauli product.

required
invert bool

Whether to invert the measurement result.

False
p float

Measurement flip error probability.

0
Source code in src/tsim/core/instructions.py
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
def mpp(
    b: GraphRepresentation,
    paulis: list[tuple[Literal["X", "Y", "Z"], int]],
    invert: bool = False,
    p: float = 0,
) -> None:
    """Measure a single Pauli product.

    Args:
        b: The graph representation to modify.
        paulis: List of (pauli_type, qubit) pairs defining the Pauli product.
        invert: Whether to invert the measurement result.
        p: Measurement flip error probability.

    """
    aux = -2
    r(b, aux)
    h(b, aux)
    _apply_pauli_controls(b, aux, paulis)
    h(b, aux)
    m(b, aux, p=p, invert=invert)

mr

mr(
    b: GraphRepresentation,
    qubit: int,
    p: float = 0,
    invert: bool = False,
) -> None

Z-basis demolition measurement (optionally noisy).

Projects each target qubit into |0> or |1>, reports its value (false=|0>, true=|1>), then resets to |0>.

Source code in src/tsim/core/instructions.py
1020
1021
1022
1023
1024
1025
1026
1027
def mr(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) -> None:
    """Z-basis demolition measurement (optionally noisy).

    Projects each target qubit into |0> or |1>, reports its value (false=|0>, true=|1>),
    then resets to |0>.
    """
    m(b, qubit, p=p, invert=invert)
    _r(b, qubit)

mrx

mrx(
    b: GraphRepresentation,
    qubit: int,
    p: float = 0,
    invert: bool = False,
) -> None

X-basis demolition measurement (optionally noisy).

Projects each target qubit into |+> or |->, reports its value (false=|+>, true=|->), then resets to |+>.

Source code in src/tsim/core/instructions.py
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
def mrx(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) -> None:
    """X-basis demolition measurement (optionally noisy).

    Projects each target qubit into |+> or |->, reports its value (false=|+>, true=|->),
    then resets to |+>.
    """
    h(b, qubit)
    m(b, qubit, p=p, invert=invert)
    _r(b, qubit)
    h(b, qubit)

mry

mry(
    b: GraphRepresentation,
    qubit: int,
    p: float = 0,
    invert: bool = False,
) -> None

Y-basis demolition measurement (optionally noisy).

Projects each target qubit into |i> or |-i>, reports its value (false=|i>, true=|-i>), then resets to |i>.

Source code in src/tsim/core/instructions.py
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
def mry(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) -> None:
    """Y-basis demolition measurement (optionally noisy).

    Projects each target qubit into |i> or |-i>, reports its value (false=|i>, true=|-i>),
    then resets to |i>.
    """
    h_yz(b, qubit)
    m(b, qubit, p=p, invert=invert)
    _r(b, qubit)
    h_yz(b, qubit)

mx

mx(
    b: GraphRepresentation,
    qubit: int,
    p: float = 0,
    invert: bool = False,
) -> None

Measure qubit in X basis.

Source code in src/tsim/core/instructions.py
1054
1055
1056
1057
1058
def mx(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) -> None:
    """Measure qubit in X basis."""
    h(b, qubit)
    m(b, qubit, p=p, invert=invert)
    h(b, qubit)

mxx

mxx(
    b: GraphRepresentation,
    q0: int,
    q1: int,
    p: float = 0,
    invert: bool = False,
) -> None

Measure two qubits in XX basis.

Source code in src/tsim/core/instructions.py
1068
1069
1070
1071
1072
def mxx(
    b: GraphRepresentation, q0: int, q1: int, p: float = 0, invert: bool = False
) -> None:
    """Measure two qubits in XX basis."""
    mpp(b, [("X", q0), ("X", q1)], invert, p=p)

my

my(
    b: GraphRepresentation,
    qubit: int,
    p: float = 0,
    invert: bool = False,
) -> None

Measure qubit in Y basis.

Source code in src/tsim/core/instructions.py
1061
1062
1063
1064
1065
def my(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) -> None:
    """Measure qubit in Y basis."""
    h_yz(b, qubit)
    m(b, qubit, p=p, invert=invert)
    h_yz(b, qubit)

myy

myy(
    b: GraphRepresentation,
    q0: int,
    q1: int,
    p: float = 0,
    invert: bool = False,
) -> None

Measure two qubits in YY basis.

Source code in src/tsim/core/instructions.py
1075
1076
1077
1078
1079
def myy(
    b: GraphRepresentation, q0: int, q1: int, p: float = 0, invert: bool = False
) -> None:
    """Measure two qubits in YY basis."""
    mpp(b, [("Y", q0), ("Y", q1)], invert, p=p)

mzz

mzz(
    b: GraphRepresentation,
    q0: int,
    q1: int,
    p: float = 0,
    invert: bool = False,
) -> None

Measure two qubits in ZZ basis.

Source code in src/tsim/core/instructions.py
1082
1083
1084
1085
1086
def mzz(
    b: GraphRepresentation, q0: int, q1: int, p: float = 0, invert: bool = False
) -> None:
    """Measure two qubits in ZZ basis."""
    mpp(b, [("Z", q0), ("Z", q1)], invert, p=p)

observable_include

observable_include(
    b: GraphRepresentation, rec: list[int], idx: int
) -> None

Add observable annotation that XORs the given measurement record bits.

Source code in src/tsim/core/instructions.py
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
def observable_include(b: GraphRepresentation, rec: list[int], idx: int) -> None:
    """Add observable annotation that XORs the given measurement record bits."""
    idx = int(idx)

    if idx not in b.observables_dict:
        row = _annotation_row(b, rec)
        v0 = b.graph.add_vertex(VertexType.X, qubit=-1, row=row, phase=f"obs[{idx}]")
        b.observables_dict[idx] = v0

    v0 = b.observables_dict[idx]
    for rec_ in rec:
        b.graph.add_edge((v0, b.rec[rec_]))

pauli_channel_1

pauli_channel_1(
    b: GraphRepresentation,
    qubit: int,
    px: float,
    py: float,
    pz: float,
) -> None

Apply single-qubit Pauli channel with given X, Y, Z error probabilities.

Source code in src/tsim/core/instructions.py
631
632
633
634
635
636
637
638
def pauli_channel_1(
    b: GraphRepresentation, qubit: int, px: float, py: float, pz: float
) -> None:
    """Apply single-qubit Pauli channel with given X, Y, Z error probabilities."""
    b.channel_probs.append(pauli_channel_1_probs(px, py, pz))
    _error(b, qubit, b.vertex_type.Z, f"e{b.num_error_bits}")
    _error(b, qubit, b.vertex_type.X, f"e{b.num_error_bits + 1}")
    b.num_error_bits += 2

pauli_channel_2

pauli_channel_2(
    b: GraphRepresentation,
    qubit_i: int,
    qubit_j: int,
    pix: float,
    piy: float,
    piz: float,
    pxi: float,
    pxx: float,
    pxy: float,
    pxz: float,
    pyi: float,
    pyx: float,
    pyy: float,
    pyz: float,
    pzi: float,
    pzx: float,
    pzy: float,
    pzz: float,
) -> None

Apply two-qubit Pauli channel with given error probabilities for all 15 Pauli pairs.

Source code in src/tsim/core/instructions.py
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
def pauli_channel_2(
    b: GraphRepresentation,
    qubit_i: int,
    qubit_j: int,
    pix: float,
    piy: float,
    piz: float,
    pxi: float,
    pxx: float,
    pxy: float,
    pxz: float,
    pyi: float,
    pyx: float,
    pyy: float,
    pyz: float,
    pzi: float,
    pzx: float,
    pzy: float,
    pzz: float,
) -> None:
    """Apply two-qubit Pauli channel with given error probabilities for all 15 Pauli pairs."""
    b.channel_probs.append(
        pauli_channel_2_probs(
            pix, piy, piz, pxi, pxx, pxy, pxz, pyi, pyx, pyy, pyz, pzi, pzx, pzy, pzz
        )
    )
    _error(b, qubit_i, b.vertex_type.Z, f"e{b.num_error_bits}")
    _error(b, qubit_i, b.vertex_type.X, f"e{b.num_error_bits + 1}")
    _error(b, qubit_j, b.vertex_type.Z, f"e{b.num_error_bits + 2}")
    _error(b, qubit_j, b.vertex_type.X, f"e{b.num_error_bits + 3}")
    b.num_error_bits += 4

r

r(b: GraphRepresentation, qubit: int) -> None

Z-basis reset.

Forces each target qubit into the |0> state by silently measuring it in the Z basis and applying an X gate if it ended up in the |1> state.

Source code in src/tsim/core/instructions.py
1089
1090
1091
1092
1093
1094
1095
def r(b: GraphRepresentation, qubit: int) -> None:
    """Z-basis reset.

    Forces each target qubit into the |0> state by silently measuring it in the Z basis
    and applying an X gate if it ended up in the |1> state.
    """
    _r(b, qubit)

r_x

r_x(
    b: GraphRepresentation, qubit: int, phase: Fraction
) -> None

Apply R_X rotation gate with given phase (in units of π).

Source code in src/tsim/core/instructions.py
149
150
151
152
def r_x(b: GraphRepresentation, qubit: int, phase: Fraction) -> None:
    """Apply R_X rotation gate with given phase (in units of π)."""
    x_phase(b, qubit, phase)
    b.graph.scalar.add_phase(-phase / 2)

r_y

r_y(
    b: GraphRepresentation, qubit: int, phase: Fraction
) -> None

Apply R_Y rotation gate with given phase (in units of π).

Source code in src/tsim/core/instructions.py
155
156
157
158
159
def r_y(b: GraphRepresentation, qubit: int, phase: Fraction) -> None:
    """Apply R_Y rotation gate with given phase (in units of π)."""
    h_yz(b, qubit)
    r_z(b, qubit, phase)
    h_yz(b, qubit)

r_z

r_z(
    b: GraphRepresentation, qubit: int, phase: Fraction
) -> None

Apply R_Z rotation gate with given phase (in units of π).

Source code in src/tsim/core/instructions.py
143
144
145
146
def r_z(b: GraphRepresentation, qubit: int, phase: Fraction) -> None:
    """Apply R_Z rotation gate with given phase (in units of π)."""
    z_phase(b, qubit, phase)
    b.graph.scalar.add_phase(-phase / 2)

rx

rx(b: GraphRepresentation, qubit: int) -> None

X-basis reset.

Forces each target qubit into the |+> state by silently measuring it in the X basis and applying a Z gate if it ended up in the |-> state.

Source code in src/tsim/core/instructions.py
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
def rx(b: GraphRepresentation, qubit: int) -> None:
    """X-basis reset.

    Forces each target qubit into the |+> state by silently measuring it in the X basis
    and applying a Z gate if it ended up in the |-> state.
    """
    if qubit in b.last_vertex:
        h(b, qubit)
    r(b, qubit)
    h(b, qubit)

ry

ry(b: GraphRepresentation, qubit: int) -> None

Y-basis reset.

Forces each target qubit into the |i> state by silently measuring it in the Y basis and applying an X gate if it ended up in the |-i> state.

Source code in src/tsim/core/instructions.py
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
def ry(b: GraphRepresentation, qubit: int) -> None:
    """Y-basis reset.

    Forces each target qubit into the |i> state by silently measuring it in the Y basis
    and applying an X gate if it ended up in the |-i> state.
    """
    if qubit in b.last_vertex:
        h_yz(b, qubit)
    r(b, qubit)
    h_yz(b, qubit)

s

s(b: GraphRepresentation, qubit: int) -> None

Apply S gate (π/2 Z rotation).

Source code in src/tsim/core/instructions.py
319
320
321
def s(b: GraphRepresentation, qubit: int) -> None:
    """Apply S gate (π/2 Z rotation)."""
    z_phase(b, qubit, Fraction(1, 2))

s_dag

s_dag(b: GraphRepresentation, qubit: int) -> None

Apply S† gate (-π/2 Z rotation).

Source code in src/tsim/core/instructions.py
358
359
360
def s_dag(b: GraphRepresentation, qubit: int) -> None:
    """Apply S† gate (-π/2 Z rotation)."""
    z_phase(b, qubit, Fraction(-1, 2))

spp

spp(
    b: GraphRepresentation,
    paulis: list[tuple[Literal["X", "Y", "Z"], int]],
    dagger: bool = False,
) -> None

Apply exp(-i pi/4 P) (up to global phase) for a Pauli product P.

Phases the -1 eigenspace of P by i (or -i if dagger). For a single qubit, SPP Z0 is the S gate and SPP_DAG Z0 is S_DAG.

Parameters:

Name Type Description Default
b GraphRepresentation

The graph representation to modify.

required
paulis list[tuple[Literal['X', 'Y', 'Z'], int]]

List of (pauli_type, qubit) pairs defining the Pauli product P.

required
dagger bool

If True, apply exp(+i pi/4 P) (phase by -i) instead.

False
Source code in src/tsim/core/instructions.py
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
def spp(
    b: GraphRepresentation,
    paulis: list[tuple[Literal["X", "Y", "Z"], int]],
    dagger: bool = False,
) -> None:
    """Apply exp(-i pi/4 P) (up to global phase) for a Pauli product P.

    Phases the -1 eigenspace of P by i (or -i if dagger). For a single qubit,
    ``SPP Z0`` is the S gate and ``SPP_DAG Z0`` is S_DAG.

    Args:
        b: The graph representation to modify.
        paulis: List of (pauli_type, qubit) pairs defining the Pauli product P.
        dagger: If True, apply exp(+i pi/4 P) (phase by -i) instead.

    """
    _pauli_product_phase(b, paulis, s, s_dag, dagger)

sqrt_x

sqrt_x(b: GraphRepresentation, qubit: int) -> None

Apply √X gate (π/2 X rotation).

Source code in src/tsim/core/instructions.py
324
325
326
def sqrt_x(b: GraphRepresentation, qubit: int) -> None:
    """Apply √X gate (π/2 X rotation)."""
    x_phase(b, qubit, Fraction(1, 2))

sqrt_x_dag

sqrt_x_dag(b: GraphRepresentation, qubit: int) -> None

Apply √X† gate (-π/2 X rotation).

Source code in src/tsim/core/instructions.py
329
330
331
def sqrt_x_dag(b: GraphRepresentation, qubit: int) -> None:
    """Apply √X† gate (-π/2 X rotation)."""
    x_phase(b, qubit, Fraction(-1, 2))

sqrt_xx

sqrt_xx(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Phases the -1 eigenspace of the XX observable by i.

Source code in src/tsim/core/instructions.py
505
506
507
508
509
def sqrt_xx(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Phases the -1 eigenspace of the XX observable by i."""
    cnot(b, qubit1, qubit2)
    sqrt_x(b, qubit1)
    cnot(b, qubit1, qubit2)

sqrt_xx_dag

sqrt_xx_dag(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Phases the -1 eigenspace of the XX observable by -i.

Source code in src/tsim/core/instructions.py
512
513
514
515
516
def sqrt_xx_dag(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Phases the -1 eigenspace of the XX observable by -i."""
    cnot(b, qubit1, qubit2)
    sqrt_x_dag(b, qubit1)
    cnot(b, qubit1, qubit2)

sqrt_y

sqrt_y(b: GraphRepresentation, qubit: int) -> None

Apply √Y gate (π/2 Y rotation).

Source code in src/tsim/core/instructions.py
334
335
336
337
338
def sqrt_y(b: GraphRepresentation, qubit: int) -> None:
    """Apply √Y gate (π/2 Y rotation)."""
    z(b, qubit)
    h(b, qubit)
    b.graph.scalar.add_phase(Fraction(1, 4))

sqrt_y_dag

sqrt_y_dag(b: GraphRepresentation, qubit: int) -> None

Apply √Y† gate (-π/2 Y rotation).

Source code in src/tsim/core/instructions.py
341
342
343
344
345
def sqrt_y_dag(b: GraphRepresentation, qubit: int) -> None:
    """Apply √Y† gate (-π/2 Y rotation)."""
    h(b, qubit)
    z(b, qubit)
    b.graph.scalar.add_phase(Fraction(-1, 4))

sqrt_yy

sqrt_yy(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Phases the -1 eigenspace of the YY observable by i.

Source code in src/tsim/core/instructions.py
519
520
521
522
523
524
525
526
527
def sqrt_yy(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Phases the -1 eigenspace of the YY observable by i."""
    s(b, qubit1)
    cnot(b, qubit2, qubit1)
    z(b, qubit1)
    h(b, qubit2)
    cnot(b, qubit2, qubit1)
    s(b, qubit1)
    b.graph.scalar.add_phase(Fraction(1, 4))

sqrt_yy_dag

sqrt_yy_dag(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Phases the -1 eigenspace of the YY observable by -i.

Source code in src/tsim/core/instructions.py
530
531
532
533
534
535
536
537
538
def sqrt_yy_dag(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Phases the -1 eigenspace of the YY observable by -i."""
    s_dag(b, qubit1)
    cnot(b, qubit2, qubit1)
    h(b, qubit2)
    z(b, qubit1)
    cnot(b, qubit2, qubit1)
    s_dag(b, qubit1)
    b.graph.scalar.add_phase(Fraction(-1, 4))

sqrt_z

sqrt_z(b: GraphRepresentation, qubit: int) -> None

Apply √Z gate (alias for S gate).

Source code in src/tsim/core/instructions.py
348
349
350
def sqrt_z(b: GraphRepresentation, qubit: int) -> None:
    """Apply √Z gate (alias for S gate)."""
    s(b, qubit)

sqrt_z_dag

sqrt_z_dag(b: GraphRepresentation, qubit: int) -> None

Apply √Z† gate (alias for S† gate).

Source code in src/tsim/core/instructions.py
353
354
355
def sqrt_z_dag(b: GraphRepresentation, qubit: int) -> None:
    """Apply √Z† gate (alias for S† gate)."""
    s_dag(b, qubit)

sqrt_zz

sqrt_zz(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Phases the -1 eigenspace of the ZZ observable by i.

Source code in src/tsim/core/instructions.py
541
542
543
544
545
def sqrt_zz(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Phases the -1 eigenspace of the ZZ observable by i."""
    cnot(b, qubit1, qubit2)
    s(b, qubit2)
    cnot(b, qubit1, qubit2)

sqrt_zz_dag

sqrt_zz_dag(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Phases the -1 eigenspace of the ZZ observable by -i.

Source code in src/tsim/core/instructions.py
548
549
550
551
552
553
554
def sqrt_zz_dag(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Phases the -1 eigenspace of the ZZ observable by -i."""
    h(b, qubit2)
    cnot(b, qubit1, qubit2)
    h(b, qubit2)
    s_dag(b, qubit1)
    s_dag(b, qubit2)

swap

swap(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Apply SWAP gate.

Source code in src/tsim/core/instructions.py
451
452
453
454
455
456
457
458
459
460
461
462
def swap(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Apply SWAP gate."""
    ensure_lane(b, qubit1)
    ensure_lane(b, qubit2)

    v1 = b.last_vertex[qubit1]
    v2 = b.last_vertex[qubit2]
    b.last_vertex[qubit1] = v2
    b.last_vertex[qubit2] = v1

    b.graph.set_qubit(v1, qubit2)
    b.graph.set_qubit(v2, qubit1)

swapcx

swapcx(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Apply SWAP then CX.

Source code in src/tsim/core/instructions.py
477
478
479
480
def swapcx(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Apply SWAP then CX."""
    swap(b, qubit1, qubit2)
    cnot(b, qubit1, qubit2)

swapcz

swapcz(
    b: GraphRepresentation, qubit1: int, qubit2: int
) -> None

Apply SWAP then CZ.

Source code in src/tsim/core/instructions.py
483
484
485
486
def swapcz(b: GraphRepresentation, qubit1: int, qubit2: int) -> None:
    """Apply SWAP then CZ."""
    swap(b, qubit1, qubit2)
    cz(b, qubit1, qubit2)

t

t(b: GraphRepresentation, qubit: int) -> None

Apply T gate (π/4 Z rotation).

Source code in src/tsim/core/instructions.py
133
134
135
def t(b: GraphRepresentation, qubit: int) -> None:
    """Apply T gate (π/4 Z rotation)."""
    z_phase(b, qubit, Fraction(1, 4))

t_dag

t_dag(b: GraphRepresentation, qubit: int) -> None

Apply T† gate (-π/4 Z rotation).

Source code in src/tsim/core/instructions.py
138
139
140
def t_dag(b: GraphRepresentation, qubit: int) -> None:
    """Apply T† gate (-π/4 Z rotation)."""
    z_phase(b, qubit, Fraction(-1, 4))

tick

tick(b: GraphRepresentation) -> None

Add a tick to the circuit (align all qubits to same row).

Source code in src/tsim/core/instructions.py
1169
1170
1171
1172
1173
1174
1175
def tick(b: GraphRepresentation) -> None:
    """Add a tick to the circuit (align all qubits to same row)."""
    if len(b.last_vertex) == 0:
        return
    row = max(last_row(b, q) for q in b.last_vertex)
    for q in b.last_vertex:
        b.graph.set_row(b.last_vertex[q], row)

tpp

tpp(
    b: GraphRepresentation,
    paulis: list[tuple[Literal["X", "Y", "Z"], int]],
    dagger: bool = False,
) -> None

Apply exp(-i pi/8 P) (up to global phase) for a Pauli product P.

Phases the -1 eigenspace of P by exp(i pi/4) (or exp(-i pi/4) if dagger). For a single qubit, TPP Z0 is the T gate and TPP_DAG Z0 is T_DAG.

Parameters:

Name Type Description Default
b GraphRepresentation

The graph representation to modify.

required
paulis list[tuple[Literal['X', 'Y', 'Z'], int]]

List of (pauli_type, qubit) pairs defining the Pauli product P.

required
dagger bool

If True, apply exp(+i pi/8 P) (phase by exp(-i pi/4)) instead.

False
Source code in src/tsim/core/instructions.py
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
def tpp(
    b: GraphRepresentation,
    paulis: list[tuple[Literal["X", "Y", "Z"], int]],
    dagger: bool = False,
) -> None:
    """Apply exp(-i pi/8 P) (up to global phase) for a Pauli product P.

    Phases the -1 eigenspace of P by exp(i pi/4) (or exp(-i pi/4) if dagger).
    For a single qubit, ``TPP Z0`` is the T gate and ``TPP_DAG Z0`` is T_DAG.

    Args:
        b: The graph representation to modify.
        paulis: List of (pauli_type, qubit) pairs defining the Pauli product P.
        dagger: If True, apply exp(+i pi/8 P) (phase by exp(-i pi/4)) instead.

    """
    _pauli_product_phase(b, paulis, t, t_dag, dagger)

u3

u3(
    b: GraphRepresentation,
    qubit: int,
    theta: Fraction,
    phi: Fraction,
    lambda_: Fraction,
) -> None

Apply U3 gate: U3(θ,φ,λ) = R_Z(φ)·R_Y(θ)·R_Z(λ).

Source code in src/tsim/core/instructions.py
162
163
164
165
166
167
168
169
170
171
172
173
def u3(
    b: GraphRepresentation,
    qubit: int,
    theta: Fraction,
    phi: Fraction,
    lambda_: Fraction,
) -> None:
    """Apply U3 gate: U3(θ,φ,λ) = R_Z(φ)·R_Y(θ)·R_Z(λ)."""
    r_z(b, qubit, lambda_)
    r_y(b, qubit, theta)
    r_z(b, qubit, phi)
    b.graph.scalar.add_phase((phi + lambda_) / 2)

x

x(b: GraphRepresentation, qubit: int) -> None

Apply Pauli X gate.

Source code in src/tsim/core/instructions.py
194
195
196
def x(b: GraphRepresentation, qubit: int) -> None:
    """Apply Pauli X gate."""
    x_phase(b, qubit, Fraction(1, 1))

x_error

x_error(
    b: GraphRepresentation, qubit: int, p: float
) -> None

Apply X error with probability p.

Source code in src/tsim/core/instructions.py
703
704
705
706
707
def x_error(b: GraphRepresentation, qubit: int, p: float) -> None:
    """Apply X error with probability p."""
    b.channel_probs.append(error_probs(p))
    _error(b, qubit, b.vertex_type.X, f"e{b.num_error_bits}")
    b.num_error_bits += 1

x_phase

x_phase(
    b: GraphRepresentation, qubit: int, phase: Fraction
) -> None

Apply X-axis rotation to qubit. This is equivalent to r_x up to a phase.

Source code in src/tsim/core/instructions.py
113
114
115
116
117
118
119
120
def x_phase(b: GraphRepresentation, qubit: int, phase: Fraction) -> None:
    """Apply X-axis rotation to qubit. This is equivalent to `r_x` up to a phase."""
    ensure_lane(b, qubit)
    v1 = b.last_vertex[qubit]
    b.graph.set_type(v1, b.vertex_type.X)
    b.graph.set_phase(v1, phase)
    v2 = add_dummy(b, qubit)
    b.graph.add_edge((v1, v2), b.edge_type.SIMPLE)

xcx

xcx(
    b: GraphRepresentation, control: int, target: int
) -> None

X-controlled X gate. Applies X to target if control is in |-> state.

Source code in src/tsim/core/instructions.py
557
558
559
560
561
def xcx(b: GraphRepresentation, control: int, target: int) -> None:
    """X-controlled X gate. Applies X to target if control is in |-> state."""
    h(b, control)
    cnot(b, control, target)
    h(b, control)

xcy

xcy(
    b: GraphRepresentation, control: int, target: int
) -> None

X-controlled Y gate. Applies Y to target if control is in |-> state.

Source code in src/tsim/core/instructions.py
564
565
566
567
568
def xcy(b: GraphRepresentation, control: int, target: int) -> None:
    """X-controlled Y gate. Applies Y to target if control is in |-> state."""
    h(b, control)
    cy(b, control, target)
    h(b, control)

xcz

xcz(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None

X-controlled Z gate. Applies Z to target if control is in |-> state.

Source code in src/tsim/core/instructions.py
571
572
573
574
575
576
577
578
579
580
581
582
583
def xcz(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None:
    """X-controlled Z gate. Applies Z to target if control is in |-> state."""
    cnot(
        b,
        target,
        control,
        classically_controlled[::-1] if classically_controlled else None,
    )

y

y(b: GraphRepresentation, qubit: int) -> None

Apply Pauli Y gate.

Source code in src/tsim/core/instructions.py
199
200
201
202
203
def y(b: GraphRepresentation, qubit: int) -> None:
    """Apply Pauli Y gate."""
    z(b, qubit)
    x(b, qubit)
    b.graph.scalar.add_phase(Fraction(1, 2))

y_error

y_error(
    b: GraphRepresentation, qubit: int, p: float
) -> None

Apply Y error with probability p.

Source code in src/tsim/core/instructions.py
710
711
712
713
714
715
716
def y_error(b: GraphRepresentation, qubit: int, p: float) -> None:
    """Apply Y error with probability p."""
    b.channel_probs.append(error_probs(p))
    # Y = X·Z, so both vertices use the same error bit
    _error(b, qubit, b.vertex_type.Z, f"e{b.num_error_bits}")
    _error(b, qubit, b.vertex_type.X, f"e{b.num_error_bits}")
    b.num_error_bits += 1

ycx

ycx(
    b: GraphRepresentation, control: int, target: int
) -> None

Y-controlled X gate. Applies X to target if control is in |-i> state.

Source code in src/tsim/core/instructions.py
586
587
588
589
590
def ycx(b: GraphRepresentation, control: int, target: int) -> None:
    """Y-controlled X gate. Applies X to target if control is in |-i> state."""
    h_yz(b, control)
    cnot(b, control, target)
    h_yz(b, control)

ycy

ycy(
    b: GraphRepresentation, control: int, target: int
) -> None

Y-controlled Y gate. Applies Y to target if control is in |-i> state.

Source code in src/tsim/core/instructions.py
593
594
595
596
597
def ycy(b: GraphRepresentation, control: int, target: int) -> None:
    """Y-controlled Y gate. Applies Y to target if control is in |-i> state."""
    h_yz(b, control)
    cy(b, control, target)
    h_yz(b, control)

ycz

ycz(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None

Y-controlled Z gate. Applies Z to target if control is in |-i> state.

Source code in src/tsim/core/instructions.py
600
601
602
603
604
605
606
607
608
609
610
611
612
def ycz(
    b: GraphRepresentation,
    control: int,
    target: int,
    classically_controlled: list[bool] | None = None,
) -> None:
    """Y-controlled Z gate. Applies Z to target if control is in |-i> state."""
    cy(
        b,
        target,
        control,
        classically_controlled[::-1] if classically_controlled else None,
    )

z

z(b: GraphRepresentation, qubit: int) -> None

Apply Pauli Z gate.

Source code in src/tsim/core/instructions.py
206
207
208
def z(b: GraphRepresentation, qubit: int) -> None:
    """Apply Pauli Z gate."""
    z_phase(b, qubit, Fraction(1, 1))

z_error

z_error(
    b: GraphRepresentation, qubit: int, p: float
) -> None

Apply Z error with probability p.

Source code in src/tsim/core/instructions.py
719
720
721
722
723
def z_error(b: GraphRepresentation, qubit: int, p: float) -> None:
    """Apply Z error with probability p."""
    b.channel_probs.append(error_probs(p))
    _error(b, qubit, b.vertex_type.Z, f"e{b.num_error_bits}")
    b.num_error_bits += 1

z_phase

z_phase(
    b: GraphRepresentation, qubit: int, phase: Fraction
) -> None

Apply Z-axis phase rotation to qubit. This is equivalent to r_z up to a phase.

Source code in src/tsim/core/instructions.py
123
124
125
126
127
128
129
130
def z_phase(b: GraphRepresentation, qubit: int, phase: Fraction) -> None:
    """Apply Z-axis phase rotation to qubit. This is equivalent to `r_z` up to a phase."""
    ensure_lane(b, qubit)
    v1 = b.last_vertex[qubit]
    b.graph.set_type(v1, b.vertex_type.Z)
    b.graph.set_phase(v1, phase)
    v2 = add_dummy(b, qubit)
    b.graph.add_edge((v1, v2), b.edge_type.SIMPLE)