-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexperiment_gaussian.py
72 lines (66 loc) · 2 KB
/
experiment_gaussian.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
'''
this file is used to run experiments on the GA algorithm for the gaussian distribution
author : Yu-Cheng Chung
email : ycchung@ntnu.edu.tw
date : 2023 13 Sep
dependencies:
GA.py
gene.py
gaussian.py
transform.py
numpy
os
multiprocessing
'''
import numpy as np
from GA import GA
from gaussian import gaussian
from transform import normalize_prob_distribution
from transform import normalize_state_vector
import multiprocessing as mp
from qiskit_algorithms import optimizers
mp.set_start_method('spawn',True)
#set the parameters
num_genes = mp.cpu_count()
num_qubit = 5
length_gene = 70
mutation_rate = 0.1
cpu_count = mp.cpu_count()//2
path = 'data/gaussian/'
optimizer = optimizers.SPSA(maxiter=1500)
optimizer2 = optimizers.COBYLA(maxiter=1500)
maxiter = 100
miniter = 10
threshold = 0.6
cpu_count = mp.cpu_count()//2
GPU = False
#set the target distribution
#generate 15 mu from 0 to 15
mu = np.linspace(0,31,8)
#generate 15 sigma from 0 to 15
sigma = np.linspace(1,20,8)
#generate the target distribution
#use mu and sigma to generate 15*15 target distribution
if __name__ == '__main__':
for i in range(2,4):
for j in range(0,8,1):
target_distribution=gaussian(np.arange(2**num_qubit),mu[i],sigma[j])
target_distribution=normalize_prob_distribution(target_distribution)
target_statevector=normalize_state_vector(np.sqrt(target_distribution))
#set the experiment name as 'gaussian_mu_{mu}_sigma_{sigma}'
experiment = 'gaussian_mu_{:.2f}_sigma_{:.2f}'.format(mu[i],sigma[j])
#do the experiment
GA(target_statevector=target_statevector,
num_qubit=num_qubit,
num_genes=num_genes,
length_gene=length_gene,
mutation_rate=mutation_rate,
cpu_count=cpu_count,
path=path,
optimizer=optimizer,
optimizer2 = optimizer2,
maxiter=maxiter,
miniter=miniter,
threshold=threshold,
experiment=experiment,
GPU=GPU)