-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimize_initial_state_nonpure.py
219 lines (201 loc) · 10.2 KB
/
optimize_initial_state_nonpure.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
207
208
209
210
211
212
213
214
215
216
217
218
import math
import numpy as np
import copy
from qiskit.quantum_info.operators.operator import Operator
from typing import List
from quantum_state_nonpure import QuantumStateNonPure
from povm import Povm
from quantum_noise import QuantumNoise, RZNoise
from utility import Utility
from input_output import Default
from equation_generator import EquationGenerator
class OptimizeInitialStateNonpure(QuantumStateNonPure):
'''A non pure quantum state that has optimization capabilities
cannot do heuristic search for non pure quantum states
only make guess on initial state
'''
def __init__(self, num_sensor: int):
super().__init__(num_sensor=num_sensor, density_matrix=None)
self._optimze_method = ''
def __str__(self):
s = f'\n{self.optimize_method}\nInitial state:\n'
parent = super().__str__()
return s + parent
@property
def optimize_method(self):
return self._optimze_method
def theorem(self, unitary_operator: Operator, unitary_theta: float) -> Povm:
'''implementing the theorem (corollary + conjecture)
return the POVM using the optimal initial state
'''
self._optimze_method = 'Theorem'
e_vals, e_vectors = np.linalg.eig(unitary_operator._data)
theta1 = Utility.get_theta(e_vals[0].real, e_vals[0].imag)
theta2 = Utility.get_theta(e_vals[1].real, e_vals[1].imag)
v1 = e_vectors[:, 0] # v1 is positive
v2 = e_vectors[:, 1] # v2 is negative
if theta1 < theta2:
v1, v2, = v2, v1
eg = EquationGenerator(self.num_sensor)
RAD = 180 / np.pi
T = 0.5 * np.arccos(-(1 - 1/math.ceil(self.num_sensor/2)))
T *= RAD
if T - Default.EPSILON <= unitary_theta <= 180 - T + Default.EPSILON: # mutual orthogonal situation (corollary)
a, b, c, partition = eg.optimal_solution_nomerge()
coeff1 = np.sqrt(1 / (c - a*np.cos(2*unitary_theta/RAD) - b)) # for the symmetric partition, no merging
coeff2squared = (-a*np.cos(2*unitary_theta/RAD) - b) / (c - a*np.cos(2*unitary_theta/RAD) - b) # for partition 0, no merging
coeff2squared = 0 if coeff2squared < 0 else coeff2squared
coeff2 = np.sqrt(coeff2squared)
states = []
for ev in partition:
e_vector = Utility.eigenvector(v1, v2, ev)
states.append(coeff1 * e_vector)
for ev in ['0'*self.num_sensor]:
e_vector = Utility.eigenvector(v1, v2, ev)
states.append(coeff2 * e_vector)
state_vector = np.sum(states, axis=0)
self.density_matrix = np.outer(state_vector, np.conj(state_vector))
else: # non mutual orthogonal situation (conjecture)
# partition = eg.optimal_solution_smallerT_i(unitary_theta, partition_i)
partition = eg.optimal_solution_smallerT()
coeff = np.sqrt(1/len(partition))
states = []
for ev in partition:
e_vector = Utility.eigenvector(v1, v2, ev)
states.append(coeff * e_vector)
state_vector = np.sum(states, axis=0)
self.density_matrix = np.outer(state_vector, np.conj(state_vector))
if self.check_matrix() is False:
raise Exception('Oops! Not a valid quantum state')
def get_povm_nonoise(self, unitary_operator: Operator, priors: List[float], eval_metric: str) -> Povm:
'''return the povm given the initial state and without noise
Args:
unitary_operator -- unitary operator that describes the evolution
priors -- prior probabilities
eval_metrix -- 'min error'
'''
init_state = QuantumStateNonPure(self.num_sensor, self.density_matrix)
quantum_states = []
for i in range(self.num_sensor):
evolve_operator = Utility.evolve_operator(unitary_operator, self.num_sensor, i)
init_state_copy = copy.deepcopy(init_state)
init_state_copy.evolve(evolve_operator)
quantum_states.append(init_state_copy)
povm = Povm()
if eval_metric == 'min error':
povm.semidefinite_programming_minerror(quantum_states, priors, debug=False)
else:
raise Exception(f'unknown eval_metric: {eval_metric}')
return povm
def get_povm_noise(self, unitary_operator: Operator, priors: List[float], eval_metric: str, quantum_noise: QuantumNoise) -> Povm:
'''return the povm computed on the final states evolved from the noisy initial state
Args:
unitary_operator -- unitary operator that describes the evolution
priors -- prior probabilities
eval_metrix -- 'min error'
depolarising_noise -- the noise
'''
init_state = QuantumStateNonPure(self.num_sensor, self.density_matrix)
init_state.apply_quantum_noise(quantum_noise) # first apply quantum noise
quantum_states = []
for i in range(self.num_sensor):
evolve_operator = Utility.evolve_operator(unitary_operator, self.num_sensor, i)
init_state_copy = copy.deepcopy(init_state)
init_state_copy.evolve(evolve_operator)
quantum_states.append(init_state_copy)
povm = Povm()
if eval_metric == 'min error':
povm.semidefinite_programming_minerror(quantum_states, priors, debug=False)
else:
raise Exception(f'unknown eval_metric: {eval_metric}')
return povm
def ghz(self, unitary_operator: Operator):
'''GHZ state in the basis composed of U's eigen vectors
'''
self._optimized_method = 'GHZ'
e_vals, e_vectors = np.linalg.eig(unitary_operator._data)
theta1 = Utility.get_theta(e_vals[0].real, e_vals[0].imag)
theta2 = Utility.get_theta(e_vals[1].real, e_vals[1].imag)
v1 = e_vectors[:, 0] # v1 is positive
v2 = e_vectors[:, 1] # v2 is negative
if theta1 < theta2:
v1, v2, = v2, v1
coeff = np.sqrt(1/2)
states = []
for ev in ['0'*self.num_sensor, '1'*self.num_sensor]:
e_vector = Utility.eigenvector(v1, v2, ev)
states.append(coeff * e_vector)
state_vector = np.sum(states, axis=0)
self.density_matrix = np.outer(state_vector, np.conj(state_vector))
if self.check_matrix() is False:
raise Exception('Oops! Not a valid quantum state')
def non_entangle(self, unitary_operator: Operator):
'''Non entangled uniform superposition state in the basis composed of U's eigen vectors
'''
self._optimized_method = 'Non entangle'
e_vals, e_vectors = np.linalg.eig(unitary_operator._data)
theta1 = Utility.get_theta(e_vals[0].real, e_vals[0].imag)
theta2 = Utility.get_theta(e_vals[1].real, e_vals[1].imag)
v1 = e_vectors[:, 0] # v1 is positive
v2 = e_vectors[:, 1] # v2 is negative
if theta1 < theta2:
v1, v2, = v2, v1
N = 2 ** self.num_sensor
coeff = np.sqrt(1/N)
states = []
for i in range(N):
ev = bin(i)[2:]
if len(ev) < self.num_sensor:
ev = '0'*(self.num_sensor - len(ev)) + ev
e_vector = Utility.eigenvector(v1, v2, ev)
states.append(coeff * e_vector)
state_vector = np.sum(states, axis=0)
self.density_matrix = np.outer(state_vector, np.conj(state_vector))
if self.check_matrix() is False:
raise Exception('Oops! Not a valid quantum state')
def evaluate_noise(self, unitary_operator: Operator, priors: List[float], povm: Povm, quantum_noise: QuantumNoise, repeat: int) -> float:
'''do simulation by appling the (not-considering noise) POVM on the set of final states that considered noise
Args:
unitary_operator -- unitary operator that describes the evolution
priors -- prior probabilities
eval_metrix -- 'min error'
povm -- the povm
quantum_noise -- the quantum noise
repeat -- # of repetation of single shot measurement
Return:
probability of error
'''
init_state = QuantumStateNonPure(self.num_sensor, self.density_matrix)
# if isinstance(quantum_noise, RZNoise):
# quantum_noise.kruas_mean_std()
init_state.apply_quantum_noise(quantum_noise) # first apply quantum noise
quantum_states = []
for i in range(self.num_sensor):
evolve_operator = Utility.evolve_operator(unitary_operator, self.num_sensor, i)
init_state_copy = copy.deepcopy(init_state)
init_state_copy.evolve(evolve_operator)
quantum_states.append(init_state_copy)
return povm.simulate(quantum_states, priors, repeat=repeat)
def evaluate_noise_shortcut(self, unitary_operator: Operator, priors: List[float], povm: Povm, quantum_noise: QuantumNoise) -> float:
'''do simulation by appling the (not-considering noise) POVM on the set of final states that considered noise
Args:
unitary_operator -- unitary operator that describes the evolution
priors -- prior probabilities
eval_metrix -- 'min error'
povm -- the povm
quantum_noise -- the quantum noise
repeat -- # of repetation of single shot measurement
Return:
probability of error
'''
init_state = QuantumStateNonPure(self.num_sensor, self.density_matrix)
# if isinstance(quantum_noise, RZNoise):
# quantum_noise.kruas_mean_std()
init_state.apply_quantum_noise(quantum_noise) # first apply quantum noise
quantum_states = []
for i in range(self.num_sensor):
evolve_operator = Utility.evolve_operator(unitary_operator, self.num_sensor, i)
init_state_copy = copy.deepcopy(init_state)
init_state_copy.evolve(evolve_operator)
quantum_states.append(init_state_copy)
return povm.simulate_shortcut(quantum_states, priors)