Static Qubit Example#
Physics: Compile a simple static two-level system (qubit) Hamiltonian.
The Model
In a typical quantum optics or AMO setup, you start with a static qubit:
where:
\(\omega_0\) is the qubit transition frequency (in units where \(\hbar = 1\))
\(\sigma_z = |0\rangle\langle 0| - |1\rangle\langle 1|\) is the Pauli Z operator
The factor \(1/2\) places the ground state at \(-\omega_0/2\) and excited state at \(+\omega_0/2\)
This is the simplest possible Hamiltonian — static (no time dependence), single qubit, no dissipation.
What you’ll learn
How to express a static Hamiltonian in LaTeX.
How to call
compile_modeland inspect the returned object.The minimal inputs needed: a LaTeX string, parameters, and Hilbert space configuration.
Code
1# flake8: noqa
2"""
3Static qubit Hamiltonian compiled from LaTeX.
4
5What this shows:
6- Minimal inputs to get a working Hamiltonian (`qubits=1`, no collapse ops).
7- Default backend dispatch (QuTiP) via `compile_latex_model`.
8- Inspecting the compiled `H` object that you can pass to solvers.
9
10Tip: change `t_name` or add `backend="numpy"` to explore other backends.
11"""
12
13from __future__ import annotations
14
15import sys
16from pathlib import Path
17
18ROOT = Path(__file__).resolve().parents[1]
19if str(ROOT) not in sys.path:
20 sys.path.insert(0, str(ROOT))
21
22from latex_parser.latex_api import compile_latex_model
23
24
25def main() -> None:
26 H_latex = r"\frac{\omega_0}{2} \sigma_{z,1}"
27 params = {"omega_0": 2.0}
28 model = compile_latex_model(H_latex=H_latex, params=params, qubits=1, t_name="t")
29 print("Static qubit Hamiltonian:\n", model.H)
30
31
32if __name__ == "__main__":
33 main()
Run it
From the project root:
python examples/example_static_qubit.py
What happens
The LaTeX string
r"\frac{\omega_0}{2} \sigma_{z,1}"is canonicalizedIt’s parsed into an IR with one term: \(\frac{\omega_0}{2}\) (scalar) × \(\sigma_z\) (operator)
The parameter
omega_0 = 2.0is substitutedQuTiP compiles it to a
Qobj(quantum object):Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True Qobj data = [[ 1. 0.] [ 0. -1.]]
Try this
Change
omega_0to other values (e.g.,1.0,10.0) and re-run to see eigenvalues scale.Switch backend to
"numpy"to get a dense ndarray instead of aQobj.Add a second qubit parameter (e.g.,
qubits=2) and modify the LaTeX tor"\omega_1 \sigma_{z,1} + \omega_2 \sigma_{z,2}"to compile a two-qubit system.
Next steps
See Time-Dependent Drive Example to add time-dependent drives.
Check Collapse Operators (Open Systems) for dissipation (open-system dynamics).