dwave.optimization.model.Model.lock#

Model.lock()#

Lock the model.

No new symbols can be added to a locked model.

Returns:

A context manager. If the context is subsequently exited then the unlock() will be called.

See also

is_locked(), unlock()

Examples

This example checks the status of a model after locking it and subsequently unlocking it.

>>> from dwave.optimization.model import Model
>>> model = Model()
>>> i = model.integer(20, upper_bound=100)
>>> cntx = model.lock()
>>> model.is_locked()
True
>>> model.unlock()
>>> model.is_locked()
False

This example locks a model temporarily with a context manager.

>>> model = Model()
>>> with model.lock():
...     # no nodes can be added within the context
...     print(model.is_locked())
True
>>> model.is_locked()
False