-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathstabilizer.py
More file actions
273 lines (236 loc) · 9.67 KB
/
stabilizer.py
File metadata and controls
273 lines (236 loc) · 9.67 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
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
from itertools import combinations, product
import numpy as np
from typing import Iterable
import random
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, AncillaRegister
from qiskit.quantum_info import random_clifford, Clifford, Pauli
from qiskit.circuit.library import PauliEvolutionGate
from quantumreservoirpy.util import randomIsing, get_Ising_circuit
from quantumreservoirpy.reservoirs import Static
class Stabilizer(Static):
def __init__(
self,
n_qubits,
n_meas, # number of stabilizer generators
tableau: dict | None = None, # if specified, overrides the tableau generation
codestate_preparation_circ: (
Iterable[QuantumCircuit] | None
) = None, # if None, will generate a random stabilizer code
memory=np.inf,
backend=None,
degree=1,
stab_method='random',
stab_deg=1,
num_reservoirs=1,
standard=False,
isingparams=None,
decode=True, # danger zone: this is only for testing
) -> None:
super().__init__(
n_qubits , memory, backend, degree=degree, num_reservoirs=num_reservoirs
)
self.n_meas = n_meas
self.decode = decode
self.stab_method=stab_method
self.stab_deg=stab_deg
if not isingparams:
steps = 1
dt = 1.645
# top = limitrange(list(combinations(range(n_qubits), 2)))
top = list(combinations(range(n_qubits), 2))
self.U = {}
self.isingparams = {}
for nr in range(1, num_reservoirs + 1):
(
self.U[nr],
self.isingparams[nr],
) = randomIsing(n_qubits, top, steps, dt)
else:
self.U = {}
for nr in range(1, num_reservoirs + 1):
self.U[nr] = get_Ising_circuit(n_qubits, isingparams[nr])
if tableau != None:
if (
len(tableau["stabilizer"]) != n_meas
or len(tableau["destabilizer"]) != n_meas
):
raise Exception(
"Error: the number of entries of the tableau does not match the dimension of the stabilizer"
)
self.tableau = tableau
else:
self.tableau = Stabilizer.generate_tableau(
n_qubits, n_meas,self.stab_deg,self.stab_method,codestate_preparation_circ
)
def before(self, circuit):
if self.decode:
circuit.add_register(AncillaRegister(1))
def during(self, circuit, timestep, reservoirnumber):
circuit.barrier()
# encode
for k in range(self.n_meas):
beta = 3**(k/self.n_meas)/2*np.pi
pauliop = Pauli(self.tableau["destabilizer"][k])
evo = PauliEvolutionGate(pauliop, -beta * timestep)
circuit.append(evo, range(self.n_qubits ))
circuit.barrier()
# reservoir
circuit.append(self.U[reservoirnumber], range(self.n_qubits))
# decode
cr = ClassicalRegister(self.n_meas)
circuit.add_register(cr)
if self.decode:
Stabilizer.decoder(circuit, self.tableau)
@staticmethod
def generate_tableau(
n_qubits: int,
n_meas: int,
stab_deg: int,
stab_method: str,
codestate_preparation_circ: Iterable[QuantumCircuit] | None = None
):
if stab_method == 'random':
print('ok')
"""generates a tableau for a stabilizer code based on 2**k codestate preparation circuits"""
logical_qubits = n_qubits - n_meas # also called k
if codestate_preparation_circ == None: # generate random stabilizer code
tableau = random_clifford(n_qubits).to_dict()
# turn the last k stabilizers into logical Zs
# tableau["logical_z"] = tableau["stabilizer"][n_meas:] #these are just for QEC fun, not useful here
tableau["stabilizer"] = tableau["stabilizer"][:n_meas]
# turn the last k destabilizers into logical Xs
# tableau["logical_x"] = tableau["destabilizer"][n_meas:]
tableau["destabilizer"] = tableau["destabilizer"][:n_meas]
elif len(codestate_preparation_circ) != 2**logical_qubits:
print(
"Error : number of code state preparation circuits does not match the dimension of the codespace"
)
return
else:
tableau = Clifford(codestate_preparation_circ[0]).to_dict()
for circ in codestate_preparation_circ[1:]:
circ_tableau = Clifford(circ).to_dict()
to_pop = []
for i in range(len(tableau["stabilizer"])):
if tableau["stabilizer"][i] not in circ_tableau["stabilizer"]:
to_pop.append(i)
for i in to_pop:
tableau["stabilizer"].pop(i)
tableau["destabilizer"].pop(i)
# check the stabilizer has the right dimension
if (
len(tableau["stabilizer"]) != n_meas
or len(tableau["destabilizer"]) != n_meas
):
print("Error : something went wrong with tableau generation")
print(tableau)
else:
deg=stab_deg
stabilizers = []
destabilizers = []
for i in range(n_qubits - deg + 1):
pauli_z = ["I"] * n_qubits
pauli_x = ["I"] * n_qubits
# fill in Z’s and X’s
for j in range(deg):
pauli_z[i + j] = "Z"
pauli_x[i + j] = "X"
stabilizers.append("+" + "".join(pauli_z))
destabilizers.append("+" + "".join(pauli_x))
indices = random.sample(range(len(stabilizers)), n_meas)
# Extract stabilizers and destabilizers at those indices
stabs = [stabilizers[i] for i in indices]
destabs = [destabilizers[i] for i in indices]
tableau= {
"stabilizer": stabs,
"destabilizer": destabs,
}
print(tableau)
return tableau
@staticmethod
def binary_array_to_integer(binary_array):
binary_string = "".join(map(str, binary_array.astype(int)))
decimal_integer = int(binary_string, 2)
return decimal_integer
@staticmethod
def indices_of_ones(input_list, n):
indices = [
n - 1 - index for index, value in enumerate(input_list) if value == 1.0
]
return indices # n-1-index because of Endian encoding of qiskit
@staticmethod
def get_parity_measurements(x):
G_list = []
for i in range(x.shape[0] - 1):
tmp = np.zeros(x.shape[0])
tmp[i] = 1
tmp[i + 1] = 1
G_list.append(tmp)
G = np.array(G_list)
return np.remainder(G @ x, 2)
@staticmethod
def build_decoder_map(n, standard):
decoder = {}
if standard:
for origin in list(product((0, 1), repeat=n - 1)):
p = np.array(origin)
p = Stabilizer.binary_array_to_integer(p)
flips = Stabilizer.indices_of_ones(origin, n - 1)
decoder[p] = flips
else:
for origin in list(product((0, 1), repeat=n)):
p = Stabilizer.get_parity_measurements(np.array(origin))
p = Stabilizer.binary_array_to_integer(p)
flips = Stabilizer.indices_of_ones(origin, n)
if p in decoder:
if len(decoder[p]) > len(flips):
decoder[p] = flips
else:
decoder[p] = flips
return decoder
@staticmethod
def apply_operations_for_integers(circ, c, integer_dict):
with circ.switch(c) as case:
for key, value in integer_dict.items():
if value:
with case(key):
circ.x(value)
@staticmethod
def decoder(circuit: QuantumCircuit, code_tableau: dict):
"""
Given a n-qubit state and a stabilizer code, detects errors and applies the operations
to project the state back to the codespace.
Args:
circuit : state preparation circuit
code_tableau : dictionary {"stabilizer": [], "destabilizer": []} each list has n-k elements
Returns:
circuit : state preparation circuit with syndrome measurement and error correction appended
"""
n_qubits = circuit.num_qubits
n_meas = len(code_tableau["stabilizer"])
qr = circuit.qregs[0]
cr = circuit.cregs[-1]
ar = circuit.ancillas
circuit.barrier()
# syndrome measurement operations
for j in range(n_meas):
circuit.reset(ar)
circuit.h(ar)
P = code_tableau["stabilizer"][j]
P_aux=P[1:][::-1]
if P[0] == str('-'):
circuit.z(ar)
for i in range(0, len(P_aux)):
if P_aux[i] == "X":
circuit.cx(ar, qr[i])
elif P_aux[i] == "Y":
circuit.cy(ar, qr[i])
elif P_aux[i] == "Z":
circuit.cz(ar, qr[i])
circuit.h(ar)
circuit.measure(ar, cr[j])
circuit.barrier()
for j in range(n_meas):
with circuit.if_test((cr[j], 1)):
circuit.pauli(code_tableau["destabilizer"][j][1:], qr)
return circuit