Example #1
0
    def check_generated_ising_model(self, feasible_configurations, decision_variables,
                                    linear, quadratic, ground_energy, infeasible_gap):
        """Check that the given Ising model has the correct energy levels"""
        if not feasible_configurations:
            return

        from dimod import ExactSolver

        response = ExactSolver().sample_ising(linear, quadratic)

        # samples are returned in order of energy
        sample, ground = next(iter(response.data(['sample', 'energy'])))
        gap = float('inf')

        self.assertIn(tuple(sample[v] for v in decision_variables), feasible_configurations)

        seen_configs = set()

        for sample, energy in response.data(['sample', 'energy']):
            config = tuple(sample[v] for v in decision_variables)

            # we want the minimum energy for each config of the decisison variables,
            # so once we've seen it once we can skip
            if config in seen_configs:
                continue

            if config in feasible_configurations:
                self.assertAlmostEqual(energy, ground)
                seen_configs.add(config)
            else:
                gap = min(gap, energy - ground)

        self.assertAlmostEqual(ground_energy, ground)
        self.assertAlmostEqual(gap, infeasible_gap)
    def test_simple_schedule_more_machines(self):
        jobs = {"j0": [(0, 1)], "j1": [(1, 1)], "j2": [(2, 1)]}
        max_time = 3

        # Get JSS BQM
        scheduler = JobShopScheduler(jobs, max_time)
        bqm = scheduler.get_bqm()

        # Expected solution
        expected = {"j0_0,0": 1, "j1_0,0": 1, "j2_0,0": 1}
        fill_with_zeros(expected, jobs, max_time)
        expected_energy = get_energy(expected, bqm)

        # Sampled solution
        # response = EmbeddingComposite(DWaveSampler()).sample(bqm, num_reads=10000)
        # response_sample, sample_energy, _, chain_break_fraction = next(response.data())
        # print("Chain Break Fraction: ", chain_break_fraction)
        # response = SimulatedAnnealingSampler().sample(bqm, num_reads=2000, beta_range=[0.01, 10])
        response = ExactSolver().sample(bqm)
        response_sample, sample_energy, _ = next(response.data())

        # Print response
        self.assertTrue(scheduler.csp.check(response_sample))
        self.assertEqual(expected_energy, sample_energy)
        self.compare(response_sample, expected)
from pyqubo import Binary, Constraint, Placeholder
from dimod import ExactSolver

# Flagpole problem from Denny's slide 2
# On slide 3, he uses an auxiliary variable p = xy
# what other kind of variables are there?
p, x, y, z = Binary("p"), Binary("x"), Binary("y"), Binary("z")

lamda = Placeholder("lamda")
H = (p * z) + (lamda * Constraint((3 * p) + (x * y) - (2 * p * (x + y)), label="subst"))

# Can I find the minimum valid energy?
# Can I find the maximum valid energy?
# and the minimum invalid energy?
# what do they require?
model = H.compile()
bqm = model.to_dimod_bqm(feed_dict={"lamda":1.4})
exact_response = ExactSolver().sample(bqm)
for smpl, energy in exact_response.data(['sample', 'energy']):
    sol, broken, eny = model.decode_solution(smpl, vartype='BINARY', feed_dict={"lamda":0.7})
    if not broken:
        print(" Good ", sol, eny)
    if broken:
        print(" Bad ", sol, eny)