distinct permutations
Perceval has a built-in method to construct efficiently the distinct permutations from an iterable. This method is comparable to the method from the more-itertools python package.
>>> import perceval as pcvl
>>> from perceval.utils.qmath import distinct_permutations
>>> print([pcvl.BasicState(perm) for perm in distinct_permutations([1, 1, 0])]) # Generate all states with n = 2 and at most 1 photon per mode
[|0,1,1>, |1,0,1>, |1,1,0>]
- perceval.utils.qmath.distinct_permutations(iterable, r=None)
Yield successive distinct permutations of the elements in iterable.
>>> sorted(distinct_permutations([1, 0, 1])) [(0, 1, 1), (1, 0, 1), (1, 1, 0)]
Equivalent to
set(permutations(iterable))
, except duplicates are not generated and thrown away. For larger input sequences this is much more efficient.Duplicate permutations arise when there are duplicated elements in the input iterable. The number of items returned is n! / (x_1! * x_2! * … * x_n!), where n is the total number of items input, and each x_i is the count of a distinct item in the input sequence.
If r is given, only the r-length permutations are yielded.
>>> sorted(distinct_permutations([1, 0, 1], r=2)) [(0, 1), (1, 0), (1, 1)] >>> sorted(distinct_permutations(range(3), r=2)) [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
This code is a copy of the method of the repository https://github.com/more-itertools/more-itertools/