pqcprep.file_tools

Collection of functions relating to file handling.

  1"""
  2Collection of functions relating to file handling. 
  3"""
  4
  5import os 
  6from fractions import Fraction
  7
  8def compress_args(n,m,L, seed, epochs,func_str,loss_str,meta,nint, mint, phase_reduce, train_superpos, real, repeat_params, WILL_p, WILL_q, delta):
  9    """
 10    Compress a set of variables used for training the function evaluation network into a dictionary . 
 11
 12    Arguments:
 13    ---- 
 14
 15    Arguments are the same as those passed to `.training_tools.train_QNN()`. See the description there.
 16
 17
 18    Returns:
 19    ---
 20
 21    - **arg_dict** : *dict* 
 22
 23        Dictionary containing variable values 
 24
 25    """
 26    arg_dict = {}
 27    arg_dict["n"]=n 
 28    arg_dict["m"]=m
 29    arg_dict["L"]=L
 30    arg_dict["seed"]=seed
 31    arg_dict["epochs"]=epochs 
 32    arg_dict["func_str"]=func_str
 33    arg_dict["loss_str"]=loss_str
 34    arg_dict["meta"]=meta
 35    arg_dict["nint"]=nint
 36    arg_dict["mint"]=mint
 37    arg_dict["phase_reduce"]=phase_reduce
 38    arg_dict["train_superpos"]=train_superpos
 39    arg_dict["real"]=real
 40    arg_dict["repeat_params"]=repeat_params
 41    arg_dict["WILL_p"]=WILL_q
 42    arg_dict["WILL_q"]=WILL_p
 43    arg_dict["delta"]=delta
 44
 45    return arg_dict
 46
 47def compress_args_ampl(n,L,x_min,x_max,seed, epochs,func_str,loss_str,meta, nint, repeat_params):
 48    """
 49    Compress a set of variables used for training the amplitude encoding network into a dictionary . 
 50
 51    Arguments:
 52    ---- 
 53
 54    Arguments are the same as those passed to `.training_tools.ampl_train_QNN()`. See the description there. 
 55
 56
 57    Returns:
 58    ----
 59
 60    - **arg_dict** : *dict* 
 61
 62        Dictionary containing variable values 
 63
 64    """
 65    arg_dict = {}
 66    arg_dict["n"]=n 
 67    arg_dict["L"]=L
 68    arg_dict["seed"]=seed
 69    arg_dict["epochs"]=epochs 
 70    arg_dict["func_str"]=func_str
 71    arg_dict["loss_str"]=loss_str
 72    arg_dict["meta"]=meta
 73    arg_dict["nint"]=nint
 74    arg_dict["x_min"]=x_min
 75    arg_dict["x_max"]=x_max
 76    arg_dict["repeat_params"]=repeat_params
 77    
 78    return arg_dict
 79
 80def vars_to_name_str(arg_dict):
 81    """
 82    Generate a name string from a set of variables used for training the function evaluation network.
 83
 84    Arguments:
 85    ---- 
 86
 87    - **arg_dict** : *dict* 
 88
 89        Dictionary created from variable values using `compress_args()`. 
 90
 91
 92    Returns:
 93    ---
 94    - **name_str** : *str* 
 95
 96        Name string in the appropriate file naming convention. 
 97    
 98    """
 99    # set precision information 
100    if arg_dict["nint"]==None or arg_dict["nint"]==arg_dict["n"]:
101        arg_dict["nis"]=""
102    else:
103        arg_dict["nis"]=f'({arg_dict["nint"]})'
104    if arg_dict["mint"]==None or arg_dict["mint"]==arg_dict["m"]:
105        arg_dict["mis"]=""
106    else:
107        arg_dict["mis"]=f'({arg_dict["mint"]})' 
108
109    # add to meta string
110    if arg_dict["train_superpos"] and '(S)' not in arg_dict["meta"]:
111        arg_dict["meta"]+='(S)'
112    if arg_dict["phase_reduce"] and '(PR)' not in arg_dict["meta"]:
113        arg_dict["mis"]="(0)"
114        arg_dict["meta"]+='(PR)' 
115    if arg_dict["phase_reduce"]:
116        arg_dict["mis"]="(0)"    
117    if arg_dict["real"] and '(r)' not in arg_dict["meta"]:
118        arg_dict["meta"]+='(r)' 
119    if arg_dict["repeat_params"] != None and f'({arg_dict["repeat_params"]})' not in arg_dict["meta"]:
120        arg_dict["meta"]+=f'({arg_dict["repeat_params"]})' 
121
122    # set WILL information   
123    if arg_dict["loss_str"] !="WILL" and '--' in arg_dict["meta"]:
124        raise ValueError("The sequence '--' is reserved and may not appear in the meta string.")    
125
126    if arg_dict["loss_str"]=="WILL" and '--' not in arg_dict["meta"]:
127            arg_dict["meta"] +=f'_{Fraction(arg_dict["WILL_p"]).numerator}--{Fraction(arg_dict["WILL_p"]).denominator}_{Fraction(arg_dict["WILL_q"]).numerator}--{Fraction(arg_dict["WILL_q"]).denominator}'
128
129    # set name string 
130    name_str = f'_{arg_dict["n"]}{arg_dict["nis"]}_{arg_dict["m"]}{arg_dict["mis"]}_{arg_dict["L"]}_{arg_dict["epochs"]}_{arg_dict["func_str"]}_{arg_dict["loss_str"]}_{arg_dict["delta"]}_{arg_dict["meta"]}_{arg_dict["seed"]}' 
131
132    return name_str       
133
134def vars_to_name_str_ampl(arg_dict):
135    """
136    Generate a name string from a set of variable used for training the amplitude encoding network.
137
138    Arguments:
139    ---- 
140    - **arg_dict** : *dict* 
141
142        Dictionary created from variable values using `compress_args_ampl()`. 
143
144
145    Returns:
146    ----
147    - **name_str** : *str* 
148
149        Name string in the appropriate file naming convention. 
150    
151    """
152    # set precision information 
153    if arg_dict["nint"]==None or  arg_dict["nint"]== arg_dict["n"]:
154        arg_dict["nis"]=""
155    else:
156        arg_dict["nis"]=f'({arg_dict["nint"]})'
157     
158    # add to meta string
159    if  arg_dict["repeat_params"] and '(RP)' not in arg_dict["meta"]:
160        arg_dict["meta"]+='(RP)' 
161
162    # set name string 
163    name_str = f'_{arg_dict["n"]}{arg_dict["nis"]}_{arg_dict["L"]}_{arg_dict["epochs"]}_{arg_dict["func_str"]}_{arg_dict["loss_str"]}_{arg_dict["x_min"]}_{arg_dict["x_max"]}_{arg_dict["meta"]}_{arg_dict["seed"]}'
164
165    return name_str 
166
167def check_duplicates(arg_dict,DIR, ampl=False):
168    """
169    For a given set of input parameters, check if training and testing results already exist. 
170
171    Arguments:
172    ---- 
173
174    - **arg_dict** : *dict* 
175
176        Dictionary created from variable values using `compress_args()` or `compress_args_ampl()`. 
177    
178    - **DIR** : *str* 
179
180        Parent directory for output files.       
181
182    - **ampl** : *boolean* 
183
184        If True, check if results exist for the amplitude-only network. Default is False     
185        
186
187    Returns:
188    ---
189    - **exist** : *boolean* 
190
191        Returns True if results already exists and False otherwise. 
192    
193    """
194    if ampl:
195        name=vars_to_name_str_ampl(arg_dict)
196        out_dir="ampl_outputs"
197        labels=["mismatch", "loss", "weights", "statevec"]   
198    else:                 
199        name=vars_to_name_str(arg_dict)
200        out_dir="outputs"
201        labels=["mismatch", "loss", "weights", "grad", "vargrad"]  
202
203    count=0 
204
205    for i in range(len(labels)):
206        if os.path.isfile(os.path.join(DIR,out_dir, f"{labels[i]}{name}.npy")):
207            count +=1
208
209    return count==len(labels)
210    
211def check_temp(arg_dict,DIR, ampl=False): 
212    """
213    For a given set of input parameters, check if temp files already exist. 
214
215    Arguments:
216    ---- 
217
218    - **arg_dict** : *dict* 
219
220        Dictionary created from variable values using `compress_args()` or `compress_args_ampl()`. 
221
222    - **DIR** : *str* 
223
224        Parent directory for output files.       
225
226    - **ampl** : *boolean* 
227
228        If True, check if results exist for the amplitude-only network. Default is False   
229
230
231    Returns:
232    ---
233    - **exist** : *boolean* 
234
235        Returns True if results already exist and False otherwise. 
236    
237    """  
238    
239    # set precision strings 
240    if ampl:
241        name=vars_to_name_str_ampl(arg_dict)
242        out_dir="ampl_outputs"
243        labels=["mismatch", "loss", "weights", "statevec"]   
244    else:                 
245        name=vars_to_name_str(arg_dict)
246        out_dir="outputs"
247        labels=["mismatch", "loss", "weights", "grad", "vargrad"]  
248
249    count=0 
250
251    for k in range(100,arg_dict["epochs"], step=100):    
252        for i in range(len(labels)):
253            if os.path.isfile(os.path.join(DIR,out_dir, f"__TEMP{k}_{labels[i]}{name}.npy")):
254                count +=1
255
256    return count==len(labels)
257
258
259def check_plots(arg_dict,DIR):  
260    """
261    For a given set of input parameters, check if plots already exist (excluding compare plots). 
262
263    Arguments:
264    ---- 
265
266    - **arg_dict** : *dict* 
267
268        Dictionary created from variable values using `compress_args()` or `compress_args_ampl()`. 
269
270    - **DIR** : *str* 
271
272        Parent directory for output files.          
273    
274    Returns:
275    ---
276    - **exist** : *boolean* 
277
278        Returns True if results already exist and False otherwise. 
279    
280    """
281    name=vars_to_name_str(arg_dict)  
282    out_dir="plots"          
283    labels=["mismatch", "loss", "bar_mismatch"]
284
285    count=0 
286
287    for i in range(len(labels)):
288        if os.path.isfile(os.path.join(DIR, out_dir, f"{labels[i]}{name}.npy")):
289            count +=1
290
291    return count==len(labels)
def compress_args( n, m, L, seed, epochs, func_str, loss_str, meta, nint, mint, phase_reduce, train_superpos, real, repeat_params, WILL_p, WILL_q, delta):
 9def compress_args(n,m,L, seed, epochs,func_str,loss_str,meta,nint, mint, phase_reduce, train_superpos, real, repeat_params, WILL_p, WILL_q, delta):
10    """
11    Compress a set of variables used for training the function evaluation network into a dictionary . 
12
13    Arguments:
14    ---- 
15
16    Arguments are the same as those passed to `.training_tools.train_QNN()`. See the description there.
17
18
19    Returns:
20    ---
21
22    - **arg_dict** : *dict* 
23
24        Dictionary containing variable values 
25
26    """
27    arg_dict = {}
28    arg_dict["n"]=n 
29    arg_dict["m"]=m
30    arg_dict["L"]=L
31    arg_dict["seed"]=seed
32    arg_dict["epochs"]=epochs 
33    arg_dict["func_str"]=func_str
34    arg_dict["loss_str"]=loss_str
35    arg_dict["meta"]=meta
36    arg_dict["nint"]=nint
37    arg_dict["mint"]=mint
38    arg_dict["phase_reduce"]=phase_reduce
39    arg_dict["train_superpos"]=train_superpos
40    arg_dict["real"]=real
41    arg_dict["repeat_params"]=repeat_params
42    arg_dict["WILL_p"]=WILL_q
43    arg_dict["WILL_q"]=WILL_p
44    arg_dict["delta"]=delta
45
46    return arg_dict

Compress a set of variables used for training the function evaluation network into a dictionary .

Arguments:

Arguments are the same as those passed to pqcprep.training_tools.train_QNN(). See the description there.

Returns:

  • arg_dict : dict

    Dictionary containing variable values

def compress_args_ampl( n, L, x_min, x_max, seed, epochs, func_str, loss_str, meta, nint, repeat_params):
48def compress_args_ampl(n,L,x_min,x_max,seed, epochs,func_str,loss_str,meta, nint, repeat_params):
49    """
50    Compress a set of variables used for training the amplitude encoding network into a dictionary . 
51
52    Arguments:
53    ---- 
54
55    Arguments are the same as those passed to `.training_tools.ampl_train_QNN()`. See the description there. 
56
57
58    Returns:
59    ----
60
61    - **arg_dict** : *dict* 
62
63        Dictionary containing variable values 
64
65    """
66    arg_dict = {}
67    arg_dict["n"]=n 
68    arg_dict["L"]=L
69    arg_dict["seed"]=seed
70    arg_dict["epochs"]=epochs 
71    arg_dict["func_str"]=func_str
72    arg_dict["loss_str"]=loss_str
73    arg_dict["meta"]=meta
74    arg_dict["nint"]=nint
75    arg_dict["x_min"]=x_min
76    arg_dict["x_max"]=x_max
77    arg_dict["repeat_params"]=repeat_params
78    
79    return arg_dict

Compress a set of variables used for training the amplitude encoding network into a dictionary .

Arguments:

Arguments are the same as those passed to pqcprep.training_tools.ampl_train_QNN(). See the description there.

Returns:

  • arg_dict : dict

    Dictionary containing variable values

def vars_to_name_str(arg_dict):
 81def vars_to_name_str(arg_dict):
 82    """
 83    Generate a name string from a set of variables used for training the function evaluation network.
 84
 85    Arguments:
 86    ---- 
 87
 88    - **arg_dict** : *dict* 
 89
 90        Dictionary created from variable values using `compress_args()`. 
 91
 92
 93    Returns:
 94    ---
 95    - **name_str** : *str* 
 96
 97        Name string in the appropriate file naming convention. 
 98    
 99    """
100    # set precision information 
101    if arg_dict["nint"]==None or arg_dict["nint"]==arg_dict["n"]:
102        arg_dict["nis"]=""
103    else:
104        arg_dict["nis"]=f'({arg_dict["nint"]})'
105    if arg_dict["mint"]==None or arg_dict["mint"]==arg_dict["m"]:
106        arg_dict["mis"]=""
107    else:
108        arg_dict["mis"]=f'({arg_dict["mint"]})' 
109
110    # add to meta string
111    if arg_dict["train_superpos"] and '(S)' not in arg_dict["meta"]:
112        arg_dict["meta"]+='(S)'
113    if arg_dict["phase_reduce"] and '(PR)' not in arg_dict["meta"]:
114        arg_dict["mis"]="(0)"
115        arg_dict["meta"]+='(PR)' 
116    if arg_dict["phase_reduce"]:
117        arg_dict["mis"]="(0)"    
118    if arg_dict["real"] and '(r)' not in arg_dict["meta"]:
119        arg_dict["meta"]+='(r)' 
120    if arg_dict["repeat_params"] != None and f'({arg_dict["repeat_params"]})' not in arg_dict["meta"]:
121        arg_dict["meta"]+=f'({arg_dict["repeat_params"]})' 
122
123    # set WILL information   
124    if arg_dict["loss_str"] !="WILL" and '--' in arg_dict["meta"]:
125        raise ValueError("The sequence '--' is reserved and may not appear in the meta string.")    
126
127    if arg_dict["loss_str"]=="WILL" and '--' not in arg_dict["meta"]:
128            arg_dict["meta"] +=f'_{Fraction(arg_dict["WILL_p"]).numerator}--{Fraction(arg_dict["WILL_p"]).denominator}_{Fraction(arg_dict["WILL_q"]).numerator}--{Fraction(arg_dict["WILL_q"]).denominator}'
129
130    # set name string 
131    name_str = f'_{arg_dict["n"]}{arg_dict["nis"]}_{arg_dict["m"]}{arg_dict["mis"]}_{arg_dict["L"]}_{arg_dict["epochs"]}_{arg_dict["func_str"]}_{arg_dict["loss_str"]}_{arg_dict["delta"]}_{arg_dict["meta"]}_{arg_dict["seed"]}' 
132
133    return name_str       

Generate a name string from a set of variables used for training the function evaluation network.

Arguments:

  • arg_dict : dict

    Dictionary created from variable values using compress_args().

Returns:

  • name_str : str

    Name string in the appropriate file naming convention.

def vars_to_name_str_ampl(arg_dict):
135def vars_to_name_str_ampl(arg_dict):
136    """
137    Generate a name string from a set of variable used for training the amplitude encoding network.
138
139    Arguments:
140    ---- 
141    - **arg_dict** : *dict* 
142
143        Dictionary created from variable values using `compress_args_ampl()`. 
144
145
146    Returns:
147    ----
148    - **name_str** : *str* 
149
150        Name string in the appropriate file naming convention. 
151    
152    """
153    # set precision information 
154    if arg_dict["nint"]==None or  arg_dict["nint"]== arg_dict["n"]:
155        arg_dict["nis"]=""
156    else:
157        arg_dict["nis"]=f'({arg_dict["nint"]})'
158     
159    # add to meta string
160    if  arg_dict["repeat_params"] and '(RP)' not in arg_dict["meta"]:
161        arg_dict["meta"]+='(RP)' 
162
163    # set name string 
164    name_str = f'_{arg_dict["n"]}{arg_dict["nis"]}_{arg_dict["L"]}_{arg_dict["epochs"]}_{arg_dict["func_str"]}_{arg_dict["loss_str"]}_{arg_dict["x_min"]}_{arg_dict["x_max"]}_{arg_dict["meta"]}_{arg_dict["seed"]}'
165
166    return name_str 

Generate a name string from a set of variable used for training the amplitude encoding network.

Arguments:

Returns:

  • name_str : str

    Name string in the appropriate file naming convention.

def check_duplicates(arg_dict, DIR, ampl=False):
168def check_duplicates(arg_dict,DIR, ampl=False):
169    """
170    For a given set of input parameters, check if training and testing results already exist. 
171
172    Arguments:
173    ---- 
174
175    - **arg_dict** : *dict* 
176
177        Dictionary created from variable values using `compress_args()` or `compress_args_ampl()`. 
178    
179    - **DIR** : *str* 
180
181        Parent directory for output files.       
182
183    - **ampl** : *boolean* 
184
185        If True, check if results exist for the amplitude-only network. Default is False     
186        
187
188    Returns:
189    ---
190    - **exist** : *boolean* 
191
192        Returns True if results already exists and False otherwise. 
193    
194    """
195    if ampl:
196        name=vars_to_name_str_ampl(arg_dict)
197        out_dir="ampl_outputs"
198        labels=["mismatch", "loss", "weights", "statevec"]   
199    else:                 
200        name=vars_to_name_str(arg_dict)
201        out_dir="outputs"
202        labels=["mismatch", "loss", "weights", "grad", "vargrad"]  
203
204    count=0 
205
206    for i in range(len(labels)):
207        if os.path.isfile(os.path.join(DIR,out_dir, f"{labels[i]}{name}.npy")):
208            count +=1
209
210    return count==len(labels)

For a given set of input parameters, check if training and testing results already exist.

Arguments:

  • arg_dict : dict

    Dictionary created from variable values using compress_args() or compress_args_ampl().

  • DIR : str

    Parent directory for output files.

  • ampl : boolean

    If True, check if results exist for the amplitude-only network. Default is False

Returns:

  • exist : boolean

    Returns True if results already exists and False otherwise.

def check_temp(arg_dict, DIR, ampl=False):
212def check_temp(arg_dict,DIR, ampl=False): 
213    """
214    For a given set of input parameters, check if temp files already exist. 
215
216    Arguments:
217    ---- 
218
219    - **arg_dict** : *dict* 
220
221        Dictionary created from variable values using `compress_args()` or `compress_args_ampl()`. 
222
223    - **DIR** : *str* 
224
225        Parent directory for output files.       
226
227    - **ampl** : *boolean* 
228
229        If True, check if results exist for the amplitude-only network. Default is False   
230
231
232    Returns:
233    ---
234    - **exist** : *boolean* 
235
236        Returns True if results already exist and False otherwise. 
237    
238    """  
239    
240    # set precision strings 
241    if ampl:
242        name=vars_to_name_str_ampl(arg_dict)
243        out_dir="ampl_outputs"
244        labels=["mismatch", "loss", "weights", "statevec"]   
245    else:                 
246        name=vars_to_name_str(arg_dict)
247        out_dir="outputs"
248        labels=["mismatch", "loss", "weights", "grad", "vargrad"]  
249
250    count=0 
251
252    for k in range(100,arg_dict["epochs"], step=100):    
253        for i in range(len(labels)):
254            if os.path.isfile(os.path.join(DIR,out_dir, f"__TEMP{k}_{labels[i]}{name}.npy")):
255                count +=1
256
257    return count==len(labels)

For a given set of input parameters, check if temp files already exist.

Arguments:

  • arg_dict : dict

    Dictionary created from variable values using compress_args() or compress_args_ampl().

  • DIR : str

    Parent directory for output files.

  • ampl : boolean

    If True, check if results exist for the amplitude-only network. Default is False

Returns:

  • exist : boolean

    Returns True if results already exist and False otherwise.

def check_plots(arg_dict, DIR):
260def check_plots(arg_dict,DIR):  
261    """
262    For a given set of input parameters, check if plots already exist (excluding compare plots). 
263
264    Arguments:
265    ---- 
266
267    - **arg_dict** : *dict* 
268
269        Dictionary created from variable values using `compress_args()` or `compress_args_ampl()`. 
270
271    - **DIR** : *str* 
272
273        Parent directory for output files.          
274    
275    Returns:
276    ---
277    - **exist** : *boolean* 
278
279        Returns True if results already exist and False otherwise. 
280    
281    """
282    name=vars_to_name_str(arg_dict)  
283    out_dir="plots"          
284    labels=["mismatch", "loss", "bar_mismatch"]
285
286    count=0 
287
288    for i in range(len(labels)):
289        if os.path.isfile(os.path.join(DIR, out_dir, f"{labels[i]}{name}.npy")):
290            count +=1
291
292    return count==len(labels)

For a given set of input parameters, check if plots already exist (excluding compare plots).

Arguments:

Returns:

  • exist : boolean

    Returns True if results already exist and False otherwise.