Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
tididi for Python
Logo
tididi for Python
  • A first circuit
  • Using and reusing circuits

Worked examples

  • 1. Configuration rules
  • 2. What are we counting?
  • 3. Probabilities under changing assumptions
  • 4. Reachability in a directed graph
  • 5. Tables and updates
  • 6. Saving and loading related circuits
  • 7. Choosing a variable grouping
  • 8. Errors, limits, and retained scratch
  • 9. A custom minimum-cost calculation
  • 10. Inspecting circuit size

Reference

  • Python API
  • Functions, vtrees, and circuit size
Back to top
View this page

Note

Go to the end to download the full example code.

6. Saving and loading related circuitsΒΆ

Save the vtree once and each circuit separately. On reload, build one vtree object and pass it to every circuit that needs to be combined.

from tididi import Circuit, Vtree, literal

vtree = Vtree.balanced(3)
local = literal(vtree, 1)
remote = literal(vtree, 2)
encrypted = literal(vtree, 3)
destination = local | remote.copy()
encryption_rule = ~remote | encrypted

vtree_text = vtree.to_text()
destination_bytes = destination.to_bytes()
encryption_bytes = encryption_rule.to_bytes()

These are ordinary Python strings and bytes, suitable for a database, file, or message. The serialization queries leave the original circuits usable. Circuit.save(path) and Circuit.load(vtree, path) also accept paths directly, including pathlib.Path objects.

restored_vtree = Vtree.from_text(vtree_text)
destination = Circuit.from_bytes(restored_vtree, destination_bytes)
encryption_rule = Circuit.from_bytes(restored_vtree, encryption_bytes)
configurations = destination & encryption_rule
print("Restored rules allow", configurations.model_count(), "configurations")
Restored rules allow 4 configurations

The restored circuits share one domain. Rebuilding the same rules on that domain lets us check their Boolean equivalence.

local = literal(restored_vtree, 1)
remote = literal(restored_vtree, 2)
encrypted = literal(restored_vtree, 3)
expected = (local | remote.copy()) & (~remote | encrypted)
print("Equivalent to the original rules:", configurations.equivalent(expected))
Equivalent to the original rules: True

Download Jupyter notebook: 05_persistence.ipynb

Download Python source code: 05_persistence.py

Download zipped: 05_persistence.zip

Next
7. Choosing a variable grouping
Previous
5. Tables and updates
Copyright ©
Made with Sphinx and @pradyunsg's Furo