qml.BooleanFn

class BooleanFn(fn, name=None)[source]

Bases: object

Wrapper for simple callables with Boolean output that can be manipulated and combined with bitwise operators.

Parameters

fn (callable) – Function to be wrapped. It can accept any number of arguments, and must return a Boolean.

Example

Consider functions that filter numbers to lie within a certain domain. We may wrap them using BooleanFn:

bigger_than_4 = qml.BooleanFn(lambda x: x > 4)
smaller_than_10 = qml.BooleanFn(lambda x: x < 10)
is_int = qml.BooleanFn(lambda x: isinstance(x, int))
>>> bigger_than_4(5.2)
True
>>> smaller_than_10(20.1)
False
>>> is_int(2.3)
False

These can then be combined into a single callable using boolean operators, such as & (logical and):

>>> between_4_and_10 = bigger_than_4 & smaller_than_10
>>> between_4_and_10(-3.2)
False
>>> between_4_and_10(9.9)
True
>>> between_4_and_10(19.7)
False

Other supported operators are | (logical or) and ~ (logical not):

smaller_equal_than_4 = ~bigger_than_4
smaller_than_10_or_int = smaller_than_10 | is_int

Warning

Note that Python conditional expressions are evaluated from left to right. As a result, the order of composition may matter, even though logical operators such as | and & are symmetric.

For example:

>>> is_int = qml.BooleanFn(lambda x: isinstance(x, int))
>>> has_bit_length_3 = qml.BooleanFn(lambda x: x.bit_length()==3)
>>> (is_int & has_bit_length_3)(4)
True
>>> (is_int & has_bit_length_3)(2.3)
False
>>> (has_bit_length_3 & is_int)(2.3)
AttributeError: 'float' object has no attribute 'bit_length'

bitwise

Determine whether the wrapped callable performs a bitwise operation or not.

conditional

Determine whether the wrapped callable is for a conditional or not.

bitwise

Determine whether the wrapped callable performs a bitwise operation or not. This checks for the operands attribute that should be defined by it.

conditional

Determine whether the wrapped callable is for a conditional or not. This checks for the condition attribute that should be defined by it.