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
80
81
82
83
84
85
86
87
88
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
91
92
93
94
95
96
97
98
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_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
209
210
211
212
213
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_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
216
217
218
219
220
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
351
352
353
354
355
356
357
358
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
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):
        if type_ == "X" or type_ == "Y":
            _error(b, qubit, VertexType.X, f"c{b.num_correlated_error_bits}")
        if type_ == "Z" or type_ == "Y":
            _error(b, qubit, VertexType.Z, f"c{b.num_correlated_error_bits}")

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

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
361
362
363
364
365
366
367
368
369
370
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
373
374
375
376
377
378
379
380
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)

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
582
583
584
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
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,
    )

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
864
865
866
867
868
869
870
871
872
873
874
875
def detector(b: GraphRepresentation, rec: list[int], *args) -> None:
    """Add detector annotation that XORs the given measurement record bits."""
    row = min(set([b.graph.row(b.rec[r]) for r in rec])) - 0.5
    d_rows = set([b.graph.row(d) for d in b.detectors + b.observables])
    while row in d_rows:
        row += 1
    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
101
102
103
104
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
633
634
635
636
637
638
639
640
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
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
223
224
225
226
227
228
229
230
231
232
233
234
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_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
237
238
239
240
241
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
244
245
246
247
248
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))

i

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

Apply identity (advances the row).

Source code in src/tsim/core/instructions.py
180
181
182
183
184
def i(b: GraphRepresentation, qubit: int) -> 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)

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
397
398
399
400
401
402
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
405
406
407
408
409
410
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
73
74
75
76
77
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
68
69
70
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
732
733
734
735
736
737
738
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)

mpp

mpp(
    b: GraphRepresentation,
    paulis: list[tuple[Literal["X", "Y", "Z"], int]],
    invert: bool = False,
) -> 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
Source code in src/tsim/core/instructions.py
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
def mpp(
    b: GraphRepresentation,
    paulis: list[tuple[Literal["X", "Y", "Z"], int]],
    invert: bool = False,
) -> 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.

    """
    aux = -2
    r(b, aux)
    h(b, aux)

    for pauli_type, qubit in paulis:
        if pauli_type == "X":
            cnot(b, aux, qubit)
        elif pauli_type == "Z":
            cz(b, aux, qubit)
        elif pauli_type == "Y":
            cy(b, aux, qubit)
        else:
            raise ValueError(f"Invalid Pauli operator: {pauli_type}")

    h(b, aux)
    m(b, aux, 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
772
773
774
775
776
777
778
779
780
781
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>.
    """
    if p > 0:
        x_error(b, qubit, p)
    m(b, qubit, p=p, invert=invert)
    _r(b, qubit, perform_trace=False)

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
784
785
786
787
788
789
790
791
792
793
794
795
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)
    if p > 0:
        x_error(b, qubit, p)
    m(b, qubit, p=p, invert=invert)
    _r(b, qubit, perform_trace=False)
    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
798
799
800
801
802
803
804
805
806
807
808
809
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)
    if p > 0:
        x_error(b, qubit, p)
    m(b, qubit, p=p, invert=invert)
    _r(b, qubit, perform_trace=False)
    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
812
813
814
815
816
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)

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
819
820
821
822
823
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)

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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
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 = min(set([b.graph.row(b.rec[r]) for r in rec])) - 0.5
        d_rows = set([b.graph.row(d) for d in b.detectors + b.observables])
        while row in d_rows:
            row += 1
        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
539
540
541
542
543
544
545
546
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 = 0,
    piy: float = 0,
    piz: float = 0,
    pxi: float = 0,
    pxx: float = 0,
    pxy: float = 0,
    pxz: float = 0,
    pyi: float = 0,
    pyx: float = 0,
    pyy: float = 0,
    pyz: float = 0,
    pzi: float = 0,
    pzx: float = 0,
    pzy: float = 0,
    pzz: float = 0,
) -> None

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

Source code in src/tsim/core/instructions.py
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
def pauli_channel_2(
    b: GraphRepresentation,
    qubit_i: int,
    qubit_j: int,
    pix: float = 0,
    piy: float = 0,
    piz: float = 0,
    pxi: float = 0,
    pxx: float = 0,
    pxy: float = 0,
    pxz: float = 0,
    pyi: float = 0,
    pyx: float = 0,
    pyy: float = 0,
    pyz: float = 0,
    pzi: float = 0,
    pzx: float = 0,
    pzy: float = 0,
    pzz: float = 0,
) -> 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
826
827
828
829
830
831
832
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, perform_trace=True)

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
148
149
150
151
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
154
155
156
157
158
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
142
143
144
145
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
835
836
837
838
839
840
841
842
843
844
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
847
848
849
850
851
852
853
854
855
856
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
251
252
253
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
290
291
292
def s_dag(b: GraphRepresentation, qubit: int) -> None:
    """Apply S† gate (-π/2 Z rotation)."""
    z_phase(b, qubit, Fraction(-1, 2))

sqrt_x

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

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

Source code in src/tsim/core/instructions.py
256
257
258
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
261
262
263
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
413
414
415
416
417
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
420
421
422
423
424
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
266
267
268
269
270
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
273
274
275
276
277
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
427
428
429
430
431
432
433
434
435
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
438
439
440
441
442
443
444
445
446
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
280
281
282
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
285
286
287
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
449
450
451
452
453
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
456
457
458
459
460
461
462
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
383
384
385
386
387
388
389
390
391
392
393
394
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)

t

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

Apply T gate (π/4 Z rotation).

Source code in src/tsim/core/instructions.py
132
133
134
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
137
138
139
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
895
896
897
898
899
900
901
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)

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
161
162
163
164
165
166
167
168
169
170
171
172
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
187
188
189
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
610
611
612
613
614
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
112
113
114
115
116
117
118
119
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
465
466
467
468
469
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
472
473
474
475
476
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
479
480
481
482
483
484
485
486
487
488
489
490
491
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
192
193
194
195
196
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
617
618
619
620
621
622
623
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
494
495
496
497
498
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
501
502
503
504
505
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
508
509
510
511
512
513
514
515
516
517
518
519
520
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
199
200
201
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
626
627
628
629
630
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
122
123
124
125
126
127
128
129
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)