.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/07_execution.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_07_execution.py: Errors, limits, and retained scratch ======================================== Ordinary operators raise exceptions on failure. Named operations additionally accept a ``limits=`` keyword. This lets a caller bound one expensive operation while using the same circuits and vtree as before. .. GENERATED FROM PYTHON SOURCE LINES 10-18 .. code-block:: Python from tididi import Limits, Vtree, and_, clause, literal vtree = Vtree.balanced(4) try: destination = clause(vtree, [1, 2], limits=Limits(memory_bytes=0)) except MemoryError: print("Not enough budget to build the destination rule") .. rst-class:: sphx-glr-script-out .. code-block:: none Not enough budget to build the destination rule .. GENERATED FROM PYTHON SOURCE LINES 19-20 Limits belong to the call. They do not remain installed on later operations. .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python destination = clause(vtree, [1, 2]) print("Destination choices:", destination.model_count()) .. rst-class:: sphx-glr-script-out .. code-block:: none Destination choices: 12 .. GENERATED FROM PYTHON SOURCE LINES 24-28 Keep inputs for a retry --------------------------- A resource error can occur after a consuming call has taken its inputs. Explicit copies preserve the originals for another attempt. .. GENERATED FROM PYTHON SOURCE LINES 28-36 .. code-block:: Python encrypted = literal(vtree, 3) try: secured = and_(destination.copy(), encrypted.copy(), limits=Limits(memory_bytes=0)) except MemoryError: print("Original operands still available:", not destination.is_consumed, not encrypted.is_consumed) secured = destination & encrypted print("Secured choices:", secured.model_count()) .. rst-class:: sphx-glr-script-out .. code-block:: none Original operands still available: True True Secured choices: 6 .. GENERATED FROM PYTHON SOURCE LINES 37-41 A query borrows its circuit even if its budget is refused. A timeout is checked cooperatively; an output-node cap can bound the number of nodes an operation emits. Byte limits cover storage charged by the native operation, not the Python process or input conversion buffers. .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python print("Bounded count:", secured.model_count(limits=Limits(memory_bytes=1_000_000))) .. rst-class:: sphx-glr-script-out .. code-block:: none Bounded count: 6 .. GENERATED FROM PYTHON SOURCE LINES 44-46 Operations on a shared vtree reuse scratch buffers automatically. Clear idle buffers between batches when retaining capacity is no longer useful. .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: Python vtree.clear_scratch() print("Circuit remains usable:", secured.is_sat()) .. rst-class:: sphx-glr-script-out .. code-block:: none Circuit remains usable: True .. _sphx_glr_download_tutorials_07_execution.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 07_execution.ipynb <07_execution.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 07_execution.py <07_execution.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 07_execution.zip <07_execution.zip>`