Skip to content

Commit dd27aa8

Browse files
feat: targets as an option for NoiseModel
1 parent 63f103b commit dd27aa8

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

mpqp/core/circuit.py

+3
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ def add(self, components: OneOrMany[Instruction | NoiseModel]):
222222
components.size = self.nb_qubits
223223

224224
if isinstance(components, NoiseModel):
225+
if len(components.targets) == 0:
226+
components.targets = [target for target in range(self.nb_qubits)]
227+
225228
basisMs = [
226229
instr for instr in self.instructions if isinstance(instr, BasisMeasure)
227230
]

mpqp/execution/providers/google.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def run_google(job: Job) -> Result:
3030
A Result after submission and execution of the job.
3131
Note:
3232
33-
This function is not meant to be used directly, please use
33+
Note:
34+
This function is not meant to be used directly, please use
3435
:func:``run<mpqp.execution.runner.run>`` instead.
3536
"""
3637
return run_local(job) if not job.device.is_remote() else run_google_remote(job)

mpqp/noise/noise_model.py

+26-22
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class NoiseModel(ABC):
3434
or negative. When the size of the gate is higher than the number of target qubits.
3535
"""
3636

37-
def __init__(self, targets: list[int], gates: Optional[list[type[Gate]]] = None):
38-
if len(targets) == 0:
39-
raise ValueError("Expected non-empty target list")
37+
def __init__(
38+
self, targets: list[int] = [], gates: Optional[list[type[Gate]]] = None
39+
):
4040

4141
if len(set(targets)) != len(targets):
4242
raise ValueError(f"Duplicate indices in targets: {targets}")
@@ -53,7 +53,9 @@ def __init__(self, targets: list[int], gates: Optional[list[type[Gate]]] = None)
5353
" the noise target, please add `nb_qubits` to this "
5454
"class as a class attribute."
5555
)
56-
if nb_qubits > len(targets): # pyright: ignore[reportOperatorIssue]
56+
if len(targets) != 0 and nb_qubits > len(
57+
targets
58+
): # pyright: ignore[reportOperatorIssue]
5759
raise ValueError(
5860
"Size mismatch between gate and noise: gate size is "
5961
f"{nb_qubits} but noise size is {len(targets)}"
@@ -125,30 +127,32 @@ class Depolarizing(NoiseModel):
125127
Examples:
126128
>>> circuit = QCircuit([H(i) for i in range(3)])
127129
>>> d1 = Depolarizing(0.32, list(range(circuit.nb_qubits)))
128-
>>> d2 = Depolarizing(0.05, [0, 1], dimension=2)
129-
>>> d3 = Depolarizing(0.12, [2], gates=[H, Rx, Ry, Rz])
130-
>>> d4 = Depolarizing(0.05, [0, 1, 2], dimension=2, gates=[CNOT, CZ])
131-
>>> circuit.add([d1, d2, d3, d4])
130+
>>> d2 = Depolarizing(0.01)
131+
>>> d3 = Depolarizing(0.05, [0, 1], dimension=2)
132+
>>> d4 = Depolarizing(0.12, [2], gates=[H, Rx, Ry, Rz])
133+
>>> d5 = Depolarizing(0.05, [0, 1, 2], dimension=2, gates=[CNOT, CZ])
134+
>>> circuit.add([d1, d2, d3, d4, d5])
132135
>>> print(circuit) # doctest: +NORMALIZE_WHITESPACE
133-
┌───┐
134-
q_0: ┤ H ├
135-
├───┤
136-
q_1: ┤ H ├
137-
├───┤
138-
q_2: ┤ H ├
139-
└───┘
140-
NoiseModel:
141-
Depolarizing(0.32, [0, 1, 2], 1)
142-
Depolarizing(0.05, [0, 1], 2)
143-
Depolarizing(0.12, [2], 1, [H, Rx, Ry, Rz])
144-
Depolarizing(0.05, [0, 1, 2], 2, [CNOT, CZ])
136+
┌───┐
137+
q_0: ┤ H ├
138+
├───┤
139+
q_1: ┤ H ├
140+
├───┤
141+
q_2: ┤ H ├
142+
└───┘
143+
NoiseModel:
144+
Depolarizing(0.32, [0, 1, 2], 1)
145+
Depolarizing(0.01, [0, 1, 2], 1)
146+
Depolarizing(0.05, [0, 1], 2)
147+
Depolarizing(0.12, [2], 1, [H, Rx, Ry, Rz])
148+
Depolarizing(0.05, [0, 1, 2], 2, [CNOT, CZ])
145149
146150
"""
147151

148152
def __init__(
149153
self,
150154
prob: float,
151-
targets: list[int],
155+
targets: list[int] = [],
152156
dimension: int = 1,
153157
gates: Optional[list[type[Gate]]] = None,
154158
):
@@ -179,7 +183,7 @@ def __init__(
179183
)
180184

181185
nb_targets = len(targets)
182-
if nb_targets < dimension:
186+
if nb_targets != 0 and nb_targets < dimension:
183187
raise ValueError(
184188
f"Number of target qubits {nb_targets} should be higher than the dimension {dimension}."
185189
)

0 commit comments

Comments
 (0)