-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrawer.py
206 lines (191 loc) · 7.14 KB
/
drawer.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'''
This file is used to draw the results from the GA
author : Yu-Cheng Chung
email : ycchung@ntnu.edu.tw
date : 2023 08 Sep
dependencies:
gene.py
GA.py
numpy
matplotlib
results structure:
result = [[fidelity, depth, theta], [fidelity, depth, theta], ...]
genes structure:
genes = [[gene], [gene], ...]
'''
import numpy as np
import matplotlib.pyplot as plt
from gene import Gene_Circuit
from GA import get_prob_distribution
import os
def draw_prob_distribution(gene : list,
theta : list|np.ndarray,
num_qubit : int,
target_distribution : list|np.ndarray,
filename:str,
method : str = 'qasm') -> None:
'''
Draw the probability distribution of a circuit and the target distribution
Args:
gene: a list of 0-10
theta: a list of theta
num_qubit: number of qubits
target_distribution: the target distribution
filename: the filename of the picture
method: the method to get the probability distribution
Returns:
None
'''
circuit = Gene_Circuit(gene, num_qubit).circuit
prob_distribution = get_prob_distribution(circuit, theta,method=method)
plt.clf()
plt.bar(range(2**num_qubit), prob_distribution)
plt.plot(np.arange(2**num_qubit), target_distribution, label='target_distribution')
plt.ylim(0,max(max(prob_distribution),max(target_distribution))*1.1)
plt.savefig(filename)
plt.close()
def load_results_from_file(filename:str)->np.ndarray:
'''
Load the results from a file
Args:
filename: the filename of the file
Returns:
result: the result
'''
result = np.load(filename, allow_pickle=True)
return result
def load_genes_from_file(filename:str)->np.ndarray:
'''
Load the genes from a file
Args:
filename: the filename of the file
Returns:
gene: the gene
'''
gene = np.load(filename)
return gene
#{generation_number: [gene, gene, gene, ...]}
def draw_gene_circuit_from_result(path : str ,
expriement : str,
num_qubit : int,
generation_number : int)->None:
'''
Draw the circuit of the smallest depth gene for each generation
Args:
path: the path of the file
expriement: the name of the expriement
num_qubit: number of qubits
generation_number: number of generation
Returns:
None
'''
genes = {}
for i in range(generation_number) :
filename = f'{path}/{expriement}/{i}st_generation/10_smallest_depth_gene.npy'
gene = load_genes_from_file(filename)
genes[i] = gene
os.makedirs(f'{path}/{expriement}/smallest_circuit',exist_ok=True)
for i in range(generation_number):
Gene_Circuit(genes[i][0], num_qubit).draw(output='mpl', filename=f'{path}/{expriement}/smallest_circuit/generation_{i}_smallest_depth_gene.png')
plt.close()
def draw_prob_distribution_from_result(path : str,
expriement : str,
target_distribution : list|np.ndarray,
num_qubit :int,
generation_number : int)->None:
'''
Draw the probability distribution of the smallest depth gene for each generation
Args:
path: the path of the file
expriement: the name of the expriement
target_distribution: the target distribution
num_qubit: number of qubits
generation_number: number of generation
Returns:
None
'''
genes = {}
for i in range(generation_number) :
filename = f'{path}/{expriement}/{i}st_generation/10_smallest_depth_gene.npy'
gene = load_genes_from_file(filename)
genes[i] = gene
os.makedirs(f'{path}/{expriement}/smallest_distribution',exist_ok=True)
results = {}
for i in range(generation_number) :
filename = f'{path}/{expriement}/{i}st_generation/10_smallest_depth_result.npy'
result = load_results_from_file(filename)
results[i] = result
# print(results)
for i in range(generation_number):
if i == 1:
continue
theta = results[i][0,2]
gene = genes[i][0]
draw_prob_distribution(gene, theta, num_qubit, target_distribution,
f'{path}/{expriement}/smallest_distribution/generation_{i}_smallest_depth_gene_prob_distribution.png')
def draw_fidelity_change_from_result(path : str,
expriement : str,
generation_number : int)->None:
'''
Draw the fidelity change with generation
Args:
path: the path of the file
expriement: the name of the expriement
generation_number: number of generation
Returns:
None
'''
results = {}
for i in range(generation_number) :
filename = f'{path}/{expriement}/{i}st_generation/10_smallest_depth_result.npy'
result = load_results_from_file(filename)
results[i] = result
fidelity_change = []
for i in range(generation_number):
fidelity_change.append(sum(results[i][:,0])/len(results[i][:,0]))
plt.clf()
plt.plot(range(generation_number), fidelity_change)
plt.savefig(f'{path}/{expriement}/fidelity_change.png')
plt.close()
def draw_depth_change_from_result(path : str,
expriement : str,
generation_number : int,
save_path : str = None)->None:
'''
Draw the depth change with generation
Args:
path: the path of the file
expriement: the name of the expriement
generation_number: number of generation
save_path: the path to save the picture
Returns:
None
'''
results = {}
for i in range(generation_number) :
filename = f'{path}/{expriement}/{i}st_generation/10_smallest_depth_result.npy'
result = load_results_from_file(filename)
results[i] = result
depth_change = []
for i in range(generation_number):
depth_change.append(sum(results[i][:,1])/len(results[i][:,1]))
plt.clf()
plt.plot(range(generation_number), depth_change)
if save_path is None:
plt.savefig(f'{path}/{expriement}/depth_change.png')
else:
plt.savefig(save_path)
plt.close()
if __name__ == '__main__':
path = 'w-state-2'
expriement = 'w-state-n=5'
num_qubit = 5
generation_number = 100
from experiment_w_state import w_state
from transform import statevector2prob
target_distribution = statevector2prob(w_state(num_qubit))
# target_distribution = np.array([0.7, 0.7,0,0,0,0,0,0])
draw_depth_change_from_result(path, expriement, generation_number)
# draw_fidelity_change_from_result(path, expriement, generation_number)
draw_prob_distribution_from_result(path, expriement, target_distribution, num_qubit, generation_number)
draw_gene_circuit_from_result(path, expriement, num_qubit, generation_number)