class perceval.components.processor.Processor(backend, m_circuit=None, source=<perceval.components.source.Source object>, name=None)

Generic definition of processor as a source + components (both unitary and non-unitary) + ports + optional post-processing logic

  • backend (Union[ABackend, str]) – Name or instance of a simulation backend

  • m_circuit (Union[int, ACircuit, None]) –

    can either be:

    • an int: number of modes of interest (MOI). A mode of interest is any non-heralded mode.

    >>> p = Processor("SLOS", 5)
    • a circuit: the input circuit to start with. Other components can still be added afterwards with add()

    >>> p = Processor("SLOS", BS() // PS() // BS())

  • source (Source) – the Source used by the processor (defaults to perfect source)

  • name (Optional[str]) – a textual name for the processor (defaults to “Local processor”)

add(mode_mapping, component, keep_port=True)

Add a component to the processor (unitary or non-unitary).

  • mode_mapping

    Describe how the new component is connected to the existing processor. Can be:

    • an int: composition uses consecutive modes starting from mode_mapping

    • a list or a dict: describes the full mapping of length the input mode count of component

  • component

    The component to append to the processor. Can be:

    • A unitary circuit

    • A non-unitary component

    • A processor

  • keep_port – if True, saves self’s output ports on modes impacted by the new component, otherwise removes them.

Adding a component on non-ordered, non-consecutive modes computes the right permutation (PERM component) which fits into the existing processor and the new component.


>>> p = Processor("SLOS", 6)
>>> p.add(0, BS())  # Modes (0, 1) connected to (0, 1) of the added beam splitter
>>> p.add([2,5], BS())  # Modes (2, 5) of the processor's output connected to (0, 1) of the added beam splitter
>>> p.add({2:0, 5:1}, BS())  # Same as above
add_herald(mode, expected, name=None)

Add a heralded mode

  • mode (int) – Mode index of the herald

  • expected (int) – number of expected photon as input AND output on the given mode (must be 0 or 1)

  • name (Optional[str]) – Herald port name. If none is passed, the name is auto-generated

are_modes_free(mode_range, location=PortLocation.OUTPUT)
Return type:



True if all modes in mode_range are free of ports, for a given location (input, output or both)


Check if a basic state input matches with the current processor configuration

property circuit_size: int

Total size of the enclosed circuit (i.e. self.m + heralded mode count)

Return type:



a component list where recursive circuits have been flattened

property in_port_names

A list of the input port names. Names are repeated for ports connected to more than one mode


Creates a linear circuit from internal components, if all internal components are unitary. :type flatten: bool :param flatten: if True, the component recursive hierarchy is discarded, making the output circuit “flat”.

Return type:


property m: int

Number of modes of interest (MOI) defined in the processor


Sets-up a state post-selection on the number of detected photons. With thresholded detectors, this will actually filter on “click” count.


n (int) – Minimum expected photons

This post-selection has an impact on the output physical performance

property out_port_names

A list of the output port names. Names are repeated for ports connected to more than one mode


Removes all components and replace them by the given circuit’s components. :return: self. Allows to directly chain this with .add


Set a logical post-selection function. Along with the heralded modes, this function has an impact on the logical performance of the processor


postselect (PostSelect) – Sets a post-selection function. Its signature must be func(s: BasicState) -> bool. If None is passed as parameter, removes the previously defined post-selection function.

property source

The photonic source

property source_distribution: SVDistribution | None

Retrieve the computed input distribution. :return: the input SVDistribution if with_input was called previously, otherwise None.


Simulate threshold detectors on output states. All detections of more than one photon on any given mode is changed to 1.


value (bool) – enables threshold detection when True, otherwise disables it.