-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo_utils.py
58 lines (50 loc) · 1.65 KB
/
demo_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import dill
import numpy as np
from tqdm import tqdm
from copy import deepcopy
from functools import reduce
def dill_dump(something, filename, make_dirs=False):
# If make_folders is True, then any directories
# in the filename that dont exist will be created
# before `something` is saved to disk
if make_dirs:
dirs = filename.split('/')[:-1]
for i in range(len(dirs)):
save_dir = '/'.join(dirs[:i+1])
if not os.path.isdir(save_dir):
os.mkdir(save_dir)
with open(filename, "wb") as f:
dill.dump(something, f)
def dill_load(filename):
with open(filename, "rb") as f:
return dill.load(f)
def get_mpf_rho(mpf_statevecs, mpf_coeffs):
statevecs = [deepcopy(sv) for sv in mpf_statevecs]
mpf_rho = reduce(lambda x,y : x+y,
[sv.to_operator()*coeff
for sv, coeff
in zip(statevecs, mpf_coeffs)
]
)
return mpf_rho
def frob_distance(rho1, rho2):
return np.linalg.norm(rho1 - rho2)
def get_pbar(N, label):
"""
This method returns a progress bar.
Provide N, the number of ticks the progress bar should have
as well as a label for it.
It's on you to call pbar.update(num_ticks) to make the progress bar
track progress. And you must call pbar.close() when you're done.
Returns: pbar a tqdm progress bar object.
"""
return tqdm(
ncols=100,
total=N,
unit_scale=1,
position=0,
dynamic_ncols=True,
bar_format="{l_bar}{bar:35}{r_bar}{bar:-10b}",
desc=label,
)