NumPy Backend Example#
This example demonstrates compiling LaTeX-defined models to dense NumPy matrices by reusing the QuTiP backend and converting Qobj objects to arrays. It is useful for small systems and quick prototyping.
What you’ll learn#
How to request the NumPy backend and inspect the resulting
ndarray.How time-dependent terms are represented for NumPy backends.
Source#
1# flake8: noqa
2"""
3Using the NumPy backend to build a dense matrix instead of QuTiP objects.
4
5What this shows:
6- Selecting `backend="numpy"` to bypass QuTiP.
7- Providing a `config` explicitly via `make_config`.
8- Result is a plain NumPy array; useful for custom workflows or JIT-wrapping.
9"""
10
11from __future__ import annotations
12
13import sys
14from pathlib import Path
15
16ROOT = Path(__file__).resolve().parents[1]
17if str(ROOT) not in sys.path:
18 sys.path.insert(0, str(ROOT))
19
20from latex_parser.latex_api import compile_latex_model, make_config
21
22
23def main() -> None:
24 cfg = make_config(qubits=[1], bosons=[])
25 H_latex = r"\delta \sigma_{x,1}"
26 params = {"delta": 0.4}
27 model = compile_latex_model(
28 H_latex=H_latex,
29 params=params,
30 backend="numpy",
31 config=cfg,
32 )
33 print("NumPy backend matrix:\n", model)
34
35
36if __name__ == "__main__":
37 main()
Run#
python examples/example_numpy_backend.py
Notes#
The example converts QuTiP objects to NumPy arrays; it is intended for clarity and education rather than performance.