diff --git a/circkit/circuit.py b/circkit/circuit.py index 8f5197d..4c34583 100644 --- a/circkit/circuit.py +++ b/circkit/circuit.py @@ -328,6 +328,16 @@ def clone_empty(self, base_ring=None, name=None): # -------------------------------- def add_node(self, node): + if not isinstance(node, Node): + if isinstance(node, Operation): + raise TypeError( + "Trying to add Operation as node, need to create a node from it." + " Call the operation with its node arguments." + ) + raise TypeError( + f"Trying to add {node:r} as node, but it is not a Node." + ) + if node.circuit is not self: raise RuntimeError(f"node's circuit {node.circuit} is not self") @@ -390,6 +400,10 @@ def add_output(self, value): raise TypeError( f"Can not output the Node {value} from a different circuit" ) + elif isinstance(value, Operation): + raise TypeError( + "Can not output Operation, need to create a node. Call the operation with its node arguments." + ) try: nodes = list(value) diff --git a/tests/test_types.py b/tests/test_types.py new file mode 100644 index 0000000..f8fb37b --- /dev/null +++ b/tests/test_types.py @@ -0,0 +1,26 @@ +import random +import pytest + +from circkit.arithmetic import ArithmeticCircuit + +def test_random(): + C = ArithmeticCircuit(name="TwoRandoms") + + # note: add_node is not needed to be called usually + op5 = C.CONST(5) + with pytest.raises(TypeError): + C.add_node(op5) + with pytest.raises(TypeError): + C.add_output(op5) + + # correct usage (create node) + C.add_output(op5()) + + op_rand = C.RND() + with pytest.raises(TypeError): + C.add_node(op_rand) + with pytest.raises(TypeError): + C.add_output(op_rand) + + # correct usage (create node) + C.add_output(op_rand())