-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.py
More file actions
190 lines (147 loc) · 6 KB
/
common.py
File metadata and controls
190 lines (147 loc) · 6 KB
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
"""Common functions on QFixed numbers, used as building blocks in other routines.
Some of these are wrappers around routines from psiqworkbench.qubricks, this is
done so we can define symbolic resource estimates for them.
"""
import psiqworkbench.qubricks as qbk
from psiqworkbench import QFixed, Qubrick, QInt, QUInt, Qubits
from psiqworkbench.symbolics.qubrick_costs import QubrickCosts
from psiqworkbench.symbolics import Min
from ..utils.symbolic import SymbolicQFixed
from ..utils.re_utils import fraction_length
class Negate(Qubrick):
"""Computes x:=-x."""
def _compute(self, x: QFixed, ctrl: Qubits | None = None):
x_as_int = QInt(x)
x_as_int.x(ctrl)
qbk.GidneyAdd().compute(x_as_int, 1, ctrl=ctrl)
def _estimate(self, x: SymbolicQFixed):
n = x.num_qubits
cost = QubrickCosts(
gidney_lelbows=n - 2,
gidney_relbows=n - 2,
local_ancillae=n - 2,
active_volume=61 * n - 118,
)
self.get_qc().add_cost_event(cost)
class AbsInPlace(Qubrick):
"""Computes x := abs(x)."""
def _compute(self, x: QFixed):
sign = self.alloc_temp_qreg(1, "sign")
sign.lelbow(x[-1])
x.x(sign)
qbk.GidneyAdd().compute(QUInt(x), 1, ctrl=sign)
def _estimate(self, x: SymbolicQFixed):
n = x.num_qubits
cost = QubrickCosts(
gidney_lelbows=n - 2,
gidney_relbows=n - 2,
toffs=n - 1,
local_ancillae=n - 1,
active_volume=105.5 * n - 150,
)
self.get_qc().add_cost_event(cost)
class Add(Qubrick):
"""Computes lhs += rhs."""
def _compute(self, lhs: QFixed, rhs: QFixed):
qbk.GidneyAdd().compute(lhs, rhs)
def _estimate(self, lhs: SymbolicQFixed, rhs: SymbolicQFixed):
# qbk.GidneyAdd has _estimate, but active volume there differs from
# what we observe from numeric RE. So we have to re-define _estimate.
n = lhs.num_qubits
cost = QubrickCosts(
gidney_lelbows=n - 1,
gidney_relbows=n - 1,
local_ancillae=n - 1,
active_volume=72 * n - 83,
)
self.get_qc().add_cost_event(cost)
class AddConst(Qubrick):
"""Computes lhs += rhs (a is aclassical number)."""
def __init__(self, rhs: float, **kwargs):
super().__init__(**kwargs)
self.rhs = rhs
if abs(self.rhs) < 1e-10:
self.rhs = 0.0
def _compute(self, lhs: QFixed):
assert abs(self.rhs) <= 2 ** (lhs.num_qubits - lhs.radix - 1), f"Constant {self.rhs} is too large."
qbk.GidneyAdd().compute(lhs, self.rhs)
def _estimate(self, lhs: SymbolicQFixed):
# This estimate might be off (overestimate) by O(1) in case when lhs
# is not "round" number, but when rounded to fixed precision, few of
# least significant bits become zeroes.
# This estimate assumes that rhs fits in given register.
# Compute n - length of rhs except trailing zeros.
n = lhs.num_qubits
fl = fraction_length(self.rhs)
if fl is not None:
n = Min(n, lhs.num_qubits - lhs.radix + fl)
cost = QubrickCosts(
gidney_lelbows=n - 2,
gidney_relbows=n - 2,
local_ancillae=n - 2,
active_volume=61 * n - 118,
)
self.get_qc().add_cost_event(cost)
class Subtract(Qubrick):
"""Computes lhs-= rhs. Negates rhs in the process."""
def _compute(self, lhs: QFixed, rhs: QFixed):
Negate().compute(rhs)
Add().compute(lhs, rhs)
class MultiplyAdd(Qubrick):
"""Computes dst += lhs * rhs."""
def _compute(self, dst: QFixed, lhs: QFixed, rhs: QFixed):
qbk.GidneyMultiplyAdd().compute(dst, lhs, rhs)
def _estimate(self, dst: SymbolicQFixed, lhs: SymbolicQFixed, rhs: SymbolicQFixed):
n = dst.num_qubits
assert lhs.num_qubits == n
assert rhs.num_qubits == n
r = lhs.radix + rhs.radix - dst.radix
# This RE is correct when n>=4, 0<r<n.
# It is within 0.1% of numerical RE for active volume and exact for other metrics.
cost = QubrickCosts(
gidney_lelbows=0.5 * ((n + r) ** 2 + 15 * n - r - 32),
gidney_relbows=0.5 * ((n + r) ** 2 + 15 * n - r - 32),
toffs=0.5 * ((n + r) ** 2 + 17 * n + r - 16),
local_ancillae=n + 2 * r + 1,
active_volume=-1170 + 821 * n - 23 * r + 59 * n**2 + 118 * n * r + 54 * r**2,
)
self.get_qc().add_cost_event(cost)
# Preparation for MultiplyConstAdd to handle negative inputs.
class _MulConstPrep(Qubrick):
def _compute(self, x: QFixed, x_sign: Qubits, y: float, dst: QFixed):
x_sign[0].lelbow(x[-1])
Negate().compute(x, ctrl=x_sign)
if y < 0:
x_sign.x()
dst.x(x_sign)
class MultiplyConstAdd(Qubrick):
"""Computes dst += lhs * rhs (rhs is a classical number)."""
def __init__(self, rhs: float, **kwargs):
super().__init__(**kwargs)
self.rhs = rhs
# z += y*x, assuming x>=0, y>0.
def _compute_positive(self, x: QFixed, y: float, z: QFixed):
assert y > 0
x = QInt(x)
z = QInt(z)
min_i = x.radix - z.radix - (x.num_qubits - 1)
max_i = x.radix - z.radix + (z.num_qubits - 1)
for i in range(min_i, max_i + 1):
shift = z.radix - x.radix + i
bit = int(y * (2 ** (-shift))) % 2
if bit == 1:
if shift < 0:
qbk.GidneyAdd().compute(z, x[(-shift):])
else:
qbk.GidneyAdd().compute(z[shift:], x)
def _compute(self, dst: QFixed, lhs: QFixed):
if self.rhs == 0:
return
x_sign = self.alloc_temp_qreg(1, "x_sign")
# Preparation to handle negative inputs.
with _MulConstPrep().computed(lhs, x_sign, self.rhs, dst):
self._compute_positive(lhs, abs(self.rhs), dst)
x_sign.release()
def _estimate(self, dst: SymbolicQFixed, lhs: SymbolicQFixed):
# TODO: implement.
pass