iqm-finland / KQCircuits

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

Validate and report misconfigured simulation export #91

Closed qpavsmi closed 3 weeks ago

qpavsmi commented 2 months ago

Description

When running a simulation script, we need to define two sets of parameters: one that guides the shape of the geometry, and the other that guides the execution of the third-party simulator, such as Elmer or Ansys. Certain parameter settings of those two parameter sets can't work together. These should be validated and reported before the simulation export work is done.

Use case

When I write a simulation export script in klayout_package/python/scripts/simulations I might not notice or I might not know that I have defined simulation and solution parameters in such a way that they can't work together. Instead of spending too much time debugging the problem after the simulation work is done, I'd like the KQC simulation exporter to tell me that the parameters don't make sense and for what reason.

Prerequisites

KQCircuits standalone installation required, GUI installation prevents an error which in practice could be circumvented.

Get started

See for example the following block of code from klayout_package/python/scripts/simulations/swissmon_fluxline_sim.py

# Sweep simulation and solution type
simulations = [
    (
        sim_class(layout, **sim_parameters, name="fluxline_mut_ind", flux_simulation=True),
        AnsysCurrentSolution(
            max_delta_e=0.01,
            frequency=0.1,
            maximum_passes=20,
            integrate_magnetic_flux=True,
            mesh_size={"squid": 2, "vacuumrefine": 4, "substraterefine": 4},
        ),
    ),
    (
        sim_class(layout, **sim_parameters, name="fluxline_decay", flux_simulation=False),
        AnsysHfssSolution(max_delta_s=0.005, frequency=4.5, maximum_passes=20, sweep_enabled=False),
    ),
]

The simulations list here consists of tuples, for which the first object is of class Simulation containing the geometry data, and the other object is a dataclass object containing parameters that need to be fed to the simulator software, Ansys in this case.

simulations list gets fed to the export_ansys function (there is also export_elmer equivalent). This would be a perfect place to perform validations. Before any of the current function code is implemented, there could be a set of manually defined validations to be performed that, if at least one of them fails, the function halts and prints a user-friendly error.

These validation checks might be Ansys and Elmer specific, but let's still define a single function for validations that both export_elmer and export_ansys will call, so that validations can be maintained in one place. The main focus of this exercise is to enable infrastructure so that additional validations could be quite easily added without being too familiar with details of simulation export code.

Definition of done

Concrete validation scenarios to start with

rmoretti9 commented 3 weeks ago

I addressed this issue by creating a ValidateSim class in \kqcircuits\simulations\export\simulation_validate.py that implements the checks mentioned in the issue description. The methods of this class are called every time export_ansys or export_elmer are used, for example in the "_sim.py" scripts in klayout_package\python\scripts\simulations.

Unit tests are in tests\simulations\simulation_validate\test_simulation_validate.py

qpavsmi commented 3 weeks ago

Thank you for your contribution and for participating in unitaryhack 2024!