You're reading the documentation of the v0.11. For the latest released version, please have a look at v0.12.

PostSelect

class perceval.utils.postselect.PostSelect

PostSelect is a callable basic state predicate intended to filter out unwanted basic states. It is designed to be a user-friendly description of any post-selection logic.

Parameters:

expression (str) – PostSelect string representation of the post-selection logic.

PostSelect Syntax

Condition

Condition syntax is [mode list] <operator> <photon count>

PostSelect expression is always composed of at least one condition.

  • Mode list:
    • string that represented a list[int]

    • should be a set of positive integer

  • Operator:
    • string that represent an operator.

    • Operator within a condition can be:
      • Equal: “==”

      • Greater than: “>”

      • Greater or equal to: “>=”

      • Less than: “<”

      • Less or equal to: “<=”

  • Photon count:
    • string that represent a non-negative integer

Logic operators

Within a PostSelect expression, several conditions can be composed with operators, grouped (with parenthesis) and even nested.

Condition(s) composed with an operator will is consider as a condition group.

Available logic operators are:
  • AND:
    • can compose 2 or more condition groups

    • possible string representation:
      • “AND”

      • “and”

      • “&” (default serialization string representation)

  • OR:
    • can compose 2 or more condition groups

    • possible string representation:
      • “OR”

      • “or”

      • “|” (default serialization string representation)

  • XOR:
    • can compose 2 or more condition groups

    • possible string representation:
      • “XOR”

      • “xor”

      • “^” (default serialization string representation)

  • NOT:
    • can be used in front of a condition group

    • possible string representation:
      • “NOT”

      • “not”

      • “!” (default serialization string representation)

Different operators cannot be used within the same condition group, parenthesis are necessary in order to explicit resolution order.

Examples

>>> ps = PostSelect("[0,1] == 1 & [2] > 1") # Means "I want exactly one photon in mode 0 or 1, and at least one photon in mode 2"
>>> print(ps(BasicState([0, 1, 2])))
True
>>> print(ps(BasicState([0, 1, 0])))
False
>>> print(ps(BasicState([1, 1, 2])))
False
>>> ps = PostSelect("([0,1] == 1 & [2] > 1) | [2] == 0") # Means "I want either exactly one photon in mode 0 or 1, and at least one photon in mode 2, or no photon in mode 2"
>>> print(ps(BasicState([0, 1, 1])))
False
>>> print(ps(BasicState([0, 1, 0])))
True
>>> print(ps(BasicState([1, 1, 2])))
False
__call__(state)

Return whether a state validates the defined post-selection logic.

Parameters:

state (FockState) – Basic state to post select

Return type:

bool

Returns:

Returns True if the input state validates the defined post-selection logic, returns False otherwise.

apply_permutation(perm_vector, first_mode=0)

Apply a given permutation on the conditions. Updates the current instance.

Parameters:
  • perm_vector (list[int]) – Permutation vector to apply (as returned by PERM.perm_vector)

  • first_mode (int) – First mode to apply the permutation on (default 0)

can_compose_with(modes)

Check if all conditions are compatible with a composition on given modes.

Compatible means that modes list is either a subset, or doesn’t intersect with any mode list of any conditions.

Parameters:

modes (list[int]) – Mode list to check compatibility

Return type:

bool

Returns:

True if the mode list is compatible, False otherwise

clear()

Clear all existing conditions

Return type:

None

property has_condition: bool

Returns True if at least one condition is defined

is_independent_with(other)

Check if other PostSelect instance is independent with current one.

Independent means that current and other instances does not share any modes within their conditions.

Parameters:

other (PostSelect) – Another PostSelect instance

Return type:

bool

Returns:

True if current and other instances are independent, False otherwise

merge(other)

Merge other PostSelect with an AND operator. Updates the current instance.

Parameters:

other (PostSelect) – Another PostSelect instance

shift_modes(shift)

Shift all mode indexes on all conditions. Updates the current instance.

Parameters:

shift (int) – Value to shift all mode indexes with