qml.BooleanFn

class BooleanFn(fn)[source]

Bases: object

Wrapper for simple callables with boolean output that can be manipulated and combined with bit-wise operators.

Parameters

fn (callable) – Function to be wrapped. It must accept a single argument, and must return a boolean.

Example

Consider functions that filter numbers to lie in 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'

__call__(obj)

Call self as a function.

__call__(obj)[source]

Call self as a function.

Contents

Using PennyLane

Development

API