Custom Subsystem Example

Custom Subsystem Example#

This example shows how to create and use a custom finite-dimensional subsystem (for example, a spin greater than 1/2) by supplying a CustomSpec with named operators.

What you’ll learn#

  • How to provide a CustomSpec to make_config or compile_model.

  • How custom operator names are referenced from LaTeX (e.g., Jx_{1}).

Source#

 1# flake8: noqa
 2"""
 3Custom finite-dimensional subsystem example (spin-1 with Jx operator).
 4
 5What this shows:
 6- How to wrap custom operator matrices (here: spin-1 ladder/Jx) into `CustomSpec`.
 7- Configuring a `HilbertConfig` with only a custom subsystem (no qubits/bosons).
 8- Compiling LaTeX that references custom operators (`Jx_{1}`) and printing `H`.
 9"""
10
11from __future__ import annotations
12
13import sys
14from pathlib import Path
15
16import numpy as np
17from qutip import Qobj  # type: ignore
18
19ROOT = Path(__file__).resolve().parents[1]
20if str(ROOT) not in sys.path:
21    sys.path.insert(0, str(ROOT))
22
23from latex_parser.latex_api import compile_model as compile_latex_model
24from latex_parser.dsl import CustomSpec, HilbertConfig
25
26
27def main() -> None:
28    sqrt2 = np.sqrt(2.0)
29    Jp = Qobj(np.array([[0, sqrt2, 0], [0, 0, sqrt2], [0, 0, 0]], dtype=complex))
30    Jm = Jp.dag()
31    Jx = 0.5 * (Jp + Jm)
32
33    spec = CustomSpec(
34        label="c",
35        index=1,
36        dim=3,
37        operators={"Jx": Jx, "Jp": Jp, "Jm": Jm},
38    )
39    cfg = HilbertConfig(qubits=[], bosons=[], customs=[spec])
40    H_latex = r"\omega_J Jx_{1}"
41    params = {"omega_J": 0.3}
42    model = compile_latex_model(H_latex=H_latex, params=params, config=cfg)
43    print("Custom subsystem Hamiltonian:\n", model.H)
44
45
46if __name__ == "__main__":
47    main()

Run#

python examples/example_custom_subsystem.py

Notes#

  • The DSL only checks that operator names exist in the provided template; values are opaque backend objects.

  • Use this pattern when modeling collective spins or other non-qubit subsystems.