|
| 1 | +.. _circuit_construction: |
| 2 | + |
| 3 | +Circuit Creation |
| 4 | +================ |
| 5 | + |
| 6 | +There are several ways to create a circuit. You can either load in a circuit compiled by a PySDD or d4, or you can manually the circuit. |
| 7 | + |
| 8 | +Loading Circuits |
| 9 | +******************** |
| 10 | + |
| 11 | +An SDD can be loaded from a file as follows. |
| 12 | + |
| 13 | +.. code-block:: Python |
| 14 | +
|
| 15 | + import klaycircuits |
| 16 | +
|
| 17 | + circuit = klaycircuits.Circuit() |
| 18 | + circuit.add_SDD_from_file("path/to/my.sdd") |
| 19 | +
|
| 20 | +Similarly, for d4 we can use |
| 21 | + |
| 22 | +.. code-block:: Python |
| 23 | +
|
| 24 | + circuit = klaycircuits.Circuit() |
| 25 | + circuit.add_D4_from_file("path/to/my.nnf") |
| 26 | +
|
| 27 | +SDDs can also be loaded directly from a PySDD :code:`SddNode` object. |
| 28 | + |
| 29 | +.. code-block:: Python |
| 30 | +
|
| 31 | + from pysdd.sdd import SddManager |
| 32 | +
|
| 33 | + manager = SddManager(var_count = 2) |
| 34 | + sdd_node = manager.literal(1) & manager.literal(2) |
| 35 | +
|
| 36 | + circuit = klaycircuits.Circuit() |
| 37 | + circuit.add_sdd(sdd_node) |
| 38 | +
|
| 39 | +
|
| 40 | +Multi-Rooted Circuits |
| 41 | +********************* |
| 42 | + |
| 43 | +If you want to evaluate multiple circuits in parallel, you can merge them into a single multi-rooted circuit. |
| 44 | + |
| 45 | +.. code-block:: Python |
| 46 | +
|
| 47 | + circuit = klaycircuits.Circuit() |
| 48 | + circuit.add_sdd(first_sdd) |
| 49 | + circuit.add_sdd(second_sdd) |
| 50 | +
|
| 51 | +Evaluating this circuit will result in an output tensor with two elements. The order in which the circuits are added |
| 52 | +determines the order in the output when evaluating. |
| 53 | + |
| 54 | + |
| 55 | +Manual Circuits |
| 56 | +*************************** |
| 57 | + |
| 58 | +If you want to create a custom circuit, you can manually define the circuit structure. |
| 59 | +We start by defining some literals. |
| 60 | + |
| 61 | +.. code-block:: Python |
| 62 | +
|
| 63 | + circuit = klaycircuits.Circuit() |
| 64 | + a = circuit.literal_node(1) |
| 65 | + b = circuit.literal_node(-2) |
| 66 | +
|
| 67 | +Next, create and/or nodes as follows. |
| 68 | + |
| 69 | +.. code-block:: Python |
| 70 | +
|
| 71 | + and_node = circuit.and_node([a, b]) |
| 72 | +
|
| 73 | +To indicate that a node is a root (i.e. it will be part of the output), you need to mark it as root. |
| 74 | + |
| 75 | +.. code-block:: Python |
| 76 | +
|
| 77 | + circuit.set_root(and_node) |
| 78 | +
|
| 79 | +As we support multi-rooted circuits, you can later add more nodes and mark them as root. |
| 80 | + |
| 81 | +.. code-block:: Python |
| 82 | +
|
| 83 | + or_node = circuit.or_node([a, b]) |
| 84 | + circuit.set_root(or_node) |
| 85 | +
|
0 commit comments