示例#1
0
    def test_invalid(self):
        sampleset = dimod.SampleSet.from_samples([[-1,  1], [-1,  1]], energy=[1, 1], vartype='SPIN') 

        # incorrect length
        with self.assertRaises(ValueError):
            sampleset = dimod.append_data_vectors(sampleset, new=[1, 2, 3])

        # appending vector with field name that already exists
        with self.assertRaises(ValueError):
            sampleset = dimod.append_data_vectors(sampleset, energy=[1, 2])

        # invalid type
        with self.assertRaises(ValueError):
            sampleset = dimod.append_data_vectors(sampleset, sets=[{0}, {1}])
示例#2
0
    def test_scalar(self):
        sampleset = dimod.SampleSet.from_samples([[-1,  1], [-1,  1]], energy=[1, 1], vartype='SPIN') 

        vect = [2] * len(sampleset.record.energy)
        sampleset = dimod.append_data_vectors(sampleset, new=vect)

        for s in sampleset.data_vectors['new']:
            self.assertEqual(s, 2)
示例#3
0
    def test_parse_ouput1(self):
        file1 = os.path.join(root_dir, "data", "small.csv")
        costs, weights, capacity = parse_inputs(file1, 10)
        samples = np.asarray([[1., 0., 1., 0., 0., 1., 0.],
                              [0., 0., 0., 0., 0., 1., 0.],
                              [0., 1., 0., 0., 0., 1., 0.],
                              [0., 0., 1., 0., 0., 1., 0.]])
        sampleset = SampleSet.from_samples(samples, 'BINARY',
                                           [-10, -11, -8, -9])
        sampleset_infeasible = append_data_vectors(sampleset,
                                                   is_feasible=[False] * 4)

        # Verify error for infeasible constraint input
        with self.assertRaises(ValueError):
            s = redirect_output(sampleset_infeasible, costs, weights)

        sampleset_feasible = append_data_vectors(
            sampleset, is_feasible=[False, True, False, True])
        s = redirect_output(sampleset_feasible, costs, weights)
        self.assertIn('Found best solution at energy -11', s)
        self.assertIn('Selected item numbers (0-indexed): [5]', s)
        self.assertIn('Selected item weights: [10]', s)
        self.assertIn('Selected item costs: [80]', s)
示例#4
0
    def test_array(self):
        sampleset = dimod.SampleSet.from_samples([[-1,  1], [-1,  1]], energy=[1, 1], vartype='SPIN') 

        vect0 = [np.array([0, 1, 2]), np.array([1, 2, 3])]
        vect1 = [[0, 1, 2], [1, 2, 3]]
        vect2 = [(0, 1, 2), (1, 2, 3)]

        sampleset = dimod.append_data_vectors(sampleset, arrs=vect0, lists=vect1, tups=vect2)
        
        a = sampleset.data_vectors['arrs']
        l = sampleset.data_vectors['lists']
        t = sampleset.data_vectors['tups']

        np.testing.assert_array_equal(a, vect0)
        np.testing.assert_array_equal(l, vect0)
        np.testing.assert_array_equal(t, vect0)
示例#5
0
    def sample(self, bqm, anneal_schedules=None, **parameters):
        """Sample the binary quadratic model using reverse annealing along a given set 
        of anneal schedules.

        Args:
            bqm (:obj:`dimod.BinaryQuadraticModel`):
                Binary quadratic model to be sampled from.

            anneal_schedules (list of lists, optional, default=[[[0, 1], [1, 0.35], [9, 0.35], [10, 1]]]): 
                Anneal schedules in order of submission. Each schedule is formatted 
                as a list of [time, s] pairs, in which time is in microseconds and s 
                is the normalized persistent current in the range [0,1].

            initial_state (dict, optional): 
                The state to reverse anneal from. If not provided, it will
                be randomly generated.

            **parameters:
                Parameters for the sampling method, specified by the child sampler.

        Returns:
            :obj:`dimod.SampleSet` that has initial_state and schedule_index fields.

        Examples:
           This example runs 100 reverse anneals each for three schedules on a problem
           constructed by setting random :math:`\pm 1` values on a clique (complete
           graph) of 15 nodes, minor-embedded on a D-Wave system using the
           :class:`.DWaveCliqueSampler` sampler.

           >>> import dimod
           >>> from dwave.system import DWaveCliqueSampler, ReverseAdvanceComposite
           ...
           >>> sampler = DWaveCliqueSampler()         # doctest: +SKIP
           >>> sampler_reverse = ReverseAdvanceComposite(sampler)    # doctest: +SKIP
           >>> schedule = [[[0.0, 1.0], [t, 0.5], [20, 1.0]] for t in (5, 10, 15)]
           ...
           >>> bqm = dimod.generators.ran_r(1, 15)
           >>> init_samples = {i: -1 for i in range(15)}
           >>> sampleset = sampler_reverse.sample(bqm,
           ...                                    anneal_schedules=schedule,
           ...                                    initial_state=init_samples,
           ...                                    num_reads=100,
           ...                                    reinitialize_state=True)  # doctest: +SKIP


        """
        child = self.child

        if anneal_schedules is None:
            anneal_schedules = [[[0, 1], [1, 0.35], [9, 0.35], [10, 1]]]    

        vartype_values = list(bqm.vartype.value)
        if 'initial_state' not in parameters:
            initial_state = dict(zip(list(bqm.variables), np.random.choice(vartype_values, len(bqm))))
        else:
            initial_state = parameters.pop('initial_state')

        if not isinstance(initial_state, abc.Mapping):
            raise TypeError("initial state provided must be a dict, but received {}".format(initial_state))

        if 'reinitialize_state' not in parameters:
            parameters['reinitialize_state'] = True

            if "answer_mode" in child.parameters:
                parameters['answer_mode'] = 'histogram'

        samplesets = None
        for schedule_idx, anneal_schedule in enumerate(anneal_schedules):
            sampleset = child.sample(bqm, anneal_schedule=anneal_schedule, initial_state=initial_state,
                                     **parameters)

            initial_state, _ = dimod.as_samples(initial_state)

            if 'initial_state' not in sampleset.record.dtype.names:
                init_state_vect = []

                if parameters['reinitialize_state']:
                    init_state_vect = [initial_state[0].copy() for i in range(len(sampleset.record.energy))]
                else:
                    # each sample is the next sample's initial state
                    init_state_vect.append(initial_state[0].copy())
                    for sample in sampleset.record.sample[:-1]:
                        init_state_vect.append(sample)

                sampleset = dimod.append_data_vectors(sampleset, initial_state=init_state_vect)
        
            if 'schedule_index' not in sampleset.record.dtype.names:
                schedule_index_vect = [schedule_idx] * len(sampleset.record.energy)
                sampleset = dimod.append_data_vectors(sampleset, schedule_index=schedule_index_vect)

            if samplesets is None:
                samplesets = sampleset
            else:
                samplesets = dimod.concatenate((samplesets, sampleset))

            if schedule_idx+1 == len(anneal_schedules):
                # no need to create the next initial state - last iteration
                break

            # prepare the initial state for the next iteration
            if parameters['reinitialize_state']:
                # if reinitialize is on, choose the lowest energy, most probable state for next iteration
                ground_state_energy = sampleset.first.energy
                lowest_energy_samples = sampleset.record[sampleset.record.energy == ground_state_energy]
                lowest_energy_samples.sort(order='num_occurrences')
                initial_state = dict(zip(sampleset.variables, lowest_energy_samples[-1].sample))
            else:
                # if not reinitialized, take the last state as the next initial state
                initial_state = dict(zip(sampleset.variables, sampleset.record.sample[-1]))

        samplesets.info['anneal_schedules'] = anneal_schedules
        return samplesets
示例#6
0
    def sample(self, bqm, initial_states=None, initial_states_generator='random', num_reads=None, 
               seed=None, **parameters):
        """Sample the binary quadratic model using reverse annealing from multiple initial states.

        Args:
            bqm (:obj:`dimod.BinaryQuadraticModel`):
                Binary quadratic model to be sampled from.

            initial_states (samples-like, optional, default=None):
                One or more samples, each defining an initial state for all the problem variables. 
                If fewer than `num_reads` initial states are defined, additional values are 
                generated as specified by `initial_states_generator`. See :func:`dimod.as_samples` 
                for a description of "samples-like".

            initial_states_generator ({'none', 'tile', 'random'}, optional, default='random'):
                Defines the expansion of `initial_states` if fewer than
                `num_reads` are specified:

                * "none":
                    If the number of initial states specified is smaller than
                    `num_reads`, raises ValueError.

                * "tile":
                    Reuses the specified initial states if fewer than `num_reads`
                    or truncates if greater.

                * "random":
                    Expands the specified initial states with randomly generated
                    states if fewer than `num_reads` or truncates if greater.

            num_reads (int, optional, default=len(initial_states) or 1):
                Equivalent to number of desired initial states. If greater than the number of 
                provided initial states, additional states will be generated. If not provided, 
                it is selected to match the length of `initial_states`. If `initial_states` 
                is not provided, `num_reads` defaults to 1.

            seed (int (32-bit unsigned integer), optional):
                Seed to use for the PRNG. Specifying a particular seed with a
                constant set of parameters produces identical results. If not
                provided, a random seed is chosen.

            **parameters:
                Parameters for the sampling method, specified by the child sampler.

        Returns:
            :obj:`dimod.SampleSet` that has initial_state field.

        Examples:
           This example runs 100 reverse anneals each from two initial states on a problem
           constructed by setting random :math:`\pm 1` values on a clique (complete
           graph) of 15 nodes, minor-embedded on a D-Wave system using the
           :class:`.DWaveCliqueSampler` sampler.

           >>> import dimod
           >>> from dwave.system import DWaveCliqueSampler, ReverseBatchStatesComposite
           ...
           >>> sampler = DWaveCliqueSampler()       # doctest: +SKIP
           >>> sampler_reverse = ReverseBatchStatesComposite(sampler)   # doctest: +SKIP
           >>> schedule = [[0.0, 1.0], [10.0, 0.5], [20, 1.0]]
           ...
           >>> bqm = dimod.generators.ran_r(1, 15)
           >>> init_samples = [{i: -1 for i in range(15)}, {i: 1 for i in range(15)}]
           >>> sampleset = sampler_reverse.sample(bqm,
           ...                                    anneal_schedule=schedule,
           ...                                    initial_states=init_samples,
           ...                                    num_reads=100,
           ...                                    reinitialize_state=True)  # doctest: +SKIP


        """
        child = self.child
        
        parsed = self.parse_initial_states(bqm, 
                                           initial_states=initial_states,
                                           initial_states_generator=initial_states_generator,
                                           num_reads=num_reads,
                                           seed=seed)
        
        parsed_initial_states = np.ascontiguousarray(parsed.initial_states.record.sample)

        # there is gonna be way too much data generated - better to histogram them if possible
        if 'answer_mode' in child.parameters:
            parameters['answer_mode'] = 'histogram'

        # reduce number of network calls if possible by aggregating init states
        if 'num_reads' in child.parameters:
            aggreg = parsed.initial_states.aggregate()
            parsed_initial_states = np.ascontiguousarray(aggreg.record.sample)
            parameters['num_reads'] = aggreg.record.num_occurrences

        samplesets = None
        
        for initial_state in parsed_initial_states:
            sampleset = child.sample(bqm, initial_state=dict(zip(bqm.variables, initial_state)), **parameters)

            if 'initial_state' not in sampleset.record.dtype.names:
                init_state_vect = [initial_state.copy() for i in range(len(sampleset.record.energy))]
                sampleset = dimod.append_data_vectors(sampleset, initial_state=init_state_vect)

            if samplesets is None:
                samplesets = sampleset
            else:
                samplesets = dimod.concatenate((samplesets, sampleset))

        return samplesets