|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import sys
|
6 |
| -from typing import Any, Sequence |
| 6 | +from typing import Any, Sequence, Optional, Union |
| 7 | +from operator import and_ |
| 8 | +from functools import reduce |
7 | 9 |
|
8 | 10 | import numpy as np
|
9 | 11 |
|
@@ -78,6 +80,128 @@ def depolarizingchannel(px: float, py: float, pz: float) -> Sequence[Gate]:
|
78 | 80 | return [i, x, y, z]
|
79 | 81 |
|
80 | 82 |
|
| 83 | +def generaldepolarizingchannel( |
| 84 | + p: Union[float, Sequence[Any]], num_qubits: int = 1 |
| 85 | +) -> Sequence[Gate]: |
| 86 | + """Return a Depolarizing Channel |
| 87 | +
|
| 88 | + .. math:: |
| 89 | + \sqrt{1-p_x-p_y-p_z} |
| 90 | + \begin{bmatrix} |
| 91 | + 1 & 0\\ |
| 92 | + 0 & 1\\ |
| 93 | + \end{bmatrix}\qquad |
| 94 | + \sqrt{p_x} |
| 95 | + \begin{bmatrix} |
| 96 | + 0 & 1\\ |
| 97 | + 1 & 0\\ |
| 98 | + \end{bmatrix}\qquad |
| 99 | + \sqrt{p_y} |
| 100 | + \begin{bmatrix} |
| 101 | + 0 & -1j\\ |
| 102 | + 1j & 0\\ |
| 103 | + \end{bmatrix}\qquad |
| 104 | + \sqrt{p_z} |
| 105 | + \begin{bmatrix} |
| 106 | + 1 & 0\\ |
| 107 | + 0 & -1\\ |
| 108 | + \end{bmatrix} |
| 109 | +
|
| 110 | + :Example: |
| 111 | +
|
| 112 | + >>> cs=tc.channels.generaldepolarizingchannel([0.1,0.1,0.1],1) |
| 113 | + >>> tc.channels.kraus_identity_check(cs) |
| 114 | + >>> cs = tc.channels.generaldepolarizingchannel(0.02,2) |
| 115 | + >>> tc.channels.kraus_identity_check(cs) |
| 116 | +
|
| 117 | +
|
| 118 | + :param p: parameter for each Pauli channel |
| 119 | + :type p: Union[float, Sequence] |
| 120 | + :param num_qubits: number of qubits, defaults to 1 |
| 121 | + :type num_qubits: int, optional |
| 122 | + :return: Sequences of Gates |
| 123 | + :rtype: Sequence[Gate] |
| 124 | + """ |
| 125 | + |
| 126 | + if num_qubits == 1: |
| 127 | + |
| 128 | + if isinstance(p, float): |
| 129 | + |
| 130 | + assert p > 0 and p < 1 / 3, "p should be >0 and <1/3" |
| 131 | + probs = [1 - 3 * p] + 3 * [p] |
| 132 | + |
| 133 | + elif isinstance(p, list): |
| 134 | + |
| 135 | + assert reduce( |
| 136 | + and_, [pi > 0 and pi < 1 for pi in p] |
| 137 | + ), "p should be >0 and <1" |
| 138 | + probs = [1 - sum(p)] + p # type: ignore |
| 139 | + |
| 140 | + elif isinstance(p, tuple): |
| 141 | + |
| 142 | + p = list[p] # type: ignore |
| 143 | + assert reduce( |
| 144 | + and_, [pi > 0 and pi < 1 for pi in p] |
| 145 | + ), "p should be >0 and <1" |
| 146 | + probs = [1 - sum(p)] + p # type: ignore |
| 147 | + |
| 148 | + else: |
| 149 | + raise ValueError("p should be float or list") |
| 150 | + |
| 151 | + elif num_qubits == 2: |
| 152 | + |
| 153 | + if isinstance(p, float): |
| 154 | + |
| 155 | + assert p > 0 and p < 1, "p should be >0 and <1/15" |
| 156 | + probs = [1 - 15 * p] + 15 * [p] |
| 157 | + |
| 158 | + elif isinstance(p, list): |
| 159 | + |
| 160 | + assert reduce( |
| 161 | + and_, [pi > 0 and pi < 1 for pi in p] |
| 162 | + ), "p should be >0 and <1" |
| 163 | + probs = [1 - sum(p)] + p # type: ignore |
| 164 | + |
| 165 | + elif isinstance(p, tuple): |
| 166 | + |
| 167 | + p = list[p] # type: ignore |
| 168 | + assert reduce( |
| 169 | + and_, [pi > 0 and pi < 1 for pi in p] |
| 170 | + ), "p should be >0 and <1" |
| 171 | + probs = [1 - sum(p)] + p # type: ignore |
| 172 | + |
| 173 | + else: |
| 174 | + raise ValueError("p should be float or list") |
| 175 | + |
| 176 | + if num_qubits == 1: |
| 177 | + tup = [gates.i().tensor, gates.x().tensor, gates.y().tensor, gates.z().tensor] # type: ignore |
| 178 | + if num_qubits == 2: |
| 179 | + tup = [ |
| 180 | + gates.ii().tensor, # type: ignore |
| 181 | + gates.ix().tensor, # type: ignore |
| 182 | + gates.iy().tensor, # type: ignore |
| 183 | + gates.iz().tensor, # type: ignore |
| 184 | + gates.xi().tensor, # type: ignore |
| 185 | + gates.xx().tensor, # type: ignore |
| 186 | + gates.xy().tensor, # type: ignore |
| 187 | + gates.xz().tensor, # type: ignore |
| 188 | + gates.yi().tensor, # type: ignore |
| 189 | + gates.yx().tensor, # type: ignore |
| 190 | + gates.yy().tensor, # type: ignore |
| 191 | + gates.yz().tensor, # type: ignore |
| 192 | + gates.zi().tensor, # type: ignore |
| 193 | + gates.zx().tensor, # type: ignore |
| 194 | + gates.zy().tensor, # type: ignore |
| 195 | + gates.zz().tensor, # type: ignore |
| 196 | + ] |
| 197 | + |
| 198 | + Gkarus = [] |
| 199 | + for pro, paugate in zip(probs, tup): |
| 200 | + Gkarus.append(Gate(_sqrt(pro) * paugate)) |
| 201 | + |
| 202 | + return Gkarus |
| 203 | + |
| 204 | + |
81 | 205 | def amplitudedampingchannel(gamma: float, p: float) -> Sequence[Gate]:
|
82 | 206 | r"""
|
83 | 207 | Return an amplitude damping channel.
|
@@ -187,25 +311,32 @@ def phasedampingchannel(gamma: float) -> Sequence[Gate]:
|
187 | 311 | return [m0, m1]
|
188 | 312 |
|
189 | 313 |
|
190 |
| -def single_qubit_kraus_identity_check(kraus: Sequence[Gate]) -> None: |
| 314 | +def kraus_identity_check(kraus: Sequence[Gate]) -> None: |
191 | 315 | r"""Check identity of a single qubit Kraus operators.
|
192 | 316 |
|
193 | 317 | :Examples:
|
194 | 318 |
|
195 | 319 | >>> cs = resetchannel()
|
196 |
| - >>> tc.channels.single_qubit_kraus_identity_check(cs) |
| 320 | + >>> tc.channels.kraus_identity_check(cs) |
197 | 321 |
|
198 | 322 | .. math::
|
199 | 323 | \sum_{k}^{} K_k^{\dagger} K_k = I
|
200 | 324 |
|
201 | 325 | :param kraus: List of Kraus operators.
|
202 | 326 | :type kraus: Sequence[Gate]
|
203 | 327 | """
|
204 |
| - placeholder = backend.zeros([2, 2]) |
| 328 | + |
| 329 | + dim = backend.shape_tuple(kraus[0].tensor) |
| 330 | + shape = (len(dim), len(dim)) |
| 331 | + placeholder = backend.zeros(shape) |
205 | 332 | placeholder = backend.cast(placeholder, dtype=cons.dtypestr)
|
206 | 333 | for k in kraus:
|
| 334 | + k = Gate(backend.reshape(k.tensor, [len(dim), len(dim)])) |
207 | 335 | placeholder += backend.conj(backend.transpose(k.tensor, [1, 0])) @ k.tensor
|
208 |
| - np.testing.assert_allclose(placeholder, np.eye(2), atol=1e-5) |
| 336 | + np.testing.assert_allclose(placeholder, np.eye(shape[0]), atol=1e-5) |
| 337 | + |
| 338 | + |
| 339 | +single_qubit_kraus_identity_check = kraus_identity_check # backward compatibility |
209 | 340 |
|
210 | 341 |
|
211 | 342 | def kraus_to_super_gate(kraus_list: Sequence[Gate]) -> Tensor:
|
|
0 commit comments