-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
195 lines (160 loc) · 5.74 KB
/
utils.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
from qiskit import assemble, transpile, QuantumCircuit
from qiskit import ClassicalRegister, QuantumRegister
from qiskit.visualization import plot_histogram
from qiskit import Aer
from qiskit.converters import circuit_to_dag
from qiskit.transpiler.passes.basis.unroller import Unroller
import pickle
import numpy as np
from matplotlib import pyplot as plt
from typing import List, Dict, Union
EQ = np.ndarray
EQs = List[np.ndarray]
# ==================== Utility Functions ====================
def evaluate(eqs: EQs, x: int, n: int, full: bool) -> Union[List[int], bool]:
"""Evaluate results of eqs(x).
monomial evaluation:
x: 01101 (x2,x3,x5=1)
monomial: 10100 (x1 * x3)
not monomial: 01011 (2**(n+1) - 1 - monomial)
x OR (not monomial): 01111
result: above+1==2**(n+1): 0 (32 != 64)
polynomial evaluation:
monomial results: 011001001001
sum and % 2: 5 % 2 = 1
return full right-hand-side when `full=True`,
otherwise return whether all right-hand-side is 0
"""
r = []
for eq in eqs:
not_eq = 2**(n+1) - 1 - eq
result = np.bitwise_or(x, not_eq) + 1 == 2 ** (n + 1)
result = np.sum(result)
if not full and result % 2 == 1:
return False
r.append(result % 2)
return True if not full else r
def see_what_is_happening_here(qc: QuantumCircuit, init_x: str, init_h: str,
measure_num=-1,
qasm_noise=True):
"""Function for debugging a quantum circuit.
With debug mode, set breakpoint at anywhere,
print the counts(frequency) of current quantum state.
"""
qc = qc.copy()
qc2 = QuantumCircuit(QuantumRegister(len(init_x)))
for i, s in enumerate(init_x):
if s == "1":
qc2.x(i)
for i, s in enumerate(init_h):
if s == "1":
qc2.h(i)
qc2.barrier()
qc = qc.compose(qc2, front=True)
if measure_num == -1:
qc.measure_all()
else:
qc.add_register(ClassicalRegister(measure_num))
qc.measure(list(range(measure_num)), list(range(measure_num)))
qc.draw("mpl", initial_state=True)
plt.title("Current Test QC")
plt.show()
qasm_sim = Aer.get_backend("statevector_simulator")
transpiled = transpile(qc, qasm_sim)
qobj = assemble(transpiled)
results = qasm_sim.run(qobj, shots=1000).result()
#
counts = results.get_counts()
print(counts)
plot_histogram(counts, figsize=(7, 7))
plt.show()
def _memorize(f, max_size=10000):
"""A general helper function. Memorize computed function results.
"""
memo = {}
def helper(*args, **kwargs):
if len(memo) > max_size:
memo.popitem()
try:
ha = hash(pickle.dumps([args, kwargs]))
except AttributeError:
result = f(*args, **kwargs)
else:
if ha not in memo:
memo[ha] = result = f(*args, **kwargs)
else:
return memo[ha]
return result
return helper
@_memorize
def determine_oracle_ancilla_usage(eq_num: int, level: int) -> int:
"""For given num of eqs and level, determine minimum ancilla usage.
For level=2: r * (r-1) / 2 + 1 = eq_num
Generally:
f_{l+1}(r) = f_l(r-1) + ... + f_l(1) + f_l(0), f_l(0) = f_l(1) = 1
f_1(r) = r, f_1(0) = 1
"""
if eq_num == 0:
raise ValueError
f_l = list(range(eq_num + 1))
f_l[0] = 1
for _ in range(level-1):
for j in range(1, eq_num + 1)[::-1]:
f_l[j] = sum(f_l[:j])
for e, i in enumerate(f_l):
if i >= eq_num:
return e or 1 # avoid the case e == 0
raise ValueError
@_memorize
def determine_maximum_allowed_eqs(ancilla_num: int, level: int) -> int:
"""For given num of ancilla and level, determine allowed num of eqs.
"""
f_l = list(range(ancilla_num + 1))
f_l[0] = 1
for _ in range(level-1):
for j in range(1, ancilla_num + 1)[::-1]:
f_l[j] = sum(f_l[:j])
return f_l[-1]
def is_good_state_reverse(eqs, x, n):
"""A simple function validate if x is solution of eqs.
"""
return evaluate(eqs, int(x[::-1][:n], 2), n, False)
def count_max(r):
"""Determine which index has the maximum value in r.
"""
return np.bincount(r).argmax()
def compute_depth(qc):
from qiskit_aer import StatevectorSimulator
dag = circuit_to_dag(qc)
unroller = Unroller(StatevectorSimulator().configuration().basis_gates)
depth = unroller.run(dag).depth()
return depth
def eqs_to_str(eqs: EQs, n: int) -> str:
usable_monomial = [0] + [2 ** i for i in range(n)] + \
[2 ** i + 2 ** j for j in range(n) for i in range(j)]
monomial_name_map = {i: f"{bin(i)[2:]:0>{n}s}" for i in
usable_monomial} # type: Dict[int, str]
s = "Equations: \n"
for eq in eqs:
row = ""
for monomial in sorted(eq, reverse=True):
str_represent = monomial_name_map[monomial]
index = [i+1 for i in range(n) if str_represent[i] == "1"]
if len(index) > 1:
row += "".join([f"x{i: <{len(str(n))}d}" for i in index])
row += " + "
elif len(index) == 1:
row += "".join([f"x{i: <{len(str(n))}d}" for i in index])
row += " " * (len(str(n))+1) + " + "
else:
row += "1 + "
s += row[:-2] + "= 0\n"
return s
def sols_to_str(xs: List[int], n: int) -> str:
if len(xs) == 0:
return "Solution: None"
s = "Solution: \n"
for x in xs:
xx = f"{bin(x)[2:]:0>{n}s}"
s += "".join([f"x{i+1} = {r}, " for i, r in enumerate(xx)]) + "\n"
return s