Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reload existing PCells without closing GUI #14

Closed
jkotilahti opened this issue May 11, 2022 · 2 comments · Fixed by #62
Closed

Reload existing PCells without closing GUI #14

jkotilahti opened this issue May 11, 2022 · 2 comments · Fixed by #62
Assignees
Labels
enhancement New feature or request feature A feature request submitted by the community good first issue Good for newcomers GUI

Comments

@jkotilahti
Copy link
Member

If I edit a PCell source code and want to see the changes within KLayout application, I need to:

  1. run Reload libraries macro
  2. make a new copy of a PCell

Would be nice if there was one less manual step and I would not need to make a new PCell. Instead:

  1. before reloading, the reload macro could extract the PCell information in the active layout
  2. removes the PCell instance
  3. reloads the libraries
  4. places instances of the PCells with same parameters and location

Maybe after this issue we should have the follow up features:

  1. watchdog for code changes to autorefresh such that I would not even need to run refresh macro manually
  2. partial refresh to refresh a single python module and corresponding PCell, not the full library (the changed PCell might be inside other PCells)
@jkotilahti jkotilahti added enhancement New feature or request good first issue Good for newcomers unitaryhack-bounty and removed good first issue Good for newcomers labels May 11, 2022
@jkotilahti jkotilahti added good first issue Good for newcomers and removed unitaryhack-bounty labels Jun 30, 2022
@qpavsmi
Copy link
Contributor

qpavsmi commented May 3, 2023

Prerequisites

  • Requires GUI KQCircuits installation.

Use case example

Open KLayout (in edit mode) and drag-and-drop, lets say, a Swissmon qubit into the layout. You will find the Swissmon qubit entry from bottom left Libraries panel by choosing the Qubit library - Library for qubits. from the dropdown. With the swissmon qubit placed somewhere in the layout, without closing the KLayout window, edit the swissmon.py code, for example, by decreasing iterations to the for loop that calls self._produce_coupler to have range(2). Then in KLayout window, from the top toolbar choose KQCircuits > Reload Libraries.

image

Seemingly, there is no difference on the Swissmon already placed in the layout. However, by double-clicking the Swissmon you will notice that the PCell parameters can no longer be changed. Now drag-and-drop another instance of Swissmon. Now you will see that the Swissmon placed after pressing Reload Libraries has a right-hand coupler missing, which is caused by our edit in swissmon.py code. You can also verify that editing PCell parameters for the newer Swissmon is possible.

image

This should be changed so that after pressing Reload Libraries the old instance of the Swissmon qubit would get updated according to the current state of the code. The PCell parameters should also be available for editing. This would involve removing PCells and memorizing their coordinates, classes and PCell parameters, then reloading the library and then placing the PCells back to their coordinates with PCell parameters intact (just as outlined in above comment).

How to get started

When Reload Libraries is pressed, it runs a macro 0reload.lym. Notice that macros are a python script wrapped in some xml structure. It could be more convenient to edit macros from KLayout's own macro editor (from top toolbar: Macros > Macro Development (F5)).

image
image

Right now 0reload.lym only calls load_libraries from library_helper.py which does the library reloading. This macro should be extended to include the extra features.

Implementation tips

Following are some of the methods from KLayout and KQCircuits API to help you implement this feature. We do not guarantee that the feature gets implemented using only these methods.

You will need to reconstruct every PCell placed on every open layout of a KLayout window where the Reload Libraries was pressed.

image

To iterate over each LayoutView use pya.Application.instance().main_window() to access the view method that returns a LayoutView by integer index. The number of available views is returned by the views method. Notice that pya is a built-in module in the KLayout macro environment. Then for each LayoutView object use the active_cellview to get a CellView object and then cell to access the current top cell of that view[*]. Then iterate over every instance under the top cell with each_inst.

To retrieve the location of an instance use trans method to get a Trans object. This object contains the full transformation of the instance, including position, rotation, scale and mirroring. The instance can be converted into a PCellDeclaration object using the pcell_declaration method. If the method returns None, then the instance was not a PCell and can be skipped. Use the python built-in method __class__ to access the implementing class of the PCellDeclaration, which would be Swissmon in our example use case. Then cast the PCellDeclaration object into Element and use pcell_params_by_name to retrieve PCell parameters of the instance.

After reloading the library, iterate over each LayoutView again and retrieve their "Top Cell", suppose it's under the variable top_cell. Then using insert_cell_into(top_cell, cell_class, cell_trans, **cell_parameters) insert a new instance of class cell_class with PCell parameters cell_parameters and under transformation cell_trans. Please refer to API docs of insert_cell_into in case additional information is required to re-add the PCells.

[*] Technically there could be other cells in a layout that are parallel to the "Top Cell" in cell hierarchy. This is usually not the case in actual KQCircuits workflow. In order to streamline implementing this issue it is fine to assume that each layout has a "Top Cell" cell which is always active for that layout.

Definition of done

  • Pressing Reload Libraries should reload all PCells in all open layouts of the KLayout window so that the PCells are rendered according to most up-to-date code, without the need of creating another PCell after the fact
  • No tests needed

More features

As outlined in above comment, it would be nice to have a mechanism that notices the change in the source files which would prompt KLayout to automatically perform library reload. We will leave this as a future task. You are welcome to submit an additional PR if you are interested in implementing this.

@qpavsmi qpavsmi changed the title Manual steps in code refresh Reload existing PCells without closing GUI May 4, 2023
@qpavsmi qpavsmi added GUI feature A feature request submitted by the community unitaryhack-bounty and removed unitaryhack-bounty labels May 4, 2023
@iqmtestd iqmtestd self-assigned this Aug 30, 2023
@iqmtestd
Copy link
Contributor

iqmtestd commented Sep 14, 2023

It is a bit undefined what should happen if a default parameter value also changes in the code. We could either use the instance's parameter value or the new default value. In this case it is probably better to stick with the instance's value. Unless the instance was using the default parameter value then after reload it could use the new default value.

iqmtestd added a commit that referenced this issue Sep 22, 2023
The "KQCircuits -> Reload libraries" command apart from reloading the
libraries also saves and re-creates all PCells in every view using
the updated code but retaining all the parameter changes of the user.

Fix #14
iqmtestd added a commit that referenced this issue Sep 25, 2023
The "KQCircuits -> Reload libraries" command apart from reloading the
libraries also saves and re-creates all PCells in every view using
the updated code but retaining all the parameter changes of the user.

Fix #14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature A feature request submitted by the community good first issue Good for newcomers GUI
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants