-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
134 lines (119 loc) · 3.94 KB
/
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
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
import math
import paddle
from paddle import reshape, argmax, argsort, real, concat, divide, matmul
import paddle_quantum
from paddle_quantum.linalg import NKron
from datetime import datetime
from paddle_quantum.ansatz import Circuit
def liner_map(b, j, psi):
"""
Input:
b, j, psi: elem-parameters from MW-enaglement
Output:
Reuturn liner map for MW-enaglement measure
"""
newpsi = []
num_qubits = math.ceil(math.log2(psi.size) )
for i in range(psi.size):
delta_i2bin = ((i>>(num_qubits-1-j))&1) ^ b ^ 1
if(delta_i2bin):
newpsi.append(psi[i])
return concat(newpsi)
def gener_distance(u, v):
"""
Input:
u, v: parameters from MW-enaglement
Output:
Returns the general distance between u and v for MW-enaglement measure
"""
uvmat = NKron(u, v) - NKron(v, u)
return 1/2 * paddle.norm(paddle.abs(uvmat))**2
def ent_state(psi):
"""
Input
psi: target quantum state
Output:
Returns the MW-enaglement measure of psi
"""
num_qubits = math.ceil(math.log2(psi.size) )
res = 0.0
psi_np = psi.numpy()
for j in range(num_qubits):
res += gener_distance(liner_map(0, j, psi), liner_map(1, j, psi))
return res * 4 / num_qubits
def entQ(now_gimg_state_in, now_gimg_state_out, k):
ent_psi_statein = ent_state(now_gimg_state_in[0,0])
ent_psi_stateout = ent_state(now_gimg_state_out[0,0])
return ent_psi_stateout - k * ent_psi_statein, ent_psi_statein, ent_psi_stateout
def anti_encoding(qimg):
"""
Input:
qimg: the image of quantum state
Output:
Return the classic image
"""
img_real = real(qimg)
dim = int(math.sqrt(len(img_real)))
div_root = img_real[argmax(img_real)]
img_anti = divide(reshape(img_real,[dim, dim]), div_root)
return img_anti, img_real[argmax(img_real)]
def anti_encoding2828(qimg):
img_real = real(qimg)
dim = 28
div_root = img_real[argmax(img_real)]
img_anti = divide(reshape(img_real[:784],[dim, dim]), div_root)
return img_anti, img_real[argmax(img_real)]
def get_signature():
now = datetime.now()
past = datetime(2023, 2, 23, 0, 0, 0, 0)
timespan = now - past
time_sig = int(timespan.total_seconds() * 1000)
return str(time_sig)
def DLFuzz2(now_outputs, ori_outputs, w):
"""
Input:
now_outputs, ori_outputs: now and origin outputs by QNN, w: weight to anti
Output:
Return the decision boundrary orientation
Decision boundary orientration from DLFuzz for 2 classfication
"""
loss1 = now_outputs[0, argmax(ori_outputs[0])]
loss2 = now_outputs[0, argsort(ori_outputs[0])[-2]]
return w * loss2 - loss1
def FGSM(loss, alhpa):
"""
Input:
loss, alhpa: parameters form FGSM
Output:
Return the cost orientation
Cost orientation from FGSM
"""
return alhpa * loss
def DLFuzz3(now_outputs, ori_outputs, w):
"""
Input:
now_outputs, ori_outputs: now and origin outputs by QNN, w: weight to anti
Output:
Return the decision boundrary orientation
Decision boundary orientration from DLFuzz for 10 classfication
"""
args_ori = argsort(ori_outputs[0])
loss1 = now_outputs[0, argmax(ori_outputs[0])]
loss2 = now_outputs[0, args_ori[-2]]
loss3 = now_outputs[0, args_ori[-3]]
return w * (loss2 + loss3) - loss1
def corherence_noise_channel(input_state, sigma=0.01):
"""
Input:
input_state, sigma: std
Output:
Return the unitary of random perturbation operator
Add a random perturbation operator on input_state with std=sigma, mean=0
"""
num_qubits = math.ceil(math.log2(input_state.size) )
Noisecir = Circuit(num_qubits)
for i in range(num_qubits):
Noisecir.u3(qubits_idx=i)
Noisecir.randomize_param(arg0=0, arg1=sigma*math.pi, initializer_type='Normal')
Utheta = Noisecir.unitary_matrix()
return matmul(input_state, Utheta)