-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_qpe.py
executable file
·365 lines (269 loc) · 12.3 KB
/
utils_qpe.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# general imports
import math
import pickle
from collections import Counter
from datetime import datetime
# general imports
import numpy as np
import math
import matplotlib.pyplot as plt
# magic word for producing visualizations in notebook
import numpy as np
# AWS imports: Import Braket SDK modules
from braket.circuits import Circuit, circuit
# local imports
from utils_qft import inverse_qft
def get_number_precision_qubits (accuracy, error_prob):
"""it returns the number of precision qubits for a given precision and accepted probability of error
P(|phi_est - phi_best| < 2^-q) <= e
inputs : accuracy (int: q) accuracy required to the estimation protocol (.
error prob (float: e): maximum error accepted in the estimation protocol
"""
assert type(accuracy == int)
n_precision_qubits = accuracy + math.ceil(np.log2(2+1/(2*error_prob)))
return n_precision_qubits
@circuit.subroutine(register=True)
def controlled_unitary(control, target_qubits, unitary):
"""
Construct a circuit object corresponding to the controlled unitary
Args:
control: The qubit on which to control the gate
target_qubits: List of qubits on which the unitary U acts
unitary: matrix representation of the unitary we wish to implement in a controlled way
"""
# Define projectors onto the computational basis
p0 = np.array([[1.0, 0.0], [0.0, 0.0]])
p1 = np.array([[0.0, 0.0], [0.0, 1.0]])
# Instantiate circuit object
circ = Circuit()
# Construct numpy matrix
id_matrix = np.eye(len(unitary))
controlled_matrix = np.kron(p0, id_matrix) + np.kron(p1, unitary)
# Set all target qubits
targets = [control] + target_qubits
# Add controlled unitary
circ.unitary(matrix=controlled_matrix, targets=targets)
return circ
@circuit.subroutine(register=True)
def qpe(precision_qubits, query_qubits, unitary, control_unitary=True):
"""
Function to implement the QPE algorithm using two registers for precision (read-out) and query.
Register qubits need not be contiguous.
Args:
precision_qubits: list of qubits defining the precision register
query_qubits: list of qubits defining the query register
unitary: Matrix representation of the unitary whose eigenvalues we wish to estimate
control_unitary: Optional boolean flag for controlled unitaries,
with C-(U^{2^k}) by default (default is True),
or C-U controlled-unitary (2**power) times
"""
qpe_circ = Circuit()
# Get number of qubits
num_precision_qubits = len(precision_qubits)
num_query_qubits = len(query_qubits)
# Apply Hadamard across precision register
qpe_circ.h(precision_qubits)
# Apply controlled unitaries. Start with the last precision_qubit, and end with the first
for ii, qubit in enumerate(reversed(precision_qubits)):
# Set power exponent for unitary
power = ii
#se ho passato un circuito applico quello
if callable(unitary):
unitary_circ = unitary
assert isinstance(unitary_circ(qubit,query_qubits),Circuit)
for _ in range(2 ** power):
qpe_circ.add_circuit(unitary_circ(qubit,query_qubits))
# Alterantive 1: Implement C-(U^{2^k})
elif control_unitary:
# Define the matrix U^{2^k}
Uexp = np.linalg.matrix_power(unitary, 2 ** power)
# Apply the controlled unitary C-(U^{2^k})
qpe_circ.controlled_unitary(qubit, query_qubits, Uexp)
# Alterantive 2: One can instead apply controlled-unitary (2**power) times to get C-U^{2^power}
else:
for _ in range(2 ** power):
qpe_circ.controlled_unitary(qubit, query_qubits, unitary)
# Apply inverse qft to the precision_qubits
qpe_circ.inverse_qft(precision_qubits,swaps=True)
return qpe_circ
# helper function to remove query bits from bitstrings
def substring(key, precision_qubits):
"""
Helper function to get substring from keys for dedicated string positions as given by precision_qubits.
This function is necessary to allow for arbitrary qubit mappings in the precision and query registers
(i.e., so that the register qubits need not be contiguous.)
Args:
key: string from which we want to extract the substring supported only on the precision qubits
precision_qubits: List of qubits corresponding to precision_qubits.
Currently assumed to be a list of integers corresponding to the indices of the qubits.
"""
short_key = ""
for idx in precision_qubits:
short_key = short_key + key[idx]
return short_key
# helper function to convert binary fractional to decimal
# reference: https://www.geeksforgeeks.org/convert-binary-fraction-decimal/
def binaryToDecimal(binary):
"""
Helper function to convert binary string (example: '01001') to decimal
Args:
binary: string which to convert to decimal fraction
"""
length = len(binary)
fracDecimal = 0
# Convert fractional part of binary to decimal equivalent
twos = 2
for ii in range(length):
fracDecimal += (ord(binary[ii]) - ord("0")) / twos
twos *= 2.0
# return fractional part
return fracDecimal
# helper function for postprocessing based on measurement shots
def get_qpe_phases(measurement_counts, precision_qubits, items_to_keep=1):
"""
Get QPE phase estimate from measurement_counts for given number of precision qubits
Args:
measurement_counts: measurement results from a device run
precision_qubits: List of qubits corresponding to precision_qubits.
Currently assumed to be a list of integers corresponding to the indices of the qubits.
items_to_keep: number of items to return (topmost measurement counts for precision register)
"""
# Aggregate the results (i.e., ignore/trace out the query register qubits):
# First get bitstrings with corresponding counts for precision qubits only
bitstrings_precision_register = [
substring(key, precision_qubits) for key in measurement_counts.keys()
]
# Then keep only the unique strings
bitstrings_precision_register_set = set(bitstrings_precision_register)
# Cast as a list for later use
bitstrings_precision_register_list = list(bitstrings_precision_register_set)
# Now create a new dict to collect measurement results on the precision_qubits.
# Keys are given by the measurement count substrings on the register qubits. Initialize the counts to zero.
precision_results_dic = {key: 0 for key in bitstrings_precision_register_list}
# Loop over all measurement outcomes
for key in measurement_counts.keys():
# Save the measurement count for this outcome
counts = measurement_counts[key]
# Generate the corresponding shortened key (supported only on the precision_qubits register)
count_key = substring(key, precision_qubits)
# Add these measurement counts to the corresponding key in our new dict
precision_results_dic[count_key] += counts
# Get topmost values only
c = Counter(precision_results_dic)
topmost = c.most_common(items_to_keep)
# get decimal phases from bitstrings for topmost bitstrings
phases_decimal = [binaryToDecimal(item[0]) for item in topmost]
# Get decimal phases from bitstrings for all bitstrings
# number_precision_qubits = len(precision_qubits)
# Generate binary decimal expansion
# phases_decimal = [int(key, 2)/(2**number_precision_qubits) for key in precision_results_dic]
# phases_decimal = [binaryToDecimal(key) for key in precision_results_dic]
return phases_decimal, precision_results_dic
def run_qpe(
unitary,
precision_qubits,
query_qubits,
query_circuit,
device,
items_to_keep=1,
shots=100,
save_to_pck=False,
debug=False):
"""
Function to run QPE algorithm end-to-end and return measurement counts.
Args:
precision_qubits: list of qubits defining the precision register
query_qubits: list of qubits defining the query register
unitary: Matrix representation of the unitary whose eigenvalues we wish to estimate, IT CAN BE ALSO A SUBROUTINE OF A CIRCUIT, IN ORDER TO RUN ON QPUS!!!
query_circuit: query circuit for state preparation of query register
items_to_keep: (optional) number of items to return (topmost measurement counts for precision register)
device: Braket device backend
shots: (optional) number of measurement shots (default is 1000)
save_to_pck: (optional) save results to pickle file if True (default is False)
"""
# get size of precision register and total number of qubits
number_precision_qubits = len(precision_qubits)
num_qubits = len(precision_qubits) + len(query_qubits)
# Define the circuit. Start by copying the query_circuit, then add the QPE:
circ = query_circuit
circ.qpe(precision_qubits, query_qubits, unitary)
# Add desired results_types
circ.probability()
if debug:
print(circ)
# Run the circuit with all zeros input.
# The query_circuit subcircuit generates the desired input from all zeros.
task = device.run(circ, shots=shots)
return get_and_analyze_result(task,num_qubits,precision_qubits,circ,items_to_keep,save_to_pck)
def get_and_analyze_result(task,num_qubits,precision_qubits,circ=None,items_to_keep=1,save_to_pck = False):
# get result for this task
result = task.result()
# get metadata
metadata = result.task_metadata
# get output probabilities (see result_types above)
probs_values = result.values[0]
# get measurement results
measurements = result.measurements
measured_qubits = result.measured_qubits
measurement_counts = result.measurement_counts
measurement_probabilities = result.measurement_probabilities
# bitstrings
format_bitstring = "{0:0" + str(num_qubits) + "b}"
bitstring_keys = [format_bitstring.format(ii) for ii in range(2 ** num_qubits)]
# QPE postprocessing
phases_decimal, precision_results_dic = get_qpe_phases(
measurement_counts, precision_qubits, items_to_keep
)
eigenvalues = [np.exp(2 * np.pi * 1j * phase) for phase in phases_decimal]
# aggregate results
out = {
"circuit": circ,
"task_metadata": metadata,
"measurements": measurements,
"measured_qubits": measured_qubits,
"measurement_counts": measurement_counts,
"measurement_probabilities": measurement_probabilities,
"probs_values": probs_values,
"bitstring_keys": bitstring_keys,
"precision_results_dic": precision_results_dic,
"phases_decimal": phases_decimal,
"eigenvalues": eigenvalues,
}
if save_to_pck:
# store results: dump output to pickle with timestamp in filename
time_now = datetime.strftime(datetime.now(), "%Y%m%d%H%M%S")
results_file = "results-" + time_now + ".pck"
pickle.dump(out, open(results_file, "wb"))
# you can load results as follows
# out = pickle.load(open(results_file, "rb"))
return out
def postprocess_qpe_results(out,print_circ = False):
"""
Function to postprocess dictionary returned by run_qpe
Args:
out: dictionary containing results/information associated with QPE run as produced by run_qpe
"""
# unpack results
circ = out['circuit']
measurement_counts = out['measurement_counts']
bitstring_keys = out['bitstring_keys']
probs_values = out['probs_values']
precision_results_dic = out['precision_results_dic']
phases_decimal = out['phases_decimal']
eigenvalues = out['eigenvalues']
# print the circuit
if print_circ:
print('Printing circuit:')
print(circ)
# print measurement results
print('Measurement counts:', measurement_counts)
# plot probabalities
if print_circ:
plt.bar(bitstring_keys, probs_values);
plt.xlabel('bitstrings');
plt.ylabel('probability');
plt.xticks(rotation=90);
# print results
print('Results in precision register:', precision_results_dic)
print('QPE phase estimates:', phases_decimal)
print('QPE eigenvalue estimates:', np.round(eigenvalues, 5))