|
11 | 11 | from pytket.pauli import QubitPauliString
|
12 | 12 | from numpy.random import Generator
|
13 | 13 | from enum import Enum
|
| 14 | +from itertools import product |
| 15 | +from scipy.linalg import fractional_matrix_power # type: ignore |
| 16 | +from numpy.typing import NDArray |
14 | 17 |
|
15 | 18 |
|
16 | 19 | Direction = Enum('Direction', ['forward', 'backward'])
|
@@ -52,9 +55,160 @@ def __init__(
|
52 | 55 | + " but should be less than or equal to 1."
|
53 | 56 | )
|
54 | 57 |
|
| 58 | + # If the given distribution is empty then no |
| 59 | + # noise will be acted. |
| 60 | + if distribution == {}: |
| 61 | + pass |
| 62 | + # If it it not empty then we check that the number |
| 63 | + # of qubits in each of the errors match. |
| 64 | + else: |
| 65 | + n_qubits = len(list(distribution.keys())[0]) |
| 66 | + if not all(len(error) == n_qubits for error in distribution.keys()): |
| 67 | + raise Exception("Errors must all act on the same number of qubits.") |
| 68 | + |
55 | 69 | self.distribution = distribution
|
56 | 70 | self.rng = rng
|
57 | 71 |
|
| 72 | + @property |
| 73 | + def identity_error_rate(self) -> float: |
| 74 | + """The rate at which no error occurs. |
| 75 | +
|
| 76 | + :return: Rate at which no error occurs. |
| 77 | + Calculated as 1 minus the total error rate of |
| 78 | + error in this distribution. |
| 79 | + :rtype: float |
| 80 | + """ |
| 81 | + return 1 - sum(self.distribution.values()) |
| 82 | + |
| 83 | + def to_ptm(self) -> Tuple[NDArray, Dict[Tuple[Pauli, ...], int]]: |
| 84 | + """Convert error distribution to Pauli Transfer Matrix (PTM) form. |
| 85 | +
|
| 86 | + :return: PTM of error distribution and Pauli index dictionary. |
| 87 | + The Pauli index dictionary maps Pauli errors to their |
| 88 | + index in the PTM |
| 89 | + :rtype: Tuple[NDArray, Dict[Tuple[Pauli, ...], int]] |
| 90 | + """ |
| 91 | + |
| 92 | + # Initialise an empty PTM and index dictionary |
| 93 | + # of the appropriate size. |
| 94 | + ptm = np.zeros((4**self.n_qubits, 4**self.n_qubits)) |
| 95 | + pauli_index = { |
| 96 | + pauli: index |
| 97 | + for index, pauli |
| 98 | + in enumerate(product({Pauli.I, Pauli.X, Pauli.Y, Pauli.Z}, repeat=self.n_qubits)) |
| 99 | + } |
| 100 | + |
| 101 | + # For each pauli, calculate the corresponding |
| 102 | + # PTM entry as a sum pf error weights multiplied by +/-1 |
| 103 | + # Depending on commutation relations. |
| 104 | + for pauli_tuple, index in pauli_index.items(): |
| 105 | + |
| 106 | + pauli = QermitPauli.from_pauli_iterable( |
| 107 | + pauli_iterable=pauli_tuple, |
| 108 | + qubit_list=[Qubit(i) for i in range(self.n_qubits)] |
| 109 | + ) |
| 110 | + |
| 111 | + # Can add the identity error rate. |
| 112 | + # This will not come up in the following for loop |
| 113 | + # as the error distribution does not save |
| 114 | + # the rate at which no errors occur. |
| 115 | + ptm[index][index] += self.identity_error_rate |
| 116 | + |
| 117 | + for error, error_rate in self.distribution.items(): |
| 118 | + error_pauli = QermitPauli.from_pauli_iterable( |
| 119 | + pauli_iterable=error, |
| 120 | + qubit_list=[Qubit(i) for i in range(self.n_qubits)] |
| 121 | + ) |
| 122 | + |
| 123 | + ptm[index][index] += error_rate * QermitPauli.commute_coeff(pauli_one=pauli, pauli_two=error_pauli) |
| 124 | + |
| 125 | + # Some checks that the form of the PTM is correct. |
| 126 | + identity = tuple(Pauli.I for _ in range(self.n_qubits)) |
| 127 | + if not abs(ptm[pauli_index[identity]][pauli_index[identity]] - 1.0) < 10**(-6): |
| 128 | + raise Exception( |
| 129 | + "The identity entry of the PTM is incorrect. " |
| 130 | + + "This is a fault in Qermit. " |
| 131 | + + "Please report this as an issue." |
| 132 | + ) |
| 133 | + |
| 134 | + if not self == ErrorDistribution.from_ptm(ptm=ptm, pauli_index=pauli_index): |
| 135 | + raise Exception( |
| 136 | + "From PTM does not match to PTM. " |
| 137 | + + "This is a fault in Qermit. " |
| 138 | + + "Please report this as an issue." |
| 139 | + ) |
| 140 | + |
| 141 | + return ptm, pauli_index |
| 142 | + |
| 143 | + @classmethod |
| 144 | + def from_ptm(cls, ptm: NDArray, pauli_index: Dict[Tuple[Pauli, ...], int]) -> ErrorDistribution: |
| 145 | + """Convert a Pauli Transfer Matrix (PTM) to an error distribution. |
| 146 | +
|
| 147 | + :param ptm: Pauli Transfer Matrix to convert. Should be a 4^n by 4^n matrix |
| 148 | + where n is the number of qubits. |
| 149 | + :type ptm: NDArray |
| 150 | + :param pauli_index: A dictionary mapping Pauli errors to |
| 151 | + their index in the PTM. |
| 152 | + :type pauli_index: Dict[Tuple[Pauli, ...], int] |
| 153 | + :return: The converted error distribution. |
| 154 | + :rtype: ErrorDistribution |
| 155 | + """ |
| 156 | + |
| 157 | + if ptm.ndim != 2: |
| 158 | + raise Exception( |
| 159 | + f"This given matrix is not has dimension {ptm.ndim} " |
| 160 | + + "but should have dimension 2." |
| 161 | + ) |
| 162 | + |
| 163 | + if ptm.shape[0] != ptm.shape[1]: |
| 164 | + raise Exception( |
| 165 | + "The dimensions of the given PTM are " |
| 166 | + + f"{ptm.shape[0]} and {ptm.shape[1]} " |
| 167 | + + "but they should match." |
| 168 | + ) |
| 169 | + |
| 170 | + n_qubit = math.log(ptm.shape[0], 4) |
| 171 | + if n_qubit % 1 != 0.0: |
| 172 | + raise Exception( |
| 173 | + "The given PTM should have a dimension of the form 4^n " |
| 174 | + + "where n is the number of qubits." |
| 175 | + ) |
| 176 | + |
| 177 | + if not np.array_equal(ptm, np.diag(np.diag(ptm))): |
| 178 | + raise Exception( |
| 179 | + "The given PTM is not diagonal as it should be." |
| 180 | + ) |
| 181 | + |
| 182 | + # calculate the error rates by solving simultaneous |
| 183 | + # linear equations. In particular the matrix to invert |
| 184 | + # is the matrix of commutation values. |
| 185 | + commutation_matrix = np.zeros(ptm.shape) |
| 186 | + for pauli_one_tuple, index_one in pauli_index.items(): |
| 187 | + pauli_one = QermitPauli.from_pauli_iterable( |
| 188 | + pauli_iterable=pauli_one_tuple, |
| 189 | + qubit_list=[Qubit(i) for i in range(len(pauli_one_tuple))] |
| 190 | + ) |
| 191 | + for pauli_two_tuple, index_two in pauli_index.items(): |
| 192 | + pauli_two = QermitPauli.from_pauli_iterable( |
| 193 | + pauli_iterable=pauli_two_tuple, |
| 194 | + qubit_list=[Qubit(i) for i in range(len(pauli_two_tuple))] |
| 195 | + ) |
| 196 | + commutation_matrix[index_one][index_two] = QermitPauli.commute_coeff(pauli_one=pauli_one, pauli_two=pauli_two) |
| 197 | + |
| 198 | + error_rate_list = np.matmul(ptm.diagonal(), np.linalg.inv(commutation_matrix)) |
| 199 | + distribution = { |
| 200 | + error: error_rate_list[index] |
| 201 | + for error, index in pauli_index.items() |
| 202 | + if (error_rate_list[index] > 10**(-6)) and error != tuple(Pauli.I for _ in range(int(n_qubit))) |
| 203 | + } |
| 204 | + return cls(distribution=distribution) |
| 205 | + |
| 206 | + @property |
| 207 | + def n_qubits(self) -> int: |
| 208 | + """The number of qubits this error distribution acts on. |
| 209 | + """ |
| 210 | + return len(list(self.distribution.keys())[0]) |
| 211 | + |
58 | 212 | def __eq__(self, other: object) -> bool:
|
59 | 213 | """Check equality of two instances of ErrorDistribution by ensuring
|
60 | 214 | that all keys in distribution match, and that the probabilities are
|
@@ -208,6 +362,22 @@ def plot(self):
|
208 | 362 |
|
209 | 363 | return fig
|
210 | 364 |
|
| 365 | + def scale(self, scaling_factor: float) -> ErrorDistribution: |
| 366 | + """Scale the error rates of this error distribution. |
| 367 | + This is done by converting the error distribution to a PTM, |
| 368 | + scaling that matrix appropriately, and converting back to a |
| 369 | + new error distribution. |
| 370 | +
|
| 371 | + :param scaling_factor: The factor by which the noise should be scaled. |
| 372 | + :type scaling_factor: float |
| 373 | + :return: A new error distribution with the noise scaled. |
| 374 | + :rtype: ErrorDistribution |
| 375 | + """ |
| 376 | + |
| 377 | + ptm, pauli_index = self.to_ptm() |
| 378 | + scaled_ptm = fractional_matrix_power(ptm, scaling_factor) |
| 379 | + return ErrorDistribution.from_ptm(ptm=scaled_ptm, pauli_index=pauli_index) |
| 380 | + |
211 | 381 |
|
212 | 382 | class LogicalErrorDistribution:
|
213 | 383 | """
|
@@ -304,6 +474,22 @@ def __init__(self, noise_model: Dict[OpType, ErrorDistribution]):
|
304 | 474 |
|
305 | 475 | self.noise_model = noise_model
|
306 | 476 |
|
| 477 | + def scale(self, scaling_factor: float) -> NoiseModel: |
| 478 | + """Generate new error model where all error rates have been scaled by |
| 479 | + the given scaling factor. |
| 480 | +
|
| 481 | + :param scaling_factor: Factor by which to scale the error rates. |
| 482 | + :type scaling_factor: float |
| 483 | + :return: New noise model with scaled error rates. |
| 484 | + :rtype: NoiseModel |
| 485 | + """ |
| 486 | + return NoiseModel( |
| 487 | + noise_model={ |
| 488 | + op_type: error_distribution.scale(scaling_factor=scaling_factor) |
| 489 | + for op_type, error_distribution in self.noise_model.items() |
| 490 | + } |
| 491 | + ) |
| 492 | + |
307 | 493 | def reset_rng(self, rng: Generator):
|
308 | 494 | """Reset randomness generator.
|
309 | 495 |
|
|
0 commit comments