mimiqcircuits.addnoise

Functions

add_noise(circuit, g, noise[, before, parallel])

Return a copy of circuit with noise added next to every g.

add_noise_inplace(circuit, g, noise[, ...])

Add noise next to every instance of g, in place.

decorate(circuit, matcher, generator, *[, ...])

Return a decorated copy, decorating one by one or by blocks.

decorate_inplace(circuit, matcher, generator)

Decorate matching instructions in place, one by one or by blocks.

decorate_on_match_parallel(circuit, matcher, ...)

Return a copy of circuit decorated block by block.

decorate_on_match_parallel_inplace(circuit, ...)

Add generated instructions next to blocks of parallel matches, in place.

decorate_on_match_single(circuit, matcher, ...)

Return a decorated copy of circuit.

decorate_on_match_single_inplace(circuit, ...)

Add generated instructions next to every matching instruction, in place.

decorate_on_match_single_inplace(circuit, matcher, generator, before=False)[source]

Add generated instructions next to every matching instruction, in place.

For each instruction inst of circuit with matcher(inst) true, the output of generator(inst) is inserted right after inst (or right before it). The generator returns one of:

  • an Instruction;

  • a tuple (operation, targets), such as (noise, inst.qubits), or (operation, qubits, bits, zvars);

  • a tuple (circuit,), whose instructions are inserted as they are.

This is the engine behind Circuit.decorate_on_match_single().

Parameters:
  • circuit (Circuit) – Circuit to change.

  • matcher (Callable[[Instruction], bool]) – Function inst -> bool selecting the instructions.

  • generator (Callable[[Instruction], Any]) – Function inst -> instruction or tuple giving what to insert.

  • before (bool) – Insert before each match instead of after it.

Returns:

circuit itself, changed.

Return type:

Circuit

Raises:

TypeError – If the generator returns an unsupported value.

Examples

>>> from mimiqcircuits import *
>>> from mimiqcircuits.addnoise import decorate_on_match_single_inplace
>>> c = Circuit()
>>> _ = c.push(GateH(), 0)
>>> _ = c.push(GateCX(), 0, 1)
>>> # add bit-flip noise after every one-qubit gate
>>> decorate_on_match_single_inplace(
...     c,
...     lambda inst: inst.operation.num_qubits == 1,
...     lambda inst: (PauliX(0.01), inst.qubits),
... )
2-qubit circuit with 3 instructions:
├── H @ q[0]
├── PauliX(0.01) @ q[0]
└── CX @ q[0], q[1]
decorate_on_match_single(circuit, matcher, generator, before=False)[source]

Return a decorated copy of circuit.

Same as decorate_on_match_single_inplace(), applied to a deep copy; circuit is not changed.

Returns:

The decorated copy.

Return type:

Circuit

decorate_on_match_parallel_inplace(circuit, matcher, generator, before=False)[source]

Add generated instructions next to blocks of parallel matches, in place.

A block is a run of consecutive matching instructions that act on disjoint qubits, bits and z-variables, such as the instructions produced by one broadcast Circuit.push(). The generated instructions of a whole block are inserted together, after the block (or before it). The matcher and generator follow the same rules as in decorate_on_match_single_inplace().

This is the engine behind Circuit.decorate_on_match_parallel().

Parameters:
  • circuit (Circuit) – Circuit to change.

  • matcher (Callable[[Instruction], bool]) – Function inst -> bool selecting the instructions.

  • generator (Callable[[Instruction], Any]) – Function inst -> instruction or tuple giving what to insert.

  • before (bool) – Insert before each block instead of after it.

Returns:

circuit itself, changed.

Return type:

Circuit

Raises:

TypeError – If the generator returns an unsupported value.

Examples

>>> from mimiqcircuits import *
>>> from mimiqcircuits.addnoise import decorate_on_match_parallel_inplace
>>> c = Circuit()
>>> _ = c.push(GateH(), range(3))
>>> # the three H gates form one block: the noise comes after all of them
>>> decorate_on_match_parallel_inplace(
...     c,
...     lambda inst: inst.operation == GateH(),
...     lambda inst: (AmplitudeDamping(0.1), inst.qubits),
... )
3-qubit circuit with 6 instructions:
├── H @ q[0]
├── H @ q[1]
├── H @ q[2]
├── AmplitudeDamping(0.1) @ q[0]
├── AmplitudeDamping(0.1) @ q[1]
└── AmplitudeDamping(0.1) @ q[2]
decorate_on_match_parallel(circuit, matcher, generator, *, before=False)[source]

Return a copy of circuit decorated block by block.

Same as decorate_on_match_parallel_inplace(), applied to a deep copy; circuit is not changed. before is keyword-only.

Returns:

The decorated copy.

Return type:

Circuit

decorate_inplace(circuit, matcher, generator, before=False, parallel=False)[source]

Decorate matching instructions in place, one by one or by blocks.

Calls decorate_on_match_parallel_inplace() when parallel is True and decorate_on_match_single_inplace() otherwise.

Returns:

circuit itself, changed.

Return type:

Circuit

decorate(circuit, matcher, generator, *, before=False, parallel=False)[source]

Return a decorated copy, decorating one by one or by blocks.

Calls decorate_on_match_parallel() when parallel is True and decorate_on_match_single() otherwise. circuit is not changed.

Returns:

The decorated copy.

Return type:

Circuit

add_noise_inplace(circuit, g, noise, before=False, parallel=False)[source]

Add noise next to every instance of g, in place.

noise acts on the same qubits as each matched instruction, so it must act on as many qubits as g. This is the engine behind Circuit.add_noise().

Parameters:
  • circuit (Circuit) – Circuit to change.

  • g (Operation | type) – Operation to match. An instance matches equal operations; an Operation subclass, such as GateH, matches all its instances.

  • noise (krauschannel | Gate) – Gate or Kraus channel to add.

  • before (bool) – Add the noise before each match instead of after it.

  • parallel (bool) – Add the noise after (or before) each block of parallel matches, as in decorate_on_match_parallel_inplace().

Returns:

circuit itself, changed.

Return type:

Circuit

Raises:
  • TypeError – If noise is not a gate or Kraus channel, or g is neither an operation nor an operation class.

  • ValueError – If noise acts on a different number of qubits than a matched instruction.

Examples

>>> from mimiqcircuits import *
>>> from mimiqcircuits.addnoise import add_noise_inplace
>>> c = Circuit()
>>> _ = c.push(GateH(), 0)
>>> _ = c.push(GateX(), 1)
>>> # match every Pauli X gate by its class
>>> add_noise_inplace(c, GateX, PauliZ(0.05))
2-qubit circuit with 3 instructions:
├── H @ q[0]
├── X @ q[1]
└── PauliZ(0.05) @ q[1]
add_noise(circuit, g, noise, before=False, parallel=False)[source]

Return a copy of circuit with noise added next to every g.

Same as add_noise_inplace(), applied to a deep copy; circuit is not changed.

Returns:

The noisy copy.

Return type:

Circuit