quimb.tensor.belief_propagation.hv1bp ===================================== .. py:module:: quimb.tensor.belief_propagation.hv1bp .. autoapi-nested-parse:: Hyper, vectorized, 1-norm, belief propagation. Classes ------- .. autoapisummary:: quimb.tensor.belief_propagation.hv1bp.HV1BP Functions --------- .. autoapisummary:: quimb.tensor.belief_propagation.hv1bp._compute_all_hyperind_messages_tree_batched quimb.tensor.belief_propagation.hv1bp._compute_all_hyperind_messages_prod_batched quimb.tensor.belief_propagation.hv1bp._compute_all_tensor_messages_tree_batched quimb.tensor.belief_propagation.hv1bp._compute_all_tensor_messages_prod_batched quimb.tensor.belief_propagation.hv1bp._compute_output_single_t quimb.tensor.belief_propagation.hv1bp._compute_output_single_m quimb.tensor.belief_propagation.hv1bp._update_output_to_input_single_batched quimb.tensor.belief_propagation.hv1bp._gather_zb quimb.tensor.belief_propagation.hv1bp._contract_index_region_single quimb.tensor.belief_propagation.hv1bp._contract_tensor_region_single quimb.tensor.belief_propagation.hv1bp._contract_messages_pair_single quimb.tensor.belief_propagation.hv1bp._extract_messages_from_inputs_batched quimb.tensor.belief_propagation.hv1bp.contract_hv1bp quimb.tensor.belief_propagation.hv1bp.run_belief_propagation_hv1bp quimb.tensor.belief_propagation.hv1bp.sample_hv1bp Module Contents --------------- .. py:function:: _compute_all_hyperind_messages_tree_batched(bm) .. py:function:: _compute_all_hyperind_messages_prod_batched(bm, smudge_factor=1e-12) .. py:function:: _compute_all_tensor_messages_tree_batched(bx, bm) Compute all output messages for a stacked tensor and messages. .. py:function:: _compute_all_tensor_messages_prod_batched(bx, bm, smudge_factor=1e-12) .. py:function:: _compute_output_single_t(bm, bx, normalize, smudge_factor=1e-12) .. py:function:: _compute_output_single_m(bm, normalize, smudge_factor=1e-12) .. py:function:: _update_output_to_input_single_batched(batched_input, batched_output, mask, _distance_fn, damping=0.0) .. py:function:: _gather_zb(zb, power=1.0) Given a vector of local contraction estimates `zb`, compute their product, avoiding underflow/overflow by accumulating the sign and exponent separately. :param zb: The local contraction estimates. :type zb: array :param power: Raise the final result to this power. :type power: float, optional :returns: * **sign** (*float*) -- The accumulated sign or phase. * **exponent** (*float*) -- The accumulated exponent. .. py:function:: _contract_index_region_single(bm) .. py:function:: _contract_tensor_region_single(rank, batched_tensors, batched_inputs_t) .. py:function:: _contract_messages_pair_single(ranki, ranko, mask, batched_inputs_m, batched_inputs_t) .. py:function:: _extract_messages_from_inputs_batched(batched_inputs_m, batched_inputs_t, input_locs_m, input_locs_t) Get all messages as a dict from the batch stacked input form. .. py:class:: HV1BP(tn, *, messages=None, damping=0.0, diis=False, update='parallel', normalize='L2', distance='L2', smudge_factor=1e-12, thread_pool=False, contract_every=None, inplace=False) Bases: :py:obj:`quimb.tensor.belief_propagation.bp_common.BeliefPropagationCommon` Object interface for hyper, vectorized, 1-norm, belief propagation. This is the fast version of belief propagation possible when there are many, small, matching tensor sizes. :param tn: The tensor network to run BP on. :type tn: TensorNetwork :param messages: Initial messages to use, if not given then uniform messages are used. :type messages: dict, optional :param damping: The damping factor to apply to messages. This simply mixes some part of the old message into the new one, with the final message being ``damping * old + (1 - damping) * new``. This makes convergence more reliable but slower. :type damping: float or callable, optional :param diis: Whether to use direct inversion in the iterative subspace to help converge the messages by extrapolating to low error guesses. If a dict, should contain options for the DIIS algorithm. The relevant options are {`max_history`, `beta`, `rcond`}. :type diis: bool or dict, optional :param update: Whether to update messages sequentially (newly computed messages are immediately used for other updates in the same iteration round) or in parallel (all messages are comptued using messages from the previous round only). Sequential generally helps convergence but parallel can possibly converge to differnt solutions. :type update: {'sequential', 'parallel'}, optional :param normalize: How to normalize messages after each update. If None choose automatically. If a callable, it should take a message and return the normalized message. If a string, it should be one of 'L1', 'L2', 'L2phased', 'Linf' for the corresponding norms. 'L2phased' is like 'L2' but also normalizes the phase of the message, by default used for complex dtypes. :type normalize: {'L1', 'L2', 'L2phased', 'Linf', callable}, optional :param distance: How to compute the distance between messages to check for convergence. If None choose automatically. If a callable, it should take two messages and return the distance. If a string, it should be one of 'L1', 'L2', 'L2phased', 'Linf', or 'cosine' for the corresponding norms. 'L2phased' is like 'L2' but also normalizes the phases of the messages, by default used for complex dtypes if phased normalization is not already being used. :type distance: {'L1', 'L2', 'L2phased', 'Linf', 'cosine', callable}, optional :param smudge_factor: A small number to add to the denominator of messages to avoid division by zero. Note when this happens the numerator will also be zero. :type smudge_factor: float, optional :param thread_pool: Whether to use a thread pool for parallelization, if ``True`` use the default number of threads, if an integer use that many threads. :type thread_pool: bool or int, optional :param contract_every: If not None, 'contract' (via BP) the tensor network every ``contract_every`` iterations. The resulting values are stored in ``zvals`` at corresponding points ``zval_its``. :type contract_every: int, optional :param inplace: Whether to perform any operations inplace on the input tensor network. :type inplace: bool, optional .. py:attribute:: smudge_factor :value: 1e-12 .. py:attribute:: pool :value: None .. py:property:: normalize .. py:property:: distance .. py:method:: initialize_messages_batched(messages=None) .. py:property:: messages .. py:method:: _compute_outputs_batched(batched_inputs, batched_tensors=None) Given stacked messsages and optionally tensors, compute stacked output messages, possibly using parallel pool. .. py:method:: _update_outputs_to_inputs_batched(batched_inputs, batched_outputs, masks) Update the stacked input messages from the stacked output messages. .. py:method:: iterate(tol=None) Perform a single iteration of belief propagation. Subclasses should implement this method, returning either `max_mdiff` or a dictionary containing `max_mdiff` and any other relevant information: { "nconv": nconv, "ncheck": ncheck, "max_mdiff": max_mdiff, } .. py:method:: get_messages_dense() Get messages in individual form from the batched stacks. .. py:method:: get_messages() .. py:method:: contract(strip_exponent=False, check_zero=False) Estimate the contraction of the tensor network using the current messages. Uses batched vectorized contractions for speed. :param strip_exponent: Whether to strip the exponent from the final result. If ``True`` then the returned result is ``(mantissa, exponent)``. :type strip_exponent: bool, optional :param check_zero: Whether to check for zero values and return zero early. Currently ``True`` is not implemented for HV1BP. :type check_zero: bool, optional :rtype: scalar or (scalar, float) .. py:method:: contract_dense(strip_exponent=False, check_zero=True) Slow contraction via explicit extranting individual dense messages. This supports check_zero=True and may be useful for debugging. .. py:function:: contract_hv1bp(tn, messages=None, max_iterations=1000, tol=5e-06, damping=0.0, diis=False, update='parallel', normalize='L2', distance='L2', tol_abs=None, tol_rolling_diff=None, smudge_factor=1e-12, strip_exponent=False, check_zero=False, info=None, progbar=False) Estimate the contraction of ``tn`` with hyper, vectorized, 1-norm belief propagation, via the exponential of the Bethe free entropy. :param tn: The tensor network to run BP on, can have hyper indices. :type tn: TensorNetwork :param messages: Initial messages to use, if not given then uniform messages are used. :type messages: dict, optional :param max_iterations: The maximum number of iterations to perform. :type max_iterations: int, optional :param tol: The convergence tolerance for messages. :type tol: float, optional :param damping: The damping factor to use, 0.0 means no damping. :type damping: float, optional :param diis: Whether to use direct inversion in the iterative subspace to help converge the messages by extrapolating to low error guesses. If a dict, should contain options for the DIIS algorithm. The relevant options are {`max_history`, `beta`, `rcond`}. :type diis: bool or dict, optional :param update: Whether to update messages sequentially or in parallel. :type update: {'parallel'}, optional :param normalize: How to normalize messages after each update. If None choose automatically. If a callable, it should take a message and return the normalized message. If a string, it should be one of 'L1', 'L2', 'L2phased', 'Linf' for the corresponding norms. 'L2phased' is like 'L2' but also normalizes the phase of the message, by default used for complex dtypes. :type normalize: {'L1', 'L2', 'L2phased', 'Linf', callable}, optional :param distance: How to compute the distance between messages to check for convergence. If None choose automatically. If a callable, it should take two messages and return the distance. If a string, it should be one of 'L1', 'L2', 'L2phased', 'Linf', or 'cosine' for the corresponding norms. 'L2phased' is like 'L2' but also normalizes the phases of the messages, by default used for complex dtypes if phased normalization is not already being used. :type distance: {'L1', 'L2', 'L2phased', 'Linf', 'cosine', callable}, optional :param tol_abs: The absolute convergence tolerance for maximum message update distance, if not given then taken as ``tol``. :type tol_abs: float, optional :param tol_rolling_diff: The rolling mean convergence tolerance for maximum message update distance, if not given then taken as ``tol``. This is used to stop running when the messages are just bouncing around the same level, without any overall upward or downward trends, roughly speaking. :type tol_rolling_diff: float, optional :param smudge_factor: A small number to add to the denominator of messages to avoid division by zero. Note when this happens the numerator will also be zero. :type smudge_factor: float, optional :param strip_exponent: Whether to strip the exponent from the final result. If ``True`` then the returned result is ``(mantissa, exponent)``. :type strip_exponent: bool, optional :param check_zero: Whether to check for zero values and return zero early. :type check_zero: bool, optional :param info: If specified, update this dictionary with information about the belief propagation run. :type info: dict, optional :param progbar: Whether to show a progress bar. :type progbar: bool, optional :rtype: scalar or (scalar, float) .. py:function:: run_belief_propagation_hv1bp(tn, messages=None, max_iterations=1000, tol=5e-06, damping=0.0, diis=False, update='parallel', normalize='L2', distance='L2', tol_abs=None, tol_rolling_diff=None, smudge_factor=1e-12, info=None, progbar=False) Run belief propagation on a tensor network until it converges. :param tn: The tensor network to run BP on. :type tn: TensorNetwork :param messages: The current messages. For every index and tensor id pair, there should be a message to and from with keys ``(ix, tid)`` and ``(tid, ix)``. If not given, then messages are initialized as uniform. :type messages: dict, optional :param max_iterations: The maximum number of iterations to run for. :type max_iterations: int, optional :param tol: The convergence tolerance. :type tol: float, optional :param damping: The damping factor to use, 0.0 means no damping. :type damping: float, optional :param diis: Whether to use direct inversion in the iterative subspace to help converge the messages by extrapolating to low error guesses. If a dict, should contain options for the DIIS algorithm. The relevant options are {`max_history`, `beta`, `rcond`}. :type diis: bool or dict, optional :param update: Whether to update messages sequentially or in parallel. :type update: {'parallel'}, optional :param normalize: How to normalize messages after each update. If None choose automatically. If a callable, it should take a message and return the normalized message. If a string, it should be one of 'L1', 'L2', 'L2phased', 'Linf' for the corresponding norms. 'L2phased' is like 'L2' but also normalizes the phase of the message, by default used for complex dtypes. :type normalize: {'L1', 'L2', 'L2phased', 'Linf', callable}, optional :param distance: How to compute the distance between messages to check for convergence. If None choose automatically. If a callable, it should take two messages and return the distance. If a string, it should be one of 'L1', 'L2', 'L2phased', 'Linf', or 'cosine' for the corresponding norms. 'L2phased' is like 'L2' but also normalizes the phases of the messages, by default used for complex dtypes if phased normalization is not already being used. :type distance: {'L1', 'L2', 'L2phased', 'Linf', 'cosine', callable}, optional :param tol_abs: The absolute convergence tolerance for maximum message update distance, if not given then taken as ``tol``. :type tol_abs: float, optional :param tol_rolling_diff: The rolling mean convergence tolerance for maximum message update distance, if not given then taken as ``tol``. This is used to stop running when the messages are just bouncing around the same level, without any overall upward or downward trends, roughly speaking. :type tol_rolling_diff: float, optional :param smudge_factor: A small number to add to the denominator of messages to avoid division by zero. Note when this happens the numerator will also be zero. :type smudge_factor: float, optional :param info: If specified, update this dictionary with information about the belief propagation run. :type info: dict, optional :param progbar: Whether to show a progress bar. :type progbar: bool, optional :returns: * **messages** (*dict*) -- The final messages. * **converged** (*bool*) -- Whether the algorithm converged. .. py:function:: sample_hv1bp(tn, messages=None, output_inds=None, max_iterations=1000, tol=0.01, damping=0.0, diis=False, update='parallel', normalize='L2', distance='L2', tol_abs=None, tol_rolling_diff=None, smudge_factor=1e-12, bias=False, seed=None, progbar=False) Sample all indices of a tensor network using repeated belief propagation runs and decimation. :param tn: The tensor network to sample. :type tn: TensorNetwork :param messages: The current messages. For every index and tensor id pair, there should be a message to and from with keys ``(ix, tid)`` and ``(tid, ix)``. If not given, then messages are initialized as uniform. :type messages: dict, optional :param output_inds: The indices to sample. If not given, then all indices are sampled. :type output_inds: sequence of str, optional :param max_iterations: The maximum number of iterations for each message passing run. :type max_iterations: int, optional :param tol: The convergence tolerance for each message passing run. :type tol: float, optional :param damping: The damping factor to use, 0.0 means no damping. :type damping: float, optional :param diis: Whether to use direct inversion in the iterative subspace to help converge the messages by extrapolating to low error guesses. If a dict, should contain options for the DIIS algorithm. The relevant options are {`max_history`, `beta`, `rcond`}. :type diis: bool or dict, optional :param update: Whether to update messages sequentially or in parallel. :type update: {'parallel'}, optional :param normalize: How to normalize messages after each update. If None choose automatically. If a callable, it should take a message and return the normalized message. If a string, it should be one of 'L1', 'L2', 'L2phased', 'Linf' for the corresponding norms. 'L2phased' is like 'L2' but also normalizes the phase of the message, by default used for complex dtypes. :type normalize: {'L1', 'L2', 'L2phased', 'Linf', callable}, optional :param distance: How to compute the distance between messages to check for convergence. If None choose automatically. If a callable, it should take two messages and return the distance. If a string, it should be one of 'L1', 'L2', 'L2phased', 'Linf', or 'cosine' for the corresponding norms. 'L2phased' is like 'L2' but also normalizes the phases of the messages, by default used for complex dtypes if phased normalization is not already being used. :type distance: {'L1', 'L2', 'L2phased', 'Linf', 'cosine', callable}, optional :param tol_abs: The absolute convergence tolerance for maximum message update distance, if not given then taken as ``tol``. :type tol_abs: float, optional :param tol_rolling_diff: The rolling mean convergence tolerance for maximum message update distance, if not given then taken as ``tol``. This is used to stop running when the messages are just bouncing around the same level, without any overall upward or downward trends, roughly speaking. :type tol_rolling_diff: float, optional :param smudge_factor: A small number to add to each message to avoid zeros. Making this large is similar to adding a temperature, which can aid convergence but likely produces less accurate marginals. :type smudge_factor: float, optional :param bias: Whether to bias the sampling towards the largest marginal. If ``False`` (the default), then indices are sampled proportional to their marginals. If ``True``, then each index is 'sampled' to be its largest weight value always. If a float, then the local probability distribution is raised to this power before sampling. :type bias: bool or float, optional :param thread_pool: Whether to use a thread pool for parallelization. If an integer, then this is the number of threads to use. If ``True``, then the number of threads is set to the number of cores. If a ``ThreadPoolExecutor``, then this is used directly. :type thread_pool: bool, int or ThreadPoolExecutor, optional :param seed: A random seed to use for the sampling. :type seed: int, optional :param progbar: Whether to show a progress bar. :type progbar: bool, optional :returns: * **config** (*dict[str, int]*) -- The sample configuration, mapping indices to values. * **tn_config** (*TensorNetwork*) -- The tensor network with all index values (or just those in `output_inds` if supllied) selected. Contracting this tensor network (which will just be a sequence of scalars if all index values have been sampled) gives the weight of the sample, e.g. should be 1 for a SAT problem and valid assignment. * **omega** (*float*) -- The probability of choosing this sample (i.e. product of marginal values). Useful possibly for importance sampling.