Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions circkit/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -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())