First of all let me congratulate with the entire team, I haven’t used Perceval in a long while and I have seen a tremendous progress!
I have a comment/question regarding the post selection functionality.
Currently is it possible to apply a PostSelect function only to “erase” unwanted states from a ProbabilityDistribution?
In that case, do you plan to extend it to StateVector? A general postselection rule allows to throw away unwanted portions of any kind of states, potentially, maintaining its coherence.
As a result, the Simulator object despite evolving a state cannot really apply a rule to the output, despite the presence of the set_postselection method (I mean, I can still get transform to distribution and postselect there but I’d like to keep the state coherent, along with its phasis!).
Thanks for your kind words, I’ll pass them to the whole software team!
Currently a PostSelect object can only do one thing: you feed him a Fock state and if answers True (select it) or False (filter it out). It’s used inside the Processor class to post-select measured states during a probability distribution or a sampling simulation.
So, in Perceval 0.10, you’d have to add some code in order to apply post-selection to an evolved StateVector. For instance, call:
def postselect_statevector(ps: PostSelect, sv: StateVector):
sv_out = StateVector()
for state, ampli in sv:
if ps(state):
sv_out += ampli*state
sv_out.normalize()
return sv_out
thanks for your answer and the code example, it was really helpful for me! I can only confirm what FGiorgino already said, it is really nice to work with perceval!
One thing that I would like to point your attention to: The added functionality of PostSelect in perceval 0.11, that is doing what you posted before, is (at least for me) not working. I think there is a tiny error in the code.
When I run the following code (I am using python 3.9.20 on a windows machine, [MSC v.1929 64 bit (AMD64)], perceval version 0.11.0):
post_sel(state_vec)
File ~\Anaconda3\lib\site-packages\perceval\utils\postselect.py:127 in __call__
s += state[i]
TypeError: unsupported operand type(s) for +=: 'int' and 'exqalibur.FockState'
I also checked it in jupyter and there a bit more of the definition of the called function is shown:
125 s = 0
126 for i in indexes:
--> 127 s += state[i]
128 if not operator(s, value):
129 return False
I think the problem is that different to your code example, where you did
sv_out = StateVector()
here the function does
s = 0
and therefore the problem with the addition between an int and a StateVector object arises.
For me it was not a problem, I just defined a function myself following your code example, but it might be nice for others to correct this error.
Starting with Perceval 0.11, you can use the post_select_statevector function which is available in perceval.utils. With it, you can use any combination of post-selection conditions and heralds to drop all basic states that do not fit in you state vectors.