-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqudit_gates.py
218 lines (160 loc) · 6.99 KB
/
qudit_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
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
import numpy as np
import sympy
import scipy.stats
import cirq
def nice_repr(parameter):
"""Nice parameter representation
SymPy symbol - as is
float number - 3 digits after comma
"""
if isinstance(parameter, float):
return f'{parameter:.3f}'
else:
return f'{parameter}'
def levels_connectivity_check(l1, l2):
"""Check ion layers connectivity for gates"""
connected_layers_list = [{0, i} for i in range(max(l1, l2) + 1)]
assert {l1, l2} in connected_layers_list, "Layers are not connected"
def generalized_sigma(index, i, j, dimension=4):
"""Generalized sigma matrix for qudit gates implementation"""
sigma = np.zeros((dimension, dimension), dtype='complex')
if index == 0:
# identity matrix elements
sigma[i][i] = 1
sigma[j][j] = 1
elif index == 1:
# sigma_x matrix elements
sigma[i][j] = 1
sigma[j][i] = 1
elif index == 2:
# sigma_y matrix elements
sigma[i][j] = -1j
sigma[j][i] = 1j
elif index == 3:
# sigma_z matrix elements
sigma[i][i] = 1
sigma[j][j] = -1
return sigma
class QuditGate(cirq.Gate):
"""Base class for qudits gates"""
def __init__(self, dimension=4, num_qubits=1):
self.d = dimension
self.n = num_qubits
self.symbol = None
def _num_qubits_(self):
return self.n
def _qid_shape_(self):
return (self.d,) * self.n
def _circuit_diagram_info_(self, args):
return (self.symbol,) * self.n
class QuditRGate(QuditGate):
"""Rotation between two specified qudit levels: l1 and l2"""
def __init__(self, l1, l2, theta, phi, dimension=4):
super().__init__(dimension=dimension)
levels_connectivity_check(l1, l2)
self.l1 = l1
self.l2 = l2
self.theta = theta
self.phi = phi
def _unitary_(self):
sigma_x = generalized_sigma(1, self.l1, self.l2, dimension=self.d)
sigma_y = generalized_sigma(2, self.l1, self.l2, dimension=self.d)
s = np.sin(self.phi)
c = np.cos(self.phi)
u = scipy.linalg.expm(-1j * self.theta / 2 * (c * sigma_x + s * sigma_y))
return u
def _is_parameterized_(self) -> bool:
return cirq.protocols.is_parameterized(any((self.theta, self.phi)))
def _resolve_parameters_(self, resolver: 'cirq.ParamResolver', recursive: bool):
return self.__class__(self.l1, self.l2, resolver.value_of(self.theta, recursive), resolver.value_of(self.phi, recursive), dimension=self.d)
def _circuit_diagram_info_(self, args):
self.symbol = 'R'
SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
return f'{self.symbol}{str(self.l1).translate(SUB)}{str(self.l2).translate(SUP)}' + f'({nice_repr(self.theta)}, {nice_repr(self.phi)})'
class QuditXXGate(QuditGate):
"""Two qudit rotation for two specified qudit levels: l1 and l2"""
def __init__(self, l1, l2, theta, dimension=4):
levels_connectivity_check(l1, l2)
super().__init__(dimension=dimension, num_qubits=2)
self.l1 = l1
self.l2 = l2
self.theta = theta
def _unitary_(self):
sigma_x = generalized_sigma(1, self.l1, self.l2, dimension=self.d)
u = scipy.linalg.expm(-1j * self.theta / 2 * np.kron(sigma_x, sigma_x))
return u
def _is_parameterized_(self) -> bool:
return cirq.protocols.is_parameterized(self.theta)
def _resolve_parameters_(self, resolver: 'cirq.ParamResolver', recursive: bool):
return self.__class__(self.l1, self.l2, resolver.value_of(self.theta, recursive), dimension=self.d)
def _circuit_diagram_info_(self, args):
self.symbol = 'XX'
SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
info = f'{self.symbol}{str(self.l1).translate(SUB)}{str(self.l2).translate(SUP)}'.translate(
SUB) + f'({nice_repr(self.theta)})'
return info, info
class QuditZZGate(QuditGate):
"""Two qudit rotation for two specified qudit levels: l1 and l2"""
def __init__(self, l1, l2, theta, dimension=4):
levels_connectivity_check(l1, l2)
super().__init__(dimension=dimension, num_qubits=2)
self.l1 = l1
self.l2 = l2
self.theta = theta
def _unitary_(self):
sigma_z = generalized_sigma(3, self.l1, self.l2, dimension=self.d)
u = scipy.linalg.expm(-1j * self.theta / 2 * np.kron(sigma_z, sigma_z))
return u
def _is_parameterized_(self) -> bool:
return cirq.protocols.is_parameterized(self.theta)
def _resolve_parameters_(self, resolver: 'cirq.ParamResolver', recursive: bool):
return self.__class__(self.l1, self.l2, resolver.value_of(self.theta, recursive), dimension=self.d)
def _circuit_diagram_info_(self, args):
self.symbol = 'ZZ'
SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
info = f'{self.symbol}{str(self.l1).translate(SUB)}{str(self.l2).translate(SUP)}'.translate(
SUB) + f'({nice_repr(self.theta)})'
return info, info
class QuditBarrier(QuditGate):
"""Just barrier for visual separation in circuit diagrams. Does nothing"""
def __init__(self, dimension=4, num_qudits=2):
super().__init__(dimension=dimension, num_qubits=num_qudits)
self.symbol = '|'
def _unitary_(self):
return np.eye(self.d * self.d)
class QuditArbitraryUnitary(QuditGate):
"""Random unitary acts on qubits"""
def __init__(self, dimension=4, num_qudits=2):
super().__init__(dimension=dimension, num_qubits=num_qudits)
self.unitary = np.array(scipy.stats.unitary_group.rvs(self.d ** self.n))
self.symbol = 'U'
def _unitary_(self):
return self.unitary
if __name__ == '__main__':
n = 3 # number of qudits
d = 4 # dimension of qudits
qudits = cirq.LineQid.range(n, dimension=d)
alpha = sympy.Symbol('alpha')
beta = sympy.Symbol('beta')
print('Qudit R Gate')
circuit = cirq.Circuit(QuditRGate(0, 1, alpha, beta, dimension=d).on(qudits[0]))
param_resolver = cirq.ParamResolver({'alpha': 0.2, 'beta': 0.3})
resolved_circuit = cirq.resolve_parameters(circuit, param_resolver)
print(resolved_circuit)
print()
print('Qudit XX Gate')
circuit = cirq.Circuit(QuditXXGate(0, 2, beta, dimension=d).on(*qudits[:2]))
param_resolver = cirq.ParamResolver({'alpha': 0.2, 'beta': 0.3})
resolved_circuit = cirq.resolve_parameters(circuit, param_resolver)
print(resolved_circuit)
print()
print('Qudit Barrier')
circuit = cirq.Circuit(QuditBarrier(num_qudits=n, dimension=d).on(*qudits))
print(circuit)
print()
print('Qudit Arbitrary Unitary Gate')
circuit = cirq.Circuit(QuditArbitraryUnitary(num_qudits=n, dimension=d).on(*qudits))
print(circuit)