Python Library
The Laylight Python library, a front end of the Laylight compiler that builds Laylight IR from Python devices.
The Laylight Python library describes layouts with Python classes and builds the Laylight IR from them. The Laylight compiler's back end, the laylight command line, turns that IR into GDSII, so a layout made in Python and one written in the Laylight DSL go through the same writer and carry the same information: cells with their parameters, ports as markers, and connections between ports.
The library was the first version of Laylight and was built on KLayout's database. It keeps that style: devices are dataclasses with an impl() method that draws a cell and input() / output() methods that return ports, and Placer chains devices port to port. KLayout is no longer needed to build or write a layout; it remains an optional dependency for PNG previews and KLayout PCells.
How a layout is built
- A
Layoutholds cells and a layer table. Devices draw into it:cell.shapes(layer).insert(shape)adds boxes, polygons and paths, andcell.insert(...)or aPlacerplaces other cells. - Coordinates are micrometres. A cell rounds them to integer database units (1 nm by default) as they are inserted, half away from zero, as KLayout does.
layout.to_ir()returns the IR text.layout.write("out.lyir")writes it;layout.write("out.gds")compiles it withlaylight build.
Devices of one class with the same parameters share one cell, named after the class (MMI) when every parameter has its default and with a hash of the parameters otherwise (MMI$1a2b3c4d), as the DSL names cells. Each device cell records its class as the source, its parameters, and its ports as the markers input and output. Placing a device on another's port records a connection between the two ports, the same record a port-to-port placement makes in the DSL, so laylight netlist and laylight lvs check a layout written with layout.write("chip.lyir").
Installation
pip install laylight # the library and the laylight command line
pip install "laylight[klayout]" # also PNG previews and KLayout PCellslaylight depends on the laylight-cli package, which installs the laylight command. The two are released together with one version number, and each laylight requires the laylight-cli of its own version (laylight==0.3.1 installs laylight-cli==0.3.1). The library finds the command through the LAYLIGHT_CLI environment variable, then where laylight-cli was installed, then on PATH. The command sends anonymous usage statistics, marked as coming from the library; set LAYLIGHT_TELEMETRY=0 to turn them off.
Minimal layout flow
import laylight
from laylight.render import new_layout
from laylight.structure.mmi import MMI
from laylight.structure.wg import WGStraight
layout, top, tech = new_layout("TOP")
mmi = MMI(layout=layout, tech=tech)
arm = WGStraight(length=50, width=0.5, layout=layout, tech=tech)
laylight.Placer(mmi).then(arm, at=laylight.Placer.OutPort(1)).into(top)
layout.write("demo.gds") # GDSII, compiled by `laylight build`
print(layout.to_ir()) # the Laylight IRnew_layout returns a layout with a 1 nm database unit, its top cell and the default Technology, whose LAYER_* attributes name the process layers (LAYER_WG is wg, GDS 0/0). The layout places the MMI and a straight on its second output, and the IR records that the straight's input is connected to that output:
cell TOP source=TOP {
inst MMI_0 MMI at (0,0)
inst WGStraight_0 WGStraight$b63e153b at (42000,-1000)
connect WGStraight_0.input[0] MMI_0.output[1]
}Reading layouts
laylight.read("chip.gds") reads GDSII (through laylight ir) or IR text into a Layout, with the cells' parameters and markers when the file was written by Laylight. Wrap a cell in an AdhocDevice to place it with a Placer.
Workspace layout
The Python packages live in pypkg/ and form a uv workspace rooted at the repository root. The library lives in pypkg/laylight, the runnable examples in pypkg/laylight-examples, and the documentation site in packages/laylight-docs.
From the repository root:
uv run --package laylight-examples laylight-examples --output-dir build/examples
uv run pytest -q pypkg/laylight/testsThe tests build the laylight command line from the repository (cargo build --release -p laylight-cli) unless LAYLIGHT_CLI names one.
To understand the abstractions behind this flow, continue with Device Abstraction and Placer Chain API. For runnable examples, see Examples.