Using configuration files#
Sometimes it is useful to define the parameters for the simulations in a separate file. In this case, we suggest to use YAML configuration files, which allow for the definition of lists and dictionaries. Moreover, a custom parser is included in the GPE package to also allow for the evaluation of simple mathematical expressions.
Once a configuration file is defined and stored in the path.yaml
file, its contents can be parsed using the parse_config()
function.
1from torchgpe.utils import parse_config
2
3data = parse_config('path.yaml')
In the following, we are going to give a few examples for a quick start using configuration files.
Data types#
Lists can be defined in a single line as [first_element, second_element, third_element]
or by writing each element in a different line, as in the example below:
1- first_element
2- second_element
3- third_element
Note
The space after the -
character is mandatory.
Regardless of which one of the two notations you decide to use, the parser will output a python list.
Analogously, dictionaries can be defined in a single line as {first_key: first_element, second_key: second_element, third_key: third_element}
or by writing each element in a different line, as in the example below:
1first_key: first_element
2second_key: second_element
3third_key: third_element
Note
The space after the :
character is mandatory.
Regardless of which one of the two notations you decide to use, the parser will output a python dictionary.
Of course lists and dictionaries can be mixed. For example, a dictionary can be an element of a list and viceversa:
1first_key:
2 - a
3 - b
4 - c
5second_key: second_element
6third_key:
7 - a1
8 - b1
9 - k1: k2
10 k3: k4
Note
The indentation level is not important, as long as it is consistent. For example, both the following lists are valid:
1first_key:
2 - a
3 - b
4 - c
5second_key:
6 - a1
7 - b1
8 - c1
Conversely, the following is not a valid list:
1first_key:
2 - a
3 - b
4 - c
The example below shows how to use the parser to input the most common data types:
1a: 1 # parses to 'a' -> 1 where 'a' is a string and 1 is an integer
2b: 2.0 # parses to 'b' -> 2.0 where 'b' is a string and 2.0 is a float
3c: 1e3 # parses to 'c' -> 1000.0 where 'c' is a string and 1000.0 is a float
4d: e # parses to 'd' -> 'e' where both 'd' and 'e' are strings
5f: [1, 2.0, g] # parses to 'f' -> [1, 2.0, 'g'] where 'f' is a string and [1, 2.0, 'g'] is a list
6h: # parses to 'h' -> [3, 4.0, 'i'] where 'h' is a string and [3, 4.0, 'i'] is a list
7- 3
8- 4.0
9- i
Variables#
Yaml supports the definition of variables:
1# Definition of variables
2j: &name_of_the_variable 780e-9
3k: *name_of_the_variable
The first line defines the mapping 'j' -> 780e-9
and creates a variable name_of_the_variable
which points to the value 780e-9
. The second line defines the mapping 'k' -> 780e-9
by using the variable name_of_the_variable
.
Math expressions#
In addition to the standard yaml syntax, the parser also supports the evaluation of simple mathematical expressions via the custom !eval
tag. !eval
takes a list of 1 or 2 values, the first being a mathematical expression to be evaluated and the second a dictionary of variables to serve the script. As before, the list can be given in a single line as well as in multiple ones.
1# Evaluates 1+2 and sets l -> 3
2l: !eval
3- 1+2
4
5# Eval has access to all the standard operators (+ - * / ** // %) and the square root via sqrt
6m: !eval [ ((((4-(2+1))*10/2)//2)**2)%3+sqrt(2) ] # Evaluates the expression and sets m -> sqrt(2)+1
7
8# Eval has access to the constants pi, c, hbar
9n: !eval [ 2*pi ]
10
11# Eval can be used to define complex values
12o: !eval [ 1+2j ]
Additional variables can be given to !eval
through a dictionary in the second parameter. For example the variable name_of_the_variable
that we defined earlier:
1p: !eval
2 - 2*pi*c/wavelength
3 - wavelength: *name_of_the_variable
Warning
The expression gets evaluated by python. Reserved keywords like lambda
cannot be used in the !eval
expression
The !eval
tag has also access to the linear_ramp()
, quench()
and s_ramp()
functions to allow for the convenient definition of time dependent variables:
1pump_strength: !eval
2 - linear_ramp(0, 0, 1, 5e-3) # linear ramp from 0 to 1 in 5 ms