Skip to content

Commit c8669ce

Browse files
committed
document circuit creation
1 parent 486b174 commit c8669ce

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed

docs/circuit_creation.rst

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+

docs/index.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ At its core, KLay transforms sparse directed acyclic graphs into layers that can
1313
.. image:: _static/scatter_reduce.png
1414
:width: 400
1515

16+
----
1617

18+
Contents
19+
********
1720

1821
.. toctree::
1922

2023
Home <self>
2124
quickstart
22-
api
25+
circuit_creation
2326

docs/quickstart.rst

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ Usage
1818
*****
1919

2020
First, we need to create a circuit. You can both manually define the circuit, or import it from a knowledge compiler.
21-
So far KLay supports the PySDD and d4 circuit formats.
22-
For example, you can load in a PySDD circuit as follows.
21+
For more information, check out the :ref:`circuit_construction` guide.
2322

2423
.. code-block:: Python
2524
@@ -28,21 +27,27 @@ For example, you can load in a PySDD circuit as follows.
2827
circuit = klaycircuits.Circuit()
2928
circuit.add_sdd(sdd_node)
3029
31-
For more information on circuit construction, check out <todo>.
32-
3330
Now that we have the circuit, we can evaluate it. To do this, we first turn the circuit into a PyTorch module.
34-
Other backends such as Jax are also supported.
3531

3632
.. code-block:: Python
3733
3834
import torch
3935
4036
module = circuit.to_torch_module()
37+
module = module.to("cuda:0")
4138
42-
We can use this module as any other PyTorch module. The expected input is a tensor with the weights for each literal.
39+
We can use our circuit as any other PyTorch module. The input should be a tensor with the weights for each literal, and the output is the result of evaluating circuit.
4340

4441
.. code-block:: Python
4542
46-
weights = torch.tensor([...])
43+
weights = torch.tensor([...], device="cuda:0")
4744
result = module(weights)
48-
result.backward()
45+
result.backward()
46+
47+
Backends
48+
********
49+
50+
KLay needs a backend to evaluate the circuit in. So far, we implemented two different backends.
51+
52+
- PyTorch: :code:`torch_module = circuit.to_torch_module()`
53+
- Jax: :code:`jax_function = circuit.to_jax_function()`

0 commit comments

Comments
 (0)