Stepper Simulator

The Stepper is a special simulator that simulates the evolution of a StateVector component by component. It’s main purpose is to be able to see what the circuit does step by step. The internal behaviour of the Stepper can be seen by using the debugger and going into the code, or by using the apply method manually.

Warning

The Stepper is much more slower than the usual Simulator, as well as being less flexible, so it shouldn’t be used for anything other than visualization.

>>> import perceval as pcvl
>>> c = pcvl.BS() // pcvl.PS(1.) // pcvl.BS()
>>> sim = pcvl.Stepper(pcvl.SLOSBackend())
>>> sim.set_circuit(c)
>>> state = pcvl.StateVector([1, 1])
>>> for r, component in c:
>>>     state = sim.apply(state, r, component)
>>>     print(component.describe(), state)
BS.Rx() 0.707I*|2,0>+0.707I*|0,2>
PS(phi=1) (-0.643-0.294I)*|2,0>+0.707I*|0,2>
BS.Rx() (-0.321-0.501I)*|2,0>+(-0.292-0.455I)*|1,1>+(0.321+0.501I)*|0,2>

The Stepper can also be used to simulate LC components, but the apply method can’t be used in that case (see the code of the compile method to see how to do it manually)

Note however that the Stepper doesn’t support annotated or polarized BasicState, nor does it apply logical selection like heralding and post-selection on its own. It can still perform physical selection, including after simulating detectors.

Also, when calling probs_svd on the Stepper, the returned dictionary has only the ‘results’ field.

class perceval.simulators.stepper.Stepper(backend=None)

Step-by-step circuit propagation algorithm, main usage is on a circuit, but could work in degraded mode on a list of components [(r, comp)].

apply(sv, r, c)

Apply a circuit on a StateVector generating another StateVector

Parameters:
  • sv (StateVector) – input StateVector

  • r (list[int]) – range of port for the circuit corresponding to StateVector position

  • c (ACircuit) – a circuit

Return type:

StateVector

Returns:

evolved StateVector

compile(input_states)

Effectively computes the evolution of the input state, and stores the result in self._out.

Parameters:

input_states (BasicState | StateVector) – The input fock state or state vector

Return type:

bool

Returns:

True if something has been computed, False otherwise (i.e. the previous call to compile was already on this circuit and input state, so the result is already stored).

compute_physical_logical_perf(value)

Tells the simulator to compute or not the physical and logical performances when possible

Parameters:

value (bool) – True to compute the physical and logical performances, False otherwise.

evolve(input_state)

Evolve a state through the circuit.

Parameters:

input_state (BasicState | StateVector) – The input fock state or state vector

Return type:

StateVector

Returns:

The output state vector

format_results(results, physical_perf, logical_perf)

Format the simulation results by computing the global performance, and returning the physical and logical performances only if needed.

Parameters:
  • results – the simulation results

  • physical_perf – the physical performance

  • logical_perf – the logical performance

keep_heralds(value)

Tells the simulator to keep or discard ancillary modes in output states

Parameters:

value (bool) – True to keep ancillaries/heralded modes, False to discard them (default is keep).

property min_detected_photons_filter: int

The simulated minimum number of photons that a state needs to have to be counted as valid. Includes the expected photons from the heralds.

probs(input_state)

Compute the probability distribution from a state input :type input_state: BasicState | StateVector :param input_state: The input fock state or state vector :rtype: BSDistribution :return: The post-selected output state distribution (BSDistribution)

probs_svd(svd, detectors=None, progress_callback=None)

Compute the probability distribution from a SVDistribution input

Parameters:
  • svd (SVDistribution) – A state vector distribution describing the input to simulate

  • detectors – An optional list of detectors

  • progress_callback (Optional[callable]) – A function with the signature func(progress: float, message: str). Not used.

Return type:

dict

Returns:

A dictionary of the form { “results”: BSDistribution }

  • results is the post-selected output state distribution

set_min_detected_photons_filter(value)

Set a minimum number of detected photons in the output distribution, counting only the non-heralded modes.

Parameters:

value (int) – The minimum photon count

set_selection(min_detected_photons_filter=None, postselect=None, heralds=None)

Set the min_detected_photons_filter, postselect, and heralds, if defined.

Parameters:
  • min_detected_photons_filter (Optional[int]) – The minimum photon count.

  • postselect (Optional[PostSelect]) – The postselect to apply at the end of the computation.

  • heralds (Optional[dict[int, int]]) – The heralds to apply at the end of the computation. Only the output heralds are considered here. dictionary of the form {mode: expected}