Ejemplo n.º 1
0
def test_symbols():

    a, b = Symbol('a'), Symbol('b')
    quso = {
        (0, ): 1.0 * a,
        (0, 1): 1.,
        (1, ): -1.0 * a,
        (1, 2): 1.,
        (): -2. * b,
        (2, ): 1.0 * a
    }
    quso1 = qubo_to_quso(quso_to_qubo(quso))
    quso1.simplify()
    quso = QUSO(quso)
    quso.simplify()
    assert quso == quso1

    a, b = Symbol('a'), Symbol('b')
    qubo = {
        (0, ): 1.0 * a,
        (0, 1): 1.,
        (1, ): -1.0 * a,
        (1, 2): 1.,
        (): -2.0 * b,
        (2, ): 1.0 * a
    }
    qubo1 = quso_to_qubo(qubo_to_quso(qubo))
    qubo1.simplify()
    qubo = QUBO(qubo)
    qubo.simplify()
    assert qubo == qubo1
Ejemplo n.º 2
0
def test_quso_to_qubo_to_quso():

    quso = {(0, 1): -4, (0, 2): 3, (): -2, (0, ): 1, (2, ): -2}
    assert quso == qubo_to_quso(quso_to_qubo(quso))

    quso = {('0', 1): -4, ('0', '2'): 3, (): -2, ('0', ): 1, ('2', '2'): -2}
    # need to reformat quso so it is sorted with the same hash and squashed key
    assert QUSO(quso) == qubo_to_quso(quso_to_qubo(quso))
Ejemplo n.º 3
0
def test_qubo_to_quso_to_qubo():

    qubo = {(0, ): 1, (0, 1): 1, (1, ): -1, (1, 2): .2, (): -2, (2, ): 1}
    assert qubo == quso_to_qubo(qubo_to_quso(qubo))

    qubo = {
        ('0', ): 1,
        ('0', 1): 1,
        (1, ): -1,
        (1, '2'): .2,
        (): -2,
        ('2', ): 1,
        (0, 0): 1
    }
    # need to reformatt qubo so it is sorted with the same hash
    assert QUBO(qubo) == quso_to_qubo(qubo_to_quso(qubo))
Ejemplo n.º 4
0
def test_quso_to_qubo_to_quso():

    quso = {(0, 1): -4, (0, 2): 3, (): -2, (0, ): 1, (2, ): -2}
    assert quso == qubo_to_quso(quso_to_qubo(quso))

    # type asserting
    assert type(quso_to_qubo(quso)) == QUBO
    assert type(quso_to_qubo(QUSOMatrix(quso))) == QUBOMatrix
    assert type(quso_to_qubo(QUSO(quso))) == QUBO

    quso = {('0', 1): -4, ('0', '2'): 3, (): -2, ('0', ): 1, ('2', '2'): -2}
    # need to reformat quso so it is sorted with the same hash and squashed key
    assert QUSO(quso) == qubo_to_quso(quso_to_qubo(quso))

    # type asserting
    assert type(quso_to_qubo(quso)) == QUBO
    assert type(quso_to_qubo(QUSO(quso))) == QUBO
Ejemplo n.º 5
0
def test_qubo_to_quso_to_qubo():

    qubo = {(0, ): 1, (0, 1): 1, (1, ): -1, (1, 2): .2, (): -2, (2, ): 1}
    assert qubo == quso_to_qubo(qubo_to_quso(qubo))

    # type asserting
    assert type(qubo_to_quso(qubo)) == QUSO
    assert type(qubo_to_quso(QUBOMatrix(qubo))) == QUSOMatrix
    assert type(qubo_to_quso(QUBO(qubo))) == QUSO

    qubo = {
        ('0', ): 1,
        ('0', 1): 1,
        (1, ): -1,
        (1, '2'): .2,
        (): -2,
        ('2', ): 1,
        (0, 0): 1
    }
    # need to reformat qubo so it is sorted with the same hash
    assert QUBO(qubo) == quso_to_qubo(qubo_to_quso(qubo))

    # type asserting
    assert type(qubo_to_quso(qubo)) == QUSO
    assert type(qubo_to_quso(QUBO(qubo))) == QUSO
Ejemplo n.º 6
0
def test_qubo_quso_equal():

    random.seed(815)
    qubo = {(i, j): random.random() for i in range(7) for j in range(7)}
    qubo.update({(i, ): random.random() for i in range(7)})
    qubo[()] = random.random()
    for sol in itertools.product((0, 1), repeat=7):
        assert_allclose(qubo_value(sol, qubo),
                        quso_value(boolean_to_spin(sol), qubo_to_quso(qubo)))
Ejemplo n.º 7
0
    def __init__(self, Q, initial_state=None):
        """__init__.

        Parameters
        ----------
        Q : dict, ``qubovert.utils.QUBOMatrix``, or ``qubovert.QUBO`` object.
            The QUBO to simulate. This should map tuples of boolean variable
            labels to their respective coefficient in the objective function.
            For more information, see the docstrings for
            ``qubovert.utils.QUBOMatrix`` and ``qubovert.QUBO``.
        initial_state : dict (optional, defaults to None).
            The initial state to start the simulation in. ``initial_state``
            should map boolean label names to their initial values, where each
            value is either 0 or 1. If ``initial_state`` is None, then it
            will be initialized to all 0s.

        """
        model = qubo_to_quso(Q)
        if initial_state is None:
            var = (range(model.max_index +
                         1) if type(model) == QUSOMatrix else model._variables)
            initial_state = {v: 0 for v in var}
        super().__init__(model, initial_state)
Ejemplo n.º 8
0
def anneal_qubo(Q, num_anneals=1, anneal_duration=1000, initial_state=None,
                temperature_range=None, schedule='geometric',
                in_order=True, seed=None):
    """anneal_qubo.

    Run a simulated annealing algorithm to try to find the minimum of the QUBO
    given by ``Q``. ``anneal_qubo`` uses a cooling schedule with the
    ``qubovert.sim.PUBOSimulation`` object. Please see all of the parameters
    for details.

    Parameters
    ----------
    Q : dict, ``qubovert.utils.QUBOMatrix`` or ``qubovert.QUBO``.
        Maps boolean labels to their values in the objective function.
        Please see the docstring of ``qubovert.QUBO`` for more info on how to
        format ``Q``.
    num_anneals : int >= 1 (optional, defaults to 1).
        The number of times to run the simulated annealing algorithm.
    anneal_duration : int >= 1 (optional, defaults to 1000).
        The total number of updates to the simulation during the anneal.
        This is related to the amount of time we spend in the cooling schedule.
        If an explicit schedule is provided, then ``anneal_duration`` will be
        ignored.
    initial_state : dict (optional, defaults to None).
        The initial state to start the anneal in. ``initial_state`` must map
        the boolean label names to their values in {0, 1}. If ``initial_state``
        is None, then a random state will be chosen to start each anneal.
        Otherwise, ``initial_state`` will be the starting state for all of the
        anneals.
    temperature_range : tuple (optional, defaults to None).
        The temperature to start and end the anneal at.
        ``temperature = (T0, Tf)``. ``T0`` must be >= ``Tf``. To see more
        details on picking a temperature range, please see the function
        ``qubovert.sim.anneal_temperature_range``. If ``temperature_range`` is
        None, then it will by default be set to
        ``T0, Tf = qubovert.sim.anneal_temperature_range(Q, spin=False)``.
    schedule : str or iterable of tuple (optional, defaults to ``'geometric'``)
        What type of cooling schedule to use. If ``schedule == 'linear'``, then
        the cooling schedule will be a linear interpolation between the values
        in ``temperature_range``. If ``schedule == 'geometric'``, then the
        cooling schedule will be a geometric interpolation between the values
        in ``temperature_range``. Otherwise, you can supply an explicit
        schedule. In this case, ``schedule`` should be an iterable of tuples,
        where each tuple is a ``(T, n)`` pair, where ``T`` denotes the
        temperature to update the simulation, and ``n`` denote the number of
        times to update the simulation at that temperature. This schedule
        will be sent directly into the
        ``qubovert.sim.PUBOSimulation.schedule_update`` method.
    in_order : bool (optional, defaults to True).
        Whether to iterate through the variables in order or randomly
        during an update step. When ``in_order`` is False, the simulation
        is more physically realistic, but when using the simulation for
        annealing, often it is better to have ``in_order = True``.
    seed : number (optional, defaults to None).
        The number to seed Python's builtin ``random`` module with. If
        ``seed is None``, then ``random.seed`` will not be called.

    Returns
    -------
    res : qubovert.sim.AnnealResults object.
        ``res`` contains information on the final states of the simulations.
        See Examples below for an example of how to read from ``res``.
        See ``help(qubovert.sim.AnnealResults)`` for more info.

    Raises
    ------
    ValueError
        If the ``schedule`` argument provided is formatted incorrectly. See the
        Parameters section.
    ValueError
        If the initial temperature is less than the final temperature.

    Warns
    -----
    qubovert.utils.QUBOVertWarning
        If both the ``temperature_range`` and explicit ``schedule`` arguments
        are provided.

    Example
    -------
    Consider the example of finding the ground state of the 1D
    antiferromagnetic Ising chain of length 5 in boolean form.

    >>> import qubovert as qv
    >>>
    >>> H = sum(qv.spin_var(i) * qv.spin_var(i+1) for i in range(4))
    >>> Q = H.to_qubo()
    >>> anneal_res = qv.sim.anneal_qubo(Q, num_anneals=3)
    >>>
    >>> print(anneal_res.best.value)
    -4
    >>> print(anneal_res.best.state)
    {0: 0, 1: 1, 2: 0, 3: 1, 4: 0}
    >>> # now sort the results
    >>> anneal_res.sort_by_value()
    >>>
    >>> # now iterate through all of the results in the sorted order
    >>> for res in anneal_res:
    >>>     print(res.value, res.state)
    -4, {0: 0, 1: 1, 2: 0, 3: 1, 4: 0}
    -4, {0: 1, 1: 0, 2: 1, 3: 0, 4: 1}
    -4, {0: 0, 1: 1, 2: 0, 3: 1, 4: 0}

    """
    return anneal_quso(
        qubo_to_quso(Q), num_anneals, anneal_duration,
        None if initial_state is None else boolean_to_spin(initial_state),
        temperature_range, schedule, in_order, seed
    ).to_boolean()
Ejemplo n.º 9
0
def test_qubo_to_quso_eq_pubo_to_puso():

    qubo = {(0, ): 1, (0, 1): 1, (1, ): -1, (1, 2): .2, (): -2, (2, ): 1}
    assert qubo_to_quso(qubo) == pubo_to_puso(qubo)