예제 #1
0
def factor_number(m, true_p, true_q, use_true_values=False):
    apply_preprocessing = True
    preprocessing_verbose = False
    optimization_verbose = False
    if m == 56153:
        p_dict, q_dict, z_dict, clauses = factor_56153()
    elif m == 291311:
        p_dict, q_dict, z_dict, clauses = factor_291311()
    elif use_true_values:
        p_dict, q_dict, z_dict, clauses = create_clauses(m, true_p, true_q, apply_preprocessing, preprocessing_verbose)
    else:
        p_dict, q_dict, z_dict, clauses = create_clauses(m, None, None, apply_preprocessing, preprocessing_verbose)

    number_of_uknowns, number_of_carry_bits = calculate_number_of_unknowns(p_dict, q_dict, z_dict)
    print("Number of unknowns:", number_of_uknowns)
    print("Number of carry bits:", number_of_carry_bits)
    if clauses[0] == 0 and len(set(clauses)) == 1:
        if number_of_uknowns == 0:
            p, q = decode_solution(p_dict, q_dict)
            return p, q, None

    optimization_engine = OptimizationEngine(clauses, m, steps=1, grid_size=5, verbose=optimization_verbose, visualize=True)
    sampling_results, mapping = optimization_engine.perform_qaoa()
    most_frequent_bit_string = max(sampling_results, key=lambda x: sampling_results[x])
    
    squared_overlap = calculate_squared_overlap(mapping, sampling_results, true_p, true_q, p_dict, q_dict)
    p_dict = update_dictionary(most_frequent_bit_string, mapping, p_dict)
    q_dict = update_dictionary(most_frequent_bit_string, mapping, q_dict)
    z_dict = update_dictionary(most_frequent_bit_string, mapping, z_dict)

    p, q = decode_solution(p_dict, q_dict)
    return p, q, squared_overlap
예제 #2
0
파일: main.py 프로젝트: yusharth/vqf
def main():
    threshold = 1e5
    primes = get_primes_lower_than_n(int(np.sqrt(threshold)))
    primes = primes[1:]

    qubits_required_no_preprocessing = []
    qubits_required_with_preprocessing = []
    initial_time = time.time()
    # file_name = "preprocessing_full_results.csv"
    # plot_name = "reprocessing_full_plot.png"
    file_name = "preprocessing_no_z2_results.csv"
    plot_name = "reprocessing_no_z2_plot.png"

    for p in primes:
        for q in primes:
            if p < q:
                continue
            m = p * q
            if m > threshold:
                continue
            start_time = time.time()
            # p_dict, q_dict, z_dict, _ = create_clauses(m, p, q, apply_preprocessing=False, verbose=False)
            # x, z = assess_number_of_unknowns(p_dict, q_dict, z_dict)

            # qubits_required_no_preprocessing.append([m, x, z])

            p_dict, q_dict, z_dict, _ = create_clauses(m, p, q, apply_preprocessing=True, verbose=False)
            x, z = assess_number_of_unknowns(p_dict, q_dict, z_dict)
            qubits_required_with_preprocessing.append([m, x, z])

            end_time = time.time()
            t = np.round(end_time - start_time, 3)
            print(p, q, m, x, z, t, "    ")#, end="\r")

        np.savetxt(file_name, np.array(qubits_required_with_preprocessing), delimiter=",", fmt='%.d', header='m,unknowns,carry_bits', comments='')

    qubits_required_no_preprocessing = np.genfromtxt('no_preprocessing', skip_header=1, delimiter=',')
    # qubits_required_with_preprocessing = np.genfromtxt('preprocessing_no_z2_results', skip_header=1, delimiter=',')
    print("Total time:", np.round((end_time - initial_time) / 60, 3), '[min]')

    data_1 = np.array(qubits_required_no_preprocessing)
    data_2 = np.array(qubits_required_with_preprocessing)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    ax.scatter(data_1[:, 0], data_1[:, 1], label="No classical preprocessing", s=10)
    ax.scatter(data_2[:, 0], data_2[:, 1], label="Classical preprocessing", s=10)
    ax.set_xlabel("Biprime to be factored")
    ax.set_ylabel("Number of qubit required")
    ax.set_xscale('log')
    plt.legend()
    plt.savefig(plot_name)
    plt.show()