YAML Place and AutoRoute#

You have two options for working with gdsfactory:

  1. python flow: you define your layout using python functions (Parametric Cells), and connect them with routing functions.

  2. YAML Place and AutoRoute: you define your Component as Place and Route in YAML. From the netlist you can simulate the Component or generate the layout.

The YAML format contains the schematic together with placement information.

YAML is a human readable version of JSON that you can use to define placements and routes

to define a a YAML Component you need to define:

  • instances: with each instance setting

  • placements: with X and Y

And optionally:

  • routes: between instance ports

  • connections: to connect instance ports to other ports (without routes)

  • ports: define input and output ports for the top level Component.

gdsfactory VSCode extension has a filewatcher for *.pic.yml files that will show them live in klayout as you edit them.

extension

The extension provides you with useful code snippets and filewatcher extension to see live modifications of *pic.yml or *.py files. Look for the telescope button on the top right of VSCode 🔭. watcher-button

import gdsfactory as gf
from IPython.display import Code

filepath = "yaml_pics/pads.pic.yml"
Code(filepath, language="yaml+jinja")
instances:
    bl:
      component: pad
    tl:
      component: pad
    br:
      component: pad
    tr:
      component: pad

placements:
    tl:
        x: -200
        y: 500

    br:
        x: 400
        y: 400

    tr:
        x: 400
        y: 600


routes:
    electrical:
        settings:
            separation: 20
            width: 10
        links:
            tl,e3: tr,e1
            bl,e3: br,e1
    optical:
        settings:
            radius: 100
        links:
            bl,e4: br,e3
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:21.241 | WARNING  | gdsfactory.read.from_yaml:from_yaml:692 - UserWarning: prefix is deprecated and will be removed soon. _from_yaml
2024-04-19 00:23:21.680 | WARNING  | gdsfactory.component:plot_klayout:1645 - UserWarning: Unnamed cells, 1 in 'Unnamed_a6480b42'
2024-04-19 00:23:21.696 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/Unnamed_a6480b42.lyp'.
../_images/17d42da90cfcaff2a503b9e0eb1fd17e5d51a89f81462d3b6c206135b0429fec.png

Lets start by defining the instances and placements section in YAML

Lets place an mmi_long where you can place the o1 port at x=20, y=10

filepath = "yaml_pics/mmis.pic.yml"
Code(filepath, language="yaml+jinja")
instances:
    mmi_long:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 10
    mmi_short:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 5

placements:
    mmi_long:
        port: o1
        x: 20
        y: 10
        mirror: False
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:21.895 | WARNING  | gdsfactory.read.from_yaml:from_yaml:692 - UserWarning: prefix is deprecated and will be removed soon. _from_yaml
2024-04-19 00:23:21.908 | WARNING  | gdsfactory.component:plot_klayout:1645 - UserWarning: Unnamed cells, 1 in 'Unnamed_1d55b19a'
2024-04-19 00:23:21.924 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/Unnamed_1d55b19a.lyp'.
../_images/7aa971db28672ee71a88518f8b71a35efb1e1af0356a0ddb6e8e42e971b70f21.png

ports#

You can expose any ports of any instance to the new Component with a ports section in YAML

Lets expose all the ports from mmi_long into the new component.

Ports are exposed as new_port_name: instance_name, port_name

filepath = "yaml_pics/ports_demo.pic.yml"
Code(filepath, language="yaml+jinja")
name: ports_demo
instances:
    mmi_long:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 5
placements:
    mmi_long:
        port: o1
        x: 20
        y: 10
        mirror: True

ports:
    o3: mmi_long,o3
    o2: mmi_long,o2
    o1: mmi_long,o1
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:22.129 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/ports_demo_5176cf16.lyp'.
../_images/749c28bc0900f3538e2782f808591e3baf32650b24771a9fba09c0361c918884.png

You can also define a mirror placement using a port

Try mirroring with other ports o2, o3 or with a number as well as with a rotation 90, 180, 270

filepath = "yaml_pics/mirror_demo.pic.yml"
Code(filepath, language="yaml+jinja")
name: mirror_demo
instances:
    mmi_long:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 5
placements:
    mmi_long:
        x: 0
        y: 0
        mirror: o1
        rotation: 0
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:22.389 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/mirror_demo_58df17bc.lyp'.
../_images/2cc544005b9b979e9f66ff4168ae1b478d4b70feba8cca4a2f0a5a541b9b134d.png

connections#

You can connect any two instances by defining a connections section in the YAML file.

it follows the syntax instance_source,port : instance_destination,port

filepath = "yaml_pics/connections_demo.pic.yml"
Code(filepath, language="yaml+jinja")
name: connections_demo
instances:
    b:
      component: bend_circular
    mmi_long:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 10
    mmi_short:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 5
placements:
    mmi_short:
        port: o1
        x: 10
        y: 20
connections:
    b,o1 : mmi_short,o2
    mmi_long,o1: b, o2

ports:
    o1: mmi_short,o1
    o2: mmi_long,o2
    o3: mmi_long,o3
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:22.596 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/connections_demo_c34dd6ff.lyp'.
../_images/11d9167b24258566433de347e93d182681fd4aa3166a83d8ba69ae6d05f90abf.png

Relative port placing

You can also place a component with respect to another instance port

You can also define an x and y offset with dx and dy

filepath = "yaml_pics/relative_port_placing.pic.yml"
Code(filepath, language="yaml+jinja")
name: relative_port_placing
instances:
    mmi_long:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 10
    mmi_short:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 5

placements:
    mmi_short:
        port: o1
        x: 0
        y: 0
    mmi_long:
        port: o1
        x: mmi_short,o2
        y: mmi_short,o2
        dx : 10
        dy: -10
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:22.767 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/relative_port_placing_5719eef4.lyp'.
../_images/89b91ef1b5adbc8529a58ed44746a119e5cbeeed0c9ae224051c7a7575e541e6.png

routes#

You can define routes between two instances by defining a routes section in YAML

it follows the syntax

routes:
    route_name:
        links:
            instance_source,port: instance_destination,port
        settings:  # for the route (optional)
            waveguide: strip
            width: 1.2
filepath = "yaml_pics/routes.pic.yml"
Code(filepath, language="yaml+jinja")
name: with_routes
instances:
    mmi_long:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 10
    mmi_short:
      component: mmi1x2
      settings:
        width_mmi: 4.5
        length_mmi: 5
placements:
    mmi_long:
        x: 100
        y: 100
routes:
    optical:
        links:
            mmi_short,o2: mmi_long,o1
        settings:
            cross_section:
                cross_section: xs_sc
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:22.989 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/with_routes_a7421786.lyp'.
../_images/166fb960272f20e44562e03e6e9bbad2725440f82874971b95ebabf99e7ae603.png

instances, placements, connections, ports, routes#

Lets combine all you learned so far.

You can define the netlist connections of a component by a netlist in YAML format

Note that you define the connections as instance_source.port -> instance_destination.port so the order is important and therefore you can only change the position of the instance_destination

You can define several routes that will be connected using gf.routing.get_bundle

filepath = "yaml_pics/routes_mmi.pic.yml"
Code(filepath, language="yaml+jinja")
name: routes_mmi

instances:
    mmi_bottom:
      component: mmi2x2
    mmi_top:
      component: mmi2x2

placements:
    mmi_top:
        x: 100
        y: 100

routes:
    optical:
        links:
            mmi_bottom,o4: mmi_top,o1
            mmi_bottom,o3: mmi_top,o2
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:23.214 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/routes_mmi_87dcb502.lyp'.
../_images/83dfc48d485097e0f064698d2e9a7494adc2ca3ea04df576f0394aba8a9b66a6.png

You can also add custom component_factories to gf.read.from_yaml

@gf.cell
def pad_new(size=(100, 100), layer=(1, 0)):
    c = gf.Component()
    compass = c << gf.components.compass(size=size, layer=layer)
    c.ports = compass.ports
    return c


gf.get_active_pdk().register_cells(pad_new=pad_new)
c = pad_new()
c.plot()
2024-04-19 00:23:23.405 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/pad_new.lyp'.
../_images/8673178092402d25379414a994e22761c73c28aa80e9737e9d559cd2c3b36967.png
filepath = "yaml_pics/new_factories.pic.yml"
Code(filepath, language="yaml+jinja")
name: new_factories

instances:
    bot:
      component: pad_new
    top:
      component: pad_new

placements:
    top:
        x: 0
        y: 200
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:23.609 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/new_factories_64af87a3.lyp'.
../_images/46a33db3a1bfb9c847310f70403d980b9e20e039f89891dfe8ce07c5e2f26b83.png
filepath = "yaml_pics/routes_custom.pic.yml"
Code(filepath, language="yaml+jinja")
name: routes_custom

instances:
    t:
      component: pad_array
      settings:
          orientation: 270
          columns: 3
    b:
      component: pad_array
      settings:
          orientation: 90
          columns: 3

placements:
    t:
        x: 200
        y: 400
routes:
    electrical:
        settings:
            width: 10.
            end_straight_length: 150
        links:
            t,e11: b,e11
            t,e13: b,e13
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:23.839 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/routes_custom_8cefd757.lyp'.
../_images/690e2a52cfeecf8a188e7e4d83e55a5817976970fed13c99229af1dedef553a1.png

Also, you can define route bundles with different settings and specify the route factory as a parameter as well as the settings for that particular route alias.

filepath = "yaml_pics/pads_path_length_match.pic.yml"
Code(filepath, language="yaml+jinja")
name: pads_path_length_match

instances:
    bl:
      component: pad
    tl:
      component: pad
    br:
      component: pad
    tr:
      component: pad

placements:
    tl:
        x: -200
        y: 500

    br:
        x: 400
        y: 400

    tr:
        x: 400
        y: 600


routes:
    electrical:
        settings:
            separation: 20
            width: 10
            path_length_match_loops: 2
            end_straight_length: 100
        links:
            tl,e3: tr,e1
            bl,e3: br,e1
    optical:
        settings:
            radius: 100
        links:
            bl,e4: br,e3
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:24.098 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/pads_path_length_match_545044af.lyp'.
../_images/35c3ebab0b17eef117279512b7fa2162a0f79a533f300230eaabd4347e5032b1.png
filepath = "yaml_pics/routes_path_length_match.pic.yml"
Code(filepath, language="yaml+jinja")
instances:
    t:
      component: pad_array
      settings:
          orientation: 270
          columns: 3
    b:
      component: pad_array
      settings:
          orientation: 90
          columns: 3

placements:
    t:
        x: 100
        y: 1000
routes:
    route1:
        routing_strategy: get_bundle_path_length_match
        settings:
            extra_length: 500
            width: 2
            end_straight_length: 500
        links:
            t,e11: b,e11
            t,e12: b,e12
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:24.326 | WARNING  | gdsfactory.component:plot_klayout:1645 - UserWarning: Unnamed cells, 1 in 'Unnamed_295dbf2b'
2024-04-19 00:23:24.341 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/Unnamed_295dbf2b.lyp'.
../_images/a9197c2849645ca10def365b54ea0d15a5388a37968ca09d3f01958cf728e565.png
filepath = "yaml_pics/routes_waypoints.pic.yml"
Code(filepath, language="yaml+jinja")
instances:
    t:
      component: pad_array
      settings:
          orientation: 270
          columns: 3
    b:
      component: pad_array
      settings:
          orientation: 90
          columns: 3

placements:
    t:
        x: -250
        y: 1000
routes:
    route1:
        routing_strategy: get_bundle_from_waypoints
        settings:
            waypoints:
                - [0, 300]
                - [400, 300]
                - [400, 400]
                - [-250, 400]
            auto_widen: False
        links:
            b,e11: t,e11
            b,e12: t,e12
c = gf.read.from_yaml(filepath)
c.plot()
2024-04-19 00:23:24.645 | WARNING  | gdsfactory.component:plot_klayout:1645 - UserWarning: Unnamed cells, 1 in 'Unnamed_e96b0c79'
2024-04-19 00:23:24.659 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/Unnamed_e96b0c79.lyp'.
../_images/6c7cf13deb8f996abb518b3bf9801d8e5ac98a4d0f4333cfdea96e5e475739a1.png

Jinja Pcells#

You use jinja templates in YAML cells to define Pcells.

from IPython.display import Code

from gdsfactory.read import cell_from_yaml_template

gf.clear_cache()

jinja_yaml = """
default_settings:
    length_mmi:
      value: 10
      description: "The length of the long MMI"
    width_mmi:
      value: 5
      description: "The width of both MMIs"

instances:
    mmi_long:
      component: mmi1x2
      settings:
        width_mmi: {{ width_mmi }}
        length_mmi: {{ length_mmi }}
    mmi_short:
      component: mmi1x2
      settings:
        width_mmi: {{ width_mmi }}
        length_mmi: 5
connections:
    mmi_long,o2: mmi_short,o1

ports:
    o1: mmi_long,o1
    o2: mmi_short,o2
    o3: mmi_short,o3
"""
pic_filename = "demo_jinja.pic.yml"

with open(pic_filename, mode="w") as f:
    f.write(jinja_yaml)

pic_cell = cell_from_yaml_template(pic_filename, name="demo_jinja")
gf.get_active_pdk().register_cells(
    demo_jinja=pic_cell
)  # let's register this cell so we can use it later
Code(filename=pic_filename, language="yaml+jinja")
default_settings:
    length_mmi:
      value: 10
      description: "The length of the long MMI"
    width_mmi:
      value: 5
      description: "The width of both MMIs"

instances:
    mmi_long:
      component: mmi1x2
      settings:
        width_mmi: {{ width_mmi }}
        length_mmi: {{ length_mmi }}
    mmi_short:
      component: mmi1x2
      settings:
        width_mmi: {{ width_mmi }}
        length_mmi: 5
connections:
    mmi_long,o2: mmi_short,o1

ports:
    o1: mmi_long,o1
    o2: mmi_short,o2
    o3: mmi_short,o3

You’ll see that this generated a python function, with a real signature, default arguments, docstring and all!

help(pic_cell)
Help on function demo_jinja:

demo_jinja(*, length_mmi=10, width_mmi=5) -> gdsfactory.component.Component
    demo_jinja: a templated yaml cell. This cell accepts keyword arguments only
    
    Keyword Args:
        length_mmi: The length of the long MMI
        width_mmi: The width of both MMIs

You can invoke this cell without arguments to see the default implementation

c = pic_cell()
c.plot()
2024-04-19 00:23:24.871 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/demo_jinja.lyp'.
../_images/00c583d10711f73c50262f59bbc7090a398018262c060c899a5e3f05065e41df.png

Or you can provide arguments explicitly, like a normal cell. Note however that yaml-based cells only accept keyword arguments, since yaml dictionaries are inherently unordered.

c = pic_cell(length_mmi=100)
c.plot()
2024-04-19 00:23:25.059 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/demo_jinja_length_mmi100.lyp'.
../_images/79433c2a42fe10c72ac026f24a8a9cf9cefd20ab76b91c6b6fd97dc3acedc85a.png

The power of jinja-templated cells become more apparent with more complex cells, like the following.

gf.clear_cache()

jinja_yaml = """
default_settings:
    length_mmis:
      value: [10, 20, 30, 100]
      description: "An array of mmi lengths for the DOE"
    spacing_mmi:
      value: 50
      description: "The vertical spacing between adjacent MMIs"
    mmi_component:
      value: mmi1x2
      description: "The mmi component to use"

instances:
{% for i in range(length_mmis|length)%}
    mmi_{{ i }}:
      component: {{ mmi_component }}
      settings:
        width_mmi: 4.5
        length_mmi: {{ length_mmis[i] }}
{% endfor %}

placements:
{% for i in range(1, length_mmis|length)%}
    mmi_{{ i }}:
      port: o1
      x: mmi_0,o1
      y: mmi_0,o1
      dy: {{ spacing_mmi * i }}
{% endfor %}

routes:
{% for i in range(1, length_mmis|length)%}
    r{{ i }}:
      routing_strategy: get_bundle_all_angle
      links:
        mmi_{{ i-1 }},o2: mmi_{{ i }},o1
{% endfor %}

ports:
{% for i in range(length_mmis|length)%}
    o{{ i }}: mmi_{{ i }},o3
{% endfor %}
"""
pic_filename = "demo_jinja_loops.pic.yml"

with open(pic_filename, mode="w") as f:
    f.write(jinja_yaml)

big_cell = cell_from_yaml_template(pic_filename, name="demo_jinja_loops")
Code(filename=pic_filename, language="yaml+jinja")
default_settings:
    length_mmis:
      value: [10, 20, 30, 100]
      description: "An array of mmi lengths for the DOE"
    spacing_mmi:
      value: 50
      description: "The vertical spacing between adjacent MMIs"
    mmi_component:
      value: mmi1x2
      description: "The mmi component to use"

instances:
{% for i in range(length_mmis|length)%}
    mmi_{{ i }}:
      component: {{ mmi_component }}
      settings:
        width_mmi: 4.5
        length_mmi: {{ length_mmis[i] }}
{% endfor %}

placements:
{% for i in range(1, length_mmis|length)%}
    mmi_{{ i }}:
      port: o1
      x: mmi_0,o1
      y: mmi_0,o1
      dy: {{ spacing_mmi * i }}
{% endfor %}

routes:
{% for i in range(1, length_mmis|length)%}
    r{{ i }}:
      routing_strategy: get_bundle_all_angle
      links:
        mmi_{{ i-1 }},o2: mmi_{{ i }},o1
{% endfor %}

ports:
{% for i in range(length_mmis|length)%}
    o{{ i }}: mmi_{{ i }},o3
{% endfor %}
bc = big_cell()
bc.plot()
2024-04-19 00:23:25.537 | WARNING  | gdsfactory.component:_write_library:1933 - UserWarning: Component demo_jinja_loops has invalid transformations. Try component.flatten_offgrid_references() first.
2024-04-19 00:23:25.551 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/demo_jinja_loops.lyp'.
../_images/6cd765ad07bf855686c8859afa352a9a58f56fcd502294aad4fd04e97e197487.png
bc2 = big_cell(
    length_mmis=[10, 20, 40, 100, 200, 150, 10, 40],
    spacing_mmi=60,
    mmi_component="demo_jinja",
)
bc2.plot()
2024-04-19 00:23:26.332 | WARNING  | gdsfactory.component:_write_library:1933 - UserWarning: Component demo_jinja_loops_6ca36734 has invalid transformations. Try component.flatten_offgrid_references() first.
2024-04-19 00:23:26.347 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/demo_jinja_loops_6ca36734.lyp'.
../_images/b63d271cf9ef49bc507985fe9c26c650a8511e4ed0512fcfd5070cf9e0141513.png

In general, the jinja-yaml parser has a superset of the functionalities and syntax of the standard yaml parser. The one notable exception is with settings. When reading any yaml files with settings blocks, the default settings will be read and applied, but they will not be settable, as the jinja parser has a different mechanism for setting injection with the default_settings block and jinja2.

filepath = "yaml_pics/mzi_lattice_filter.pic.yml"
mzi_lattice = cell_from_yaml_template(filepath, name="mzi_lattice_filter")
Code(filepath, language="yaml")
default_settings:
  delta_length:
    value: 20
    description: "The delta length"

instances:
  mzi1:
    component: mzi
    settings:
      delta_length: {{ delta_length }}

  mzi2:
    component: mzi
    settings:
      delta_length: {{ delta_length }}

  gc1:
    component: grating_coupler_te

  gc2:
    component: grating_coupler_te

placements:
  mzi2:
    ymax: mzi1,north
    dy: 100
    xmin: mzi1,east
    dx: 50

  gc1:
    xmax: mzi1,west
    mirror: True
    dx: -100
    dy: -20

  gc2:
    xmin: mzi2,east
    dx: 100
    dy: 100

routes:
  optical:
    links:
      mzi1,o2: mzi2,o1
    settings:
      auto_widen: True

  gc1:
    links:
      gc1,o1: mzi1,o1

  gc2:
    links:
      gc2,o1: mzi2,o2
c = mzi_lattice(delta_length=10)
c.plot()
2024-04-19 00:23:26.594 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/mzi_lattice_filter_delta_length10.lyp'.
../_images/b4262882b5e84323b819f85883c67bce83b53b099d2edba479f44b1ca55ec5f4.png
c = mzi_lattice(delta_length=100)
c.plot()
2024-04-19 00:23:26.878 | INFO     | gdsfactory.technology.layer_views:to_lyp:1018 - LayerViews written to '/tmp/gdsfactory/mzi_lattice_filter_delta_length100.lyp'.
../_images/e018ce228114ce2d283d3939fb8428da494a7c83c603b47cc436e363dec843e5.png