-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_braket.py
275 lines (178 loc) · 7.19 KB
/
test_braket.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
264
265
266
267
268
269
270
271
272
273
274
275
import operator
import tequila as tq
import numpy as np
from tequila.circuit.gates import PauliGate
from tequila.objective.braket import make_overlap, make_transition
from tequila.tools.random_generators import make_random_circuit, make_random_hamiltonian
def test_simple_overlap():
'''
Function that tests if make_overlap function is working correctly.
It creates a simple circuit in order to check that both real and imaginary
part are calculated in the right way.
Returns
-------
None.
'''
# two circuits to test
U0 = tq.gates.Rz(angle=1.0, target=1)#tq.gates.H(target=1) + tq.gates.CNOT (1 ,2)
U1 = tq.gates.Rz(angle=2, target=1)#tq.gates.X(target=[1,2])#
objective_real, objective_im = make_overlap(U0,U1)
Ex = tq.simulate(objective_real)
Ey = tq.simulate(objective_im)
exp_val = Ex + 1.0j*Ey
#print('Evaluated overlap between the two states: {}\n'.format(exp_val))
# we want the overlap of the wavefunctions
# # to test we can compute it manually
wfn0 = tq.simulate(U0)
wfn1 = tq.simulate(U1)
test = wfn0.inner(wfn1)
#print('Correct overlap between the two states: {}'.format(test))
#print('The two result are approximately the same?',np.isclose(test, exp_val, atol=1.e-4))
assert np.isclose(test, exp_val, atol=1.e-4)
return
def test_random_overlap():
'''
Function that tests if make_overlap function is working correctly.
It creates circuits with random number of qubits, random rotations and
random angles.
Returns
-------
None.
'''
# make random circuits
#np.random.seed(111)
n_qubits = np.random.randint(1, high=5)
U = {k:tq.make_random_circuit(n_qubits) for k in range(2)}
objective_real, objective_im = make_overlap(U[0],U[1])
Ex = tq.simulate(objective_real)
Ey = tq.simulate(objective_im)
exp_val = Ex + 1.0j*Ey
# we want the overlap of the wavefunctions
# # to test we can compute it manually
wfn0 = tq.simulate(U[0])
wfn1 = tq.simulate(U[1])
test = wfn0.inner(wfn1)
#print(test, '\n', exp_val)
assert np.isclose(test, exp_val, atol=1.e-4)
return
def test_simple_transition():
'''
Function that tests if make_transition function is working correctly.
It creates a simple circuit in order to check that both real and imaginary
part of the transition elementare calculated in the right way for a given
Hamiltonian.
Returns
-------
None.
'''
# two circuits to test
U0 = tq.gates.H(target=1) + tq.gates.CNOT (1 ,2)#tq.gates.Rx(angle=1.0, target=1)#
U1 = tq.gates.X(target=[1,2]) + tq.gates.Ry(angle=2, target=1)#
# defining the hamiltonian
H = tq.QubitHamiltonian("1.0*Y(0)X(1)+0.5*Y(1)Z(0)")
#print('Hamiltonian',H,'\n')
# calculating the transition element
trans_real, trans_im = make_transition(U0=U0, U1=U1, H=H)
tmp_real = tq.simulate(trans_real)
tmp_im = tq.simulate(trans_im)
trans_el = tmp_real + 1.0j*tmp_im
#print('Evaluated transition element between the two states: {}'.format( trans_el))
# # to test we can compute it manually
#print()
correct_trans_el = 0.0 + 0.0j
wfn0 = tq.simulate(U0)
for ps in H.paulistrings:
c_k = ps.coeff
U_k = PauliGate(ps)
wfn1 = tq.simulate(U1+U_k)
tmp = wfn0.inner(wfn1)
#print('contribution',c_k*tmp)
correct_trans_el += c_k*tmp
#print('Correct transition element value: {}'.format(correct_trans_el))
#print('The two result are approximately the same?',np.isclose(correct_trans_el, trans_el, atol=1.e-4))
assert np.isclose(correct_trans_el, trans_el, atol=1.e-4)
return
def test_random_transition():
'''
Function that tests if make_transition function is working correctly.
It creates circuits with random number of qubits, random rotations and
random angles and a random Hamiltonian with random number of (random)
Pauli strings.
Returns
-------
None.
'''
#np.random.seed(111)
n_qubits = np.random.randint(1, high=5)
#print(n_qubits)
U = {k:tq.make_random_circuit(n_qubits) for k in range(2)}
#print(U[0])
#print(U[1])
#make random hamiltonian
paulis = ['X','Y','Z']
n_ps = np.random.randint(1, high=2*n_qubits+1)
H = make_random_hamiltonian(n_qubits, paulis=paulis, n_ps=n_ps)
trans_real, trans_im = make_transition(U0=U[0], U1=U[1], H=H)
tmp_real = tq.simulate(trans_real)
tmp_im = tq.simulate(trans_im)
trans_el = tmp_real + 1.0j*tmp_im
correct_trans_el = 0.0 + 0.0j
wfn0 = tq.simulate(U[0])
#print()
#print(wfn0)
for ps in H.paulistrings:
c_k = ps.coeff
U_k = PauliGate(ps)
wfn1 = tq.simulate(U[1]+U_k)
tmp = wfn0.inner(wfn1)
correct_trans_el += c_k*tmp
wfn1 = tq.simulate(U[1])
# print(wfn1)
correct_trans_el_2nd = wfn0.inner(H(wfn1))
#print()
#print(correct_trans_el, '\n',trans_el, '\n', correct_trans_el_2nd)
assert np.isclose(correct_trans_el, trans_el, atol=1.e-4)
assert np.isclose(correct_trans_el, correct_trans_el_2nd, atol=1.e-4)
return
def test_braket():
"""_summary_
"""
# make random circuits
#np.random.seed(111)
n_qubits = np.random.randint(1, high=5)
U = {k:tq.make_random_circuit(n_qubits) for k in range(2)}
######## Testing self overlap #########
self_overlap = tq.braket(ket=U[0])
assert np.isclose(self_overlap, 1, atol=1.e-4)
######## Testing expectation value #########
# make random hamiltonian
paulis = ['X','Y','Z']
n_ps = np.random.randint(1, high=2*n_qubits+1)
H = make_random_hamiltonian(n_qubits, paulis=paulis, n_ps=n_ps)
exp_value_tmp = tq.ExpectationValue(H=H, U=U[0])
br_exp_value_tmp = tq.braket(ket=U[0], operator=H)
exp_value= tq.simulate(exp_value_tmp)
br_exp_value = tq.simulate(br_exp_value_tmp)
#print(exp_value, br_exp_value)
assert np.isclose(exp_value, br_exp_value, atol=1.e-4)
######## Testing overlap #########
objective_real, objective_im = make_overlap(U[0],U[1])
Ex = tq.simulate(objective_real)
Ey= tq.simulate(objective_im)
overlap = Ex + 1.0j*Ey
br_objective_real, br_objective_im = tq.braket(ket=U[0], bra=U[1])
br_Ex = tq.simulate(br_objective_real)
br_Ey = tq.simulate(br_objective_im)
br_overlap = br_Ex + 1.0j*br_Ey
assert np.isclose(br_overlap, overlap, atol=1.e-4)
######## Testing transition element #########
trans_real, trans_im = make_transition(U0=U[0], U1=U[1], H=H)
tmp_real = tq.simulate(trans_real)
tmp_im = tq.simulate(trans_im)
trans_el = tmp_real + 1.0j*tmp_im
br_trans_real, br_trans_im = tq.braket(ket=U[0], bra=U[1], operator=H)
br_tmp_real = tq.simulate(br_trans_real)
br_tmp_im = tq.simulate(br_trans_im)
br_trans_el = br_tmp_real + 1.0j*br_tmp_im
assert np.isclose(br_trans_el, trans_el, atol=1.e-4)
return