# Colab Setup (Run this first)
!pip install litebird_sim rich
Requirement already satisfied: litebird_sim in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (0.17.0)
Requirement already satisfied: rich in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (15.0.0)
Requirement already satisfied: asciimatics>=1.15.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (1.15.0)
Requirement already satisfied: astropy>=6.1.7 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (8.0.1)
Requirement already satisfied: deprecated>=1.2.18 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (1.3.1)
Requirement already satisfied: ducc0>=0.41.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (0.41.0)
Requirement already satisfied: h5py>=3.9 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (3.16.0)
Requirement already satisfied: healpy>=1.18.1 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (1.20.0)
Requirement already satisfied: jinja2>=3.1 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (3.1.6)
Requirement already satisfied: jplephem>=2.22 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (2.24)
Requirement already satisfied: libinsdb>=0.9.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (0.9.0)
Requirement already satisfied: markdown-katex>=202406.1035 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (202406.1035)
Requirement already satisfied: markdown>=3.9 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (3.10.3)
Requirement already satisfied: matplotlib>=3.10.8 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (3.11.1)
Requirement already satisfied: numba>=0.65.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (0.67.0)
Requirement already satisfied: numpy>=2.4.1 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (2.5.2)
Requirement already satisfied: pygithub>=2.6.1 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (2.10.0)
Requirement already satisfied: pyperclip>=1.9.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (1.11.0)
Requirement already satisfied: pysm3>=3.4.5 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (3.4.6)
Requirement already satisfied: pyyaml>=6.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (6.0.3)
Requirement already satisfied: requests>=2.32.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (2.34.2)
Requirement already satisfied: scipy>=1.15.3 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (1.18.1)
Requirement already satisfied: tomlkit>=0.12.1 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from litebird_sim) (0.12.5)
Requirement already satisfied: markdown-it-py>=2.2.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from rich) (4.2.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from rich) (2.21.0)
Requirement already satisfied: pyfiglet>=0.7.2 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from asciimatics>=1.15.0->litebird_sim) (1.0.4)
Requirement already satisfied: Pillow>=2.7.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from asciimatics>=1.15.0->litebird_sim) (12.3.0)
Requirement already satisfied: wcwidth in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from asciimatics>=1.15.0->litebird_sim) (0.8.3)
Requirement already satisfied: astropy-iers-data>=0.2026.6.22.1.23.34 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from astropy>=6.1.7->litebird_sim) (0.2026.8.31.0.57.9)
Requirement already satisfied: packaging>=25.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from astropy>=6.1.7->litebird_sim) (26.3)
Requirement already satisfied: pyerfa>=2.0.1.3 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from astropy>=6.1.7->litebird_sim) (2.0.1.5)
Requirement already satisfied: wrapt<3,>=1.10 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from deprecated>=1.2.18->litebird_sim) (2.4.0)
Requirement already satisfied: MarkupSafe>=2.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from jinja2>=3.1->litebird_sim) (3.0.3)
Requirement already satisfied: coverage<8,>=7.3.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from libinsdb>=0.9.0->litebird_sim) (7.16.0)
Requirement already satisfied: python-dateutil<3,>=2.8.2 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from libinsdb>=0.9.0->litebird_sim) (2.9.0.post0)
Requirement already satisfied: requests-mock<2,>=1.11.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from libinsdb>=0.9.0->litebird_sim) (1.12.1)
Requirement already satisfied: types-pyyaml<7,>=6.0.12.12 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from libinsdb>=0.9.0->litebird_sim) (6.0.12.20260815)
Requirement already satisfied: six>=1.5 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from python-dateutil<3,>=2.8.2->libinsdb>=0.9.0->litebird_sim) (1.17.0)
Requirement already satisfied: charset_normalizer<4,>=2 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from requests>=2.32.0->litebird_sim) (3.5.1)
Requirement already satisfied: idna<4,>=2.5 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from requests>=2.32.0->litebird_sim) (3.19)
Requirement already satisfied: urllib3<3,>=1.26 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from requests>=2.32.0->litebird_sim) (2.7.0)
Requirement already satisfied: certifi>=2023.5.7 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from requests>=2.32.0->litebird_sim) (2026.7.22)
Requirement already satisfied: mdurl~=0.1 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from markdown-it-py>=2.2.0->rich) (0.1.2)
Requirement already satisfied: setuptools in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from markdown-katex>=202406.1035->litebird_sim) (84.0.0)
Requirement already satisfied: contourpy>=1.0.1 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from matplotlib>=3.10.8->litebird_sim) (1.3.3)
Requirement already satisfied: cycler>=0.10 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from matplotlib>=3.10.8->litebird_sim) (0.12.1)
Requirement already satisfied: fonttools>=4.28.2 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from matplotlib>=3.10.8->litebird_sim) (4.64.0)
Requirement already satisfied: kiwisolver>=1.3.1 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from matplotlib>=3.10.8->litebird_sim) (1.5.1)
Requirement already satisfied: pyparsing>=3 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from matplotlib>=3.10.8->litebird_sim) (3.3.2)
Requirement already satisfied: llvmlite<0.50,>=0.49.0dev0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from numba>=0.65.0->litebird_sim) (0.49.0)
Requirement already satisfied: pynacl>=1.4.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from pygithub>=2.6.1->litebird_sim) (1.6.2)
Requirement already satisfied: pyjwt>=2.4.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from pyjwt[crypto]>=2.4.0->pygithub>=2.6.1->litebird_sim) (2.13.0)
Requirement already satisfied: typing-extensions>=4.5.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from pygithub>=2.6.1->litebird_sim) (4.16.0)
Requirement already satisfied: cryptography>=3.4.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from pyjwt[crypto]>=2.4.0->pygithub>=2.6.1->litebird_sim) (50.0.1)
Requirement already satisfied: cffi>=2.0.0 in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from cryptography>=3.4.0->pyjwt[crypto]>=2.4.0->pygithub>=2.6.1->litebird_sim) (2.1.1)
Requirement already satisfied: pycparser in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from cffi>=2.0.0->cryptography>=3.4.0->pyjwt[crypto]>=2.4.0->pygithub>=2.6.1->litebird_sim) (3.0)
Requirement already satisfied: toml in /Users/gomes/Documents/LiteBIRD/test_problem/venv/lib/python3.14/site-packages (from pysm3>=3.4.5->litebird_sim) (0.10.2)

[notice] A new release of pip is available: 26.1.2 -> 26.2.1
[notice] To update, run: pip install --upgrade pip

Simulation with an explicit HWP harmonics expansion#

To run this notebook, you have several options:

  • If you are running this under Binder, you should already be set!

  • If you are running this under Google Colab, be sure to run the cell with !pip install… that is right above the title.

  • If you are running this locally, you should first create and activate a new virtual environment with the commands

    python -m venv ./my_venv
    source ./my_venv/bin/activate
    

    (you can use Conda environments, if you prefer) and install Jupyter and litebird_sim in it:

    pip install jupyter litebird_sim
    

    If you have a local copy of the litebird_sim repository cloned from litebird/litebird_sim (e.g., because you’re part of the Simulation Team!), you can use a development install instead:

    cd /my/local/copy/litebird_sim
    pip install -e .
    

Import Modules#

import healpy as hp
import matplotlib.pyplot as plt
import numpy as np

import litebird_sim as lbs

Define Parameters#

start_time = 0
time_span_s = 100 * 24 * 3600
nside = 64
imo_location = "schema.json"
imo = lbs.Imo(flatfile_location=lbs.PTEP_IMO_LOCATION)
imo_version = "vPTEP"

Create Simulation#

sim = lbs.Simulation(
    start_time=start_time, duration_s=time_span_s, random_seed=0, imo=imo
)

comm = sim.mpi_comm
[2026-09-03 17:22:17,231 WARNING MPI#0000] MPI layer is already initialized. Reinitializing the entire RNG hierarchy.
[2026-09-03 17:22:17,231 WARNING MPI#0000] MPI layer is already initialized. Reinitializing the entire RNG hierarchy.

Define Frequency Channel#

telescope = "MFT"
channel = "M2-119"

channelinfo = lbs.FreqChannelInfo.from_imo(
    url=f"/releases/{imo_version}/satellite/{telescope}/{channel}/channel_info",
    imo=imo,
)

Define Scanning Strategy#

sim.set_scanning_strategy(
    imo_url=f"/releases/{imo_version}/satellite/scanning_parameters/",
)

Define Instrument#

instrument_info = lbs.InstrumentInfo.from_imo(
    imo,
    url=f"/releases/{imo_version}/satellite/{telescope}/instrument_info",
)

sim.set_instrument(instrument_info)

hwp_radpsec = instrument_info.hwp_rpm * 2 * np.pi / 60

Set HWP#

# if Mueller formalism
#
# sim.set_hwp(
#    lbs.NonIdealHWP(hwp_radpsec, harmonic_expansion=True, calculus=lbs.HWPFormalism.MUELLER)
# )

# if Jones formalism
sim.set_hwp(
    lbs.NonIdealHWP(
        hwp_radpsec, harmonic_expansion=True, calculus=lbs.HWPFormalism.MUELLER
    )
)

Define Detectors#

det = lbs.DetectorInfo.from_imo(
    url=f"/releases/{imo_version}/satellite/{telescope}/{channel}/{channelinfo.detector_names[0]}/detector_info",
    imo=imo,
)

# This is usually not needed. PTEP IMo has no pointing_theta_phi_psi_deg value, so we're setting some values in here so that the code does not break.
det.pointing_theta_phi_psi_deg = [0, 0, 0]
det.pol_angle_rad = 0
# We're also reducing the sampling rate so that the simulation goes faster.
det.sampling_rate_hz = 1

# if Mueller
det.mueller_hwp = {
    "0f": np.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]], dtype=np.float64),
    "2f": np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]], dtype=np.float64),
    "4f": np.array(
        [
            [0, 0, 0],
            [3.5e-5, 1, 1],
            [3.5e-5, 1, 1],
        ],
        dtype=np.float64,
    ),
}


mueller_phases = {
    "2f": np.zeros((3, 3)),
    "4f": np.array(
        [
            [0, 0, 0],
            [0, 0, -np.pi / 2],
            [0, -np.pi / 2, np.pi],
        ],
        dtype=np.float64,
    ),
}


# if Jones
# det.jones_hwp = {
#    "0f": np.array([[1, 0], [0, -1]], dtype=np.complex128,
#    ),
#    "2f": np.array(
#        [
#            [
#                9.67e-01 * np.exp(1j * np.deg2rad(5.51e01)),
#                6.16e-02 * np.exp(1j * np.deg2rad(1.26e02)),
#            ],
#            [
#                6.18e-02 * np.exp(1j * np.deg2rad(1.25e02)),
#                9.85e-01 * np.exp(1j * np.deg2rad(-1.46e02)),
#            ],
#        ],
#        dtype=np.complex128,),
# }

Create Observations and Prepare Pointings#

(obs,) = sim.create_observations(
    detectors=[det],
)

sim.prepare_pointings(append_to_report=False)
sim.precompute_pointings()

Generate Input Sky Maps#

sky_params = lbs.SkyGenerationParams(
    make_cmb=True,
    make_dipole=True,
    make_fg=True,
    fg_models=["s1", "d1"],
    seed_cmb=1,  # set this seed if you want to fix the CMB realization
    apply_beam=True,  # if True, smooths the input map by the beam of the channel
    bandpass_integration=False,  # if True, integrates over the top-hat bandpass of the channel
    units="K_CMB",
    output_type="map",
    nside=nside,
)

sky = sim.get_sky(
    parameters=sky_params,
    channels=channelinfo,
)
[2026-09-03 17:22:19,073 INFO MPI#0000] Generating CMB...
[2026-09-03 17:22:19,097 INFO MPI#0000] Generating Foregrounds...
[2026-09-03 17:22:19,098 INFO MPI#0000] Retrieve data for pysm_2/synch_t_new.fits (if not cached already)
[2026-09-03 17:22:19,099 INFO MPI#0000] NSIDE = 512
[2026-09-03 17:22:19,099 INFO MPI#0000] ORDERING = RING in fits file
[2026-09-03 17:22:19,099 INFO MPI#0000] INDXSCHM = IMPLICIT
[2026-09-03 17:22:19,133 INFO MPI#0000] Retrieve data for pysm_2/synch_q_new.fits (if not cached already)
[2026-09-03 17:22:19,135 INFO MPI#0000] NSIDE = 512
[2026-09-03 17:22:19,135 INFO MPI#0000] ORDERING = RING in fits file
[2026-09-03 17:22:19,135 INFO MPI#0000] INDXSCHM = IMPLICIT
[2026-09-03 17:22:19,162 INFO MPI#0000] Retrieve data for pysm_2/synch_u_new.fits (if not cached already)
[2026-09-03 17:22:19,163 INFO MPI#0000] NSIDE = 512
[2026-09-03 17:22:19,163 INFO MPI#0000] ORDERING = RING in fits file
[2026-09-03 17:22:19,164 INFO MPI#0000] INDXSCHM = IMPLICIT
[2026-09-03 17:22:19,190 INFO MPI#0000] Retrieve data for pysm_2/synch_beta.fits (if not cached already)
[2026-09-03 17:22:19,191 INFO MPI#0000] NSIDE = 512
[2026-09-03 17:22:19,192 INFO MPI#0000] ORDERING = RING in fits file
[2026-09-03 17:22:19,192 INFO MPI#0000] INDXSCHM = IMPLICIT
[2026-09-03 17:22:19,218 INFO MPI#0000] Retrieve data for pysm_2/dust_t_new.fits (if not cached already)
[2026-09-03 17:22:19,219 INFO MPI#0000] NSIDE = 512
[2026-09-03 17:22:19,219 INFO MPI#0000] ORDERING = RING in fits file
[2026-09-03 17:22:19,219 INFO MPI#0000] INDXSCHM = IMPLICIT
[2026-09-03 17:22:19,245 INFO MPI#0000] Retrieve data for pysm_2/dust_q_new.fits (if not cached already)
[2026-09-03 17:22:19,246 INFO MPI#0000] NSIDE = 512
[2026-09-03 17:22:19,247 INFO MPI#0000] ORDERING = RING in fits file
[2026-09-03 17:22:19,247 INFO MPI#0000] INDXSCHM = IMPLICIT
[2026-09-03 17:22:19,273 INFO MPI#0000] Retrieve data for pysm_2/dust_u_new.fits (if not cached already)
[2026-09-03 17:22:19,274 INFO MPI#0000] NSIDE = 512
[2026-09-03 17:22:19,275 INFO MPI#0000] ORDERING = RING in fits file
[2026-09-03 17:22:19,275 INFO MPI#0000] INDXSCHM = IMPLICIT
[2026-09-03 17:22:19,300 INFO MPI#0000] Retrieve data for pysm_2/dust_beta.fits (if not cached already)
[2026-09-03 17:22:19,302 INFO MPI#0000] NSIDE = 512
[2026-09-03 17:22:19,302 INFO MPI#0000] ORDERING = RING in fits file
[2026-09-03 17:22:19,302 INFO MPI#0000] INDXSCHM = IMPLICIT
[2026-09-03 17:22:19,328 INFO MPI#0000] Retrieve data for pysm_2/dust_temp.fits (if not cached already)
[2026-09-03 17:22:19,329 INFO MPI#0000] NSIDE = 512
[2026-09-03 17:22:19,329 INFO MPI#0000] ORDERING = RING in fits file
[2026-09-03 17:22:19,329 INFO MPI#0000] INDXSCHM = IMPLICIT
[2026-09-03 17:22:20,776 INFO MPI#0000] Generating Dipole...
[2026-09-03 17:22:20,777 INFO MPI#0000] Summing components...

Fill TOD#

lbs.scan_map_in_observations(
    observations=[obs], maps=sky, mueller_phases=mueller_phases
)
output_maps = sim.make_binned_map(nside=nside).binned_map
input_maps = sky["M2-119"].values

Show Output Maps#

plt.figure(figsize=(32, 10))
stokes_parameters = ["T", "Q", "U"]
diff = output_maps - input_maps
for i in range(3):
    hp.mollview(
        input_maps[i] * 1e6,
        cmap="jet",
        sub=(3, 3, i + 1),
        title="Input " + stokes_parameters[i],
    )
    hp.mollview(
        output_maps[i] * 1e6,
        min=np.min(input_maps[i] * 1e6),
        max=np.max(input_maps[i] * 1e6),
        cmap="jet",
        sub=(3, 3, i + 4),
        title="Output " + stokes_parameters[i],
    )
    hp.mollview(
        diff[i] * 1e6,
        min=-0.05,
        max=0.05,
        cmap="jet",
        sub=(3, 3, i + 7),
        title="Output " + stokes_parameters[i],
    )
../_images/5d9b5e85a51ddbc80878639af7eda6c1dd82e90d66e5100b78fa47ecbcd5b57c.png