MitigationFactory

MitigationFactory creates an ordered list of standard mitigations suitable for assigning to a Computer. Start from a preset level, optionally customize individual techniques, then call build():

>>> import perceval as pcvl
>>>
>>> factory = pcvl.MitigationFactory(pcvl.MitigationLevel.medium)
>>> factory.set_compilation_averaging(repetitions=4)
>>> factory.set_detector_balancing(False)  # Deactivate detector balancing
>>> mitigations = factory.build()
>>> computer.mitigations = mitigations

Preset levels

The MitigationLevel values provide progressively stronger starting configurations:

  • MitigationLevel.none produces an empty list.

  • MitigationLevel.low enables detector balancing.

  • MitigationLevel.medium adds compilation averaging with three repetitions and distinguishable-photon mitigation at order one.

  • MitigationLevel.high uses five compilation repetitions and distinguishable-photon mitigation at order three, together with detector balancing.

Higher levels can require more sub-computations and compilation work. Choose a level based on the known imperfections, the available execution budget, and the mitigation requirements—not only on the level name.

Customizing a factory

Every built-in technique has a setter. Passing None disables compilation averaging or distinguishable-photon mitigation; passing False disables photon recycling or detector balancing:

>>> factory = pcvl.MitigationFactory(pcvl.MitigationLevel.none)
>>> factory.set_compilation_averaging(3)
>>> factory.set_distinguishable_photon_mitigation(1)
>>> factory.set_photon_recycling(True)
>>> factory.set_detector_balancing(True)

Use set_custom_mitigation() to supply an already configured instance of one of the built-in mitigation types:

>>> factory.set_custom_mitigation(pcvl.CompilationAveraging(4, starting_seed=10))

This method replaces the instance for that built-in type. It does not register arbitrary new mitigation classes.

Calling reset_to_level() discards the custom settings and restores a preset:

>>> factory.reset_to_level(pcvl.MitigationLevel.low)
class perceval.runtime.error_mitigation.mitigation_factory.MitigationLevel(value)

Preset mitigation levels understood by MitigationFactory.

Higher levels enable stronger corrections but may require more sub-computations and pre/post-processing work. none disables every factory-managed mitigation.

class perceval.runtime.error_mitigation.mitigation_factory.MitigationFactory(level=MitigationLevel.low)

Build an ordered list of standard error mitigations.

A preset level provides a convenient starting point. Individual mitigations can then be enabled, disabled, or configured before build() returns the list to assign to a computer’s mitigations property.

Parameters:

level (MitigationLevel) – Initial preset level. The default is MitigationLevel.low.

build()

Return the configured mitigations in their recommended application order.

Return type:

list[AMitigation]

reset_to_level(level)

Discard custom settings and restore a preset mitigation level.

Parameters:

level (MitigationLevel) – Preset level to apply.

Raises:

ValueError – If level is not a MitigationLevel value.

Return type:

None

set_compilation_averaging(repetitions, starting_seed=None)

Configure compilation averaging, or disable it with None.

Parameters:
  • repetitions (Optional[int]) – Number of compilations to average, or None to disable it.

  • starting_seed (Optional[int]) – Optional first compilation seed.

set_custom_mitigation(mitigation)

Replace the configured instance of a supported mitigation type.

This method accepts the exact built-in types listed in MITIGATION_ORDER; it does not register new mitigation classes.

Parameters:

mitigation (AMitigation) – Configured built-in mitigation instance.

Raises:

ValueError – If its exact type is not managed by this factory.

set_detector_balancing(use_it=True)

Enable or disable detector balancing.

Parameters:

use_it (bool) – Whether detector balancing should be included.

set_distinguishable_photon_mitigation(order)

Configure distinguishable-photon mitigation, or disable it with None.

Parameters:

order (UnionType[int, dict[int, int], None]) – Correction order, per-photon-count order mapping, or None to disable it.

set_photon_recycling(use_it=True)

Enable or disable photon recycling.

Parameters:

use_it (bool) – Whether photon recycling should be included.