iqm-finland / KQCircuits

KLayout Python library for integrated quantum circuit design.
GNU General Public License v3.0
142 stars 73 forks source link

Reload Libraries macro issues: doesn't restore PCells on error, and fails for non-KQC PCells #89

Closed qpavsmi closed 5 months ago

qpavsmi commented 6 months ago

What happened? What you expected to happen?

I place a KQC element, for example the Double Pads from the Qubit Library. Then I open klayout_package/python/kqcircuits/qubits/double_pads.py and make _build_island2 return empty pya.Region(). I go back to the same KLayout session and select KQCircuits > Reload Libraries. I get what I expected, which is that the bottom island on the qubit is not present anymore and there are no errors in File > Log Viewer.

Now I open Basic from the library panel, and place a Circle. For better demonstration I double click the Circle cell, choose PCell Parameters and change Radius to 100 microns and layer to 1t1 base metal wo grid 130/1. Now do some change in double pads code (e.g. revert change made above) and try KQCircuits > Reload Libraries. Now it gives error AttributeError: type object 'PCellDeclaration_Native' has no attribute 'get_schema'. Also the double pads qubit will disappear.

Prerequisites

KQCircuits GUI installation required

Where to start?

The Reload Libraries command is a macro at klayout_package/python/scripts/macros/0system/0reload.lym. It is an XML file with python code embedded in the <text> field. The macro code is simple:

views = extract_pcell_data_from_views()  # Collect all cells currently placed in the KLayout session, removing them in the process
load_libraries(flush=True)               # Make KLayout reload the KQC plugin code
restore_pcells_to_views(views)           # Reconstruct all cells that were collected before KQC reload

It seems that the extract_pcell_data_from_views function does not filter the cells based on whether they are introduced by the KQC library or if they are native KLayout objects or cells defined from some other library. Every cell that comes from the KQC library is a subclass of kqcircuits.elements.element.Element class, so it should be easy to filter using the isinstance python function.

The described bug happens because: the macro collects the pcells, deleting some of them in the process, then it encounters a cell that it cannot work with, throws an error, and then leaves it at that - without restoring the deleted cells. This bug should be fixed by performing the macro actions only on KQC cells, and writing a good try except block where the restore_pcells_to_views is called in case of exception.

Definition of done

Versions

Version independent issue

Issue Severity

Low: it is annoying

PietroCampana commented 5 months ago

Hi, I would like to work on this issue as part of unitaryhack 2024

qpavsmi commented 5 months ago

Hi, I would like to work on this issue as part of unitaryhack 2024

Sounds great! I encourage to make a pull request early even if the solution is not completely finalized to get early feedback.

qpavsmi commented 5 months ago

Completed by https://github.com/iqm-finland/KQCircuits/pull/95 thanks @PietroCampana