|
| 1 | +""" |
| 2 | +General Noise Model Construction. |
| 3 | +""" |
| 4 | +import logging |
| 5 | +from typing import Any, Sequence, Optional, List, Dict |
| 6 | + |
| 7 | +from .abstractcircuit import AbstractCircuit |
| 8 | +from . import gates |
| 9 | +from . import Circuit, DMCircuit |
| 10 | +from .cons import backend |
| 11 | +from .channels import KrausList |
| 12 | + |
| 13 | +Gate = gates.Gate |
| 14 | +Tensor = Any |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class NoiseConf: |
| 19 | + """ |
| 20 | + ``Noise Configuration`` class. |
| 21 | +
|
| 22 | + .. code-block:: python |
| 23 | +
|
| 24 | + error1 = tc.channels.generaldepolarizingchannel(0.1, 1) |
| 25 | + error2 = tc.channels.thermalrelaxationchannel(300, 400, 100, "ByChoi", 0) |
| 26 | + readout_error = [[0.9, 0.75], [0.4, 0.7]] |
| 27 | +
|
| 28 | + noise_conf = NoiseConf() |
| 29 | + noise_conf.add_noise("x", error1) |
| 30 | + noise_conf.add_noise("h", [error1, error2], [[0], [1]]) |
| 31 | + noise_conf.add_noise("readout", readout_error) |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self) -> None: |
| 35 | + """ |
| 36 | + Establish a noise configuration. |
| 37 | + """ |
| 38 | + self.nc = {} # type: ignore |
| 39 | + self.has_quantum = False |
| 40 | + self.has_readout = False |
| 41 | + |
| 42 | + def add_noise( |
| 43 | + self, |
| 44 | + gate_name: str, |
| 45 | + kraus: Sequence[KrausList], |
| 46 | + qubit: Optional[Sequence[Any]] = None, |
| 47 | + ) -> None: |
| 48 | + """ |
| 49 | + Add noise channels on specific gates and specific qubits in form of Kraus operators. |
| 50 | +
|
| 51 | + :param gate_name: noisy gate |
| 52 | + :type gate_name: str |
| 53 | + :param kraus: noise channel |
| 54 | + :type kraus: Sequence[Gate] |
| 55 | + :param qubit: the list of noisy qubit, defaults to None, indicating applying the noise channel on all qubits |
| 56 | + :type qubit: Optional[Sequence[Any]], optional |
| 57 | + """ |
| 58 | + if gate_name not in self.nc: |
| 59 | + qubit_kraus = {} |
| 60 | + else: |
| 61 | + qubit_kraus = self.nc[gate_name] |
| 62 | + |
| 63 | + if qubit is None: |
| 64 | + qubit_kraus["Default"] = kraus |
| 65 | + else: |
| 66 | + for i in range(len(qubit)): |
| 67 | + qubit_kraus[tuple(qubit[i])] = kraus[i] |
| 68 | + self.nc[gate_name] = qubit_kraus |
| 69 | + |
| 70 | + if gate_name == "readout": |
| 71 | + self.has_readout = True |
| 72 | + else: |
| 73 | + self.has_quantum = True |
| 74 | + |
| 75 | + |
| 76 | +def apply_qir_with_noise( |
| 77 | + c: Any, |
| 78 | + qir: List[Dict[str, Any]], |
| 79 | + noise_conf: NoiseConf, |
| 80 | + status: Optional[Tensor] = None, |
| 81 | +) -> Any: |
| 82 | + """ |
| 83 | +
|
| 84 | + :param c: A newly defined circuit |
| 85 | + :type c: AbstractCircuit |
| 86 | + :param qir: The qir of the clean circuit |
| 87 | + :type qir: List[Dict[str, Any]] |
| 88 | + :param noise_conf: Noise Configuration |
| 89 | + :type noise_conf: NoiseConf |
| 90 | + :param status: The status for Monte Carlo sampling, defaults to None |
| 91 | + :type status: 1D Tensor, optional |
| 92 | + :return: A newly constructed circuit with noise |
| 93 | + :rtype: AbstractCircuit |
| 94 | + """ |
| 95 | + quantum_index = 0 |
| 96 | + for d in qir: |
| 97 | + if "parameters" not in d: # paramized gate |
| 98 | + c.apply_general_gate_delayed(d["gatef"], d["name"])(c, *d["index"]) |
| 99 | + else: |
| 100 | + c.apply_general_variable_gate_delayed(d["gatef"], d["name"])( |
| 101 | + c, *d["index"], **d["parameters"] |
| 102 | + ) |
| 103 | + |
| 104 | + if isinstance(c, DMCircuit): |
| 105 | + if d["name"] in noise_conf.nc: |
| 106 | + if ( |
| 107 | + "Default" in noise_conf.nc[d["name"]] |
| 108 | + or d["index"] in noise_conf.nc[d["name"]] |
| 109 | + ): |
| 110 | + |
| 111 | + if "Default" in noise_conf.nc[d["name"]]: |
| 112 | + noise_kraus = noise_conf.nc[d["name"]]["Default"] |
| 113 | + if d["index"] in noise_conf.nc[d["name"]]: |
| 114 | + noise_kraus = noise_conf.nc[d["name"]][d["index"]] |
| 115 | + |
| 116 | + c.general_kraus(noise_kraus, *d["index"]) |
| 117 | + |
| 118 | + else: |
| 119 | + if d["name"] in noise_conf.nc: |
| 120 | + if ( |
| 121 | + "Default" in noise_conf.nc[d["name"]] |
| 122 | + or d["index"] in noise_conf.nc[d["name"]] |
| 123 | + ): |
| 124 | + |
| 125 | + if "Default" in noise_conf.nc[d["name"]]: |
| 126 | + noise_kraus = noise_conf.nc[d["name"]]["Default"] |
| 127 | + if d["index"] in noise_conf.nc[d["name"]]: |
| 128 | + noise_kraus = noise_conf.nc[d["name"]][d["index"]] |
| 129 | + |
| 130 | + if noise_kraus.is_unitary is True: |
| 131 | + c.unitary_kraus( |
| 132 | + noise_kraus, |
| 133 | + *d["index"], |
| 134 | + status=status[quantum_index] # type: ignore |
| 135 | + ) |
| 136 | + else: |
| 137 | + c.general_kraus( |
| 138 | + noise_kraus, |
| 139 | + *d["index"], |
| 140 | + status=status[quantum_index] # type: ignore |
| 141 | + ) |
| 142 | + quantum_index += 1 |
| 143 | + |
| 144 | + return c |
| 145 | + |
| 146 | + |
| 147 | +def circuit_with_noise( |
| 148 | + c: AbstractCircuit, noise_conf: NoiseConf, status: Optional[Tensor] = None |
| 149 | +) -> Any: |
| 150 | + """Noisify a clean circuit. |
| 151 | +
|
| 152 | + :param c: A clean circuit |
| 153 | + :type c: AbstractCircuit |
| 154 | + :param noise_conf: Noise Configuration |
| 155 | + :type noise_conf: NoiseConf |
| 156 | + :param status: The status for Monte Carlo sampling, defaults to None |
| 157 | + :type status: 1D Tensor, optional |
| 158 | + :return: A newly constructed circuit with noise |
| 159 | + :rtype: AbstractCircuit |
| 160 | + """ |
| 161 | + qir = c.to_qir() |
| 162 | + cnew: AbstractCircuit |
| 163 | + if isinstance(c, DMCircuit): |
| 164 | + cnew = DMCircuit(c._nqubits) |
| 165 | + else: |
| 166 | + cnew = Circuit(c._nqubits) |
| 167 | + cnew = apply_qir_with_noise(cnew, qir, noise_conf, status) |
| 168 | + return cnew |
| 169 | + |
| 170 | + |
| 171 | +def expectation_ps_noisfy( |
| 172 | + c: Any, |
| 173 | + x: Optional[Sequence[int]] = None, |
| 174 | + y: Optional[Sequence[int]] = None, |
| 175 | + z: Optional[Sequence[int]] = None, |
| 176 | + noise_conf: Optional[NoiseConf] = None, |
| 177 | + nmc: int = 1000, |
| 178 | + status: Optional[Tensor] = None, |
| 179 | +) -> Tensor: |
| 180 | + |
| 181 | + if noise_conf is None: |
| 182 | + noise_conf = NoiseConf() |
| 183 | + else: |
| 184 | + pass |
| 185 | + |
| 186 | + num_quantum = c.gate_count(list(noise_conf.nc.keys())) |
| 187 | + |
| 188 | + if noise_conf.has_readout is True: |
| 189 | + logger.warning("expectation_ps_noisfy can't support readout error.") |
| 190 | + else: |
| 191 | + pass |
| 192 | + |
| 193 | + if noise_conf.has_quantum is True: |
| 194 | + |
| 195 | + # density matrix |
| 196 | + if isinstance(c, DMCircuit): |
| 197 | + cnoise = circuit_with_noise(c, noise_conf) |
| 198 | + return cnoise.expectation_ps(x=x, y=y, z=z) |
| 199 | + |
| 200 | + # monte carlo |
| 201 | + else: |
| 202 | + |
| 203 | + def mcsim(status: Optional[Tensor]) -> Tensor: |
| 204 | + cnoise = circuit_with_noise(c, noise_conf, status) # type: ignore |
| 205 | + return cnoise.expectation_ps(x=x, y=y, z=z) |
| 206 | + |
| 207 | + mcsim_vmap = backend.vmap(mcsim, vectorized_argnums=0) |
| 208 | + if status is None: |
| 209 | + status = backend.implicit_randu([nmc, num_quantum]) |
| 210 | + else: |
| 211 | + pass |
| 212 | + value = backend.mean(mcsim_vmap(status)) |
| 213 | + |
| 214 | + return value |
| 215 | + |
| 216 | + else: |
| 217 | + return c.expectation_ps(x=x, y=y, z=z) |
| 218 | + |
| 219 | + |
| 220 | +def sample_expectation_ps_noisfy( |
| 221 | + c: Any, |
| 222 | + x: Optional[Sequence[int]] = None, |
| 223 | + y: Optional[Sequence[int]] = None, |
| 224 | + z: Optional[Sequence[int]] = None, |
| 225 | + noise_conf: Optional[NoiseConf] = None, |
| 226 | + nmc: int = 1000, |
| 227 | + shots: Optional[int] = None, |
| 228 | + status: Optional[Tensor] = None, |
| 229 | +) -> Tensor: |
| 230 | + |
| 231 | + if noise_conf is None: |
| 232 | + noise_conf = NoiseConf() |
| 233 | + else: |
| 234 | + pass |
| 235 | + |
| 236 | + num_quantum = c.gate_count(list(noise_conf.nc.keys())) |
| 237 | + |
| 238 | + if noise_conf.has_readout is True: |
| 239 | + readout_error = noise_conf.nc["readout"]["Default"] |
| 240 | + else: |
| 241 | + readout_error = None |
| 242 | + |
| 243 | + if noise_conf.has_quantum is True: |
| 244 | + |
| 245 | + # density matrix |
| 246 | + if isinstance(c, DMCircuit): |
| 247 | + cnoise = circuit_with_noise(c, noise_conf) # type: ignore |
| 248 | + return cnoise.sample_expectation_ps( |
| 249 | + x=x, y=y, z=z, shots=shots, readout_error=readout_error |
| 250 | + ) |
| 251 | + |
| 252 | + # monte carlo |
| 253 | + else: |
| 254 | + |
| 255 | + def mcsim(status: Optional[Tensor]) -> Tensor: |
| 256 | + cnoise = circuit_with_noise(c, noise_conf, status) # type: ignore |
| 257 | + return cnoise.sample_expectation_ps( |
| 258 | + x=x, y=y, z=z, shots=shots, readout_error=readout_error |
| 259 | + ) |
| 260 | + |
| 261 | + mcsim_vmap = backend.vmap(mcsim, vectorized_argnums=0) |
| 262 | + if status is None: |
| 263 | + status = backend.implicit_randu([nmc, num_quantum]) |
| 264 | + else: |
| 265 | + pass |
| 266 | + value = backend.mean(mcsim_vmap(status)) |
| 267 | + return value |
| 268 | + |
| 269 | + else: |
| 270 | + value = c.sample_expectation_ps( |
| 271 | + x=x, y=y, z=z, shots=shots, readout_error=readout_error |
| 272 | + ) |
| 273 | + return value |
0 commit comments