-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
263 lines (185 loc) · 7.6 KB
/
main.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#This is an example of the file you must have in your main git branch
import perceval as pcvl
from auto_grader import loss_function_prob_amplitudes
from scipy.optimize import minimize
import random
import perceval as pcvl
from perceval.components.unitary_components import PS, BS, PERM
from perceval.components import Circuit, Processor, PERM, BS, Port
import numpy as np
from perceval.utils import Encoding, PostSelect
from perceval.components import BS, Circuit, catalog
from scipy import optimize
def create_ccz_with_cnot_and_rx_and_hadamard(cnot="postprocessed cnot"):
QPU = pcvl.Processor("SLOS", 6)
QPU.add_port(0, Port(Encoding.DUAL_RAIL, 'ctrl0'))
QPU.add_port(2, Port(Encoding.DUAL_RAIL, 'ctrl1'))
QPU.add_port(4, Port(Encoding.DUAL_RAIL, 'data'))
QPU.add([2, 3, 4, 5], catalog[cnot].build_processor())
theta = np.pi / 4
QPU.add(4, BS.H())
QPU.add(4, BS.Rx(theta=-theta))
QPU.add(4, BS.H())
QPU.add([0, 1, 4, 5], catalog[cnot].build_processor())
QPU.add(4, BS.H())
QPU.add(4, BS.Rx(theta=theta))
QPU.add(4, BS.H())
QPU.add([2, 3, 4, 5], catalog[cnot].build_processor())
QPU.add(4, BS.H())
QPU.add(4, BS.Rx(theta=-theta))
QPU.add(4, BS.H())
QPU.add([0, 1, 4, 5], catalog[cnot].build_processor())
QPU.add(2, BS.H())
QPU.add(2, BS.Rx(theta=theta))
QPU.add(2, BS.H())
QPU.add(4, BS.H())
QPU.add(4, BS.Rx(theta=theta))
QPU.add(4, BS.H())
QPU.add([0, 1, 2, 3], catalog[cnot].build_processor())
QPU.add(0, BS.H())
QPU.add(0, BS.Rx(theta=theta))
QPU.add(0, BS.H())
QPU.add(2, BS.H())
QPU.add(2, BS.Rx(theta=-theta))
QPU.add(2, BS.H())
return QPU
#return create_ansatz()
def VQE_optimise_params():
#List of the parameters φ1,φ2,...,φ8
List_Parameters=[] # to store the parameters used in the ansatz.
# VQE is a 6 optical mode circuit
VQE=pcvl.Circuit(9) # Circuit Initialization
# add entry for mode 0
List_Parameters.append(pcvl.Parameter("φ1"))
VQE.add((1, 3),pcvl.PS(phi=List_Parameters[-1]))
List_Parameters.append(pcvl.Parameter("φ2"))
VQE.add((3, 5),pcvl.PS(phi=List_Parameters[-1]))
List_Parameters.append(pcvl.Parameter("φ3"))
VQE.add((5, ),pcvl.PS(phi=List_Parameters[-1]))
List_Parameters.append(pcvl.Parameter("φ4"))
VQE.add((5,),pcvl.PS(phi=List_Parameters[-1]))
return VQE
def create_vqe_ansatz_old():
List_Parameters = []
VQE=pcvl.Circuit(6)
VQE.add((1,2), pcvl.BS())
VQE.add((3,4), pcvl.BS())
List_Parameters.append(pcvl.Parameter("φ1"))
VQE.add((2,),pcvl.PS(phi=List_Parameters[-1]))
List_Parameters.append(pcvl.Parameter("φ3"))
VQE.add((4,),pcvl.PS(phi=List_Parameters[-1]))
VQE.add((1,2), pcvl.BS())
VQE.add((3,4), pcvl.BS())
List_Parameters.append(pcvl.Parameter("φ2"))
VQE.add((2,),pcvl.PS(phi=List_Parameters[-1]))
List_Parameters.append(pcvl.Parameter("φ4"))
VQE.add((4,),pcvl.PS(phi=List_Parameters[-1]))
# CNOT ( Post-selected with a success probability of 1/9)
VQE.add([0,1,2,3,4,5], pcvl.PERM([0,1,2,3,4,5]))#Identity PERM (permutation) for the purpose of drawing a nice circuit
VQE.add((3,4), pcvl.BS())
VQE.add([0,1,2,3,4,5], pcvl.PERM([0,1,2,3,4,5]))#Identity PERM (permutation) for the same purpose
VQE.add((0,1), pcvl.BS(pcvl.BS.r_to_theta(1/3)))
VQE.add((2,3), pcvl.BS(pcvl.BS.r_to_theta(1/3)))
VQE.add((4,5), pcvl.BS(pcvl.BS.r_to_theta(1/3)))
VQE.add([0,1,2,3,4,5], pcvl.PERM([0,1,2,3,4,5]))#Identity PERM (permutation) for the same purpose
VQE.add((3,4), pcvl.BS())
VQE.add([0,1,2,3,4,5], pcvl.PERM([0,1,2,3,4,5]))#Identity PERM (permutation) for the same purpose
List_Parameters.append(pcvl.Parameter("φ5"))
VQE.add((2,),pcvl.PS(phi=List_Parameters[-1]))
List_Parameters.append(pcvl.Parameter("φ7"))
VQE.add((4,),pcvl.PS(phi=List_Parameters[-1]))
VQE.add((1,2), pcvl.BS())
VQE.add((3,4), pcvl.BS())
List_Parameters.append(pcvl.Parameter("φ6"))
VQE.add((2,),pcvl.PS(phi=List_Parameters[-1]))
List_Parameters.append(pcvl.Parameter("φ8"))
VQE.add((4,),pcvl.PS(phi=List_Parameters[-1]))
VQE.add((1,2), pcvl.BS())
VQE.add((3,4), pcvl.BS())
return VQE
N = 9
def create_vqe_ansatz():
#List of the parameters φ1,φ2,...,φ8
List_Parameters=[] # to store the parameters used in the ansatz.
# VQE is a 6 optical mode circuit
VQE=pcvl.Processor("SLOS", N) # Circuit Initialization
VQE.add_port(0, Port(Encoding.DUAL_RAIL, 'ctrl0'))
VQE.add_port(2, Port(Encoding.DUAL_RAIL, 'ctrl1'))
VQE.add_port(4, Port(Encoding.DUAL_RAIL, 'data'))
VQE.add([0, 1], pcvl.BS())
VQE.add([2, 3], pcvl.BS())
VQE.add([4, 5], pcvl.BS())
# add Beam Splitter on mode 1, 3, 5
List_Parameters.append(pcvl.Parameter("φ1"))
VQE.add([1],pcvl.PS(phi=List_Parameters[-1]))
List_Parameters.append(pcvl.Parameter("φ2"))
VQE.add([3],pcvl.PS(phi=List_Parameters[-1]))
List_Parameters.append(pcvl.Parameter("φ3"))
VQE.add([5],pcvl.PS(phi=List_Parameters[-1]))
VQE = VQE.add_herald(6,0).add_herald(7,0).add_herald(8,0)
VQE.set_postselection(PostSelect("[0,1]==1 & [2,3]==1 & [4,5]==1"))
return VQE
def create_ansatz():
return create_vqe_ansatz()
def assign_processor_params(processor, params):
param_circuit = processor.get_circuit_parameters()
for i, (k, param) in enumerate(param_circuit.items()):
param.set_value(params[i])
def loss_function(params):
# Assign parameters to circuit
ansatz = create_ansatz()
assign_processor_params(ansatz, params)
loss = loss_function_prob_amplitudes(ansatz)
print(f"{loss=}")
return loss
def optimize_ansatz():
ansatz = create_ansatz()
param_circuit = ansatz.get_circuit_parameters()
n_params = len(param_circuit)
print(f"{n_params=}")
params_init = [random.random()*np.pi for _ in param_circuit]
methods = ["COBYLA"]
o = optimize.minimize(loss_function, params_init, method="COBYLA", options={"maxiter": 1000 })
return o.x
def get_CCZ():
ansatz = create_ansatz()
best_params = optimize_ansatz()
assign_processor_params(ansatz, best_params)
return ansatz
def get_performance_and_fidelity(ccz_processor):
processor = pcvl.Processor("SLOS", 6)
processor.add(4, pcvl.BS.H())
processor.add(0, ccz_processor)
processor.add(4, pcvl.BS.H())
states = {
pcvl.BasicState([1, 0, 1, 0, 1, 0]): "000", # |000⟩
pcvl.BasicState([1, 0, 1, 0, 0, 1]): "001", # |001⟩
pcvl.BasicState([1, 0, 0, 1, 1, 0]): "010", # |010⟩
pcvl.BasicState([1, 0, 0, 1, 0, 1]): "011", # |011⟩
pcvl.BasicState([0, 1, 1, 0, 1, 0]): "100", # |100⟩
pcvl.BasicState([0, 1, 1, 0, 0, 1]): "101", # |101⟩
pcvl.BasicState([0, 1, 0, 1, 1, 0]): "110", # |110⟩
pcvl.BasicState([0, 1, 0, 1, 0, 1]): "111", # |111⟩
}
ca = pcvl.algorithm.Analyzer(processor, states)
#pcvl.pdisplay(processor, recursive=False)
truth_table = {
"000": "000", # No change
"001": "001", # No change
"010": "010", # No change
"011": "011", # No change
"100": "100", # No change
"101": "101", # No change
"110": "111", # Target flips
"111": "110", # Target flips
}
ca.compute(expected=truth_table)
state = pcvl.BasicState("|0,1,0,1,0,1>")
sim = pcvl.SimulatorFactory().build(processor)
return ca.performance, ca.fidelity.real
optimize_ansatz()
#ccz = get_CCZ()
#performance, fidelity = get_performance_and_fidelity(ccz)
#score = score_processor()
#print(f"{performance=}, {fidelity=}")
#print(f"{score=}")