Skip to content

Commit 7a08d7f

Browse files
committed
revise noisemodel-1
1 parent 3e81705 commit 7a08d7f

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

CHANGELOG.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020

2121
- Fixed `unitary_kraus` of Circuit class support for multi-qubit kraus channels, previous implementation fails to reshape the kraus tensor as matrix
2222

23-
<<<<<<< HEAD
24-
=======
2523
- Fixed `kraus_to_super_gate` bug when multi-qubit kraus channels are presented on tensorflow backend
2624

27-
>>>>>>> master
25+
2826
## 0.5.0
2927

3028
### Added

tensorcircuit/noisemodel.py

+34-35
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
General Noise Model Construction.
33
"""
44
import logging
5-
65
from typing import Any, Sequence, Optional, List, Dict
7-
from tensorcircuit.abstractcircuit import AbstractCircuit
6+
7+
from .abstractcircuit import AbstractCircuit
88
from . import gates
99
from . import Circuit, DMCircuit
1010
from .cons import backend
11+
from .channels import KrausList
1112

1213
Gate = gates.Gate
1314
Tensor = Any
@@ -22,19 +23,17 @@ class NoiseConf:
2223
2324
error1 = tc.channels.generaldepolarizingchannel(0.1, 1)
2425
error2 = tc.channels.thermalrelaxationchannel(300, 400, 100, "ByChoi", 0)
25-
readout_error = [[0.9, 0.75],[0.4, 0.7]]
26+
readout_error = [[0.9, 0.75], [0.4, 0.7]]
2627
2728
noise_conf = NoiseConf()
28-
noise_conf.add_noise("x",error1)
29-
noise_conf.add_noise("h",[error1,error2],[[0],[1]])
29+
noise_conf.add_noise("x", error1)
30+
noise_conf.add_noise("h", [error1, error2], [[0], [1]])
3031
noise_conf.add_noise("readout", readout_error)
31-
3232
"""
3333

3434
def __init__(self) -> None:
3535
"""
3636
Establish a noise configuration.
37-
3837
"""
3938
self.nc = {} # type: ignore
4039
self.has_quantum = False
@@ -43,16 +42,17 @@ def __init__(self) -> None:
4342
def add_noise(
4443
self,
4544
gate_name: str,
46-
kraus: Sequence[Gate],
45+
kraus: Sequence[KrausList],
4746
qubit: Optional[Sequence[Any]] = None,
4847
) -> None:
49-
"""Add noise channels on specific gates and specific qubits in form of Kraus operators.
48+
"""
49+
Add noise channels on specific gates and specific qubits in form of Kraus operators.
5050
5151
:param gate_name: noisy gate
5252
:type gate_name: str
5353
:param kraus: noise channel
5454
:type kraus: Sequence[Gate]
55-
:param qubit: the list of noisy qubit, defaults to None
55+
:param qubit: the list of noisy qubit, defaults to None, indicating applying the noise channel on all qubits
5656
:type qubit: Optional[Sequence[Any]], optional
5757
"""
5858
if gate_name not in self.nc:
@@ -74,25 +74,24 @@ def add_noise(
7474

7575

7676
def apply_qir_with_noise(
77-
c: AbstractCircuit,
77+
c: Any,
7878
qir: List[Dict[str, Any]],
7979
noise_conf: NoiseConf,
80-
status: Optional[Sequence[Any]] = None,
81-
) -> AbstractCircuit:
80+
status: Optional[Tensor] = None,
81+
) -> Any:
8282
"""
8383
8484
:param c: A newly defined circuit
8585
:type c: AbstractCircuit
86-
:param qir: The qir of the objective circuit
86+
:param qir: The qir of the clean circuit
8787
:type qir: List[Dict[str, Any]]
8888
:param noise_conf: Noise Configuration
8989
:type noise_conf: NoiseConf
9090
:param status: The status for Monte Carlo sampling, defaults to None
91-
:type status: Optional[Sequence[Any]], optional
91+
:type status: 1D Tensor, optional
9292
:return: A newly constructed circuit with noise
9393
:rtype: AbstractCircuit
9494
"""
95-
9695
quantum_index = 0
9796
for d in qir:
9897
if "parameters" not in d: # paramized gate
@@ -129,13 +128,13 @@ def apply_qir_with_noise(
129128
noise_kraus = noise_conf.nc[d["name"]][d["index"]]
130129

131130
if noise_kraus.is_unitary is True:
132-
c.unitary_kraus( # type: ignore
131+
c.unitary_kraus(
133132
noise_kraus,
134133
*d["index"],
135134
status=status[quantum_index] # type: ignore
136135
)
137136
else:
138-
c.general_kraus( # type: ignore
137+
c.general_kraus(
139138
noise_kraus,
140139
*d["index"],
141140
status=status[quantum_index] # type: ignore
@@ -146,16 +145,16 @@ def apply_qir_with_noise(
146145

147146

148147
def circuit_with_noise(
149-
c: AbstractCircuit, noise_conf: NoiseConf, status: Optional[Sequence[Any]] = None
150-
) -> AbstractCircuit:
151-
"""Noisify an objective circuit.
148+
c: AbstractCircuit, noise_conf: NoiseConf, status: Optional[Tensor] = None
149+
) -> Any:
150+
"""Noisify a clean circuit.
152151
153-
:param c: An objective circuit
152+
:param c: A clean circuit
154153
:type c: AbstractCircuit
155154
:param noise_conf: Noise Configuration
156155
:type noise_conf: NoiseConf
157156
:param status: The status for Monte Carlo sampling, defaults to None
158-
:type status: Optional[Sequence[Any]], optional
157+
:type status: 1D Tensor, optional
159158
:return: A newly constructed circuit with noise
160159
:rtype: AbstractCircuit
161160
"""
@@ -170,13 +169,13 @@ def circuit_with_noise(
170169

171170

172171
def expectation_ps_noisfy(
173-
c: AbstractCircuit,
172+
c: Any,
174173
x: Optional[Sequence[int]] = None,
175174
y: Optional[Sequence[int]] = None,
176175
z: Optional[Sequence[int]] = None,
177176
noise_conf: Optional[NoiseConf] = None,
178177
nmc: int = 1000,
179-
status: Optional[Sequence[Any]] = None,
178+
status: Optional[Tensor] = None,
180179
) -> Tensor:
181180

182181
if noise_conf is None:
@@ -201,8 +200,8 @@ def expectation_ps_noisfy(
201200
# monte carlo
202201
else:
203202

204-
def mcsim(status): # type: ignore
205-
cnoise = circuit_with_noise(c, noise_conf, status) # type: ignore
203+
def mcsim(status: Optional[Tensor]) -> Tensor:
204+
cnoise = circuit_with_noise(c, noise_conf, status) # type: ignore
206205
return cnoise.expectation_ps(x=x, y=y, z=z)
207206

208207
mcsim_vmap = backend.vmap(mcsim, vectorized_argnums=0)
@@ -219,14 +218,14 @@ def mcsim(status): # type: ignore
219218

220219

221220
def sample_expectation_ps_noisfy(
222-
c: AbstractCircuit,
221+
c: Any,
223222
x: Optional[Sequence[int]] = None,
224223
y: Optional[Sequence[int]] = None,
225224
z: Optional[Sequence[int]] = None,
226225
noise_conf: Optional[NoiseConf] = None,
227226
nmc: int = 1000,
228227
shots: Optional[int] = None,
229-
status: Optional[Sequence[Any]] = None,
228+
status: Optional[Tensor] = None,
230229
) -> Tensor:
231230

232231
if noise_conf is None:
@@ -245,17 +244,17 @@ def sample_expectation_ps_noisfy(
245244

246245
# density matrix
247246
if isinstance(c, DMCircuit):
248-
cnoise = circuit_with_noise(c, noise_conf)
249-
return cnoise.sample_expectation_ps( # type: ignore
247+
cnoise = circuit_with_noise(c, noise_conf) # type: ignore
248+
return cnoise.sample_expectation_ps(
250249
x=x, y=y, z=z, shots=shots, readout_error=readout_error
251250
)
252251

253252
# monte carlo
254253
else:
255254

256-
def mcsim(status): # type: ignore
257-
cnoise = circuit_with_noise(c, noise_conf, status) # type: ignore
258-
return cnoise.sample_expectation_ps( # type: ignore
255+
def mcsim(status: Optional[Tensor]) -> Tensor:
256+
cnoise = circuit_with_noise(c, noise_conf, status) # type: ignore
257+
return cnoise.sample_expectation_ps(
259258
x=x, y=y, z=z, shots=shots, readout_error=readout_error
260259
)
261260

@@ -268,7 +267,7 @@ def mcsim(status): # type: ignore
268267
return value
269268

270269
else:
271-
value = c.sample_expectation_ps( # type: ignore
270+
value = c.sample_expectation_ps(
272271
x=x, y=y, z=z, shots=shots, readout_error=readout_error
273272
)
274273
return value

0 commit comments

Comments
 (0)