-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
piecewise_linear_pauli_rotations.py
277 lines (218 loc) · 9.47 KB
/
piecewise_linear_pauli_rotations.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
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Piecewise-linearly-controlled rotation."""
from __future__ import annotations
import numpy as np
from qiskit.circuit import QuantumRegister, AncillaRegister, QuantumCircuit
from qiskit.circuit.exceptions import CircuitError
from .functional_pauli_rotations import FunctionalPauliRotations
from .linear_pauli_rotations import LinearPauliRotations
from .integer_comparator import IntegerComparator
class PiecewiseLinearPauliRotations(FunctionalPauliRotations):
r"""Piecewise-linearly-controlled Pauli rotations.
For a piecewise linear (not necessarily continuous) function :math:`f(x)`, which is defined
through breakpoints, slopes and offsets as follows.
Suppose the breakpoints :math:`(x_0, ..., x_J)` are a subset of :math:`[0, 2^n-1]`, where
:math:`n` is the number of state qubits. Further on, denote the corresponding slopes and
offsets by :math:`a_j` and :math:`b_j` respectively.
Then f(x) is defined as:
.. math::
f(x) = \begin{cases}
0, x < x_0 \\
a_j (x - x_j) + b_j, x_j \leq x < x_{j+1}
\end{cases}
where we implicitly assume :math:`x_{J+1} = 2^n`.
"""
def __init__(
self,
num_state_qubits: int | None = None,
breakpoints: list[int] | None = None,
slopes: list[float] | np.ndarray | None = None,
offsets: list[float] | np.ndarray | None = None,
basis: str = "Y",
name: str = "pw_lin",
) -> None:
"""Construct piecewise-linearly-controlled Pauli rotations.
Args:
num_state_qubits: The number of qubits representing the state.
breakpoints: The breakpoints to define the piecewise-linear function.
Defaults to ``[0]``.
slopes: The slopes for different segments of the piecewise-linear function.
Defaults to ``[1]``.
offsets: The offsets for different segments of the piecewise-linear function.
Defaults to ``[0]``.
basis: The type of Pauli rotation (``'X'``, ``'Y'``, ``'Z'``).
name: The name of the circuit.
"""
# store parameters
self._breakpoints = breakpoints if breakpoints is not None else [0]
self._slopes = slopes if slopes is not None else [1]
self._offsets = offsets if offsets is not None else [0]
super().__init__(num_state_qubits=num_state_qubits, basis=basis, name=name)
@property
def breakpoints(self) -> list[int]:
"""The breakpoints of the piecewise linear function.
The function is linear in the intervals ``[point_i, point_{i+1}]`` where the last
point implicitly is ``2**(num_state_qubits + 1)``.
"""
return self._breakpoints
@breakpoints.setter
def breakpoints(self, breakpoints: list[int]) -> None:
"""Set the breakpoints.
Args:
breakpoints: The new breakpoints.
"""
self._invalidate()
self._breakpoints = breakpoints
if self.num_state_qubits and breakpoints:
self._reset_registers(self.num_state_qubits)
@property
def slopes(self) -> list[float] | np.ndarray:
"""The breakpoints of the piecewise linear function.
The function is linear in the intervals ``[point_i, point_{i+1}]`` where the last
point implicitly is ``2**(num_state_qubits + 1)``.
"""
return self._slopes
@slopes.setter
def slopes(self, slopes: list[float]) -> None:
"""Set the slopes.
Args:
slopes: The new slopes.
"""
self._invalidate()
self._slopes = slopes
@property
def offsets(self) -> list[float] | np.ndarray:
"""The breakpoints of the piecewise linear function.
The function is linear in the intervals ``[point_i, point_{i+1}]`` where the last
point implicitly is ``2**(num_state_qubits + 1)``.
"""
return self._offsets
@offsets.setter
def offsets(self, offsets: list[float]) -> None:
"""Set the offsets.
Args:
offsets: The new offsets.
"""
self._invalidate()
self._offsets = offsets
@property
def mapped_slopes(self) -> np.ndarray:
"""The slopes mapped to the internal representation.
Returns:
The mapped slopes.
"""
mapped_slopes = np.zeros_like(self.slopes)
for i, slope in enumerate(self.slopes):
mapped_slopes[i] = slope - sum(mapped_slopes[:i])
return mapped_slopes
@property
def mapped_offsets(self) -> np.ndarray:
"""The offsets mapped to the internal representation.
Returns:
The mapped offsets.
"""
mapped_offsets = np.zeros_like(self.offsets)
for i, (offset, slope, point) in enumerate(
zip(self.offsets, self.slopes, self.breakpoints)
):
mapped_offsets[i] = offset - slope * point - sum(mapped_offsets[:i])
return mapped_offsets
@property
def contains_zero_breakpoint(self) -> bool | np.bool_:
"""Whether 0 is the first breakpoint.
Returns:
True, if 0 is the first breakpoint, otherwise False.
"""
return np.isclose(0, self.breakpoints[0])
def evaluate(self, x: float) -> float:
"""Classically evaluate the piecewise linear rotation.
Args:
x: Value to be evaluated at.
Returns:
Value of piecewise linear function at x.
"""
y = (x >= self.breakpoints[0]) * (x * self.mapped_slopes[0] + self.mapped_offsets[0])
for i in range(1, len(self.breakpoints)):
y = y + (x >= self.breakpoints[i]) * (
x * self.mapped_slopes[i] + self.mapped_offsets[i]
)
return y
def _check_configuration(self, raise_on_failure: bool = True) -> bool:
"""Check if the current configuration is valid."""
valid = True
if self.num_state_qubits is None:
valid = False
if raise_on_failure:
raise AttributeError("The number of qubits has not been set.")
if self.num_qubits < self.num_state_qubits + 1:
valid = False
if raise_on_failure:
raise CircuitError(
"Not enough qubits in the circuit, need at least "
f"{self.num_state_qubits + 1}."
)
if len(self.breakpoints) != len(self.slopes) or len(self.breakpoints) != len(self.offsets):
valid = False
if raise_on_failure:
raise ValueError("Mismatching sizes of breakpoints, slopes and offsets.")
return valid
def _reset_registers(self, num_state_qubits: int | None) -> None:
"""Reset the registers."""
self.qregs = []
if num_state_qubits is not None:
qr_state = QuantumRegister(num_state_qubits)
qr_target = QuantumRegister(1)
self.qregs = [qr_state, qr_target]
# add ancillas if required
if len(self.breakpoints) > 1:
num_ancillas = num_state_qubits
qr_ancilla = AncillaRegister(num_ancillas)
self.add_register(qr_ancilla)
def _build(self):
"""If not already built, build the circuit."""
if self._is_built:
return
super()._build()
circuit = QuantumCircuit(*self.qregs, name=self.name)
qr_state = circuit.qubits[: self.num_state_qubits]
qr_target = [circuit.qubits[self.num_state_qubits]]
qr_ancilla = circuit.ancillas
# apply comparators and controlled linear rotations
for i, point in enumerate(self.breakpoints):
if i == 0 and self.contains_zero_breakpoint:
# apply rotation
lin_r = LinearPauliRotations(
num_state_qubits=self.num_state_qubits,
slope=self.mapped_slopes[i],
offset=self.mapped_offsets[i],
basis=self.basis,
)
circuit.append(lin_r.to_gate(), qr_state[:] + qr_target)
else:
qr_compare = [qr_ancilla[0]]
qr_helper = qr_ancilla[1:]
# apply Comparator
comp = IntegerComparator(num_state_qubits=self.num_state_qubits, value=point)
qr = qr_state[:] + qr_compare[:] # add ancilla as compare qubit
circuit.append(comp.to_gate(), qr[:] + qr_helper[: comp.num_ancillas])
# apply controlled rotation
lin_r = LinearPauliRotations(
num_state_qubits=self.num_state_qubits,
slope=self.mapped_slopes[i],
offset=self.mapped_offsets[i],
basis=self.basis,
)
circuit.append(lin_r.to_gate().control(), qr_compare[:] + qr_state[:] + qr_target)
# uncompute comparator
circuit.append(comp.to_gate().inverse(), qr[:] + qr_helper[: comp.num_ancillas])
self.append(circuit.to_gate(), self.qubits)