How to use the openjij.Response function in openjij

To help you get started, we’ve selected a few openjij examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github OpenJij / OpenJij / tests / test_utils.py View on Github external
def test_str_key_success_prob(self):
        solutions = [{'a': 1, 'b': -1, 'c': -1}]
        response = oj.Response(indices=['c', 'b', 'a'], var_type=oj.SPIN)
        response.update_ising_states_energies(
            states=[[-1, -1, 1], [1, -1, -1], [1, -1, -1]], energies=[0, 0, 0])

        ps = oj.utils.success_probability(response, solutions)

        self.assertEqual(ps, 1/3)
github OpenJij / OpenJij / examples / python / sample_model.py View on Github external
else:
                    sqa_system.reset_spins(previous_state)
                _exec_time = measure_time(sqa_algorithm)(sqa_system)
                execution_time.append(_exec_time)
                # trotter_spins is transposed because it is stored as [Spin ​​space][Trotter].
                # [-1] is excluded because it is a tentative spin of s = 1 for convenience in SQA.
                q_state = self._post_process4state(
                    sqa_system.trotter_spins[:-1].T)
                q_energies.append(
                    [model.calc_energy(state,
                                       need_to_convert_from_spin=True)
                     for state in q_state])
                q_states.append(q_state.astype(np.int))

        sampling_time = exec_sampling()
        response = openjij.Response(
            var_type=model.var_type, indices=self.indices)
        response.update_quantum_ising_states_energies(q_states, q_energies)

        response.info['sampling_time'] = sampling_time * \
            10**6              # micro sec
        response.info['execution_time'] = np.mean(
            execution_time) * 10**6   # micro sec
        response.info['list_exec_times'] = np.array(
            execution_time) * 10**6  # micro sec

        return response
github OpenJij / OpenJij / openjij / utils / res_convertor.py View on Github external
def convert_response(response):

    if isinstance(response, openjij.Response):
        return response

    try:
        from dimod.response import Response
        from dimod.sampleset import SampleSet
    except ImportError:
        raise ImportError('Import dwave dimod : "pip install dimod"')
    
    if isinstance(response, Response) or isinstance(response, SampleSet):
        from dimod.vartypes import BINARY, SPIN
        var_type = 'BINARY' if response.vartype == BINARY else 'SPIN'
        o_res = openjij.Response(var_type=var_type, indices=list(response.variables))
        states = []
        energies = []
        for rec in response.record:
            for _ in range(rec[2]):
                states.append(rec[0])
                energies.append(rec[1])
        o_res.update_ising_states_energies(states=states, energies=energies)
                
        o_res.info = response.info
        if 'qpu_sampling_time' in response.info:
            o_res.info['sampling_time'] = response.info['qpu_sampling_time']
        if 'anneal_time_per_run' in response.info:
            o_res.info['execution_time'] = response.info['anneal_time_per_run']
        o_res.info['dimod'] = response
        return o_res
    else:
github OpenJij / OpenJij / openjij / sampler / sa_sampler.py View on Github external
self.schedule_info = {'schedule': 'custom schedule'}
        else:
            schedule = default_schedule(
                bhom,
                beta_min=self._schedule_setting['beta_min'],
                beta_max=self._schedule_setting['beta_max'],
                num_sweeps=self._schedule_setting['num_sweeps'])
            self.schedule_info = {
                'beta_max': schedule[-1][0],
                'beta_min': schedule[0][0],
                'num_sweeps': self._schedule_setting['num_sweeps']
            }

        init_state = init_state if init_state else np.random.choice(
            [1, -1], len(bhom.indices))
        response = openjij.Response(
            var_type=var_type, indices=bhom.indices
        )
        execution_time = []
        @measure_time
        def exec_sampling():
            for _ in range(num_reads):
                _exec_time, state = measure_time(
                    hubo_simulated_annealing)(bhom, init_state, schedule,
                                              var_type=var_type)
                execution_time.append(_exec_time)
                response.states.append(state)
                response.energies.append(bhom.calc_energy(state))

        sampling_time, _ = exec_sampling()

        response.info['sampling_time'] = sampling_time * 10**6  # micro sec
github OpenJij / OpenJij / openjij / sampler / sampler.py View on Github external
initial_state=None,
                  reinitialize_state=True, seed=None, **kwargs):
        self._sampling_kwargs_setting(**kwargs)
        self._set_model(model)

        # seed for MonteCarlo
        if seed is None:
            def sampling_algorithm(system): return algorithm(
                system, self.schedule)
        else:
            def sampling_algorithm(system): return algorithm(
                system, seed, self.schedule)

        # run algorithm
        execution_time = []
        response = openjij.Response(
            var_type=model.var_type, indices=self.indices)

        @measure_time
        def exec_sampling():
            previous_state = init_generator()
            for _ in range(self.iteration):
                if reinitialize_state:
                    system.reset_spins(init_generator())
                else:
                    system.reset_spins(previous_state)
                _exec_time = measure_time(sampling_algorithm)(system)
                execution_time.append(_exec_time)
                previous_state = cxxjij.result.get_solution(system)
                self._post_save(previous_state, system, model, response)

        sampling_time = exec_sampling()