-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding_gates.py
193 lines (140 loc) · 5.94 KB
/
encoding_gates.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
import functools
import numpy as np
import pennylane as qml
from pennylane.operation import AnyWires, Operation
# from .non_parametric_ops import Hadamard, PauliX, PauliY, PauliZ
def _can_replace(x, y):
"""
Convenience function that returns true if x is close to y and if
x does not require grad
"""
return (
not qml.math.is_abstract(x)
and not qml.math.requires_grad(x)
and qml.math.allclose(x, y)
)
stack_last = functools.partial(qml.math.stack, axis=-1)
class RX(Operation):
r"""
The single qubit X rotation
.. math:: R_x(\phi) = e^{-i\phi\sigma_x/2} = \begin{bmatrix}
\cos(\phi/2) & -i\sin(\phi/2) \\
-i\sin(\phi/2) & \cos(\phi/2)
\end{bmatrix}.
**Details:**
* Number of wires: 1
* Number of parameters: 1
* Number of dimensions per parameter: (0,)
* Gradient recipe: :math:`\frac{d}{d\phi}f(R_x(\phi)) = \frac{1}{2}\left[f(R_x(\phi+\pi/2)) - f(R_x(\phi-\pi/2))\right]`
where :math:`f` is an expectation value depending on :math:`R_x(\phi)`.
Args:
phi (float): rotation angle :math:`\phi`
wires (Sequence[int] or int): the wire the operation acts on
id (str or None): String representing the operation (optional)
"""
num_wires = 1
num_params = 1
"""int: Number of trainable parameters that the operator depends on."""
ndim_params = (0,)
"""tuple[int]: Number of dimensions per trainable parameter that the operator depends on."""
basis = "X"
grad_method = "A"
parameter_frequencies = [(1,)]
def generator(self):
return -0.5 * self.prefactor * qml.PauliX(wires=self.wires)
def __init__(self, phi, wires, id=None, prefactor=1):
super().__init__(phi, wires=wires, id=id)
self.prefactor = prefactor
@staticmethod
def compute_matrix(theta): # pylint: disable=arguments-differ
r"""Representation of the operator as a canonical matrix in the computational basis (static method).
The canonical matrix is the textbook matrix representation that does not consider wires.
Implicitly, this assumes that the wires of the operator correspond to the global wire order.
.. seealso:: :meth:`~.RX.matrix`
Args:
theta (tensor_like or float): rotation angle
Returns:
tensor_like: canonical matrix
**Example**
>>> qml.RX.compute_matrix(torch.tensor(0.5))
tensor([[0.9689+0.0000j, 0.0000-0.2474j],
[0.0000-0.2474j, 0.9689+0.0000j]])
"""
c = qml.math.cos(theta / 2)
s = qml.math.sin(theta / 2)
if qml.math.get_interface(theta) == "tensorflow":
c = qml.math.cast_like(c, 1j)
s = qml.math.cast_like(s, 1j)
# The following avoids casting an imaginary quantity to reals when backpropagating
c = (1 + 0j) * c
js = -1j * s
return qml.math.stack([stack_last([c, js]), stack_last([js, c])], axis=-2)
def adjoint(self):
return RX(-self.data[0], wires=self.wires)
def pow(self, z):
return [RX(self.data[0] * z, wires=self.wires)]
def _controlled(self, wire):
return qml.CRX(*self.parameters, wires=wire + self.wires)
def simplify(self):
theta = self.data[0] % (4 * np.pi)
if _can_replace(theta, 0):
return qml.Identity(wires=self.wires)
return RX(theta, wires=self.wires)
def single_qubit_rot_angles(self):
# RX(\theta) = RZ(-\pi/2) RY(\theta) RZ(\pi/2)
pi_half = qml.math.ones_like(self.data[0]) * (np.pi / 2)
return [pi_half, self.data[0], -pi_half]
class DiagonalRotationUnitary(Operation):
r"""DiagonalRotationUnitary(D, wires)
Apply the rotation of an arbitrary diagonal unitary matrix with a dimension that is a power of two.
.. math:: R_U(\phi) = e^{-i\phi U}
**Details:**
* Number of wires: Any (the operation can act on any number of wires)
* Number of parameters: 1
* Number of dimensions per parameter: (1,)
* Gradient recipe: None
Args:
phi (float): rotation angle :math:`\phi`
D (array[complex]): diagonal of unitary matrix
wires (Sequence[int] or int): the wire(s) the operation acts on
id (str or None): String representing the operation (optional)
"""
num_wires = AnyWires
num_params = 1
ndim_params = (0,)
grad_method = None
def __init__(self, phi, D, wires, id=None, prefactor=1):
super().__init__(phi, wires=wires, id=id)
self.prefactor = prefactor
self.D = D
# def generator(self):
# return qml. (D,wires=self.wires)
#@classmethod
def compute_matrix(self,phi):
r"""Representation of the operator as a canonical matrix in the computational basis (static method).
The canonical matrix is the textbook matrix representation that does not consider wires.
Implicitly, this assumes that the wires of the operator correspond to the global wire order.
Args:
phi (tensor_like or float): rotation angle
D (tensor_like): diagonal of unitary matrix
Returns:
tensor_like: canonical matrix
"""
return qml.math.diag(qml.math.exp(-1j * phi * self.D))
# def adjoint(self):
# return DiagonalRotationUnitary(
# qml.math.conj(self.parameters[0]), wires=self.wires
# )
# def pow(self, z):
# cast_data = qml.math.cast(self.data[0], np.complex128)
# return [DiagonalRotationUnitary(cast_data**z, wires=self.wires)]
# def _controlled(self, control):
# return DiagonalRotationUnitary(
# qml.math.hstack([np.ones_like(self.parameters[0]), self.parameters[0]]),
# wires=control + self.wires,
# )
# def simplify(self):
# theta = self.data[0] % (4 * np.pi)
# if _can_replace(theta, 0):
# return qml.Identity(wires=self.wires)
# return DiagonalRotationUnitary(theta, wires=self.wires)