Skip to content

Commit 227a344

Browse files
joamatabclaude
andcommitted
fix: patch meow/sax incompatibility and add missing test __init__.py files
Convert meow's old-style connections dict to sax 0.16's new nets format, and add __init__.py to test directories to fix conftest collection errors. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 30b04ad commit 227a344

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

gplugins/meow/meow_eme.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pathlib
33
import pprint
44
import time
5-
from typing import Literal
5+
from typing import Literal, cast
66

77
import gdsfactory as gf
88
import meow as mw
@@ -48,6 +48,54 @@ def list_unique_layer_stack_z(
4848
}
4949

5050

51+
def _compute_s_matrix(modes_per_cell, cells):
52+
"""Compute S-matrix, compatible with sax >= 0.16 nets format.
53+
54+
meow's compute_s_matrix passes connections as a dict, but sax >= 0.16
55+
expects a list of {"p1": ..., "p2": ...} dicts (Nets). This wrapper
56+
converts the format.
57+
"""
58+
from meow.eme.sax import (
59+
_get_netlist,
60+
compute_interface_s_matrices,
61+
compute_propagation_s_matrices,
62+
)
63+
64+
propagations = compute_propagation_s_matrices(modes_per_cell, cells)
65+
interfaces = compute_interface_s_matrices(modes_per_cell)
66+
67+
net = _get_netlist(propagations, interfaces)
68+
_, analyze_circuit, evaluate_circuit = sax.circuit_backends[
69+
sax.into[sax.Backend]("default")
70+
]
71+
net["instances"] = {k: sax.scoo(v) for k, v in net["instances"].items()}
72+
73+
# Convert old-style connections dict to new-style nets list
74+
connections = net["connections"]
75+
if isinstance(connections, dict):
76+
nets = [{"p1": k, "p2": v} for k, v in connections.items()]
77+
else:
78+
nets = connections
79+
80+
analyzed = analyze_circuit(net["instances"], nets, net["ports"])
81+
S, port_map = sax.sdense(evaluate_circuit(analyzed, instances=net["instances"]))
82+
S = np.asarray(S)
83+
84+
# Sort ports consistently
85+
current_port_map = {
86+
(p, int(i)): j
87+
for (p, i), j in {
88+
tuple(pm.split("@")): idx for pm, idx in port_map.items()
89+
}.items()
90+
}
91+
desired_port_map = {pm: i for i, pm in enumerate(sorted(current_port_map))}
92+
idxs = [current_port_map[pm] for pm in desired_port_map]
93+
S = S[idxs, :][:, idxs]
94+
port_map = {f"{p}@{m}": v for (p, m), v in desired_port_map.items()}
95+
96+
return cast(sax.SDenseMM, (S, port_map))
97+
98+
5199
class MEOW:
52100
def __init__(
53101
self,
@@ -430,7 +478,7 @@ def rename(p):
430478

431479
self.compute_all_modes()
432480

433-
self.S, self.port_map = mw.compute_s_matrix(self.modes_per_cell, self.cells)
481+
self.S, self.port_map = _compute_s_matrix(self.modes_per_cell, self.cells)
434482

435483
sdict = sax.sdict((self.S, self.port_map))
436484

0 commit comments

Comments
 (0)