def __init__(self, function, **kwargs): """ Initialize a control function oracle. Args: function (int): Function truth table. Keyword Args: synth: A RevKit synthesis command which creates a reversible circuit based on a truth table and requires no additional ancillae (e.g., ``revkit.esopbs``). Can also be a nullary lambda that calls several RevKit commands. **Default:** ``revkit.esopbs`` """ if isinstance(function, int): self.function = function else: try: import dormouse # pylint: disable=import-outside-toplevel self.function = dormouse.to_truth_table(function) except ImportError as err: # pragma: no cover raise RuntimeError( "The dormouse library needs to be installed in order to " "automatically compile Python code into functions. Try " "to install dormouse with 'pip install dormouse'." ) from err self.kwargs = kwargs self._check_function()
def test_to_boolean_function_ite(): def _func_ite(a, b, c): return b if a else c a, b, c = sympy.symbols("a b c") assert to_boolean_function(_func_ite) == sympy.ITE(a, b, c) assert to_truth_table(_func_ite) == 0xd8 assert to_binary_truth_table(_func_ite) == "11011000" assert to_hex_truth_table(_func_ite) == "d8"
def test_to_boolean_function_assignment_expressions(): def _func_majority(a, b, c): t1 = a & b t2 = a & c t3 = b & c return t1 | t2 | t3 def _func_majority2(a, b, c): t1 = a != b t2 = a | b t3 = c < t1 t4 = t2 > t3 return t4 a, b, c = sympy.symbols("a b c") assert to_boolean_function(_func_majority) == (a & b) | (a & c) | (b & c) assert to_truth_table(_func_majority2) == 0xe8