forked from miles0428/GA-Quantum-State-Prepare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_w_state.py
76 lines (69 loc) · 1.77 KB
/
experiment_w_state.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
'''
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/w-state/'
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
def w_state(n):
"""
Generate the w state with n qubits.
------------------------
args:
n: the number of qubits
return:
the w state
------------------------
example:
w_state(3) = 1/sqrt(3)(|100>+|010>+|001>)
"""
state = np.zeros(2**n)
for i in range(n):
state[2**i] = 1
state = normalize_state_vector(state)
if __name__ == '__main__':
target_statevector = w_state(num_qubit)
experiment = f'w-state/{num_qubit}'
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)