quantcore-circuits 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. quantcore_circuits/__init__.py +57 -0
  2. quantcore_circuits/analysis/__init__.py +12 -0
  3. quantcore_circuits/analysis/_metal.py +70 -0
  4. quantcore_circuits/analysis/amplifiers.py +263 -0
  5. quantcore_circuits/analysis/circuits.py +117 -0
  6. quantcore_circuits/analysis/epr.py +231 -0
  7. quantcore_circuits/analysis/epr_lumped.py +372 -0
  8. quantcore_circuits/analysis/extract.py +119 -0
  9. quantcore_circuits/analysis/lom.py +80 -0
  10. quantcore_circuits/analysis/results.py +549 -0
  11. quantcore_circuits/analysis/store.py +81 -0
  12. quantcore_circuits/analysis/synthesis.py +118 -0
  13. quantcore_circuits/analysis/transmon.py +126 -0
  14. quantcore_circuits/assembly/__init__.py +17 -0
  15. quantcore_circuits/assembly/builder.py +127 -0
  16. quantcore_circuits/assembly/drc.py +683 -0
  17. quantcore_circuits/assembly/last_good.py +137 -0
  18. quantcore_circuits/assembly/netlist.py +174 -0
  19. quantcore_circuits/assembly/plan.py +605 -0
  20. quantcore_circuits/assembly/sidecar.py +270 -0
  21. quantcore_circuits/autotrack.py +605 -0
  22. quantcore_circuits/cards.py +878 -0
  23. quantcore_circuits/circuit/__init__.py +14 -0
  24. quantcore_circuits/circuit/adapters.py +305 -0
  25. quantcore_circuits/circuit/bridges.py +353 -0
  26. quantcore_circuits/circuit/consensus.py +158 -0
  27. quantcore_circuits/circuit/derive.py +28 -0
  28. quantcore_circuits/circuit/draw.py +198 -0
  29. quantcore_circuits/circuit/fill.py +828 -0
  30. quantcore_circuits/circuit/hamiltonian.py +193 -0
  31. quantcore_circuits/circuit/kinetic.py +102 -0
  32. quantcore_circuits/circuit/schema.py +139 -0
  33. quantcore_circuits/cli.py +1085 -0
  34. quantcore_circuits/codesign.py +1038 -0
  35. quantcore_circuits/data/selftest_q1_r1.json +44 -0
  36. quantcore_circuits/design/__init__.py +44 -0
  37. quantcore_circuits/design/io.py +98 -0
  38. quantcore_circuits/design/pyscript.py +395 -0
  39. quantcore_circuits/design/schema.py +398 -0
  40. quantcore_circuits/designer.py +625 -0
  41. quantcore_circuits/guiflow.py +991 -0
  42. quantcore_circuits/kqc_files.py +208 -0
  43. quantcore_circuits/layouts/__init__.py +118 -0
  44. quantcore_circuits/layouts/catalog.py +520 -0
  45. quantcore_circuits/layouts/chips/__init__.py +0 -0
  46. quantcore_circuits/layouts/chips/design_chip.py +325 -0
  47. quantcore_circuits/layouts/chips/frame.py +235 -0
  48. quantcore_circuits/layouts/junctions/__init__.py +0 -0
  49. quantcore_circuits/layouts/junctions/jj_fingers.py +99 -0
  50. quantcore_circuits/layouts/junctions/legacy_junctions.py +237 -0
  51. quantcore_circuits/layouts/junctions/sim_gap.py +48 -0
  52. quantcore_circuits/layouts/junctions/trilayer.py +541 -0
  53. quantcore_circuits/layouts/metal/__init__.py +105 -0
  54. quantcore_circuits/layouts/metal/component.py +332 -0
  55. quantcore_circuits/layouts/metal/geometry.py +462 -0
  56. quantcore_circuits/layouts/others/__init__.py +0 -0
  57. quantcore_circuits/layouts/others/amplifiers.py +1139 -0
  58. quantcore_circuits/layouts/others/bond_pad.py +119 -0
  59. quantcore_circuits/layouts/others/connector_pads.py +190 -0
  60. quantcore_circuits/layouts/others/couplers.py +21 -0
  61. quantcore_circuits/layouts/others/cpw_segment.py +131 -0
  62. quantcore_circuits/layouts/others/cpwe.py +661 -0
  63. quantcore_circuits/layouts/others/keepout.py +66 -0
  64. quantcore_circuits/layouts/others/legacy_resonators.py +645 -0
  65. quantcore_circuits/layouts/others/markers.py +201 -0
  66. quantcore_circuits/layouts/others/qpdk_cells.py +263 -0
  67. quantcore_circuits/layouts/others/shapes.py +114 -0
  68. quantcore_circuits/layouts/others/spirals.py +140 -0
  69. quantcore_circuits/layouts/others/sqdmetal_cells.py +90 -0
  70. quantcore_circuits/layouts/others/test_pads.py +461 -0
  71. quantcore_circuits/layouts/ports.py +119 -0
  72. quantcore_circuits/layouts/qubits/__init__.py +0 -0
  73. quantcore_circuits/layouts/qubits/legacy_transmon.py +124 -0
  74. quantcore_circuits/layouts/qubits/qpdk_cells.py +374 -0
  75. quantcore_circuits/layouts/qubits/sqdmetal_cells.py +376 -0
  76. quantcore_circuits/layouts/qubits/trilayer_concentric.py +65 -0
  77. quantcore_circuits/layouts/qubits/xmon.py +375 -0
  78. quantcore_circuits/layouts/registry.py +418 -0
  79. quantcore_circuits/layouts/wafers/__init__.py +0 -0
  80. quantcore_circuits/layouts/wafers/fab_export.py +454 -0
  81. quantcore_circuits/layouts/wafers/legacy_wafer.py +111 -0
  82. quantcore_circuits/layouts/wafers/wafer.py +469 -0
  83. quantcore_circuits/live/__init__.py +6 -0
  84. quantcore_circuits/live/klayout.py +225 -0
  85. quantcore_circuits/live/klive.py +61 -0
  86. quantcore_circuits/live/snapshot.py +185 -0
  87. quantcore_circuits/live/watch.py +93 -0
  88. quantcore_circuits/lom/__init__.py +15 -0
  89. quantcore_circuits/lom/builder.py +932 -0
  90. quantcore_circuits/lom/calibration.py +82 -0
  91. quantcore_circuits/lom/estimates.py +235 -0
  92. quantcore_circuits/lom/fragment.py +421 -0
  93. quantcore_circuits/lom/kqc_models.py +734 -0
  94. quantcore_circuits/lom/metal_models.py +917 -0
  95. quantcore_circuits/lom/quantcore_models.py +784 -0
  96. quantcore_circuits/lom/simulate.py +636 -0
  97. quantcore_circuits/lom/synthesize.py +189 -0
  98. quantcore_circuits/loop.py +610 -0
  99. quantcore_circuits/optimise.py +423 -0
  100. quantcore_circuits/pdk/__init__.py +451 -0
  101. quantcore_circuits/plots.py +876 -0
  102. quantcore_circuits/plugins.py +69 -0
  103. quantcore_circuits/review/__init__.py +57 -0
  104. quantcore_circuits/review/server.py +251 -0
  105. quantcore_circuits/review/static/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
  106. quantcore_circuits/review/static/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
  107. quantcore_circuits/review/static/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
  108. quantcore_circuits/review/static/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
  109. quantcore_circuits/review/static/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
  110. quantcore_circuits/review/static/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
  111. quantcore_circuits/review/static/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
  112. quantcore_circuits/review/static/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
  113. quantcore_circuits/review/static/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
  114. quantcore_circuits/review/static/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
  115. quantcore_circuits/review/static/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
  116. quantcore_circuits/review/static/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
  117. quantcore_circuits/review/static/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
  118. quantcore_circuits/review/static/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
  119. quantcore_circuits/review/static/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
  120. quantcore_circuits/review/static/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
  121. quantcore_circuits/review/static/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
  122. quantcore_circuits/review/static/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
  123. quantcore_circuits/review/static/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
  124. quantcore_circuits/review/static/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
  125. quantcore_circuits/review/static/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
  126. quantcore_circuits/review/static/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
  127. quantcore_circuits/review/static/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
  128. quantcore_circuits/review/static/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
  129. quantcore_circuits/review/static/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
  130. quantcore_circuits/review/static/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
  131. quantcore_circuits/review/static/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
  132. quantcore_circuits/review/static/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
  133. quantcore_circuits/review/static/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
  134. quantcore_circuits/review/static/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
  135. quantcore_circuits/review/static/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
  136. quantcore_circuits/review/static/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
  137. quantcore_circuits/review/static/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
  138. quantcore_circuits/review/static/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
  139. quantcore_circuits/review/static/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
  140. quantcore_circuits/review/static/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
  141. quantcore_circuits/review/static/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
  142. quantcore_circuits/review/static/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
  143. quantcore_circuits/review/static/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
  144. quantcore_circuits/review/static/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
  145. quantcore_circuits/review/static/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
  146. quantcore_circuits/review/static/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
  147. quantcore_circuits/review/static/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
  148. quantcore_circuits/review/static/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
  149. quantcore_circuits/review/static/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
  150. quantcore_circuits/review/static/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
  151. quantcore_circuits/review/static/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
  152. quantcore_circuits/review/static/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
  153. quantcore_circuits/review/static/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
  154. quantcore_circuits/review/static/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
  155. quantcore_circuits/review/static/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
  156. quantcore_circuits/review/static/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
  157. quantcore_circuits/review/static/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
  158. quantcore_circuits/review/static/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
  159. quantcore_circuits/review/static/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
  160. quantcore_circuits/review/static/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
  161. quantcore_circuits/review/static/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
  162. quantcore_circuits/review/static/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
  163. quantcore_circuits/review/static/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
  164. quantcore_circuits/review/static/assets/index-CDHdrFyA.css +1 -0
  165. quantcore_circuits/review/static/assets/index-CSx05Og4.js +409 -0
  166. quantcore_circuits/review/static/assets/inter-cyrillic-400-normal-HOLc17fK.woff +0 -0
  167. quantcore_circuits/review/static/assets/inter-cyrillic-400-normal-obahsSVq.woff2 +0 -0
  168. quantcore_circuits/review/static/assets/inter-cyrillic-500-normal-BasfLYem.woff2 +0 -0
  169. quantcore_circuits/review/static/assets/inter-cyrillic-500-normal-CxZf_p3X.woff +0 -0
  170. quantcore_circuits/review/static/assets/inter-cyrillic-600-normal-4D_pXhcN.woff +0 -0
  171. quantcore_circuits/review/static/assets/inter-cyrillic-600-normal-CWCymEST.woff2 +0 -0
  172. quantcore_circuits/review/static/assets/inter-cyrillic-700-normal-CjBOestx.woff2 +0 -0
  173. quantcore_circuits/review/static/assets/inter-cyrillic-700-normal-DrXBdSj3.woff +0 -0
  174. quantcore_circuits/review/static/assets/inter-cyrillic-ext-400-normal-BQZuk6qB.woff2 +0 -0
  175. quantcore_circuits/review/static/assets/inter-cyrillic-ext-400-normal-DQukG94-.woff +0 -0
  176. quantcore_circuits/review/static/assets/inter-cyrillic-ext-500-normal-B0yAr1jD.woff2 +0 -0
  177. quantcore_circuits/review/static/assets/inter-cyrillic-ext-500-normal-BmqWE9Dz.woff +0 -0
  178. quantcore_circuits/review/static/assets/inter-cyrillic-ext-600-normal-Bcila6Z-.woff +0 -0
  179. quantcore_circuits/review/static/assets/inter-cyrillic-ext-600-normal-Dfes3d0z.woff2 +0 -0
  180. quantcore_circuits/review/static/assets/inter-cyrillic-ext-700-normal-BjwYoWNd.woff2 +0 -0
  181. quantcore_circuits/review/static/assets/inter-cyrillic-ext-700-normal-LO58E6JB.woff +0 -0
  182. quantcore_circuits/review/static/assets/inter-greek-400-normal-B4URO6DV.woff2 +0 -0
  183. quantcore_circuits/review/static/assets/inter-greek-400-normal-q2sYcFCs.woff +0 -0
  184. quantcore_circuits/review/static/assets/inter-greek-500-normal-BIZE56-Y.woff2 +0 -0
  185. quantcore_circuits/review/static/assets/inter-greek-500-normal-Xzm54t5V.woff +0 -0
  186. quantcore_circuits/review/static/assets/inter-greek-600-normal-BZpKdvQh.woff +0 -0
  187. quantcore_circuits/review/static/assets/inter-greek-600-normal-plRanbMR.woff2 +0 -0
  188. quantcore_circuits/review/static/assets/inter-greek-700-normal-BUv2fZ6O.woff +0 -0
  189. quantcore_circuits/review/static/assets/inter-greek-700-normal-C3JjAnD8.woff2 +0 -0
  190. quantcore_circuits/review/static/assets/inter-greek-ext-400-normal-DGGRlc-M.woff2 +0 -0
  191. quantcore_circuits/review/static/assets/inter-greek-ext-400-normal-KugGGMne.woff +0 -0
  192. quantcore_circuits/review/static/assets/inter-greek-ext-500-normal-2j5mBUwD.woff +0 -0
  193. quantcore_circuits/review/static/assets/inter-greek-ext-500-normal-C4iEst2y.woff2 +0 -0
  194. quantcore_circuits/review/static/assets/inter-greek-ext-600-normal-B8X0CLgF.woff +0 -0
  195. quantcore_circuits/review/static/assets/inter-greek-ext-600-normal-DRtmH8MT.woff2 +0 -0
  196. quantcore_circuits/review/static/assets/inter-greek-ext-700-normal-BoQ6DsYi.woff +0 -0
  197. quantcore_circuits/review/static/assets/inter-greek-ext-700-normal-qfdV9bQt.woff2 +0 -0
  198. quantcore_circuits/review/static/assets/inter-latin-400-normal-C38fXH4l.woff2 +0 -0
  199. quantcore_circuits/review/static/assets/inter-latin-400-normal-CyCys3Eg.woff +0 -0
  200. quantcore_circuits/review/static/assets/inter-latin-500-normal-BL9OpVg8.woff +0 -0
  201. quantcore_circuits/review/static/assets/inter-latin-500-normal-Cerq10X2.woff2 +0 -0
  202. quantcore_circuits/review/static/assets/inter-latin-600-normal-CiBQ2DWP.woff +0 -0
  203. quantcore_circuits/review/static/assets/inter-latin-600-normal-LgqL8muc.woff2 +0 -0
  204. quantcore_circuits/review/static/assets/inter-latin-700-normal-BLAVimhd.woff +0 -0
  205. quantcore_circuits/review/static/assets/inter-latin-700-normal-Yt3aPRUw.woff2 +0 -0
  206. quantcore_circuits/review/static/assets/inter-latin-ext-400-normal-77YHD8bZ.woff +0 -0
  207. quantcore_circuits/review/static/assets/inter-latin-ext-400-normal-C1nco2VV.woff2 +0 -0
  208. quantcore_circuits/review/static/assets/inter-latin-ext-500-normal-BxGbmqWO.woff +0 -0
  209. quantcore_circuits/review/static/assets/inter-latin-ext-500-normal-CV4jyFjo.woff2 +0 -0
  210. quantcore_circuits/review/static/assets/inter-latin-ext-600-normal-CIVaiw4L.woff +0 -0
  211. quantcore_circuits/review/static/assets/inter-latin-ext-600-normal-D2bJ5OIk.woff2 +0 -0
  212. quantcore_circuits/review/static/assets/inter-latin-ext-700-normal-Ca8adRJv.woff2 +0 -0
  213. quantcore_circuits/review/static/assets/inter-latin-ext-700-normal-TidjK2hL.woff +0 -0
  214. quantcore_circuits/review/static/assets/inter-vietnamese-400-normal-Bbgyi5SW.woff +0 -0
  215. quantcore_circuits/review/static/assets/inter-vietnamese-400-normal-DMkecbls.woff2 +0 -0
  216. quantcore_circuits/review/static/assets/inter-vietnamese-500-normal-DOriooB6.woff2 +0 -0
  217. quantcore_circuits/review/static/assets/inter-vietnamese-500-normal-mJboJaSs.woff +0 -0
  218. quantcore_circuits/review/static/assets/inter-vietnamese-600-normal-BuLX-rYi.woff +0 -0
  219. quantcore_circuits/review/static/assets/inter-vietnamese-600-normal-Cc8MFFhd.woff2 +0 -0
  220. quantcore_circuits/review/static/assets/inter-vietnamese-700-normal-BZaoP0fm.woff +0 -0
  221. quantcore_circuits/review/static/assets/inter-vietnamese-700-normal-DlLaEgI2.woff2 +0 -0
  222. quantcore_circuits/review/static/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
  223. quantcore_circuits/review/static/assets/jetbrains-mono-cyrillic-400-normal-ugxPyKxw.woff +0 -0
  224. quantcore_circuits/review/static/assets/jetbrains-mono-cyrillic-500-normal-DJqRU3vO.woff +0 -0
  225. quantcore_circuits/review/static/assets/jetbrains-mono-cyrillic-500-normal-DmUKJPL_.woff2 +0 -0
  226. quantcore_circuits/review/static/assets/jetbrains-mono-greek-400-normal-B9oWc5Lo.woff +0 -0
  227. quantcore_circuits/review/static/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
  228. quantcore_circuits/review/static/assets/jetbrains-mono-greek-500-normal-D7SFKleX.woff +0 -0
  229. quantcore_circuits/review/static/assets/jetbrains-mono-greek-500-normal-JpySY46c.woff2 +0 -0
  230. quantcore_circuits/review/static/assets/jetbrains-mono-latin-400-normal-6-qcROiO.woff +0 -0
  231. quantcore_circuits/review/static/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
  232. quantcore_circuits/review/static/assets/jetbrains-mono-latin-500-normal-BWZEU5yA.woff2 +0 -0
  233. quantcore_circuits/review/static/assets/jetbrains-mono-latin-500-normal-CJOVTJB7.woff +0 -0
  234. quantcore_circuits/review/static/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
  235. quantcore_circuits/review/static/assets/jetbrains-mono-latin-ext-400-normal-fXTG6kC5.woff +0 -0
  236. quantcore_circuits/review/static/assets/jetbrains-mono-latin-ext-500-normal-Cut-4mMH.woff2 +0 -0
  237. quantcore_circuits/review/static/assets/jetbrains-mono-latin-ext-500-normal-ckzbgY84.woff +0 -0
  238. quantcore_circuits/review/static/assets/jetbrains-mono-vietnamese-400-normal-CqNFfHCs.woff +0 -0
  239. quantcore_circuits/review/static/assets/jetbrains-mono-vietnamese-500-normal-DNRqzVM1.woff +0 -0
  240. quantcore_circuits/review/static/index.html +14 -0
  241. quantcore_circuits/review/static/logo-quantcore-black.png +0 -0
  242. quantcore_circuits/review/static/logo-quantcore-white.png +0 -0
  243. quantcore_circuits/review/static/sample/chip.png +0 -0
  244. quantcore_circuits/review/static/sample/chip_rev1.png +0 -0
  245. quantcore_circuits/review/static/sample/q1.png +0 -0
  246. quantcore_circuits/review/static/sample/q2.png +0 -0
  247. quantcore_circuits/review/static/sample-state.json +3747 -0
  248. quantcore_circuits/route/__init__.py +5 -0
  249. quantcore_circuits/route/router.py +694 -0
  250. quantcore_circuits/route/waveguide.py +187 -0
  251. quantcore_circuits/selftest.py +224 -0
  252. quantcore_circuits/sim_tools/__init__.py +6 -0
  253. quantcore_circuits/sim_tools/aedt_attach.py +66 -0
  254. quantcore_circuits/sim_tools/aedt_progress.py +224 -0
  255. quantcore_circuits/sim_tools/aedt_project.py +302 -0
  256. quantcore_circuits/sim_tools/aedt_scripts.py +397 -0
  257. quantcore_circuits/sim_tools/aedt_session.py +275 -0
  258. quantcore_circuits/sim_tools/ansys.py +90 -0
  259. quantcore_circuits/sim_tools/builder.py +1690 -0
  260. quantcore_circuits/sim_tools/export.py +470 -0
  261. quantcore_circuits/sim_tools/geometry_check.py +342 -0
  262. quantcore_circuits/sim_tools/hfss.py +583 -0
  263. quantcore_circuits/sim_tools/palace.py +197 -0
  264. quantcore_circuits/sim_tools/runner.py +179 -0
  265. quantcore_circuits/sim_tools/subset.py +364 -0
  266. quantcore_circuits/track.py +671 -0
  267. quantcore_circuits-0.0.1.dist-info/METADATA +204 -0
  268. quantcore_circuits-0.0.1.dist-info/RECORD +272 -0
  269. quantcore_circuits-0.0.1.dist-info/WHEEL +5 -0
  270. quantcore_circuits-0.0.1.dist-info/entry_points.txt +2 -0
  271. quantcore_circuits-0.0.1.dist-info/licenses/LICENSE +674 -0
  272. quantcore_circuits-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,694 @@
1
+ """Route specs → KQC ``WaveguideComposite`` node lists.
2
+
3
+ Routing policy (deliberately simple and predictable, everything else is KQC):
4
+
5
+ * A route leaves a port straight along the port's outward direction for one turn radius (the KQC ``port_*_corner``
6
+ convention) and enters the destination port the same way.
7
+ * Between the two stubs it follows the user ``nodes``; without nodes it auto-routes Manhattan style
8
+ (one elbow if possible, otherwise a Z through the midpoint), never doubling back on itself.
9
+ * A route between ports that face an angle (a StarQubit pin at 72° steps, a ConcentricTransmon coupler at 60°) may
10
+ run one oblique straight instead of a Manhattan stair: straight from stub to stub when the two ports face each
11
+ other along one line (within a few degrees), else with one bend where the two ports' axes meet (see
12
+ :func:`_oblique`). Only where the bends fit; routes between axis-aligned ports are Manhattan as before.
13
+ * ``type="meander"`` keeps the path and inserts a KQC ``Meander`` on one straight segment so that the *total*
14
+ route length equals ``length``. The correction uses KQC's ``Node(length_increment=...)`` and is verified by
15
+ building the waveguide (two-step correction like ``WaveguideComposite.produce_fixed_length_waveguide``). A path
16
+ that is already about the right length gets no meander, or its free corners' radius trimmed, never a flattened
17
+ zigzag (:func:`fix_length`).
18
+ * ``airbridges="auto"`` puts ``n_bridges=-1`` on every straight segment (KQC spaces them by ``ab_to_ab_spacing``).
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ import math
24
+ from dataclasses import dataclass, field
25
+
26
+ from kqcircuits.elements.airbridge_connection import AirbridgeConnection
27
+ from kqcircuits.pya_resolver import pya
28
+ from kqcircuits.util.node import Node
29
+
30
+ from quantcore_circuits.design.schema import FreeEnd, Route
31
+ from quantcore_circuits.layouts.ports import Port, direction_vector
32
+
33
+ _EPS = 1e-6
34
+
35
+
36
+ class RoutingError(ValueError):
37
+ pass
38
+
39
+
40
+ @dataclass
41
+ class RouteEnd:
42
+ """Resolved route end: position, direction pointing from the end into the route, termination.
43
+
44
+ For a port this is the port's outward direction (``corner - port``). A free end uses the same convention, so it
45
+ behaves like a virtual port.
46
+ """
47
+
48
+ pos: pya.DPoint
49
+ dir: pya.DVector | None
50
+ a: float | None = None
51
+ b: float | None = None
52
+ term: float | None = None # None for ports (no termination), else KQC term1/term2
53
+ # fixed points the route passes through right before ``pos`` (in travel order), e.g. a hanger coupler
54
+ approach: list = field(default_factory=list)
55
+
56
+ @classmethod
57
+ def from_port(cls, port: Port) -> RouteEnd:
58
+ return cls(port.pos, port.dir, port.a, port.b)
59
+
60
+ @classmethod
61
+ def from_free(cls, end: FreeEnd) -> RouteEnd:
62
+ d = None if end.dir is None else direction_vector(end.dir)
63
+ return cls(pya.DPoint(*end.at), d, term=end.term)
64
+
65
+
66
+ @dataclass
67
+ class RoutePlan:
68
+ name: str
69
+ nodes: list[Node]
70
+ params: dict = field(default_factory=dict) # WaveguideComposite params (a, b, r, term1, term2, ...)
71
+ target_length: float | None = None
72
+ length: float | None = None # as built (filled in by the builder)
73
+ fixed_segments: set = field(default_factory=set) # segment indices that must not host a meander
74
+ # ends at a placed cell's port, [{"inst", "node": 0 | -1}]: DesignChip closes a hairline to the cell's etch there
75
+ # (route.waveguide.joined_nodes)
76
+ joins: list = field(default_factory=list)
77
+
78
+ def node_strings(self) -> list[str]:
79
+ return [str(n) for n in self.nodes]
80
+
81
+ def polyline(self) -> list[pya.DPoint]:
82
+ return [n.position for n in self.nodes]
83
+
84
+
85
+ def _is_axis(v: pya.DVector) -> bool:
86
+ return abs(v.x) < _EPS or abs(v.y) < _EPS
87
+
88
+
89
+ def _dedupe(points: list[pya.DPoint]) -> list[pya.DPoint]:
90
+ """Drop repeated points. Collinear points are kept on purpose: port stubs stay separate segments, so a meander
91
+ never starts right at a port and segment indices stay predictable."""
92
+ out = []
93
+ for p in points:
94
+ if not out or (p - out[-1]).length() > _EPS:
95
+ out.append(p)
96
+ return out
97
+
98
+
99
+ def _on_axis(origin: pya.DPoint, direction: pya.DVector, p: pya.DPoint) -> bool:
100
+ """True if ``p`` lies on the ray from ``origin`` along ``direction``."""
101
+ v = p - origin
102
+ return v.length() > _EPS and abs(v.x * direction.y - v.y * direction.x) < _EPS and v.sprod(direction) > 0
103
+
104
+
105
+ def _reverses(points: list[pya.DPoint]) -> bool:
106
+ """True if the polyline doubles back (≈180° turn) anywhere."""
107
+ for i in range(1, len(points) - 1):
108
+ v1, v2 = points[i] - points[i - 1], points[i + 1] - points[i]
109
+ if v1.length() < _EPS or v2.length() < _EPS:
110
+ continue
111
+ if v1.sprod(v2) < -0.999 * v1.length() * v2.length():
112
+ return True
113
+ return False
114
+
115
+
116
+ def merge_collinear(points: list[pya.DPoint], angle_tol: float = 1e-6) -> list[pya.DPoint]:
117
+ """Drop interior points where the polyline goes straight on (no bend there): a straight leg split into pieces
118
+ must not be checked piece by piece."""
119
+ out = list(points[:1])
120
+ for i in range(1, len(points) - 1):
121
+ v1, v2 = points[i] - out[-1], points[i + 1] - points[i]
122
+ if v1.length() < _EPS or v2.length() < _EPS:
123
+ continue
124
+ if abs(v1.vprod(v2)) <= angle_tol * v1.length() * v2.length() and v1.sprod(v2) > 0:
125
+ continue
126
+ out.append(points[i])
127
+ out += points[-1:] if len(points) > 1 else []
128
+ return out
129
+
130
+
131
+ def _key(p: pya.DPoint) -> tuple[float, float]:
132
+ return (round(p.x, 6), round(p.y, 6))
133
+
134
+
135
+ def corner_violations(
136
+ points: list[pya.DPoint], r: float, tol: float = 1e-3, radii: dict | None = None
137
+ ) -> list[tuple[int, float, float]]:
138
+ """Segments too short for their bends: (segment index, length, required).
139
+
140
+ A bend of angle θ with radius ``r`` uses ``r·tan(θ/2)`` of each adjacent straight segment (KQC
141
+ ``WaveguideCoplanar`` corner model). ``radii`` maps corner positions (``(x, y)`` rounded to 1e-6 µm) to their own
142
+ radius (``Waypoint.r``); every other corner uses ``r``. Call it on :func:`merge_collinear` points (the router
143
+ does): indices refer to the list passed in.
144
+ """
145
+ radii = radii or {}
146
+
147
+ def cut(i): # length used by the bend at interior point i
148
+ if i <= 0 or i >= len(points) - 1:
149
+ return 0.0
150
+ v1, v2 = points[i] - points[i - 1], points[i + 1] - points[i]
151
+ if v1.length() < _EPS or v2.length() < _EPS:
152
+ return 0.0
153
+ cos = max(-1.0, min(1.0, v1.sprod(v2) / (v1.length() * v2.length())))
154
+ theta = math.acos(cos)
155
+ rad = radii.get(_key(points[i]), r)
156
+ return rad * math.tan(theta / 2) if theta < math.pi - 1e-9 else float("inf")
157
+
158
+ out = []
159
+ for i in range(len(points) - 1):
160
+ need = cut(i) + cut(i + 1)
161
+ length = (points[i + 1] - points[i]).length()
162
+ if length + tol < need:
163
+ out.append((i + 1, length, need))
164
+ return out
165
+
166
+
167
+ def _manhattan(
168
+ p0: pya.DPoint,
169
+ d0: pya.DVector | None,
170
+ p1: pya.DPoint,
171
+ d1: pya.DVector | None,
172
+ r: float = 0.0,
173
+ prefix: list | None = None,
174
+ suffix: list | None = None,
175
+ radii: dict | None = None,
176
+ ) -> list[pya.DPoint]:
177
+ """Intermediate points between stub ends p0 and p1.
178
+
179
+ ``d0`` is the travel direction arriving at p0, ``d1`` the travel direction leaving p1 (towards the route end).
180
+ Candidates that double back are rejected; among the rest the first whose bends fit (radius ``r``, checked on the
181
+ full path ``prefix + [p0, ..., p1] + suffix``) wins.
182
+ """
183
+ v = p1 - p0
184
+ if v.length() < _EPS or abs(v.x) < _EPS or abs(v.y) < _EPS:
185
+ candidates = [[]]
186
+ else:
187
+ mid = p0 + v * 0.5
188
+ candidates = [
189
+ [pya.DPoint(p1.x, p0.y)],
190
+ [pya.DPoint(p0.x, p1.y)],
191
+ [pya.DPoint(mid.x, p0.y), pya.DPoint(mid.x, p1.y)],
192
+ [pya.DPoint(p0.x, mid.y), pya.DPoint(p1.x, mid.y)],
193
+ ]
194
+ # prefer continuing straight out of the start port
195
+ if d0 is not None and abs(d0.y) > abs(d0.x):
196
+ candidates[0], candidates[1] = candidates[1], candidates[0]
197
+ candidates[2], candidates[3] = candidates[3], candidates[2]
198
+ pre = [] if d0 is None else [p0 - d0] # point before p0, to test the turn at p0
199
+ post = [] if d1 is None else [p1 + d1] # point after p1, to test the turn at p1
200
+ ok = [m for m in candidates if not _reverses(_dedupe(pre + [p0] + m + [p1] + post))]
201
+
202
+ def fits(mids, merge=True):
203
+ path = _dedupe(prefix + [p0] + mids + [p1] + (suffix or []))
204
+ return prefix is None or not corner_violations(merge_collinear(path) if merge else path, r, radii=radii)
205
+
206
+ for mids in ok:
207
+ if fits(mids):
208
+ return mids
209
+ if prefix is not None and r > 0:
210
+ # none of the one- and two-corner paths fits its bends (e.g. a port facing sideways under a hanger coupler
211
+ # that starts less than 2r beside it): detour paths with three corners, the shortest that fits. Only reached
212
+ # where the paths above all fail, so routes that built before keep their geometry.
213
+ best = None
214
+ for mids in _detours(p0, p1, r):
215
+ path = _dedupe(pre + [p0] + mids + [p1] + post)
216
+ # KQC checks each node pair: a corner closer than r to the stub's (collinear) end node does not fit either
217
+ if _reverses(path) or not fits(mids) or not fits(mids, merge=False):
218
+ continue
219
+ length = sum((q - p).length() for p, q in zip(path[:-1], path[1:]))
220
+ if best is None or length < best[0] - 1e-9:
221
+ best = (length, mids)
222
+ if best is not None:
223
+ return best[1]
224
+ return ok[0] if ok else candidates[0]
225
+
226
+
227
+ _ALIGN_TOL = math.radians(5.0) # an oblique stub-to-stub straight may turn this much at each stub end
228
+
229
+
230
+ def _turn(d_from: pya.DVector, d_to: pya.DVector) -> float:
231
+ """Angle (rad) between two travel directions."""
232
+ c = d_from.sprod(d_to) / (d_from.length() * d_to.length())
233
+ return math.acos(max(-1.0, min(1.0, c)))
234
+
235
+
236
+ def _oblique(
237
+ p0: pya.DPoint,
238
+ d0: pya.DVector | None,
239
+ p1: pya.DPoint,
240
+ d1: pya.DVector | None,
241
+ r: float,
242
+ prefix: list,
243
+ suffix: list,
244
+ radii: dict | None,
245
+ ) -> list[pya.DPoint] | None:
246
+ """Oblique alternative to the Manhattan stair between stub ends p0 and p1 (``d0`` the travel direction arriving at
247
+ p0, ``d1`` leaving p1), or None. Only for ports that face an angle (some direction is off-axis); the candidates,
248
+ first that fits its bends and does not double back wins:
249
+
250
+ 1. one straight p0 → p1 (no corner), when it leaves p0 and reaches p1 within ``_ALIGN_TOL`` of both directions:
251
+ two ports facing each other along one line (exactly, or a few µm off);
252
+ 2. one corner where the line through p0 along ``d0`` meets the line through p1 along ``d1`` (ahead of p0, behind
253
+ p1), turning by at most 90°: an angled port whose ray meets the other end's axis.
254
+ """
255
+ if (d0 is None or _is_axis(d0)) and (d1 is None or _is_axis(d1)):
256
+ return None
257
+ v = p1 - p0
258
+ if v.length() < _EPS:
259
+ return None
260
+ cands = []
261
+ ends = [d for d in (d0, d1) if d is not None]
262
+ if ends and all(_turn(d, v) <= _ALIGN_TOL for d in ends):
263
+ cands.append([])
264
+ if d0 is not None and d1 is not None:
265
+ den = d0.vprod(d1)
266
+ if abs(den) > 1e-3 and _turn(d0, d1) <= math.pi / 2 + 1e-6: # not (nearly) parallel, no hairpin
267
+ t = v.vprod(d1) / den
268
+ u = d0.vprod(v) / den
269
+ if t > _EPS and u > _EPS:
270
+ cands.append([p0 + d0 * t])
271
+ pre = [] if d0 is None else [p0 - d0]
272
+ post = [] if d1 is None else [p1 + d1]
273
+ for mids in cands:
274
+ full = _dedupe(prefix + [p0] + mids + [p1] + suffix)
275
+ if _reverses(_dedupe(pre + [p0] + mids + [p1] + post)):
276
+ continue
277
+ if not corner_violations(merge_collinear(full), r, radii=radii) and not corner_violations(full, r, radii=radii):
278
+ return mids
279
+ return None
280
+
281
+
282
+ def _detours(p0: pya.DPoint, p1: pya.DPoint, r: float) -> list[list[pya.DPoint]]:
283
+ """Three-corner Manhattan paths p0 → p1 (x, y, x or y, x, y legs) through coordinates beside both ends: their
284
+ own, the middle, and 2r / 3r beyond either end on both sides."""
285
+ offsets = (0.0, -2 * r, 2 * r, -3 * r, 3 * r)
286
+ xs = sorted({round(c + o, 6) for c in (p0.x, p1.x) for o in offsets} | {round((p0.x + p1.x) / 2, 6)})
287
+ ys = sorted({round(c + o, 6) for c in (p0.y, p1.y) for o in offsets} | {round((p0.y + p1.y) / 2, 6)})
288
+ out = []
289
+ for x in xs:
290
+ for y in ys:
291
+ out.append([pya.DPoint(x, p0.y), pya.DPoint(x, y), pya.DPoint(p1.x, y)]) # horizontal first
292
+ out.append([pya.DPoint(p0.x, y), pya.DPoint(x, y), pya.DPoint(x, p1.y)]) # vertical first
293
+ return out
294
+
295
+
296
+ def _unit(v: pya.DVector) -> pya.DVector | None:
297
+ return v / v.length() if v.length() > _EPS else None
298
+
299
+
300
+ def _enter_straight(pts_start, middle, pts_end, d_start, e_in, r, radii) -> list | None:
301
+ """``middle`` (the user's nodes) with Manhattan corners added between the start stub and the first node and / or
302
+ between the last node and the end stub (a port's, or a hanger coupler's approach), so both stubs are entered
303
+ along their axis; the first variant whose bends fit, or None."""
304
+ variants = []
305
+ if len(pts_end) > 1: # the end has a stub / approach: corners from the last node into it
306
+ p0, p1 = middle[-1], pts_end[0]
307
+ d0 = _unit(middle[-1] - middle[-2]) if len(middle) > 1 else _unit(middle[-1] - pts_start[-1])
308
+ tail = _manhattan(p0, d0, p1, e_in, r, pts_start + middle[:-1], pts_end[1:], radii)
309
+ variants.append(middle + tail)
310
+ if len(pts_start) > 1: # the start has a stub: corners from it into the first node
311
+ p0, p1 = pts_start[-1], middle[0]
312
+ d1 = _unit(middle[1] - middle[0]) if len(middle) > 1 else _unit(pts_end[0] - middle[0])
313
+ head = _manhattan(p0, d_start, p1, d1, r, pts_start[:-1], middle[1:] + pts_end, radii)
314
+ variants.append(head + middle)
315
+ if len(variants) == 2:
316
+ variants.append(head + variants[0])
317
+ for v in variants:
318
+ pts = merge_collinear(_dedupe(pts_start + v + pts_end))
319
+ if not corner_violations(pts, r, radii=radii) and not corner_violations(
320
+ _dedupe(pts_start + v + pts_end), r, radii=radii):
321
+ return v
322
+ return None
323
+
324
+
325
+ def plan_route(
326
+ name: str,
327
+ route: Route,
328
+ start: RouteEnd,
329
+ end: RouteEnd,
330
+ defaults: dict,
331
+ ) -> RoutePlan:
332
+ """Build the node list for one route. ``defaults`` supplies ``a``, ``b``, ``r`` when neither route nor port does."""
333
+ a = route.a if route.a is not None else (start.a if start.a is not None else end.a)
334
+ b = route.b if route.b is not None else (start.b if start.b is not None else end.b)
335
+ a = defaults["a"] if a is None else a
336
+ b = defaults["b"] if b is None else b
337
+ r = route.r if route.r is not None else defaults["r"]
338
+
339
+ # stub points: leave/enter ports straight for one turn radius
340
+ pts_start = [start.pos]
341
+ s0 = start.pos
342
+ if start.dir is not None:
343
+ s0 = start.pos + start.dir * r
344
+ pts_start.append(s0)
345
+ if end.approach:
346
+ pts_end = list(end.approach) + [end.pos]
347
+ e0 = pts_end[0]
348
+ v = pts_end[1] - pts_end[0]
349
+ e_in = v / v.length()
350
+ else:
351
+ pts_end = [end.pos]
352
+ e0, e_in = end.pos, None
353
+ if end.dir is not None:
354
+ e0 = end.pos + end.dir * r
355
+ pts_end.insert(0, e0)
356
+ e_in = -end.dir
357
+
358
+ waypoints = route.waypoints()
359
+ oblique = False # auto-routed with an oblique straight (an angled port)
360
+ # per-corner radii (Waypoint.r): bend-fit checks use each corner's own radius
361
+ radii = {(round(w.at[0], 6), round(w.at[1], 6)): float(w.r) for w in waypoints if w.r is not None}
362
+ if waypoints:
363
+ middle = [pya.DPoint(*w.at) for w in waypoints]
364
+ # a waypoint straight in front of a port, closer than the stub, already is that port's stub; further away
365
+ # the stub stays a separate (fixed) segment, so the rest of that straight can still host a meander
366
+ if start.dir is not None and _on_axis(start.pos, start.dir, middle[0]):
367
+ if (middle[0] - start.pos).length() <= r + _EPS:
368
+ pts_start = [start.pos]
369
+ if not end.approach and end.dir is not None and _on_axis(end.pos, end.dir, middle[-1]):
370
+ if (middle[-1] - end.pos).length() <= r + _EPS:
371
+ pts_end = [end.pos]
372
+ else:
373
+ middle = _oblique(s0, start.dir, e0, e_in, r, pts_start[:-1], pts_end[1:], radii)
374
+ oblique = middle is not None
375
+ if middle is None:
376
+ middle = _manhattan(s0, start.dir, e0, e_in, r, pts_start[:-1], pts_end[1:], radii)
377
+
378
+ raw = pts_start + middle + pts_end
379
+ points = _dedupe(raw)
380
+ if len(points) < 2:
381
+ raise RoutingError(f"route {name}: start and end coincide")
382
+ merged = merge_collinear(points)
383
+ bad = corner_violations(merged, r, radii=radii)
384
+ ends = {_key(q) for q in pts_start + pts_end}
385
+ if bad and waypoints and any(_key(merged[i - 1]) in ends or _key(merged[i]) in ends for i, _, _ in bad):
386
+ # the user's first / last node is off the axis of a port stub or a hanger coupler's approach (whose short
387
+ # straight has room for one bend only): add the corners that enter it straight. Only where the nodes as
388
+ # given do not fit, so routes that built before keep their geometry.
389
+ fixed_up = _enter_straight(pts_start, middle, pts_end, start.dir, e_in, r, radii)
390
+ if fixed_up is not None:
391
+ middle = fixed_up
392
+ raw = pts_start + middle + pts_end
393
+ points = _dedupe(raw)
394
+ merged = merge_collinear(points)
395
+ bad = corner_violations(merged, r, radii=radii)
396
+ if bad:
397
+ i, length, need = bad[0]
398
+ own = [f"{radii[_key(q)]:g}" for q in (merged[i - 1], merged[i]) if _key(q) in radii]
399
+ at_r = f"r={r:g} µm" + (f" (corner r {', '.join(own)} µm)" if own else "")
400
+ raise RoutingError(
401
+ f"route {name}: segment {i} ({merged[i - 1].x:.1f},{merged[i - 1].y:.1f})→({merged[i].x:.1f},"
402
+ f"{merged[i].y:.1f}) is {length:.1f} µm but its bends need {need:.1f} µm at {at_r}; "
403
+ "move the ports, add 'nodes' or reduce 'r'"
404
+ )
405
+ if not waypoints and not oblique:
406
+ # a port facing at an angle (ConcentricTransmon couplers, StarQubit pins) leaves along its own direction for
407
+ # its stub; the Manhattan path starts at the stub's end, the bend there takes r·tan(θ/2) (checked above)
408
+ stubs = {(_key(start.pos), _key(s0))}
409
+ if not end.approach and end.dir is not None:
410
+ stubs.add((_key(e0), _key(end.pos)))
411
+ for p, q in zip(points[:-1], points[1:]):
412
+ if not _is_axis(q - p) and (_key(p), _key(q)) not in stubs:
413
+ raise RoutingError(f"route {name}: could not auto-route Manhattan path; add 'nodes'")
414
+
415
+ # attach waypoint extras to the matching node positions
416
+ extras = {}
417
+ for w in waypoints:
418
+ key = (round(w.at[0], 6), round(w.at[1], 6))
419
+ node_params = dict(w.params)
420
+ if w.a is not None:
421
+ node_params["a"] = w.a
422
+ if w.b is not None:
423
+ node_params["b"] = w.b
424
+ if w.r is not None and abs(float(w.r) - r) > _EPS: # this corner only (route.waveguide.CORNER_R)
425
+ node_params["corner_r"] = float(w.r)
426
+ extras[key] = (w.airbridge, node_params)
427
+
428
+ nodes = []
429
+ for i, p in enumerate(points):
430
+ airbridge, node_params = extras.get((round(p.x, 6), round(p.y, 6)), (False, {}))
431
+ if route.airbridges == "auto" and i > 0:
432
+ node_params.setdefault("n_bridges", -1)
433
+ elif isinstance(route.airbridges, int) and not isinstance(route.airbridges, bool) and i > 0:
434
+ node_params.setdefault("n_bridges", route.airbridges)
435
+ element = AirbridgeConnection if airbridge else None
436
+ nodes.append(Node(p, element, **node_params))
437
+
438
+ params = {"a": a, "b": b, "r": r, **route.params}
439
+ if start.term is not None:
440
+ params["term1"] = start.term
441
+ if end.term is not None:
442
+ params["term2"] = end.term
443
+ # segments (by index of their end node) that must stay straight: port stubs and hanger couplers
444
+ fixed = set()
445
+ if start.dir is not None and len(points) > 2:
446
+ fixed.add(1)
447
+ n_end_fixed = len(end.approach) if end.approach else (1 if end.dir is not None else 0)
448
+ fixed.update(range(len(points) - n_end_fixed, len(points)))
449
+ plan = RoutePlan(name, nodes, params, route.length if route.type == "meander" else None)
450
+ plan.fixed_segments = fixed
451
+ return plan
452
+
453
+
454
+ def meander_segment_index(nodes: list[Node], preferred: int | None = None, fixed: set | None = None) -> int:
455
+ """Index of the node *ending* the segment that hosts the meander (KQC puts length_* on that node)."""
456
+ if preferred is not None:
457
+ if not 1 <= preferred < len(nodes):
458
+ raise RoutingError(f"meander_segment {preferred} out of range 1..{len(nodes) - 1}")
459
+ return preferred
460
+ candidates = [i for i in range(1, len(nodes)) if i not in (fixed or set())] or list(range(1, len(nodes)))
461
+ return max(candidates, key=lambda i: (nodes[i].position - nodes[i - 1].position).length())
462
+
463
+
464
+ def meander_span(plan: RoutePlan, segment: int) -> float:
465
+ """Length (µm) of the straight KQC lays a meander on for the segment ending at node ``segment``: the node-to-node
466
+ distance less the bend cut at each end (KQC ``Meander`` start / end points)."""
467
+ nodes = plan.nodes
468
+ r = float(plan.params.get("r", 100))
469
+ pts = [n.position for n in nodes]
470
+
471
+ def cut(i): # bend cut at interior node i (0 at the route's ends and on collinear nodes)
472
+ if i <= 0 or i >= len(pts) - 1:
473
+ return 0.0
474
+ v1, v2 = pts[i] - pts[i - 1], pts[i + 1] - pts[i]
475
+ if v1.length() < _EPS or v2.length() < _EPS:
476
+ return 0.0
477
+ turn = math.atan2(v1.x * v2.y - v1.y * v2.x, v1.x * v2.x + v1.y * v2.y)
478
+ ri = float(nodes[i].params.get("corner_r", r))
479
+ return ri * math.tan(abs(turn) / 2)
480
+
481
+ return (pts[segment] - pts[segment - 1]).length() - cut(segment - 1) - cut(segment)
482
+
483
+
484
+ def max_meanders(span: float, r: float) -> int:
485
+ """Most meander turns that stay on a straight of length ``span``: KQC puts the first bend ``r`` after the straight's
486
+ start and each turn takes 2·r along it, so ``int(span / 2r - 1)`` (the count KQC itself picks when automatic).
487
+ A larger count runs the first and last bends past the straight's ends (over whatever lies beyond the node)."""
488
+ return int(span / (2 * r) - 1 + 1e-9)
489
+
490
+
491
+ def with_length_increment(
492
+ plan: RoutePlan, increment: float, segment: int, meanders: int = -1, direction: int | None = None
493
+ ) -> RoutePlan:
494
+ """``plan`` with a meander of ``increment`` extra length on ``segment``; ``direction`` (1 / −1) picks the side of
495
+ the meander's first turn (KQC ``Node.meander_direction``). A requested number of turns that does not fit on the
496
+ straight is reduced to what fits (the meander gets wider instead, the length is the same)."""
497
+ if meanders > 0:
498
+ span, r = meander_span(plan, segment), float(plan.params.get("r", 100))
499
+ fit = max_meanders(span, r)
500
+ if 1 <= fit < meanders:
501
+ import warnings # pylint: disable=import-outside-toplevel
502
+
503
+ # say so: the meander's shape changes (fewer, taller turns) where the design asked for more
504
+ warnings.warn(f"route {plan.name}: {meanders} meander turns need {2 * r * (meanders + 1):.0f} µm of "
505
+ f"straight at r = {r:g} µm, segment {segment} has {span:.0f} µm: {fit} turns used (same "
506
+ "length, wider meander); lower 'meanders', use a longer straight or reduce 'r'",
507
+ UserWarning, stacklevel=2)
508
+ meanders = fit
509
+ nodes = []
510
+ for i, n in enumerate(plan.nodes):
511
+ if i == segment:
512
+ params = dict(n.params)
513
+ if meanders > 0:
514
+ params["meanders"] = meanders
515
+ turn = n.meander_direction if direction is None else direction
516
+ n = Node(n.position, n.element, n.inst_name, n.align, n.angle, None, increment, turn, **params)
517
+ nodes.append(n)
518
+ return RoutePlan(plan.name, nodes, dict(plan.params), plan.target_length, None, set(plan.fixed_segments))
519
+
520
+
521
+ def _kqc_bend_increment(w: float, r: float) -> float:
522
+ """Extra length of one bend of a KQC ``Meander`` of half-width ``w`` (``Meander.build``'s
523
+ ``bend_length_increment``): below ``w = r`` the bend is less than 90°."""
524
+ if w >= r:
525
+ return r * (math.pi / 2 - 2) + w
526
+ h = w / r
527
+ x = (1 - h) / (1 - h / 2)
528
+ return r * (2 * math.atan(1 - x) + (x + h * (h - 1)) / math.sqrt(x**2 + h**2) - 1)
529
+
530
+
531
+ def meander_width(increment: float, r: float, meanders: int) -> float:
532
+ """Total width (µm) of the KQC ``Meander`` that adds ``increment`` with ``meanders`` turns of radius ``r`` (the
533
+ same formulas as ``Meander.build``). At ``4·r`` and above every bend is 90°; below, KQC flattens the bends, and a
534
+ width under ``r`` is a near-straight zigzag along the whole straight."""
535
+ def added(w):
536
+ return 4 * _kqc_bend_increment(w / 4, r) + 2 * (meanders - 1) * _kqc_bend_increment(w / 2, r)
537
+
538
+ full = added(4 * r)
539
+ if increment >= full:
540
+ return 4 * r + (increment - full) / meanders
541
+ lo, hi = 0.0, 4 * r
542
+ for _ in range(60):
543
+ mid = (lo + hi) / 2
544
+ lo, hi = (mid, hi) if added(mid) < increment else (lo, mid)
545
+ return lo
546
+
547
+
548
+ def meander_turns(increment: float, span: float, r: float, meanders: int = -1) -> int:
549
+ """The turn count for a length-fixing meander of ``increment`` on a straight of ``span``: ``meanders`` as given
550
+ (``-1``: as many as fit, KQC's choice) unless that meander would be narrower than ``r`` — KQC then draws its
551
+ flattened bends along the whole straight, a slanted zigzag. Then the most turns that keep it ``r`` wide (at least
552
+ one: a single bump in the middle of the straight)."""
553
+ fit = max_meanders(span, r)
554
+ m = min(meanders, fit) if meanders > 0 else fit
555
+ if m < 1 or meander_width(increment, r, m) >= r:
556
+ return meanders
557
+ return next((k for k in range(m - 1, 0, -1) if meander_width(increment, r, k) >= r), 1)
558
+
559
+
560
+ TRIM_FRACTION = 0.05 # most a corner's bend radius may shrink to absorb a small length leftover (5 % of r)
561
+
562
+
563
+ def _free_corners(plan: RoutePlan) -> list[tuple[int, float]]:
564
+ """``(node index, dL/dr)`` of the corners whose radius may be trimmed: plain interior nodes with the route radius
565
+ (no own ``corner_r``, no element) between two segments that are not fixed (port stubs and hanger couplers keep
566
+ their bends). ``dL/dr = θ - 2·tan(θ/2)`` (< 0) for a turn of θ: a smaller radius makes the route longer."""
567
+ nodes = plan.nodes
568
+ out = []
569
+ for i in range(1, len(nodes) - 1):
570
+ n = nodes[i]
571
+ if n.element is not None or "corner_r" in n.params or i in plan.fixed_segments or i + 1 in plan.fixed_segments:
572
+ continue
573
+ v1, v2 = n.position - nodes[i - 1].position, nodes[i + 1].position - n.position
574
+ if v1.length() < _EPS or v2.length() < _EPS:
575
+ continue
576
+ theta = abs(_turn(v1, v2))
577
+ if 1e-6 < theta < math.pi - 1e-3:
578
+ out.append((i, theta - 2 * math.tan(theta / 2)))
579
+ return out
580
+
581
+
582
+ def trim_corners(plan: RoutePlan, increment: float, measure, tol: float = 0.01) -> RoutePlan | None:
583
+ """``plan`` lengthened by ``increment`` without a meander: the free corners (:func:`_free_corners`) get a slightly
584
+ smaller bend radius (node ``corner_r``, one value for all of them), so the path, its nodes and its fixed parts stay
585
+ as drawn. None where the route has no free corner, a node sets its own ``r`` (KQC then changes the radius for the
586
+ rest of the route), or the radius would shrink by more than :data:`TRIM_FRACTION`."""
587
+ r = float(plan.params.get("r", 100))
588
+ corners = _free_corners(plan)
589
+ gain = -sum(g for _, g in corners) # length gained per µm of radius taken off
590
+ if gain < 1e-9 or any("r" in n.params for n in plan.nodes):
591
+ return None
592
+ target = plan.target_length
593
+
594
+ def trial(dr):
595
+ at = {i for i, _ in corners}
596
+ nodes = [
597
+ Node(n.position, n.element, n.inst_name, n.align, n.angle, n.length_before, n.length_increment,
598
+ n.meander_direction, **{**n.params, "corner_r": r - dr}) if i in at else n
599
+ for i, n in enumerate(plan.nodes)
600
+ ]
601
+ return RoutePlan(plan.name, nodes, dict(plan.params), target, None, set(plan.fixed_segments))
602
+
603
+ # the analytic step, then corrections: KQC's arcs are polygons on the 1 nm grid, so the built length moves in
604
+ # steps of a few nm; accepted within tol or 10 ppm of the length (the meander path accepts 0.1 %)
605
+ accept = max(tol, 1e-5 * target)
606
+ dr, best = increment / gain, None
607
+ for _ in range(4):
608
+ if not 0 < dr <= TRIM_FRACTION * r:
609
+ break
610
+ out = trial(dr)
611
+ got = measure(out)
612
+ if best is None or abs(got - target) < abs(best[1] - target):
613
+ best = (out, got)
614
+ if abs(got - target) <= tol:
615
+ break
616
+ dr += (target - got) / gain
617
+ if best is None or abs(best[1] - target) > accept:
618
+ return None
619
+ best[0].length = best[1]
620
+ return best[0]
621
+
622
+
623
+ def fix_length(
624
+ plan: RoutePlan,
625
+ measure,
626
+ segment: int | None = None,
627
+ meanders: int = -1,
628
+ tol: float = 0.01,
629
+ direction: int | None = None,
630
+ ) -> RoutePlan:
631
+ """Make the built length equal ``plan.target_length``.
632
+
633
+ ``measure(plan) -> float`` must build the waveguide and return its length (µm). By the leftover ``target -
634
+ length of the path as drawn`` (2026-11-18: no slanted zigzags on a route that is already about the right
635
+ length):
636
+
637
+ * within ``tol``: the path as drawn, no meander;
638
+ * small enough: the free corners' bend radius trimmed by up to :data:`TRIM_FRACTION` (:func:`trim_corners`);
639
+ * else a KQC ``Meander`` on the longest free straight (or ``segment``), with as many turns as fit unless they
640
+ would flatten into a zigzag (:func:`meander_turns`: then fewer, wider turns).
641
+ """
642
+ target = plan.target_length
643
+ base = measure(plan)
644
+ if target < base - tol:
645
+ raise RoutingError(
646
+ f"route {plan.name}: target length {target:.1f} µm is shorter than the shortest path {base:.1f} µm"
647
+ )
648
+ if abs(target - base) <= tol:
649
+ return RoutePlan(plan.name, list(plan.nodes), dict(plan.params), target, base, set(plan.fixed_segments))
650
+ if segment is None:
651
+ trimmed = trim_corners(plan, target - base, measure, tol)
652
+ if trimmed is not None:
653
+ return trimmed
654
+ seg = meander_segment_index(plan.nodes, segment, plan.fixed_segments)
655
+
656
+ def on(seg):
657
+ r = float(plan.params.get("r", 100))
658
+ turns = meander_turns(target - base, meander_span(plan, seg), r, meanders)
659
+ if meanders > 0 and turns != meanders:
660
+ import warnings # pylint: disable=import-outside-toplevel
661
+
662
+ warnings.warn(f"route {plan.name}: {meanders} meander turns for +{target - base:.2f} µm would flatten into "
663
+ f"a zigzag narrower than r = {r:g} µm; {turns} used (same length)", UserWarning, stacklevel=3)
664
+ trial = with_length_increment(plan, target - base, seg, turns, direction)
665
+ got = measure(trial)
666
+ if abs(got - target) > tol: # one correction step absorbs meander end-bend effects
667
+ trial = with_length_increment(plan, target - base + (target - got), seg, turns, direction)
668
+ got = measure(trial)
669
+ if abs(got - target) > max(tol, 1e-3 * target):
670
+ raise RoutingError(
671
+ f"route {plan.name}: could only reach {got:.2f} µm of {target:.2f} µm (segment too short?)")
672
+ trial.length = got
673
+ return trial
674
+
675
+ try:
676
+ return on(seg)
677
+ except Exception as first: # noqa: BLE001 the meander does not fit on that straight (KQC: "too close")
678
+ if segment is not None:
679
+ raise RoutingError(
680
+ f"route {plan.name}: the meander does not fit on segment {segment} ({first}); choose a longer "
681
+ "straight (route dialog: Meander on segment, or Ctrl+Shift+M) or make this one longer") from first
682
+ # the longest free straight could not take it (a node edit made the route tight): the other free straights,
683
+ # longest first. Only reached where the build failed before.
684
+ others = sorted((i for i in range(1, len(plan.nodes)) if i != seg and i not in plan.fixed_segments),
685
+ key=lambda i: -(plan.nodes[i].position - plan.nodes[i - 1].position).length())
686
+ for i in others:
687
+ try:
688
+ return on(i)
689
+ except Exception: # noqa: BLE001
690
+ continue
691
+ turns = f", or let the number of meander turns be automatic (now {meanders})" if meanders > 0 else ""
692
+ raise RoutingError(
693
+ f"route {plan.name}: the meander fits on no straight of this route ({first}); make a straight longer "
694
+ f"(Route handles: move or insert a node) or shorten the length{turns}") from first