qubiter.PlaceholderManager module¶
-
class
qubiter.PlaceholderManager.
PlaceholderManager
(no_vars=False, eval_all_vars=True, var_num_to_rads=None, fun_name_to_fun=None)[source]¶ Bases:
object
The word “placeholder” (as used here and in Tensorflow) refers to a variable whose evaluation is postponed until a later time. In this class, we consider gate angle variables-placeholders.
Legal variable names are described in the docstring of the function is_legal_var_name() in this class.
An object of this class is owned by SEO_reader and subclasses thereof. When a circuit is read, the methods owned by this class are called to try to replace the placeholders by values. This class owns a dictionary var_num_to_rads that is used to store values of all angle variables used in all placeholders. The class also owns a dictionary fun_name_to_fun that is used to store functions used in “functional” type placeholders. Together, these 2 dictionaries are used to find a float value (“resolve”) each placeholder.
Variables: - all_fun_names (list[str]) – a list of all the distinct function names encountered in circuit
- all_var_nums (list[int]) – a list of all distinct variable numbers encountered in circuit
- eval_all_vars (bool) – will abort if this is True and a variable can’t be evaluated
- fun_name_to_fun (dict[str, function]) – dictionary mapping a function name to a function
- fun_name_to_hist (dict[str, list[function|None]]) – dictionary mapping a function name to its history (a list of function or None). This is only used when using loops in the English file. See LoopFileGenerator and LoopyPlaceholder classes for more info about this variable.
- fun_name_to_use_count (dict[str, int]) – dictionary mapping a function name to its current use count (the number of times it has been used. Repeat appearances inside a loop or nested loops are considered as different uses.)
- no_vars (bool) – will abort if this is True and a variable is detected
- var_num_to_hist (dict[int, list[float|None]]) – dictionary mapping a variable number to its history (a list of float or None). This is only used when using loops in the English file. See LoopFileGenerator and LoopyPlaceholder classes for more info about this variable.
- var_num_to_rads (dict[int, float]) – dictionary mapping a variable number to its value in radians
- var_num_to_use_count (dict[int, int]) – dictionary mapping a variable number to its current use count (the number of times it has been used. Repeat appearances inside a loop or nested loops are considered as different uses.)
-
__init__
(no_vars=False, eval_all_vars=True, var_num_to_rads=None, fun_name_to_fun=None)[source]¶ Constructor
Parameters: - no_vars (bool) –
- eval_all_vars (bool) –
- var_num_to_rads (dict[int, float]) – a dict mapping variable numbers to a float for radians. Used by a SEO_reader or its children to evaluate hash prefixed angle variables in a placeholder.
- fun_name_to_fun (dict[str, function]) – a dictionary mapping each function name to a function that returns a float that stands for radians. Used by a SEO_reader or its children to evaluate function names in a functional placeholder.
-
degs_str_to_rads
(degs_str, line_count)[source]¶ This method takes in a string degs_str which might represent either a str() of a float expressing degrees or a legal var name.
If degs_str is not a legal_var_name, the method assumes (This assumption is okay because the file was written by SEO_writer so it is always well formed) it’s a str() of a float and returns float( deg_str)*pi/180.
If degs_str is a legal variable name, the method tries to resolve it into a rads_value using the 2 dictionaries: self.var_num_to_rads and self.fun_name_to_fun. If it finds a rads_value, the method returns rads_value. If no rads_value can be found, the method outputs the input string unchanged (unless eval_all_vars=True in which case the method aborts).
Parameters: - degs_str (str) –
- line_count (int) – this is the line_count in the English file being read. This is not used by this function but is used by overrides of it like the one in LoopyPlaceholderManager
Returns: Return type: float | str
-
static
get_leg_var_fun_name
(var_name)[source]¶ Assumes var_name is a legal variable name (doesn’t check). Returns the function name if there is one or None otherwise.
Parameters: var_name (str) – Returns: Return type: str | None
-
static
get_leg_var_scale_fac
(var_name)[source]¶ Assumes var_name is a legal variable name (doesn’t check). If it’s a functional variable, it returns None. If it’s not a functional variable, it returns the scale factor, the float after the *, if there is a * or a 1 if there is no *
Parameters: var_name (str) – Returns: Return type: float | None
-
static
get_leg_var_sign
(var_name)[source]¶ Assumes var_name is a legal variable name (doesn’t check). Returns sign of variable, either +1 or -1
Parameters: var_name (str) – Returns: Return type: int
-
static
get_leg_var_var_nums
(var_name)[source]¶ Assumes var_name is a legal variable name (doesn’t check). Returns a list of the variable numbers
Parameters: var_name (str) – Returns: Return type: list[int]
-
static
have_resolved_history
(hist)[source]¶ This function tries to resolve a history. It returns True iff it succeeds.
A history of a variable is a list of all the values it will assume all the times it is used in the circuit (each repetition in a loop or nested loops is counted as a different use.) For a hash placeholder, a history is a list of floats or None’s. For a functional placeholder, a history is a list of functions or None’s.
Resolving a history successfully means replacing all None’s by a value that is not a None. This is accomplished by moving in order of ascending index on the list and replacing each None by its predecessor. The very first use cannot be a None.
Parameters: hist (list[float | function | None]) – Returns: Return type: bool
-
static
is_functional_var
(var_name)[source]¶ Assumes var_name is a legal variable name (doesn’t check). Returns True iff var_name is a functional variable name
Parameters: var_name (str) – Returns: Return type: bool
-
static
is_legal_var_name
(name)[source]¶ This method returns True iff name is a legal variable name.
Legal variable names must be of form #3 or -#3 or #3*.5 or -#3*.5 where 3 can be replaced by any non-negative int, and .5 can be replaced by anything that can be an argument of float() without throwing an exception. In this example, the 3 that follows the hash character is called the variable number
NEW! (functional placeholder variables)
Now legal variable names can ALSO be of the form my_fun#1#2 or -my_fun#1#2, where
- the 1 and 2 can be replaced by any non-negative integers and there
might be any number > 0 of hash variables. Thus, there need not always be precisely 2 hash variables as in the example.
- my_fun can be replaced by the name of any function with one or
more input floats (2 inputs in the example), as long as the first character of the function’s name is a lower case letter.
The strings my_fun#1#2 or -my_fun#1#2 indicate than one wants to use for the angle being replaced, the values of my_fun(#1, #2) or -my_fun(#1, #2), respectively, where the inputs #1 and #2 are floats standing for radians and the output is also a float standing for radians.
Parameters: name (str) – Returns: Return type: bool