polycirc.serialize

Serialize and deserialize circuits.

Serialize a diagram to JSON with diagram_to_json(), and deserialize with diagram_from_json().

>>> diagram_to_json(ir.add(1))
'{"wires": 3, "sources": [0, 1], "targets": [2], "operations": [{"operation": "+", "sources": [0, 1], "targets": [2]}]}'

You can also “serialize” diagrams to dicts using diagram_to_dict(), and deserialize with diagram_from_dict().

Format

Circuits are serialized in a graph-like format. The top-level object, corresponding to the SerializedDiagram DTO class, has four fields:

{ "wires":   <number of wires in the circuit>
, "sources": <wires corresponding to the circuit's inputs>
, "targets": <wires corresponding to outputs>
, "operations": [ <a list of basic operations in the circuit> ]
}

Each of the values in the "operations" list is of the form below, described by the SerializedOperation DTO class.

{ "operation": <text representation of the operation, or an integer constant>
, "sources": <wires corresponding to operation inputs>
, "targets": <wires corresponding to operation outputs>
}

This represents an operation like polycirc.operation.Add, along with its “inputs” (sources) and “outputs” (targets). For example, an add operation could be represented like this:

{ "operation": "+"
, "sources": [0, 1]
, "targets": [2]
}

As a more complex example, let’s construct the expression (x₀ + x₁) * x₂. Graphically, that looks like this circuit:

     0 ┌───┐
x₀ ────┤   │ 3
     1 │ + ├─┐
x₁ ────┤   │ │ ┌───┐
       └───┘ └─┤   │  4
        2      │ * ├──── (x₀ + x₁) * x₂
x₂ ────────────┤   │
               └───┘

There are 5 wires here, labeled [0, 1, 2, 3, 4]. A dict representing this diagram might look like this:

{ "wires": 5
, "sources": [0, 1, 2]
, "targets": [4]
, "operations": [
    { "operation": "+"
    , "sources": [0, 1]
    , "targets": [3]
    },

    { "operation": "*"
    , "sources": [3, 2]
    , "targets": [4]
    }]
}
polycirc.serialize.diagram_to_dict(d: Diagram) dict

Serialize a diagram as a dict

polycirc.serialize.diagram_to_json(d: Diagram) str

Serialize a diagram as a JSON string

polycirc.serialize.diagram_from_dict(d: dict) Diagram

Deserialize a Diagram from a dict

polycirc.serialize.diagram_from_json(s: str) Diagram

Deserialize a Diagram from a JSON string

class polycirc.serialize.SerializedOperation(operation: str | int, sources: List[int], targets: List[int])

DTO corresponding to a single operation within a hypergraph

operation: str | int
sources: List[int]
targets: List[int]
to_op_label() Operation
to_singleton() SingletonOp
static from_singleton(s: SingletonOp) SerializedOperation
class polycirc.serialize.SerializedDiagram(wires: int, sources: List[int], targets: List[int], operations: List[SerializedOperation])

DTO for a complete circuit (diagram).

wires: int
sources: List[int]
targets: List[int]
operations: List[SerializedOperation]
static from_diagram(d: Diagram) SerializedDiagram

Serialize a diagram to a dict

to_diagram() Diagram

Deserialize a diagram from a dict