Пример #1
0
    def test_individual_no_pairs(self):
        edges_problem = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 3)]
        q = qubo.get_Q(edges_problem)

        state_triangle = [(0, 1), (1, 2), (2, 0)]
        energy_state_triangle = qubo.calculate_energy_for_state(q, state_triangle)

        state_triangle_and_pair = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 3)]
        energy_state_triangle_and_pair = qubo.calculate_energy_for_state(q, state_triangle_and_pair)

        assert energy_state_triangle_and_pair > energy_state_triangle
        assert energy_state_triangle == -3
        assert np.isclose(energy_state_triangle_and_pair, -5 + 2.01)
Пример #2
0
def solve(input_graph: list, params: dict) -> pd.DataFrame:
    """Find the frequency of valid solutions for a given input graph with
    specified parameters for the solver algorithm.
    When the a solution is present, also outputs the edges defining the
    solution(s).

    :param input_graph: graph defining the problem to be solved
    :param params: parameters to be used by the quantum solver

    :return: dataframe containing the frequency of solution, and the edges
    defining the solution (when a solution is present)

    """
    t0 = time.time()
    Q = get_Q(input_graph)
    t1 = time.time()
    time_qubo = t1 - t0
    print(f"Time to get Q: {time_qubo:.2f} s")

    t2 = time.time()
    response = _get_dwave_response(
        Q,
        params['num_reads'],
        params['anneal_time'],
        params['pause_duration'],
        params['pause_start'],
        params['seed_embedding'],
    )
    t3 = time.time()
    time_dwave_response = t3 - t2
    print(f"D-Wave time (including finding embedding): "
          f"{time_dwave_response:.2f} s")
    states_df = _extract_states_and_counts(response)
    states_df['relative_frequency'] = states_df['absolute_frequency'] \
        / params['num_reads']

    enriched_states_df, solution_frequency, edges_solution = \
        get_valid_solutions(states_df, input_graph)
    print(f'the frequency is {solution_frequency:.2%}')
    if 0 < solution_frequency < 1:
        runs_to_solution = np.log(1 - 0.99) / np.log(1 - solution_frequency)
    elif solution_frequency == 1:
        runs_to_solution = 0
    else:
        runs_to_solution = None
    t4 = time.time()
    time_overall_computation = t4 - t0
    print(f"Time to solve overall: {time_overall_computation:.2f} s")
    data = [[
        params['tag'], params['n_cycles'], params['cycle_length'],
        params['n_vertices'], params['p_noise'], params['n_edges_noise'],
        params['seed_input_graph'], params['seed_embedding'],
        params['num_reads'], params['anneal_time'], params['pause_duration'],
        params['pause_start'], time_qubo, time_dwave_response,
        time_overall_computation, solution_frequency, runs_to_solution,
        input_graph, edges_solution,
        enriched_states_df.to_json(orient='records'),
        json.dumps(response.info['embedding_context'])
    ]]

    return data