-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqemzmsepc.py
307 lines (242 loc) · 13.5 KB
/
qemzmsepc.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# Hello readers, I am Hangming Zhang, the author of "Joint Mitigation of Quantum Gate and Measurement Errors
# via the Z-mixed-state Expression of the Pauli Channel".
# I am delighted to share my code here.
# I conducted simulations using the Pennylane library, and the specific code is shown below.
# Please feel free to reach out to us with any questions at the following email addresses:
# For general inquiries: '[email protected]'
# For technical inquiries: '[email protected]' or '[email protected]'
# In[2]:
import pennylane as qml
from pennylane import numpy as np
import random
# In[3]:
# To obtain all Pauli basis matrices on N qubits,
# we first define a class in order to accomplish this task.
class NqubitsPauliMatrices:
def __init__(self, n_qubits):
self.n_qubits = n_qubits
def get_pauli_matrices_of_n_qubits(self):
pauli_i_matrix_1_qubit = np.array([[1, 0], [0, 1]])
pauli_x_matrix_1_qubit = np.array([[0, 1], [1, 0]])
pauli_y_matrix_1_qubit = np.array([[0, -1j], [1j, 0]])
pauli_z_matrix_1_qubit = np.array([[1, 0], [0, -1]])
pauli_set_n_qubits = []
pauli_set = []
pauli_set_1_qubit = []
pauli_set_1_qubit.append(pauli_i_matrix_1_qubit)
pauli_set_1_qubit.append(pauli_x_matrix_1_qubit)
pauli_set_1_qubit.append(pauli_y_matrix_1_qubit)
pauli_set_1_qubit.append(pauli_z_matrix_1_qubit)
for i in pauli_set_1_qubit:
for j in pauli_set_1_qubit:
temp = np.kron(i, j)
pauli_set.append(temp)
if self.n_qubits == 1:
return pauli_set_1_qubit
if self.n_qubits == 2:
return pauli_set
for _ in range(self.n_qubits - 2):
for i in pauli_set_1_qubit:
for j in pauli_set:
temp = np.kron(i, j)
pauli_set_n_qubits.append(temp)
pauli_set = pauli_set_n_qubits
pauli_set_n_qubits = []
pauli_set_n_qubits = pauli_set.copy()
return pauli_set_n_qubits
# In[4]:
# After obtaining all Pauli bases on N qubits,
# we can define a function to obtain all Kraus matrices of a depolarizing channel on N qubits.
# Next, we define a function to randomly generate Pauli channels on N qubits,
# which will be considered as measurement noise in the subsequent analysis.
# We're going to encapsulate these functions in a class.
class NqubitsChannel:
def __init__(self, n_qubits):
self.n_qubits = n_qubits
self.nqubitspaulimatrices = NqubitsPauliMatrices(n_qubits)
def nqubitsdepolarizingchannel(self, p):
pauli_set_n_qubits = self.nqubitspaulimatrices.get_pauli_matrices_of_n_qubits()
kraus_matrices = pauli_set_n_qubits.copy()
for i in range(1, len(kraus_matrices)):
kraus_matrices[i] = kraus_matrices[i] * np.sqrt((1 - p)/(4 ** self.n_qubits - 1))
kraus_matrices[0] = np.sqrt(p) * kraus_matrices[0]
return kraus_matrices
def nqubitsrandompaulichannel(self, p_identity=0.5):
pauli_set_n_qubits = self.nqubitspaulimatrices.get_pauli_matrices_of_n_qubits()
kraus_matrices = pauli_set_n_qubits.copy()
p_total = 1
coefficient_0 = random.uniform(p_identity, p_total)
kraus_matrices[0] = kraus_matrices[0] * np.sqrt(coefficient_0)
p_total -= coefficient_0
for i in range(1, len(kraus_matrices) - 1):
coefficient_i = random.uniform(0, p_total)
kraus_matrices[i] = kraus_matrices[i] * np.sqrt(coefficient_i)
p_total -= coefficient_i
kraus_matrices[-1] = kraus_matrices[-1] * np.sqrt(p_total)
return kraus_matrices
def nqubitsidentitychannel(self):
pauli_set_n_qubits = self.nqubitspaulimatrices.get_pauli_matrices_of_n_qubits()
kraus_matrices = pauli_set_n_qubits.copy()
for i in range(1, len(kraus_matrices)):
kraus_matrices[i] = kraus_matrices[i] * 0
return kraus_matrices
# In[5]:
class QEMZMSEPC:
def __init__(self, n_qubits):
self.n_qubits = n_qubits
self.nqubitschannel = NqubitsChannel(n_qubits)
def circuit_output(self, operations, paras, dev, p=1,
kraus_matrices_of_a_pauli_channel=None,
need_gate_noise=False, need_measurement_noise=False):
# operations: Operation can only be 'RX', 'RY', 'RZ' or 'CNOT'.
# In our protocol,
# we only accept rotation gates and CNOT gates as input parameters for acceptable gates,
# as other gates can be constructed using a combination of these gates.
# This restriction simplifies the input parameters and
# allows for a more efficient implementation of our algorithm in quantum circuits.
# Example of operations: ['RX', 'CNOT', 'RY'].
# paras: Parameters associated with each operation in operations.
# For rotation gates, these parameters include the wire and rotation angle,
# while for CNOT gates, these parameters consist of the control qubit index and the target qubit index.
# Example of paras: [[0, 0.5], [1, 0], [1, 1.6]],
# which means to apply an RX gate on qubit 0 with a rotation angle of 0.5, a CNOT gate on [1, 0]
# and an RY on qubit 1 with a rotation angle of 1.6.
# kraus_matrices_of_a_pauli_channel: This is a custom Pauli channel on N qubits, representing measurement noise.
return qml.QNode(self.__get_circuit, dev)(operations=operations, paras=paras, p=p,
kraus_matrices_of_a_pauli_channel=kraus_matrices_of_a_pauli_channel,
need_gate_noise=need_gate_noise, need_measurement_noise=need_measurement_noise)
def __get_circuit(self, operations, paras, p=1, kraus_matrices_of_a_pauli_channel=None,
need_gate_noise=False, need_measurement_noise=False):
if p < 0:
raise ValueError("p can not less than 0.")
if p > 1:
raise ValueError("p can not greater than 1.")
for i, operation in enumerate(operations):
if operation == 'RX':
qml.RX(paras[i][1], wires=paras[i][0])
elif operation == 'RY':
qml.RY(paras[i][1], wires=paras[i][0])
elif operation == 'RZ':
qml.RZ(paras[i][1], wires=paras[i][0])
elif operation == 'CNOT':
qml.CNOT(wires=[paras[i][0], paras[i][1]])
else:
raise ValueError("Operation can only be 'RX', 'RY', 'RZ' or 'CNOT'.")
if need_gate_noise:
kraus_matrices_of_a_depolarizing_channel = self.nqubitschannel.nqubitsdepolarizingchannel(p)
qml.QubitChannel(K_list=kraus_matrices_of_a_depolarizing_channel,
wires=[i for i in range(self.n_qubits)])
if need_measurement_noise:
if kraus_matrices_of_a_pauli_channel is None:
kraus_matrices_of_a_pauli_channel = self.nqubitschannel.nqubitsidentitychannel()
qml.QubitChannel(kraus_matrices_of_a_pauli_channel,
wires=[i for i in range(self.n_qubits)])
m = qml.PauliZ(0)
for i in range(1, self.n_qubits):
m = m @ qml.PauliZ(i)
return qml.expval(m)
def ufolding_output(self, noise_factor, operations, paras, dev, p=1,
kraus_matrices_of_a_pauli_channel=None,
need_gate_noise=False, need_measurement_noise=False):
# This involves simulating the global unitary gate folding operation.
return qml.QNode(self.__ufolding, dev)(operations=operations, paras=paras, p=p, noise_factor=noise_factor,
kraus_matrices_of_a_pauli_channel=kraus_matrices_of_a_pauli_channel,
need_gate_noise=need_gate_noise, need_measurement_noise=need_measurement_noise)
def __ufolding(self, noise_factor, operations, paras, p=1, kraus_matrices_of_a_pauli_channel=None,
need_gate_noise=False, need_measurement_noise=False):
if (noise_factor - 1) % 2 != 0:
raise ValueError("noise_factor can only be odd.")
if noise_factor < 3:
raise ValueError("noise_factor can not less than 3 during the global folding.")
if p < 0:
raise ValueError("p can not less than 0.")
if p > 1:
raise ValueError("p can not greater than 1.")
epoch = int((noise_factor - 1) / 2)
for _ in range(epoch + 1):
for i, operation in enumerate(operations):
if operation == 'RX':
qml.RX(paras[i][1], wires=paras[i][0])
elif operation == 'RY':
qml.RY(paras[i][1], wires=paras[i][0])
elif operation == 'RZ':
qml.RZ(paras[i][1], wires=paras[i][0])
elif operation == 'CNOT':
qml.CNOT(wires=[paras[i][0], paras[i][1]])
else:
raise ValueError("Operation can only be 'RX', 'RY', 'RZ' or 'CNOT'.")
if need_gate_noise:
kraus_matrices_of_a_depolarizing_channel = self.nqubitschannel.nqubitsdepolarizingchannel(p)
qml.QubitChannel(K_list=kraus_matrices_of_a_depolarizing_channel,
wires=[i for i in range(self.n_qubits)])
for _ in range(epoch):
for i, operation in enumerate(operations[::-1]):
if operation == 'RX':
qml.RX(-paras[-i-1][1], wires=paras[-i-1][0])
elif operation == 'RY':
qml.RY(-paras[-i-1][1], wires=paras[-i-1][0])
elif operation == 'RZ':
qml.RZ(-paras[-i-1][1], wires=paras[-i-1][0])
elif operation == 'CNOT':
qml.CNOT(wires=[paras[-i-1][0], paras[-i-1][1]])
if need_gate_noise:
kraus_matrices_of_a_depolarizing_channel = self.nqubitschannel.nqubitsdepolarizingchannel(p)
qml.QubitChannel(K_list=kraus_matrices_of_a_depolarizing_channel,
wires=[i for i in range(self.n_qubits)])
if need_measurement_noise:
if kraus_matrices_of_a_pauli_channel is None:
kraus_matrices_of_a_pauli_channel = self.nqubitschannel.nqubitsidentitychannel()
qml.QubitChannel(kraus_matrices_of_a_pauli_channel,
wires=[i for i in range(self.n_qubits)])
m = qml.PauliZ(0)
for i in range(1, self.n_qubits):
m = m @ qml.PauliZ(i)
return qml.expval(m)
def __calibration_cir1_output(self, dev, kraus_matrices_of_a_pauli_channel=None):
return qml.QNode(self.__calibration_cir1, dev)(kraus_matrices_of_a_pauli_channel)
def __calibration_cir1(self, kraus_matrices_of_a_pauli_channel=None):
if kraus_matrices_of_a_pauli_channel is None:
kraus_matrices_of_a_pauli_channel = self.nqubitschannel.nqubitsidentitychannel()
qml.QubitChannel(kraus_matrices_of_a_pauli_channel,
wires=[i for i in range(self.n_qubits)])
m = qml.PauliZ(0)
for i in range(1, self.n_qubits):
m = m @ qml.PauliZ(i)
return qml.expval(m)
def __calibration_cir2_output(self, dev, kraus_matrices_of_a_pauli_channel=None):
return qml.QNode(self.__calibration_cir2, dev)(kraus_matrices_of_a_pauli_channel)
def __calibration_cir2(self, kraus_matrices_of_a_pauli_channel=None):
if kraus_matrices_of_a_pauli_channel is None:
kraus_matrices_of_a_pauli_channel = self.nqubitschannel.nqubitsidentitychannel()
for i in range(self.n_qubits):
qml.PauliX(wires=i)
qml.QubitChannel(kraus_matrices_of_a_pauli_channel,
wires=[i for i in range(self.n_qubits)])
m = qml.PauliZ(0)
for i in range(1, self.n_qubits):
m = m @ qml.PauliZ(i)
return qml.expval(m)
def qemzmsepc(self, operations, paras, p, dev, kraus_matrices_of_a_pauli_channel=None):
# This is the output expectation value of the original noisy quantum circuit on the device dev.
if kraus_matrices_of_a_pauli_channel is None:
kraus_matrices_of_a_pauli_channel = self.nqubitschannel.nqubitsidentitychannel()
z_unmitigated = self.circuit_output(operations=operations, paras=paras, p=p, dev=dev,
kraus_matrices_of_a_pauli_channel=kraus_matrices_of_a_pauli_channel,
need_gate_noise=True, need_measurement_noise=True)
z_noise_factor_3 = self.ufolding_output(noise_factor=3, operations=operations, paras=paras, p=p, dev=dev,
kraus_matrices_of_a_pauli_channel=kraus_matrices_of_a_pauli_channel,
need_gate_noise=True, need_measurement_noise=True)
if z_unmitigated * z_noise_factor_3 < 0:
raise ValueError("It is recommended to increase shots to obtain more stable measurement results to do error mitigation.")
p_u = np.sqrt(z_noise_factor_3 / z_unmitigated)
if self.n_qubits % 2 == 0:
p_t = p_u * 0.5 * (self.__calibration_cir1_output(dev, kraus_matrices_of_a_pauli_channel) +
self.__calibration_cir2_output(dev, kraus_matrices_of_a_pauli_channel))
else:
p_t = p_u * 0.5 * (self.__calibration_cir1_output(dev, kraus_matrices_of_a_pauli_channel) -
self.__calibration_cir2_output(dev, kraus_matrices_of_a_pauli_channel))
z_mitigated = z_unmitigated / p_t
return z_mitigated, p_t