-
Notifications
You must be signed in to change notification settings - Fork 54
/
channels.py
643 lines (504 loc) · 22.6 KB
/
channels.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import warnings
from itertools import product
from math import exp, sqrt
from typing import Tuple
from qibo.config import PRECISION_TOL, raise_error
from qibo.gates.abstract import Gate
from qibo.gates.gates import I, Unitary, X, Y, Z
from qibo.gates.special import FusedGate
class Channel(Gate):
"""Abstract class for channels."""
def __init__(self):
super().__init__()
self.coefficients = tuple()
self.gates = tuple()
def controlled_by(self, *q):
""""""
raise_error(ValueError, "Noise channel cannot be controlled on qubits.")
def on_qubits(self, qubit_map): # pragma: no cover
# future TODO
raise_error(
NotImplementedError,
"`on_qubits` method is not available " "for the `Channel` gate.",
)
def apply(self, backend, state, nqubits): # pragma: no cover
raise_error(
NotImplementedError, f"{self.name} cannot be applied to state vector."
)
def apply_density_matrix(self, backend, state, nqubits):
return backend.apply_channel_density_matrix(self, state, nqubits)
def to_choi(self, order: str = "row", backend=None):
"""Returns the Choi representation :math:`\\mathcal{E}`
of the Kraus channel :math:`\\{K_{\\alpha}\\}_{\\alpha}`.
.. math::
\\mathcal{E} = \\sum_{\\alpha} \\, |K_{\\alpha}\\rangle\\rangle \\langle\\langle K_{\\alpha}|
Args:
order (str, optional): If ``"row"``, vectorization of
Kraus operators is performed row-wise. If ``"column"``,
vectorization is done column-wise. If ``"system"``,
vectorization is done block-wise. Defaut is ``"row"``.
backend (``qibo.backends.abstract.Backend``, optional):
backend to be used in the execution. If ``None``,
it uses ``GlobalBackend()``. Defaults to ``None``.
Returns:
Choi representation of the Kraus channel.
"""
import numpy as np
from qibo.quantum_info.superoperator_transformations import vectorization
if backend is None: # pragma: no cover
from qibo.backends import GlobalBackend
backend = GlobalBackend()
self.nqubits = 1 + max(self.target_qubits)
if self.name not in [
"KrausChannel",
"ThermalRelaxationChannel",
"ReadoutErrorChannel",
]:
p0 = 1
for coeff in self.coefficients:
p0 = p0 - coeff
self.coefficients += (p0,)
self.gates += (I(*self.target_qubits),)
if self.name == "DepolarizingChannel":
num_qubits = len(self.target_qubits)
num_terms = 4**num_qubits
prob_pauli = self.init_kwargs["lam"] / num_terms
probs = (num_terms - 1) * [prob_pauli]
gates = []
for pauli_list in list(product([I, X, Y, Z], repeat=num_qubits))[1::]:
fgate = FusedGate(*self.target_qubits)
for j, pauli in enumerate(pauli_list):
fgate.append(pauli(j))
gates.append(fgate)
self.gates = tuple(gates)
self.coefficients = tuple(probs)
if self.name == "ThermalRelaxationChannel":
raise_error(
NotImplementedError,
"Superoperator representation not implemented for ThermalRelaxationChannel.",
)
super_op = np.zeros((4**self.nqubits, 4**self.nqubits), dtype="complex")
for coeff, gate in zip(self.coefficients, self.gates):
kraus_op = FusedGate(*range(self.nqubits))
kraus_op.append(gate)
kraus_op = kraus_op.asmatrix(backend)
kraus_op = vectorization(kraus_op, order=order)
super_op += coeff * np.outer(kraus_op, np.conj(kraus_op))
del kraus_op
super_op = backend.cast(super_op, dtype=super_op.dtype)
return super_op
def to_superop(self, order: str = "row", backend=None):
"""Returns the Liouville representation of the Kraus channel.
Args:
order (str, optional): If ``"row"``, vectorization of
Kraus operators is performed row-wise. If ``"column"``,
vectorization is done column-wise. If ``"system"``,
it raises ``NotImplementedError``. Defaut is ``"row"``.
backend (``qibo.backends.abstract.Backend``, optional):
backend to be used in the execution. If ``None``,
it uses ``GlobalBackend()``. Defaults to ``None``.
Returns:
Liouville representation of the channel.
"""
import numpy as np
from qibo.quantum_info.superoperator_transformations import choi_to_liouville
if backend is None: # pragma: no cover
from qibo.backends import GlobalBackend
backend = GlobalBackend()
super_op = self.to_choi(order=order, backend=backend)
super_op = choi_to_liouville(super_op, order=order)
super_op = backend.cast(super_op, dtype=super_op.dtype)
return super_op
def to_pauli_liouville(self, normalize: bool = False, backend=None):
"""Returns the Liouville representation of the Kraus channel
in the Pauli basis.
Args:
normalize (bool, optional): If ``True``, normalized basis ir returned.
Defaults to False.
backend (``qibo.backends.abstract.Backend``, optional): backend
to be used in the execution. If ``None``, it uses
``GlobalBackend()``. Defaults to ``None``.
Returns:
Pauli-Liouville representation of the channel.
"""
import numpy as np
from qibo.quantum_info.basis import comp_basis_to_pauli
if backend is None: # pragma: no cover
from qibo.backends import GlobalBackend
backend = GlobalBackend()
super_op = self.to_superop(backend=backend)
# unitary that transforms from comp basis to pauli basis
U = comp_basis_to_pauli(self.nqubits, normalize)
U = backend.cast(U, dtype=U.dtype)
super_op = U @ super_op @ np.transpose(np.conj(U))
super_op = backend.cast(super_op, dtype=super_op.dtype)
return super_op
class KrausChannel(Channel):
"""General channel defined by arbitrary Kraus operators.
Implements the following transformation:
.. math::
\\mathcal{E}(\\rho ) = \\sum _k A_k \\rho A_k^\\dagger
where A are arbitrary Kraus operators given by the user. Note that Kraus
operators set should be trace preserving, however this is not checked.
Simulation of this gate requires the use of density matrices.
For more information on channels and Kraus operators please check
`J. Preskill's notes <http://theory.caltech.edu/~preskill/ph219/chap3_15.pdf>`_.
Args:
ops (list): List of Kraus operators as pairs ``(qubits, Ak)`` where
``qubits`` refers the qubit ids that ``Ak`` acts on and ``Ak`` is
the corresponding matrix as a ``np.ndarray`` or ``tf.Tensor``.
Example:
.. testcode::
import numpy as np
from qibo.models import Circuit
from qibo import gates
# initialize circuit with 3 qubits
c = Circuit(3, density_matrix=True)
# define a sqrt(0.4) * X gate
a1 = np.sqrt(0.4) * np.array([[0, 1], [1, 0]])
# define a sqrt(0.6) * CNOT gate
a2 = np.sqrt(0.6) * np.array([[1, 0, 0, 0], [0, 1, 0, 0],
[0, 0, 0, 1], [0, 0, 1, 0]])
# define the channel rho -> 0.4 X{1} rho X{1} + 0.6 CNOT{0, 2} rho CNOT{0, 2}
channel = gates.KrausChannel([((1,), a1), ((0, 2), a2)])
# add the channel to the circuit
c.add(channel)
"""
def __init__(self, ops):
super().__init__()
self.name = "KrausChannel"
if isinstance(ops[0], Gate):
self.gates = tuple(ops)
self.target_qubits = tuple(
sorted({q for gate in ops for q in gate.target_qubits})
)
else:
gates, qubitset = [], set()
for qubits, matrix in ops:
rank = 2 ** len(qubits)
shape = tuple(matrix.shape)
if shape != (rank, rank):
raise_error(
ValueError,
f"Invalid Krauss operator shape {shape} for "
+ f"acting on {len(qubits)} qubits.",
)
qubitset.update(qubits)
gates.append(Unitary(matrix, *list(qubits)))
self.gates = tuple(gates)
self.target_qubits = tuple(sorted(qubitset))
self.init_args = [self.gates]
self.coefficients = len(self.gates) * (1,)
self.coefficient_sum = 1
class UnitaryChannel(KrausChannel):
"""Channel that is a probabilistic sum of unitary operations.
Implements the following transformation:
.. math::
\\mathcal{E}(\\rho ) = \\left (1 - \\sum _k p_k \\right )\\rho +
\\sum _k p_k U_k \\rho U_k^\\dagger
where U are arbitrary unitary operators and p are floats between 0 and 1.
Note that unlike :class:`qibo.gates.KrausChannel` which requires
density matrices, it is possible to simulate the unitary channel using
state vectors and probabilistic sampling. For more information on this
approach we refer to :ref:`Using repeated execution <repeatedexec-example>`.
Args:
probabilities (list): List of floats that correspond to the probability
that each unitary Uk is applied.
ops (list): List of operators as pairs ``(qubits, Uk)`` where
``qubits`` refers the qubit ids that ``Uk`` acts on and ``Uk`` is
the corresponding matrix as a ``np.ndarray``/``tf.Tensor``.
Must have the same length as the given probabilities ``p``.
"""
def __init__(self, probabilities, ops):
if len(probabilities) != len(ops):
raise_error(
ValueError,
f"Probabilities list has length {len(probabilities)} while "
+ f"{len(ops)} gates were given.",
)
for p in probabilities:
if p < 0 or p > 1:
raise_error(
ValueError,
f"Probabilities should be between 0 and 1 but {p} was given.",
)
super().__init__(ops)
self.name = "UnitaryChannel"
self.coefficients = tuple(probabilities)
self.coefficient_sum = sum(probabilities)
if self.coefficient_sum > 1 + PRECISION_TOL or self.coefficient_sum <= 0:
raise_error(
ValueError,
"UnitaryChannel probability sum should be "
+ f"between 0 and 1 but is {self.coefficient_sum}.",
)
self.init_args = [probabilities, self.gates]
def apply(self, backend, state, nqubits):
return backend.apply_channel(self, state, nqubits)
class PauliNoiseChannel(UnitaryChannel):
"""Noise channel that applies Pauli operators with given probabilities.
Implements the following transformation:
.. math::
\\mathcal{E}(\\rho ) = (1 - p_x - p_y - p_z) \\rho + p_x X\\rho X + p_y Y\\rho Y + p_z Z\\rho Z
which can be used to simulate phase flip and bit flip errors.
This channel can be simulated using either density matrices or state vectors
and sampling with repeated execution.
See :ref:`How to perform noisy simulation? <noisy-example>` for more
information.
Args:
q (int): Qubit id that the noise acts on.
px (float): Bit flip (X) error probability.
py (float): Y-error probability.
pz (float): Phase flip (Z) error probability.
"""
def __init__(self, q, px=0, py=0, pz=0):
warnings.warn(
"This channel will be removed in a later release. "
+ "Use GeneralizedPauliNoiseChannel instead.",
DeprecationWarning,
)
probs, gates = [], []
for p, gate in [(px, X), (py, Y), (pz, Z)]:
if p > 0:
probs.append(p)
gates.append(gate(q))
super().__init__(probs, gates)
self.name = "PauliNoiseChannel"
assert self.target_qubits == (q,)
self.init_args = [q]
self.init_kwargs = {"px": px, "py": py, "pz": pz}
class GeneralizedPauliNoiseChannel(UnitaryChannel):
"""Multi-qubit noise channel that applies Pauli operators with given probabilities.
Implements the following transformation:
.. math::
\\mathcal{E}(\\rho ) = \\left (1 - \\sum _{k} p_{k} \\right ) \\, \\rho +
\\sum_{k} \\, p_{k} \\, P_{k} \\, \\rho \\, P_{k}
where :math:`P_{k}` is the :math:`k`-th Pauli ``string`` and :math:`p_{k}` is
the probability associated to :math:`P_{k}`.
Example:
.. testcode::
import numpy as np
from itertools import product
from qibo.gates.channels import GeneralizedPauliNoiseChannel
qubits = (0, 2)
nqubits = len(qubits)
# excluding the Identity operator
paulis = list(product(["I", "X"], repeat=nqubits))[1:]
# this next line is optional
paulis = [''.join(pauli) for pauli in paulis]
probabilities = np.random.rand(len(paulis) + 1)
probabilities /= np.sum(probabilities)
#Excluding probability of Identity operator
probabilities = probabilities[1:]
channel = GeneralizedPauliNoiseChannel(
qubits, list(zip(paulis, probabilities))
)
This channel can be simulated using either density matrices or state vectors
and sampling with repeated execution.
See :ref:`How to perform noisy simulation? <noisy-example>` for more
information.
Args:
qubits (int or list or tuple): Qubits that the noise acts on.
operators (list): list of operators as pairs :math:`(P_{k}, p_{k})`.
"""
def __init__(self, qubits: Tuple[int, list, tuple], operators: list):
warnings.warn(
"The class GeneralizedPauliNoiseChannel will be renamed "
+ "PauliNoiseChannel in a later release."
)
if isinstance(qubits, int) is True:
qubits = (qubits,)
probabilities, paulis = [], []
for pauli, probability in operators:
probabilities.append(probability)
paulis.append(pauli)
single_paulis = {"I": I, "X": X, "Y": Y, "Z": Z}
gates = []
for pauli in paulis:
fgate = FusedGate(*qubits)
for q, p in zip(qubits, pauli):
fgate.append(single_paulis[p](q))
gates.append(fgate)
self.gates = tuple(gates)
self.coefficients = tuple(probabilities)
super().__init__(probabilities, gates)
self.name = "GeneralizedPauliNoiseChannel"
class DepolarizingChannel(Channel):
""":math:`n`-qubit Depolarizing quantum error channel,
.. math::
\\mathcal{E}(\\rho ) = (1 - \\lambda) \\rho +\\lambda \\text{Tr}_q[\\rho]\\otimes \\frac{I}{2^n}
where :math:`\\lambda` is the depolarizing error parameter
and :math:`0 \\le \\lambda \\le 4^n / (4^n - 1)`.
* If :math:`\\lambda = 1` this is a completely depolarizing channel
:math:`E(\\rho) = I / 2^n`
* If :math:`\\lambda = 4^n / (4^n - 1)` this is a uniform Pauli
error channel: :math:`E(\\rho) = \\sum_j P_j \\rho P_j / (4^n - 1)` for
all :math:`P_j \\neq I`.
Args:
q (tuple): Qubit ids that the noise acts on.
lam (float): Depolarizing error parameter.
"""
def __init__(self, q, lam: str = 0):
super().__init__()
num_qubits = len(q)
num_terms = 4**num_qubits
max_param = num_terms / (num_terms - 1)
if lam < 0 or lam > max_param:
raise_error(
ValueError,
f"Depolarizing parameter must be in between 0 and {max_param}.",
)
self.name = "DepolarizingChannel"
self.target_qubits = q
self.init_args = [q]
self.init_kwargs = {"lam": lam}
def apply_density_matrix(self, backend, state, nqubits):
lam = self.init_kwargs["lam"]
state_evolved = (1 - lam) * backend.cast(state) + (
lam / 2**nqubits
) * backend.cast(I(*range(nqubits)).asmatrix(backend))
return state_evolved
def apply(self, backend, state, nqubits):
num_qubits = len(self.target_qubits)
num_terms = 4**num_qubits
prob_pauli = self.init_kwargs["lam"] / num_terms
probs = (num_terms - 1) * [prob_pauli]
gates = []
for pauli_list in list(product([I, X, Y, Z], repeat=num_qubits))[1::]:
fgate = FusedGate(*self.target_qubits)
for j, pauli in enumerate(pauli_list):
fgate.append(pauli(j))
gates.append(fgate)
self.gates = tuple(gates)
self.coefficients = tuple(probs)
return backend.apply_channel(self, state, nqubits)
class ThermalRelaxationChannel(Channel):
"""Single-qubit thermal relaxation error channel.
Implements the following transformation:
If :math:`T_1 \\geq T_2`:
.. math::
\\mathcal{E} (\\rho ) = (1 - p_z - p_0 - p_1)\\rho + p_zZ\\rho Z
+ \\mathrm{Tr}_q[\\rho] \\otimes (p_0|0\\rangle \\langle 0| + p_1|1\\rangle \\langle 1|)
while if :math:`T_1 < T_2`:
.. math::
\\mathcal{E}(\\rho ) = \\mathrm{Tr} _\\mathcal{X}\\left [\\Lambda _{\\mathcal{X}\\mathcal{Y}}(\\rho _\\mathcal{X} ^T \\otimes \\mathbb{I}_\\mathcal{Y})\\right ]
with
.. math::
\\Lambda = \\begin{pmatrix}
1 - p_1 & 0 & 0 & e^{-t / T_2} \\\\
0 & p_1 & 0 & 0 \\\\
0 & 0 & p_0 & 0 \\\\
e^{-t / T_2} & 0 & 0 & 1 - p_0
\\end{pmatrix}
where :math:`p_0 = (1 - e^{-t / T_1})(1 - \\eta )` :math:`p_1 = (1 - e^{-t / T_1})\\eta`
and :math:`p_z = 1 - e^{-t / T_1} + e^{-t / T_2} - e^{t / T_1 - t / T_2}`.
Here :math:`\\eta` is the ``excited_population``
and :math:`t` is the ``time``, both controlled by the user.
This gate is based on
`Qiskit's thermal relaxation error channel <https://qiskit.org/documentation/stubs/qiskit.providers.aer.noise.thermal_relaxation_error.html#qiskit.providers.aer.noise.thermal_relaxation_error>`_.
Args:
q (int): Qubit id that the noise channel acts on.
t1 (float): T1 relaxation time. Should satisfy ``t1 > 0``.
t2 (float): T2 dephasing time.
Should satisfy ``t1 > 0`` and ``t2 < 2 * t1``.
time (float): the gate time for relaxation error.
excited_population (float): the population of the excited state at
equilibrium. Default is 0.
"""
def __init__(self, q, t1, t2, time, excited_population=0):
super().__init__()
self.name = "ThermalRelaxationChannel"
self.target_qubits = (q,)
self.init_args = [q, t1, t2, time]
self.init_kwargs = {"excited_population": excited_population}
# check given parameters
if excited_population < 0 or excited_population > 1:
raise_error(
ValueError, f"Invalid excited state population {excited_population}."
)
if time < 0:
raise_error(ValueError, "Invalid gate_time ({time} < 0).")
if t1 <= 0:
raise_error(
ValueError, "Invalid T_1 relaxation time parameter: " "T_1 <= 0."
)
if t2 <= 0:
raise_error(
ValueError, "Invalid T_2 relaxation time parameter: " "T_2 <= 0."
)
if t2 > 2 * t1:
raise_error(
ValueError,
"Invalid T_2 relaxation time parameter: " "T_2 greater than 2 * T_1.",
)
# calculate probabilities
self.t1, self.t2 = t1, t2
p_reset = 1 - exp(-time / t1)
self.coefficients = [
p_reset * (1 - excited_population),
p_reset * excited_population,
]
if t1 < t2:
self.coefficients.append(exp(-time / t2))
else:
pz = p_reset + exp(-time / t2) * (1 - exp(time / t1))
self.coefficients.append(pz)
def apply_density_matrix(self, backend, state, nqubits):
q = self.target_qubits[0]
if self.t1 < self.t2:
preset0, preset1, exp_t2 = self.coefficients
matrix = [
[1 - preset1, 0, 0, preset0],
[0, exp_t2, 0, 0],
[0, 0, exp_t2, 0],
[preset1, 0, 0, 1 - preset0],
]
qubits = (q, q + nqubits)
gate = Unitary(matrix, *qubits)
return backend.thermal_error_density_matrix(gate, state, nqubits)
else:
pz = self.coefficients[-1]
return (
backend.reset_error_density_matrix(self, state, nqubits)
- pz * backend.cast(state)
+ pz * backend.apply_gate_density_matrix(Z(0), state, nqubits)
)
class ReadoutErrorChannel(KrausChannel):
def __init__(self, q: Tuple[int, list, tuple], probabilities):
if any(sum(row) < 1 - PRECISION_TOL for row in probabilities) or any(
sum(row) > 1 + PRECISION_TOL for row in probabilities
):
raise_error(ValueError, "all rows of probabilities must sum to 1.")
if isinstance(q, int) is True:
q = (q,)
import numpy as np
d = len(probabilities)
operators = []
for j in range(d):
for k in range(d):
operator = np.zeros((d, d))
operator[j, k] = sqrt(probabilities[k, j])
operators.append(operator)
operators = list(zip([q] * len(operators), operators))
super().__init__(ops=operators)
self.name = "ReadoutErrorChannel"
class ResetChannel(Channel):
"""Single-qubit reset channel.
Implements the following transformation:
.. math::
\\mathcal{E}(\\rho ) = (1 - p_0 - p_1) \\rho
+ \\mathrm{Tr}_q[\\rho] \\otimes (p_0|0\\rangle \\langle 0| + p_1|1\\rangle \\langle 1|),
Args:
q (int): Qubit id that the channel acts on.
p0 (float): Probability to reset to 0.
p1 (float): Probability to reset to 1.
"""
def __init__(self, q, p0=0.0, p1=0.0):
super().__init__()
self.name = "ResetChannel"
self.target_qubits = (q,)
self.coefficients = (p0, p1)
self.init_args = [q]
self.init_kwargs = {"p0": p0, "p1": p1}
def apply_density_matrix(self, backend, state, nqubits):
return backend.reset_error_density_matrix(self, state, nqubits)