-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmult_cntl_gate.py
244 lines (169 loc) · 5.99 KB
/
mult_cntl_gate.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
#import cytnx as cy
from tools import *
import numpy as np
## study the decomp from NC 4.10
def multi_cntl_gate(state,gate,Xmax=None,trunc_err=0,is_measure=False,verbose=False):
# assuming gate operate on the last site.
# assuming state is in NN friendly form.
# assuming state tis in Lform. (v3)
# Xmax: maximum of vbond dimension
# Trunc_err: setting the truncation err
is_trunc = False
max_vbdim = 0
if Xmax is not None:
is_trunc = True
else:
Xmax = 9999999 ## some large value
if trunc_err > 0:
is_trunc = True
n = int(len(state)/2+1)
Tf = cy.qgates.toffoli();
Sout=[]
for i in range(n-2):
A=state[i*2]
B=state[i*2+1]
C=state[i*2+2]
phys_lbl = [A.labels()[1],B.labels()[1],C.labels()[1]]
phys_lbl_out = [A.labels()[1]+3,B.labels()[1]+3,C.labels()[1]+3]
Tf.set_labels(phys_lbl+phys_lbl_out)
tmp = cy.Contract(cy.Contract(cy.Contract(A,B),C),Tf)
new_lbl = [tmp.labels()[0],tmp.labels()[1]] + phys_lbl
tmp.set_labels(new_lbl)
tmp.permute_([0,2,3,4,1],rowrank=2)
#tmp.contiguous_()
## go back to PS form, or truncate:
if is_trunc:
out = cy.linalg.Svd_truncate(tmp,keepdim=Xmax,err=trunc_err)
else:
out = cy.linalg.Svd(tmp)
if out[0].shape()[0] > max_vbdim:
max_vbdim = out[0].shape()[0]
A = out[1]
tmp = cy.Contract(out[0],out[2])
tmp.set_rowrank(2)
if is_trunc:
out = cy.linalg.Svd_truncate(tmp,keepdim=Xmax,err=trunc_err)
else:
out = cy.linalg.Svd(tmp)
if out[0].shape()[0] > max_vbdim:
max_vbdim = out[0].shape()[0]
#print(out[0])
B = out[1]
C = cy.Contract(out[0],out[2])
#A.print_diagram()
#B.print_diagram()
#C.print_diagram()
## relabel:
A.set_label(2,A.labels()[0]-1)
B.set_label(0,A.labels()[2])
B.set_label(2,B.labels()[0]-1)
C.set_label(0,B.labels()[2])
A.set_rowrank(1)
B.set_rowrank(1)
C.set_rowrank(1)
#R = cy.Contract(cy.Contract(A,B),C)
state[i*2] = A
state[i*2+1]=B
state[i*2+2]=C
if is_measure:
Sv = get_entanglement(state)
Sout.append(Sv)
## cntl gate apply:
CG = cy.qgates.cntl_gate_2q(gate)
#print("CG",CG.get_block_().reshape(4,4))
phys_lbl = [state[-2].labels()[1],state[-1].labels()[1]]
phys_lbl_out = [state[-2].labels()[1]+2,state[-1].labels()[1]+2]
CG.set_labels(phys_lbl+phys_lbl_out)
tmp = cy.Contract(cy.Contract(state[-2],state[-1]),CG)
new_lbl = [tmp.labels()[0],tmp.labels()[1]] + phys_lbl
tmp.set_labels(new_lbl)
tmp.permute_([0,2,3,1],rowrank=2)
if is_trunc:
out = cy.linalg.Svd_truncate(tmp,keepdim=Xmax,err=trunc_err)
else:
out = cy.linalg.Svd(tmp)
if out[0].shape()[0] > max_vbdim:
max_vbdim = out[0].shape()[0]
state[-2] = out[1].relabel(2,out[1].labels()[0]-1)
state[-1] = cy.Contract(out[0],out[2])
state[-1].set_label(0,state[-2].labels()[2])
state[-2].set_rowrank(1)
state[-1].set_rowrank(1)
if is_measure:
Sv = get_entanglement(state)
Sout.append(Sv)
## ladder up:
for i in range(n-2):
C=state[-i*2-2]
B=state[-i*2-3]
A=state[-i*2-4]
phys_lbl = [A.labels()[1],B.labels()[1],C.labels()[1]]
phys_lbl_out = [A.labels()[1]+3,B.labels()[1]+3,C.labels()[1]+3]
Tf.set_labels(phys_lbl+phys_lbl_out)
tmp = cy.Contract(cy.Contract(cy.Contract(A,B),C),Tf)
new_lbl = [tmp.labels()[0],tmp.labels()[1]] + phys_lbl
tmp.set_labels(new_lbl)
tmp.permute_([0,2,3,4,1],rowrank=3)
## go back to PS form, or truncate:
if is_trunc:
out = cy.linalg.Svd_truncate(tmp,keepdim=Xmax,err=trunc_err)
else:
out = cy.linalg.Svd(tmp)
if out[0].shape()[0] > max_vbdim:
max_vbdim = out[0].shape()[0]
C = out[2]
#C.print_diagram()
tmp = cy.Contract(out[1],out[0])
tmp.set_rowrank(2)
if is_trunc:
out = cy.linalg.Svd_truncate(tmp,keepdim=Xmax,err=trunc_err)
else:
out = cy.linalg.Svd(tmp)
if out[0].shape()[0] > max_vbdim:
max_vbdim = out[0].shape()[0]
B = out[2]
A = cy.Contract(out[1],out[0])
## relabel:
A.set_label(2,A.labels()[0]-1)
B.set_label(0,A.labels()[2])
B.set_label(2,B.labels()[0]-1)
C.set_label(0,B.labels()[2])
A.set_rowrank(1)
B.set_rowrank(1)
C.set_rowrank(1)
state[-i*2-2]=C
state[-i*2-3]=B
state[-i*2-4]=A
if is_measure:
Sv = get_entanglement(state)
Sout.append(Sv)
if verbose:
print("[truncate? %d][multi-cntl] Maxdim = %d "%(is_trunc,max_vbdim))
if is_measure:
return state,Sout
else:
return state
if __name__=="__main__":
Nc = 3
Nv = Nc-2
N = Nc + Nv
H = cy.qgates.hadamard().get_block()
H = cy.UniTensor(H.real(),1)/np.sqrt(2)
X = cy.UniTensor(cy.physics.pauli('x').real(),1)
Z = cy.UniTensor(cy.physics.pauli('z').real(),1)
I = cy.UniTensor(cy.eye(2),1)
# * arrangement: c_i:claus bits, v_i: ancilla bits
# c 1.c2.v1.c3.v2.c4.v3.....v_{n-1}.c_n
state = productMPS([0,0,0,0])
PVF_measure_amp(state)
state = multi_Op(state,[0,0,1,0],H)
exit(1)
PVF_measure_amp(state)
state = multi_cntl_gate(state,Z)
#print_mps(state)
state2 = make_Lform(state)
#print_mps(state2)
#R = cy.Contract(cy.Contract(cy.Contract(state[0],state[1]),state[2]),state[3])
#print(R.get_block_().reshape(16))
#print(get_amp(state,[1,1,1,1]))
#PVF_measure_amp(state)