Predicates and Filters¶
qecc.Predicate
: Class representing predicate functions¶
The qecc
package provides a class Predicate
to represent a
predicate function; that is, a function which returns a bool
.
-
class
qecc.
Predicate
(fn)[source]¶ Class representing a predicate function on one or more arguments.
>>> from qecc import Predicate >>> p = Predicate(lambda x: x > 0) >>> p(1) True >>> p(-1) False
Instances can also be constructed by logical operations on existing Predicate instances:
>>> q = Predicate(lambda x: x < 3) >>> (p & q)(1) True >>> (p | q)(-1) True >>> (~p)(2) False
-
combine
(other, outer_fn)[source]¶ Returns a new
Predicate
that combines this predicate with another predicate using a given function to combine the results.>>> gt_2 = Predicate(lambda x: x > 2) >>> even = Predicate(lambda x: x % 2 == 0) >>> nand = lambda x, y: not (x and y) >>> r = gt_2.combine(even, nand) >>> map(r, range(1,5)) [True, True, True, False]
-
Specific Predicates¶
Several useful predefined predicates are provided by qecc
.
-
class
qecc.
SetMembershipPredicate
(S)[source]¶ Given an iterable
S
, constructs a predicate that returnsTrue
if and only if its argument is inS
.>>> from qecc import SetMembershipPredicate >>> p = SetMembershipPredicate(range(4)) >>> map(p, range(-1, 5)) [False, True, True, True, True, False]
-
class
qecc.
PauliMembershipPredicate
(S, ignore_phase=True)[source]¶ Given a set
S
of Pauli operators represented asqecc.Pauli
instances, constructs a predicate that returnsTrue
for a PauliP
if and only ifP
is inS
.If the keyword argument
ignore_phase
isTrue
, then the comparison to determine whetherP
is inS
only considers the operator part ofP
.
In addition, utility functions are provided for constructing predicates based on commutation properties of the Pauli group.
Usage Examples¶
Predicate functions can be used to quickly generate collections of
qecc.Pauli
operators having a given set of properties.
>>> from qecc import commutes_with, in_group_generated_by, pauli_group
>>> print filter(
... commutes_with('XX', 'ZZ') & ~in_group_generated_by('XX'),
... pauli_group(2)
... )
[i^0 YY, i^0 ZZ]
Since searching in this way requires examining every element of a given iterator, it can be significantly faster to instead use constraint solvers such as those documented in Constraint Solvers.