mimiqcircuits.noisemodel¶
Noise model definitions and application.
A NoiseModel is a list of noise rules. apply_noise_model()
goes through a circuit and, for each instruction, applies the first rule that
matches it. Rules are tried by priority (lower value first).
Examples
>>> from mimiqcircuits import *
>>> # depolarizing noise after every CX, readout error after every measurement
>>> model = NoiseModel()
>>> _ = model.add_operation_noise(GateCX(), Depolarizing2(0.01))
>>> _ = model.add_readout_noise(ReadoutErr(0.01, 0.02))
>>> c = Circuit()
>>> _ = c.push(GateCX(), 0, 1)
>>> _ = c.push(Measure(), 1, 0)
>>> apply_noise_model(c, model)
2-qubit, 1-bit circuit with 4 instructions:
├── CX @ q[0], q[1]
├── Depolarizing(0.01) @ q[0:1]
├── M @ q[1], c[0]
└── RErr(0.01, 0.02) @ c[0]
Functions
|
Return a copy of |
Classes
Base class for the rules of a |
|
|
Add noise chosen by user-supplied functions. |
|
Add noise to an operation acting on exactly the given qubits. |
|
Add a readout error after measurements on exactly the given qubits. |
|
Add a readout error after every measurement. |
|
Replace every |
|
Collection of noise rules applied to circuits. |
|
Add noise to every instance of an operation. |
|
Replace every |
|
Add noise to an operation whose qubits are all in a set. |
|
Add a readout error after measurements whose qubits are all in a set. |
- class AbstractNoiseRule[source]¶
Bases:
AbstractCircuitRuleBase class for the rules of a
NoiseModel.A rule says which instructions it matches (
matches), what to emit for a match (apply_rule), where to put it (before,replaces) and how early it is tried (priority, lower first).
- class GlobalReadoutNoise(noise)[source]¶
Bases:
AbstractNoiseRuleAdd a readout error after every measurement.
The readout error acts on the classical bits the measurement writes to, so it is placed right after the measurement and does not replace it.
- Parameters:
noise (ReadoutErr) – The
ReadoutErrto apply.
Examples
>>> from mimiqcircuits import * >>> # every measurement gets a 1% / 2% readout error >>> GlobalReadoutNoise(ReadoutErr(0.01, 0.02)) GlobalReadoutNoise(noise=RErr(0.01,0.02))
- priority()[source]¶
Return the priority of the rule. Lower values are tried first.
The default is
100.
- matches(inst)[source]¶
Return
Trueif the rule applies to the instructioninst.- Raises:
NotImplementedError – Unless a subclass implements it.
- apply_rule(inst)[source]¶
Return what the rule produces for
inst, orNoneif it does not apply.- Raises:
NotImplementedError – Unless a subclass implements it.
- class ExactQubitReadoutNoise(qubits, noise)[source]¶
Bases:
AbstractNoiseRuleAdd a readout error after measurements on exactly the given qubits.
A measurement matches only when its qubits are
qubits, in the same order. The readout error acts on the bits the measurement writes to.- Parameters:
qubits (Sequence[int]) – Qubits the measurement must act on, in order.
noise (ReadoutErr) – The
ReadoutErrto apply.
- Raises:
ValueError – If
qubitsis empty or has repetitions.
Examples
>>> from mimiqcircuits import * >>> # only a measurement of exactly (q0, q1), in this order, matches >>> ExactQubitReadoutNoise([0, 1], ReadoutErr(0.01, 0.02)) ExactQubitReadoutNoise(qubits=(0, 1), noise=RErr(0.01,0.02))
- priority()[source]¶
Return the priority of the rule. Lower values are tried first.
The default is
100.
- matches(inst)[source]¶
Return
Trueif the rule applies to the instructioninst.- Raises:
NotImplementedError – Unless a subclass implements it.
- apply_rule(inst)[source]¶
Return what the rule produces for
inst, orNoneif it does not apply.- Raises:
NotImplementedError – Unless a subclass implements it.
- class SetQubitReadoutNoise(qubits, noise)[source]¶
Bases:
AbstractNoiseRuleAdd a readout error after measurements whose qubits are all in a set.
The readout error acts on the bits the measurement writes to.
- Parameters:
qubits (Iterable[int]) – Qubits where the readout error applies.
noise (ReadoutErr) – The
ReadoutErrto apply.
- Raises:
ValueError – If
qubitsis empty or has repetitions.
Examples
>>> from mimiqcircuits import * >>> # measurements of qubits 0, 2 or 4 get the readout error >>> SetQubitReadoutNoise([0, 2, 4], ReadoutErr(0.01, 0.02)) SetQubitReadoutNoise(qubits=[0, 2, 4], noise=RErr(0.01,0.02))
- priority()[source]¶
Return the priority of the rule. Lower values are tried first.
The default is
100.
- matches(inst)[source]¶
Return
Trueif the rule applies to the instructioninst.- Raises:
NotImplementedError – Unless a subclass implements it.
- apply_rule(inst)[source]¶
Return what the rule produces for
inst, orNoneif it does not apply.- Raises:
NotImplementedError – Unless a subclass implements it.
- class OperationInstanceNoise(operation, noise, before=False, replace=False)[source]¶
Bases:
AbstractNoiseRuleAdd noise to every instance of an operation.
An instruction matches when its operation has the same type as
operation. Ifoperationhas symbolic parameters, such asGateRX(a), any parameter value matches and the symbols innoiseare replaced by the values of the matched instance. Otherwise the operation must be equal tooperation.- Parameters:
operation (Operation) – Operation to match: a gate, measurement, reset,
Block,Repeat,IfStatementorWhileStatement. Symbolic parameters must be plain symbols.noise (krauschannel | Gate) – Kraus channel or gate to add, on the same number of qubits as
operation.before (bool) – Add the noise before the operation instead of after it.
replace (bool) – Replace the operation with the noise.
- Raises:
ValueError – If
operationis of an unsupported kind, ifnoiseacts on a different number of qubits, or if bothbeforeandreplaceare set.
Examples
>>> from mimiqcircuits import * >>> from symengine import symbols, pi >>> # the noise strength grows with the rotation angle >>> a = symbols("a") >>> rule = OperationInstanceNoise(GateRX(a), Depolarizing1(a / pi)) >>> c = Circuit().push(GateRX(pi / 2), 0) >>> NoiseModel([rule]).apply_noise_model(c) 1-qubit circuit with 2 instructions: ├── RX((1/2)*pi) @ q[0] └── Depolarizing(0.5) @ q[0]
- priority()[source]¶
Return the priority of the rule. Lower values are tried first.
The default is
100.
- replaces()[source]¶
Return
Trueif the rule’s output replaces the matched instruction.The default is
False: the matched instruction is kept.
- matches(inst)[source]¶
Return
Trueif the rule applies to the instructioninst.- Raises:
NotImplementedError – Unless a subclass implements it.
- apply_rule(inst)[source]¶
Return what the rule produces for
inst, orNoneif it does not apply.- Raises:
NotImplementedError – Unless a subclass implements it.
- class ExactOperationInstanceQubitNoise(operation, qubits, noise, before=False, replace=False)[source]¶
Bases:
AbstractNoiseRuleAdd noise to an operation acting on exactly the given qubits.
Matches like
OperationInstanceNoise, and also requires the instruction’s qubits to bequbits, in the same order.- Parameters:
operation (Operation) – Operation to match (see
OperationInstanceNoise).qubits (Sequence[int]) – Qubits the operation must act on, in order.
noise (krauschannel | Gate) – Kraus channel or gate to add, on the same number of qubits as
operation.before (bool) – Add the noise before the operation instead of after it.
replace (bool) – Replace the operation with the noise.
- Raises:
ValueError – If
operationis of an unsupported kind, ifqubitshas repetitions or a length other than the number of qubits ofoperation, ifnoiseacts on a different number of qubits, or if bothbeforeandreplaceare set.
Examples
>>> from mimiqcircuits import * >>> # only the CX with control 0 and target 1 gets noise >>> rule = ExactOperationInstanceQubitNoise(GateCX(), [0, 1], Depolarizing2(0.01)) >>> c = Circuit().push(GateCX(), 0, 1).push(GateCX(), 1, 0) >>> NoiseModel([rule]).apply_noise_model(c) 2-qubit circuit with 3 instructions: ├── CX @ q[0], q[1] ├── Depolarizing(0.01) @ q[0:1] └── CX @ q[1], q[0]
- priority()[source]¶
Return the priority of the rule. Lower values are tried first.
The default is
100.
- replaces()[source]¶
Return
Trueif the rule’s output replaces the matched instruction.The default is
False: the matched instruction is kept.
- matches(inst)[source]¶
Return
Trueif the rule applies to the instructioninst.- Raises:
NotImplementedError – Unless a subclass implements it.
- apply_rule(inst)[source]¶
Return what the rule produces for
inst, orNoneif it does not apply.- Raises:
NotImplementedError – Unless a subclass implements it.
- class SetOperationInstanceQubitNoise(operation, qubits, noise, before=False, replace=False)[source]¶
Bases:
AbstractNoiseRuleAdd noise to an operation whose qubits are all in a set.
Matches like
OperationInstanceNoise, and also requires every qubit of the instruction to be inqubits.- Parameters:
operation (Operation) – Operation to match (see
OperationInstanceNoise).noise (krauschannel | Gate) – Kraus channel or gate to add, on the same number of qubits as
operation.before (bool) – Add the noise before the operation instead of after it.
replace (bool) – Replace the operation with the noise.
- Raises:
ValueError – If
operationis of an unsupported kind, ifqubitshas repetitions or fewer qubits thanoperation, ifnoiseacts on a different number of qubits, or if bothbeforeandreplaceare set.
Examples
>>> from mimiqcircuits import * >>> # H on qubit 0 or 1 gets noise, H on qubit 2 does not >>> rule = SetOperationInstanceQubitNoise(GateH(), [0, 1], AmplitudeDamping(0.001)) >>> c = Circuit().push(GateH(), 0).push(GateH(), 2) >>> NoiseModel([rule]).apply_noise_model(c) 3-qubit circuit with 3 instructions: ├── H @ q[0] ├── AmplitudeDamping(0.001) @ q[0] └── H @ q[2]
- priority()[source]¶
Return the priority of the rule. Lower values are tried first.
The default is
100.
- replaces()[source]¶
Return
Trueif the rule’s output replaces the matched instruction.The default is
False: the matched instruction is kept.
- matches(inst)[source]¶
Return
Trueif the rule applies to the instructioninst.- Raises:
NotImplementedError – Unless a subclass implements it.
- apply_rule(inst)[source]¶
Return what the rule produces for
inst, orNoneif it does not apply.- Raises:
NotImplementedError – Unless a subclass implements it.
- class IdleNoise(relation)[source]¶
Bases:
AbstractNoiseRuleReplace every
Delaywith a noise operation.Idle time is marked explicitly with
Delay(t)instructions; qubits that are simply not acted upon get no noise. Each matchingDelayis replaced by the noise on the same qubits.- Parameters:
relation (tuple | Operation) – Either a constant noise operation, or a tuple
(t, noise(t))wheretis a symbol. In the second formtis replaced by the duration of eachDelay.- Raises:
ValueError – If
relationis neither an operation nor a(symbol, operation)tuple.
Examples
>>> from mimiqcircuits import * >>> from symengine import symbols >>> # a Delay(t) becomes amplitude damping with gamma = t / 1000 >>> t = symbols("t") >>> rule = IdleNoise((t, AmplitudeDamping(t / 1000))) >>> c = Circuit().push(GateH(), 0).push(Delay(10), 1) >>> NoiseModel([rule]).apply_noise_model(c) 2-qubit circuit with 2 instructions: ├── H @ q[0] └── AmplitudeDamping(0.01) @ q[1]
- priority()[source]¶
Return the priority of the rule. Lower values are tried first.
The default is
100.
- replaces()[source]¶
Return
Trueif the rule’s output replaces the matched instruction.The default is
False: the matched instruction is kept.
- matches(inst)[source]¶
Return
Trueif the rule applies to the instructioninst.- Raises:
NotImplementedError – Unless a subclass implements it.
- apply_rule(inst)[source]¶
Return what the rule produces for
inst, orNoneif it does not apply.- Raises:
NotImplementedError – Unless a subclass implements it.
- class SetIdleQubitNoise(relation, qubits)[source]¶
Bases:
AbstractNoiseRuleReplace every
Delayon qubits in a set with a noise operation.Like
IdleNoise, restricted toDelayinstructions whose qubits are all inqubits.- Parameters:
- Raises:
ValueError – If
qubitsis empty, orrelationis neither an operation nor a(symbol, operation)tuple.
Examples
>>> from mimiqcircuits import * >>> # only delays on qubit 0 become noise >>> rule = SetIdleQubitNoise(AmplitudeDamping(1e-4), [0]) >>> c = Circuit().push(Delay(10), 0).push(Delay(10), 1) >>> NoiseModel([rule]).apply_noise_model(c) 2-qubit circuit with 2 instructions: ├── AmplitudeDamping(0.0001) @ q[0] └── Delay(10) @ q[1]
- priority()[source]¶
Return the priority of the rule. Lower values are tried first.
The default is
100.
- replaces()[source]¶
Return
Trueif the rule’s output replaces the matched instruction.The default is
False: the matched instruction is kept.
- matches(inst)[source]¶
Return
Trueif the rule applies to the instructioninst.- Raises:
NotImplementedError – Unless a subclass implements it.
- apply_rule(inst)[source]¶
Return what the rule produces for
inst, orNoneif it does not apply.- Raises:
NotImplementedError – Unless a subclass implements it.
- class CustomNoiseRule(matcher, generator, priority_val=0, before=False, replace=False)[source]¶
Bases:
AbstractNoiseRuleAdd noise chosen by user-supplied functions.
- Parameters:
matcher (Callable[[Instruction], bool]) – Function
inst -> boolthat selects the instructions.generator (Callable[[Instruction], Instruction]) – Function
inst -> Instructionthat builds the noise instruction for a matched instruction, with the circuit’s qubit and bit indices.priority_val (int) – Priority of the rule; lower runs first. Defaults to
PRIORITY_USER_OVERRIDE, ahead of every built-in rule.before (bool) – Add the noise before the instruction instead of after it.
replace (bool) – Replace the instruction with the noise.
A noise model containing a
CustomNoiseRulecannot be saved withsaveproto: the matcher and generator are Python callables with no serialized form.Examples
>>> from mimiqcircuits import * >>> # two-qubit depolarizing noise after every two-qubit operation >>> rule = CustomNoiseRule( ... lambda inst: inst.get_operation().num_qubits == 2, ... lambda inst: Instruction(Depolarizing2(0.01), inst.get_qubits()), ... ) >>> c = Circuit().push(GateCX(), 0, 1) >>> NoiseModel([rule]).apply_noise_model(c) 2-qubit circuit with 2 instructions: ├── CX @ q[0], q[1] └── Depolarizing(0.01) @ q[0:1]
- priority()[source]¶
Return the priority of the rule. Lower values are tried first.
The default is
100.
- matches(inst)[source]¶
Return
Trueif the rule applies to the instructioninst.- Raises:
NotImplementedError – Unless a subclass implements it.
- apply_rule(inst)[source]¶
Return what the rule produces for
inst, orNoneif it does not apply.- Raises:
NotImplementedError – Unless a subclass implements it.
- class NoiseModel(rules=<factory>, name='')[source]¶
Bases:
objectCollection of noise rules applied to circuits.
apply_noise_model()goes through a circuit and gives each instruction to the rules in order; the first rule that matches adds its noise (after the instruction, before it, or in its place) and the others are skipped. Instructions that no rule matches are kept as they are.Rules are kept sorted by
priority(), lower first, and rules of equal priority keep the order they were added in. The built-in rules make specific rules win over general ones:Rule
Priority
CustomNoiseRule(default)0
40
50
60
70
80
90
190
200
The
add_*methods build these rules for you and return the model, so calls can be chained.- Parameters:
rules (List[AbstractNoiseRule]) – Initial rules. The list is sorted in place.
name (str) – Name shown by
describe().
Examples
>>> from mimiqcircuits import * >>> from symengine import symbols, pi >>> theta = symbols("theta") >>> model = NoiseModel(name="demo") >>> # RX(theta) is followed by depolarizing noise of strength theta/pi >>> _ = model.add_operation_noise(GateRX(theta), Depolarizing1(theta / pi)) >>> # every CX is followed by two-qubit depolarizing noise >>> _ = model.add_operation_noise(GateCX(), Depolarizing2(0.01)) >>> # every measured bit gets a readout error >>> _ = model.add_readout_noise(ReadoutErr(0.01, 0.02)) >>> # every Delay becomes amplitude damping >>> _ = model.add_idle_noise(AmplitudeDamping(0.001)) >>> c = Circuit() >>> _ = c.push(GateRX(0.4), 0) >>> _ = c.push(GateCX(), 0, 1) >>> _ = c.push(Delay(5), 1) >>> _ = c.push(Measure(), 0, 0) >>> model.apply_noise_model(c) 2-qubit, 1-bit circuit with 7 instructions: ├── RX(0.4) @ q[0] ├── Depolarizing(0.4/pi) @ q[0] ├── CX @ q[0], q[1] ├── Depolarizing(0.01) @ q[0:1] ├── AmplitudeDamping(0.001) @ q[1] ├── M @ q[0], c[0] └── RErr(0.01, 0.02) @ c[0]
>>> # rules can also be given directly >>> model = NoiseModel( ... [ ... ExactOperationInstanceQubitNoise(GateCX(), [0, 1], Depolarizing(2, 0.01)), ... GlobalReadoutNoise(ReadoutErr(0.01, 0.02)), ... ], ... name="explicit", ... )
- add_rule(rule)[source]¶
Add a rule and return the model.
The rule is placed after the existing rules of the same priority.
- Parameters:
rule (AbstractNoiseRule) – Any
AbstractNoiseRule.- Returns:
This model, changed in place.
- Return type:
- add_readout_noise(noise, *, qubits=None, exact=False)[source]¶
Add a rule that puts a readout error after measurements.
Without
qubitsthe rule is aGlobalReadoutNoise. Withqubitsit is aSetQubitReadoutNoise, or anExactQubitReadoutNoisewhenexactisTrue.- Parameters:
noise (ReadoutErr) – Readout error to add.
qubits – Keyword-only. Restrict the rule to measurements on these qubits.
exact – Keyword-only. Require the measured qubits to equal
qubits, in order, instead of being a subset of them.
- Returns:
This model, changed in place.
- Return type:
- Raises:
ValueError – If
qubitsis empty or has repetitions.
Examples
>>> from mimiqcircuits import * >>> model = NoiseModel() >>> # all measurements >>> model.add_readout_noise(ReadoutErr(0.01, 0.02)) NoiseModel(rules=[GlobalReadoutNoise(noise=RErr(0.01,0.02))], name='') >>> # measurements on qubits 0 and 2 only; tried before the global rule >>> model.add_readout_noise(ReadoutErr(0.03, 0.04), qubits=[0, 2]) NoiseModel(rules=[SetQubitReadoutNoise(qubits=(0, 2), noise=RErr(0.03,0.04)), GlobalReadoutNoise(noise=RErr(0.01,0.02))], name='') >>> # a measurement of exactly (q2, q0); tried first >>> model.add_readout_noise(ReadoutErr(0.05, 0.06), qubits=[2, 0], exact=True) NoiseModel(rules=[ExactQubitReadoutNoise(qubits=(2, 0), noise=RErr(0.05,0.06)), SetQubitReadoutNoise(qubits=(0, 2), noise=RErr(0.03,0.04)), GlobalReadoutNoise(noise=RErr(0.01,0.02))], name='')
- add_operation_noise(operation, noise, *, qubits=None, exact=False, before=False, replace=False)[source]¶
Add a rule that puts noise next to an operation.
Without
qubitsthe rule is anOperationInstanceNoise. Withqubitsit is aSetOperationInstanceQubitNoise, or anExactOperationInstanceQubitNoisewhenexactisTrue.A concrete
operationmatches only equal operations. One with symbolic parameters matches any operation of the same type, and its symbols are replaced innoiseby the values of the matched operation.- Parameters:
operation – Operation pattern: a gate, measurement, reset,
Block,Repeat,IfStatementorWhileStatement.noise – Kraus channel or gate to add, on as many qubits as
operation.qubits – Keyword-only. Restrict the rule to instructions on these qubits.
exact – Keyword-only. Require the instruction’s qubits to equal
qubits, in order, instead of being a subset of them.before – Keyword-only. Add the noise before the operation.
replace – Keyword-only. Replace the operation with the noise.
- Returns:
This model, changed in place.
- Return type:
- Raises:
ValueError – If
operationis not a supported kind,noiseacts on a different number of qubits,qubitsis invalid, or bothbeforeandreplaceare set.
Examples
>>> from mimiqcircuits import * >>> from symengine import symbols, pi >>> theta, alpha, beta = symbols("theta alpha beta") >>> model = NoiseModel() >>> # only RX(pi/2) matches >>> _ = model.add_operation_noise(GateRX(pi / 2), AmplitudeDamping(0.001)) >>> # any RX matches; theta in the noise takes the gate's angle >>> _ = model.add_operation_noise(GateRX(theta), Depolarizing(1, theta / pi)) >>> # several parameters at once >>> _ = model.add_operation_noise( ... GateU(alpha, beta, 0), ... Depolarizing(1, (alpha**2 + beta**2) / (2 * pi**2)), ... ) >>> # restricted to RX on qubits 0, 1 or 2 >>> _ = model.add_operation_noise( ... GateRX(theta), ... Depolarizing(1, theta / pi), ... qubits=[0, 1, 2], ... ) >>> # restricted to RX on exactly qubit 0 >>> _ = model.add_operation_noise( ... GateRX(theta), ... Depolarizing(1, theta / pi), ... qubits=[0], ... exact=True, ... ) >>> # a bit flip just before every measurement >>> _ = model.add_operation_noise(Measure(), PauliX(0.02), before=True) >>> _ = model.add_operation_noise(Reset(), Depolarizing(1, 0.01)) >>> # H is removed and only the noise is left >>> _ = model.add_operation_noise(GateH(), AmplitudeDamping(0.001), replace=True)
- add_idle_noise(noise, qubits=None)[source]¶
Add a rule that replaces
Delayinstructions with noise.Without
qubitsthe rule is anIdleNoise, otherwise aSetIdleQubitNoise. Only explicitDelayinstructions are affected; qubits that are simply not acted upon get no noise.- Parameters:
noise – A noise operation, or a tuple
(t, noise(t))wheretis a symbol replaced by the duration of eachDelay.qubits – Restrict the rule to delays on these qubits.
- Returns:
This model, changed in place.
- Return type:
- Raises:
ValueError – If
noiseis neither an operation nor a(symbol, operation)tuple, orqubitsis empty.
Examples
>>> from mimiqcircuits import * >>> from symengine import symbols >>> t = symbols("t") >>> model = NoiseModel() >>> # the same noise for every delay >>> model.add_idle_noise(AmplitudeDamping(1e-4)) NoiseModel(rules=[IdleNoise(relation=AmplitudeDamping(0.0001))], name='') >>> # noise depending on the delay duration, on qubits 0 to 2 >>> model.add_idle_noise((t, AmplitudeDamping(t / 1000)), qubits=[0, 1, 2]) NoiseModel(rules=[SetIdleQubitNoise(relation=(t, AmplitudeDamping((1/1000)*t)), qubits=frozenset({0, 1, 2})), IdleNoise(relation=AmplitudeDamping(0.0001))], name='')
- apply_noise_model(circuit)[source]¶
Return a noisy copy of
circuit; seeapply_noise_model().
- describe()[source]¶
Print a readable summary of the rules, in the order they are tried.
Examples
>>> from mimiqcircuits import * >>> NoiseModel(name="demo").add_idle_noise(AmplitudeDamping(0.001)).describe() NoiseModel: demo ================================================================================ Rule 1 (priority 200): IdleNoise -> Applies AmplitudeDamping(0.001) to all idle qubits (REPLACES the matched operation) --------------------------------------------------------------------------------
- saveproto(file)[source]¶
Save the model to a protobuf file.
- Parameters:
file – Path or writable binary file object.
- Raises:
ValueError – If the model contains a
CustomNoiseRule, which cannot be serialized.
- static loadproto(file)[source]¶
Load a model saved with
saveproto().- Parameters:
file – Path or readable binary file object.
- Returns:
The loaded model.
- Return type:
- apply_noise_model(circuit, model)[source]¶
Return a copy of
circuitwith the noise ofmodeladded.Each instruction is given to the rules of
modelin priority order, and only the first rule that matches is applied: its noise goes after the instruction, before it, or in its place.circuitis not changed.When no rule matches a wrapper operation, the rules are applied to the operations inside it:
Block,IfStatement,WhileStatement,Parallel,Repeat,GateCallandBlockCall. A wrapper whose content gets noise is turned into aBlock(or keeps its condition, forIfStatementandWhileStatement); one whose content gets none is kept as it is.- Parameters:
circuit (Circuit) – Circuit to make noisy.
model (NoiseModel) – Noise model to apply.
- Returns:
A new circuit with the noise added.
- Return type:
Examples
>>> from mimiqcircuits import * >>> from symengine import symbols, pi >>> theta = symbols("theta") >>> c = Circuit() >>> _ = c.push(GateRX(0.4), 0) >>> _ = c.push(GateRX(0.8), 1) >>> _ = c.push(Measure(), 0, 0) >>> _ = c.push(Measure(), 1, 1) >>> # noise after each RX that depends on its angle, and readout errors >>> model = NoiseModel([ ... OperationInstanceNoise(GateRX(theta), Depolarizing(1, theta / pi)), ... GlobalReadoutNoise(ReadoutErr(0.01, 0.02)), ... ]) >>> apply_noise_model(c, model) 2-qubit, 2-bit circuit with 8 instructions: ├── RX(0.4) @ q[0] ├── Depolarizing(0.4/pi) @ q[0] ├── RX(0.8) @ q[1] ├── Depolarizing(0.8/pi) @ q[1] ├── M @ q[0], c[0] ├── RErr(0.01, 0.02) @ c[0] ├── M @ q[1], c[1] └── RErr(0.01, 0.02) @ c[1]
Noise also reaches operations inside wrappers. Here every
Hgets amplitude damping;decompose_stepopens the wrapper to show it.>>> model = NoiseModel([OperationInstanceNoise(GateH(), AmplitudeDamping(0.01))]) >>> inner = Circuit().push(GateH(), 0) >>> # inside a Block >>> c_block = Circuit().push(Block(inner), 0) >>> apply_noise_model(c_block, model).decompose_step() 1-qubit circuit with 2 instructions: ├── H @ q[0] └── AmplitudeDamping(0.01) @ q[0] >>> # inside a gate declaration call >>> decl = GateDecl("local_h", (), inner) >>> c_call = Circuit().push(GateCall(decl, ()), 0) >>> apply_noise_model(c_call, model).decompose_step() 1-qubit circuit with 2 instructions: ├── H @ q[0] └── AmplitudeDamping(0.01) @ q[0] >>> # each copy of a Parallel gets its own noise >>> c_parallel = Circuit().push(Parallel(2, GateH()), 0, 1) >>> apply_noise_model(c_parallel, model).decompose_step() 2-qubit circuit with 4 instructions: ├── H @ q[0] ├── AmplitudeDamping(0.01) @ q[0] ├── H @ q[1] └── AmplitudeDamping(0.01) @ q[1] >>> # so does each repetition of a Repeat >>> c_repeat = Circuit().push(Repeat(2, GateH()), 0) >>> apply_noise_model(c_repeat, model).decompose_step() 1-qubit circuit with 4 instructions: ├── H @ q[0] ├── AmplitudeDamping(0.01) @ q[0] ├── H @ q[0] └── AmplitudeDamping(0.01) @ q[0] >>> # a conditional gate keeps its condition, and so does its noise >>> c_if = Circuit().push(IfStatement(GateH(), BitString("1")), 0, 0) >>> apply_noise_model(c_if, model).decompose_step() 1-qubit, 1-bit circuit with 2 instructions: ├── IF(c==1) H @ q[0], condition[0] └── IF(c==1) AmplitudeDamping(0.01) @ q[0], condition[0] >>> # nested gate calls are opened at every level >>> decl_inner = GateDecl("inner_h", (), inner) >>> middle = Circuit().push(GateCall(decl_inner, ()), 0) >>> decl_outer = GateDecl("outer_call", (), middle) >>> c_nested = Circuit().push(GateCall(decl_outer, ()), 0).push(GateCall(decl_outer, ()), 1) >>> apply_noise_model(c_nested, model).decompose_step().decompose_step() 2-qubit circuit with 4 instructions: ├── H @ q[0] ├── AmplitudeDamping(0.01) @ q[0] ├── H @ q[1] └── AmplitudeDamping(0.01) @ q[1]