Skip to content

Commit b2f0025

Browse files
authored
feat: add optimization (#5)
* feat: add optimization * chore: add optimization docs regarding integration with design flow * v.01.11
1 parent 2c5896e commit b2f0025

File tree

4 files changed

+174
-3
lines changed

4 files changed

+174
-3
lines changed

axiomatic_mcp/servers/pic/README.md

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# AxPhotonicsPreview
22

3-
An MCP server for designing, and simulating Photonic Integrated Circuits (PICs) using the Axiomatic AI platform.
3+
An MCP server for designing, optimizing, and simulating Photonic Integrated Circuits (PICs) using the Axiomatic AI platform.
44

55
## Overview
66

7-
The AxPhotonicsPreview Server enables AI assistants to create photonic integrated circuit designs using natural language descriptions. It leverages the Axiomatic AI platform to generate GDSFactory-compatible Python code for photonic components and circuits, and provides tools to simulate them.
7+
The AxPhotonicsPreview Server enables AI assistants to create photonic integrated circuit designs using natural language descriptions. It leverages the Axiomatic AI platform to generate GDSFactory-compatible Python code for photonic components and circuits, and provides tools to simulate and optimize them.
88

99
## Tools Available
1010

@@ -47,6 +47,101 @@ Simulates a previously generated circuit and returns both the wavelength range (
4747

4848
---
4949

50+
### `optimize_circuit`
51+
52+
Optimizes a photonic circuit by refining the generated code using its formalized statements.
53+
54+
**Parameters:**
55+
56+
- `code_path` (Path, required): Path to the Python file containing the circuit code.
57+
- `statements_path` (Path, required): Path to the JSON file containing the circuit statements.
58+
59+
**Outputs:**
60+
61+
- **Optimized Python code (`*_optimized.py`)**: Refined version of the input circuit code.
62+
- **Structured Output**: Returns the optimized file path and the optimized code.
63+
64+
**Example Usage:**
65+
66+
Suppose you have the following circuit in `circuit.py`:
67+
68+
```python
69+
import gdsfactory as gf
70+
import cspdk.si220.cband
71+
72+
pdk = cspdk.si220.cband.get_pdk()
73+
pdk.activate()
74+
75+
c = gf.Component()
76+
77+
# Add MZI component
78+
mzi = c << pdk.get_component("mzi", delta_length=10, cross_section="strip")
79+
mzi.move((-34.55, 2.14))
80+
mzi.name = "mzi"
81+
82+
# Add ports
83+
c.add_port("in0", port=mzi.ports["o1"])
84+
c.add_port("in1", port=mzi.ports["o2"])
85+
c.add_port("out0", port=mzi.ports["o3"])
86+
c.add_port("out1", port=mzi.ports["o4"])
87+
88+
c
89+
```
90+
91+
And the following `statements.json`:
92+
93+
```json
94+
{
95+
"statements": [
96+
{
97+
"type": "PARAMETER_CONSTRAINT",
98+
"text": "The circuit has one input port named 'in0' (corresponding to 'o1') and one output port named 'out0' (corresponding to 'o2').",
99+
"formalization": {
100+
"code": "n_ports == 2",
101+
"default_tolerance": 0.05,
102+
"mapping": {
103+
"n_ports": {
104+
"name": "number_of_ports",
105+
"arguments": { "port_type": "all", "component": "netlist" }
106+
}
107+
}
108+
},
109+
"validation": null
110+
},
111+
{
112+
"type": "PARAMETER_CONSTRAINT",
113+
"text": "The circuit has exactly one input port.",
114+
"formalization": {
115+
"code": "n_ports_in == 1",
116+
"default_tolerance": 0.05,
117+
"mapping": {
118+
"n_ports_in": {
119+
"name": "number_of_ports",
120+
"arguments": { "port_type": "in", "component": "netlist" }
121+
}
122+
}
123+
},
124+
"validation": null
125+
}
126+
]
127+
}
128+
```
129+
130+
Running `optimize_circuit` with these inputs will produce a new file:
131+
132+
- `circuit_optimized.py` – optimized code satisfying the given constraints.
133+
134+
**Sample Output (structured):**
135+
136+
```json
137+
{
138+
"optimized_file_path": "/path/to/circuit_optimized.py",
139+
"optimized_code": "import gdsfactory as gf ..."
140+
}
141+
```
142+
143+
---
144+
50145
### `list_available_pdks`
51146

52147
Gets a list of all available Process Design Kits (PDKs), with its granted status.
@@ -184,6 +279,7 @@ This server requires extra dependencies, please see [Check system requirements](
184279
1. **Design Generation**: Use the MCP server to create initial designs
185280
2. **Local Execution**: Run generated code with GDSFactory
186281
3. **Simulation**: Use `simulate_circuit` to generate wavelength sweeps and notebook results
282+
4. **Optimization**: Use `optimize_circuit` to refine the circuit code and parameters based on formalized design constraints
187283

188284
---
189285

axiomatic_mcp/servers/pic/server.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ...shared.utils.prompt_utils import get_feedback_prompt
1616
from .services.circuit_service import CircuitService
1717
from .services.notebook_service import NotebookService
18+
from .services.optimization_service import OptimizationService
1819
from .services.pdk_service import PdkService
1920
from .services.simulation_service import SimulationService
2021
from .services.statements_service import StatementsService
@@ -38,6 +39,7 @@
3839
notebook_service = NotebookService()
3940
pdk_service = PdkService()
4041
statements_service = StatementsService()
42+
optimization_service = OptimizationService()
4143

4244

4345
@mcp.tool(
@@ -259,6 +261,56 @@ async def validate_statements(
259261
)
260262

261263

264+
@mcp.tool(
265+
name="optimize_circuit",
266+
description="Optimizes a photonic circuit by refining the generated code using its statements.",
267+
tags=["optimize", "gfsfactory"],
268+
)
269+
async def optimize_circuit(
270+
code_path: Annotated[Path, "Path to the Python file containing the circuit code"],
271+
statements_path: Annotated[Path, "Path to the JSON file containing the circuit statements"],
272+
) -> ToolResult:
273+
"""Optimize a photonic integrated circuit."""
274+
if not code_path.exists():
275+
raise FileNotFoundError(f"Circuit code not found: {code_path}")
276+
if not statements_path.exists():
277+
raise FileNotFoundError(f"Statements file not found: {statements_path}")
278+
279+
code = code_path.read_text(encoding="utf-8")
280+
281+
with statements_path.open("r", encoding="utf-8") as f:
282+
statements_json = json.load(f)
283+
284+
statements_list = statements_json.get("statements", [])
285+
286+
request_body = {
287+
"code": code,
288+
"statements": statements_list,
289+
}
290+
291+
response = await optimization_service.optimize_code(request_body)
292+
293+
optimized_code = response.get("optimized_code", "")
294+
295+
optimized_file_path = code_path.parent / f"{code_path.stem}_optimized.py"
296+
297+
with optimized_file_path.open("w", encoding="utf-8") as f:
298+
f.write(optimized_code)
299+
300+
return ToolResult(
301+
content=[
302+
TextContent(
303+
type="text",
304+
text=f"Optimized circuit saved at {optimized_file_path}",
305+
)
306+
],
307+
structured_content={
308+
"optimized_file_path": str(optimized_file_path),
309+
"optimized_code": optimized_code,
310+
},
311+
)
312+
313+
262314
@mcp.tool(
263315
name="list_available_pdks",
264316
description="Get a list of all available PDKs that the user has access to.",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import Any
2+
3+
from ....shared import AxiomaticAPIClient
4+
from ....shared.models.singleton_base import SingletonBase
5+
from ...constants.api_constants import ApiRoutes
6+
7+
8+
class OptimizationService(SingletonBase):
9+
async def optimize_code(self, query: dict[str, Any]) -> dict[str, Any]:
10+
"""
11+
Call the GET_OPTIMIZED_CODE API endpoint with optimization request.
12+
query: {
13+
"code": str, # Python code (gdsfactory circuit)
14+
"statements": list # Parsed statements from statements.json
15+
}
16+
"""
17+
18+
response = AxiomaticAPIClient().post(ApiRoutes.GET_OPTIMIZED_CODE, data=query)
19+
20+
if not response:
21+
raise RuntimeError("No response from get_optimized_code API")
22+
23+
return response

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "axiomatic-mcp"
7-
version = "0.1.10"
7+
version = "0.1.11"
88
description = "Modular MCP servers for Axiomatic_AI"
99
authors = [{name = "Axiomatic Team", email = "[email protected]"}]
1010
readme = "README.md"

0 commit comments

Comments
 (0)