Simulator
The Simulator
class is a mid-level class that can be used to compute
output probabilities or amplitudes for photonic experiment.
It adds logic that allows computing with objects that are more complex than non-annotated BasicStates, contrarily to the backend it build upon to do its computations.
Note that the Simulator
aims at computing exact probabilities,
so it requires a backend able to compute probability amplitudes, such as SLOS, SLAP, Naive… (see Simulation Back-ends)
If possible, it will also automatically use masks on the backend to reduce the computation time and memory.
Also, contrarily to Experiment or Processor, it relies on being given complete information from the start and cannot be used to do composition, remote computing…
The basic Simulator
is only able to perform computation with non-polarized unitary circuits.
For polarized circuits, see the PolarizationSimulator
.
For non-unitary circuits, see the DelaySimulator
, LossSimulator
, and FFSimulator
.
More generally, if you are unsure about which simulator to use, see SimulatorFactory
.
Using a Simulator
import perceval as pcvl
sim = pcvl.Simulator(pcvl.SLOSBackend())
sim.set_circuit(pcvl.BS()) # Now, sim holds a 2 modes circuit
# Physical and logical selection
sim.set_selection(min_detected_photons_filter = 2) # Other fields: heralds (accounting only the output), postselect
sim.set_precision(1e-6) # Relative precision; only input states having more than this times the highest input prob will be computed
# Computation state by state
print(sim.prob_amplitude(pcvl.BasicState("|{_:0}, {_:1}>"), pcvl.BasicState("|{_:0}, {_:1}>"))) # No selection
# (0.5000000000000001+0j)
# Compute everything
print(sim.probs(pcvl.BasicState([2, 1]))) # Computes the BSD with selection; no performance and no automatic usage of masks
# {
# |1,2>: 0.12500000000000003
# |3,0>: 0.375
# |2,1>: 0.12500000000000003
# |0,3>: 0.375
# }
# Compute everything from anything
svd = pcvl.SVDistribution({pcvl.StateVector([1, 1]) + 0.5j * pcvl.StateVector([0, 2]): 0.7,
pcvl.StateVector([1, 0]): 0.3})
print(sim.probs_svd(svd, [pcvl.Detector.threshold()] * 2)) # Can also simulate detectors
# {'results': BSDistribution(<class 'float'>, {|1,1>: 1.0}), 'physical_perf': 0.06999999999999998, 'logical_perf': 1.0000000000000004}
Computation methods
A lot of computation methods exist in the Simulator
for different usages.
Here is a list of the simulation methods in the Simulator
method name |
parameters |
output |
prob_amplitude |
input_state: BasicState or StateVector, output_state: BasicState |
prob. amplitude (complex) |
probability |
input_state: BasicState or StateVector, output_state: BasicState |
probability (float [0;1]) |
probs |
input_state: BasicState or StateVector |
prob. distribution (BSD) |
probs_svd |
input_dist: SVDistribution |
Python dictionary |
probs_density_matrix |
dm: DensityMatrix |
Python dictionary |
evolve |
input_state: BasicState or StateVector |
evolved StateVector |
evolve_svd |
input_state: SVDistribution or StateVector or BasicState |
Python dictionary |
evolve_density_matrix |
dm: DensityMatrix |
Python dictionary |
- class perceval.simulators.Simulator(backend)
A simulator class relying on a probability amplitude capable backend to simulate the output of a unitary non-polarized circuit given an BasicState, StateVector or SVDistribution input. The simulator is able to evolve or simulate the sampling a states with annotated photons.
- Parameters:
backend (
AStrongSimulationBackend
) – A probability amplitude capable backend object
- clear_postselection()
Clear the post-selection function
- 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. If the simulator has logical selection, the performance for this input state is stored in self.logical_perf, and only the states matching the logical selection are kept.
- Parameters:
input_state (BasicState | StateVector) – The input fock state or state vector
- Return type:
- Returns:
The output state vector
- evolve_density_matrix(dm)
Compute the DensityMatrix evolved from “dm” through a linear optics circuit
- Parameters:
dm (
DensityMatrix
) – The density Matrix to evolve- Return type:
- Returns:
The evolved DensityMatrix
- evolve_svd(svd, progress_callback=None)
Compute the SVDistribution evolved through a linear optics circuit
- Parameters:
svd (SVDistribution | StateVector | BasicState) – The input StateVector distribution
progress_callback (callable) – A function with the signature func(progress: float, message: str)
- Return type:
dict
- Returns:
A dictionary of the form { “results”: SVDistribution, “physical_perf”: float, “logical_perf”: float }
results is the post-selected output SVDistribution
physical_perf is the performance computed from the detected photon filter
logical_perf is the performance computed from the post-selection
- 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).
- log_resources(method, extra_parameters)
Log resources of the simulator
- Parameters:
method (
str
) – name of the method usedextra_parameters (
dict
) –extra parameters to log
Extra parameter can be:
n
- 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_density_matrix(dm)
gives the output probability distribution, after evolving some density matrix through the simulator :type dm:
DensityMatrix
:param dm: the input DensityMatrix- Return type:
dict
- Returns:
A dictionary of the form { “results”: BSDistribution, “physical_perf”: float, “logical_perf”: float }
results is the post-selected output state distribution
physical_perf is the performance computed from the detected photon filter
logical_perf is the performance computed from the post-selection
- probs_svd(input_dist, detectors=None, progress_callback=None)
Compute the probability distribution from a SVDistribution input and as well as performance scores
- Parameters:
input_dist (SVDistribution | tuple[Source, FockState]) – A state vector distribution describing the input to simulate
detectors (list[IDetector]) – An optional list of detectors
progress_callback (callable) – A function with the signature func(progress: float, message: str)
- Return type:
dict[str, any]
- Returns:
A dictionary of the form { “results”: BSDistribution, “physical_perf”: float, “logical_perf”: float }
results is the post-selected output state distribution
physical_perf is the performance computed from the detected photon filter
logical_perf is the performance computed from the post-selection
- set_circuit(circuit, m=None)
Set a circuit for simulation.
- Parameters:
circuit (
ACircuit
) – a unitary circuit without polarized componentsm – The number of modes in the circuit. Only used in LC and TD simulators. If not provided, it is inferred from the modes of the components of the circuit list.
- set_heralds(heralds)
Set the output heralds of the simulator. Any output that does not match the heralds will be discarded. Only used in probs, probs_svd, evolve, evolve_svd
- 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_postselection(postselect)
Set a post-selection function
- Parameters:
postselect (
PostSelect
) – a PostSelect object
- set_precision(precision)
Set the precision of the simulator. When using probs_svd, states having a probability inferior to the precision times the highest probability will be discarded.
- 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}