Processor

class perceval.components.processor.Processor(backend, m_circuit=None, source=None, noise=None, name='Local processor')

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

Parameters:
  • 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 (Optional[Source]) – the Source used by the processor (defaults to perfect source)

  • noise (Optional[NoiseModel]) – a NoiseModel containing noise parameters (defaults to no noise) Note: source and noise are mutually exclusive

  • name (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).

Parameters:
  • 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.

Example:

>>> 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

Parameters:
  • 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:

bool

Returns:

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

check_input(input_state)

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

property circuit_size: int
Returns:

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

flatten()
Return type:

List

Returns:

a component list where recursive circuits have been flattened

property in_port_names
Returns:

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

linear_circuit(flatten=False)

Creates a linear circuit from internal components, if all internal components are unitary. Takes phase imprecision noise into account.

Parameters:

flatten (bool) – if True, the component recursive hierarchy is discarded, making the output circuit “flat”.

Raises:

RuntimeError – If any component is non-unitary

Return type:

Circuit

Returns:

The resulting Circuit object

log_resources(method, extra_parameters)

Log resources of the processor

Parameters:
  • method (str) – name of the method used

  • extra_parameters (Dict) –

    extra parameters to log.

    Extra parameter can be:

    • max_samples

    • max_shots

    • precision

property m: int
Returns:

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

min_detected_photons_filter(n)

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

Parameters:

n (int) – Minimum expected photons

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

property out_port_names
Returns:

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

set_circuit(circuit)

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

set_postselection(postselect)

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

Parameters:

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
Returns:

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.

thresholded_output(value)

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

Parameters:

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