Example #1
0
def solve_ising_dwave(hii, Jij):
    config_file = '/media/sf_QWorld/QWorld/QA_DeNovoAsb/dwcloud.conf'
    client = Client.from_config(config_file, profile='aritra')
    solver = client.get_solver(
    )  # Available QPUs: DW_2000Q_2_1 (2038 qubits), DW_2000Q_5 (2030 qubits)
    dwsampler = DWaveSampler(config_file=config_file)

    edgelist = solver.edges
    adjdict = edgelist_to_adjacency(edgelist)
    embed = minorminer.find_embedding(Jij.keys(), edgelist)
    [h_qpu, j_qpu] = embed_ising(hii, Jij, embed, adjdict)

    response_qpt = dwsampler.sample_ising(h_qpu,
                                          j_qpu,
                                          num_reads=solver.max_num_reads())
    client.close()

    bqm = dimod.BinaryQuadraticModel.from_ising(hii, Jij)
    unembedded = unembed_sampleset(response_qpt,
                                   embed,
                                   bqm,
                                   chain_break_method=majority_vote)
    print("Maximum Sampled Configurations from D-Wave\t===>")
    solnsMaxSample = sorted(unembedded.record, key=lambda x: -x[2])
    for i in range(0, 10):
        print(solnsMaxSample[i])
    print("Minimum Energy Configurations from D-Wave\t===>")
    solnsMinEnergy = sorted(unembedded.record, key=lambda x: +x[1])
    for i in range(0, 10):
        print(solnsMinEnergy[i])
Example #2
0
n = 3
#Quadratic coefficients (ring topology)
J = {(i, (i+1)%n): np.random.choice([-1,1]) for i in range(n-1)} 
#Linear coefficients
h = {i: 0 for i in range(n)}
# Embed logical qubits on QPU's qubits
embedding = minorminer.find_embedding(J, target_edgelist)


# Check, whether an embedding was succesfull
if not embedding:
    raise ValueError("no embedding found")


#Create a complete Ising model on a QPU 
target_h, target_J = embed_ising(h, J, embedding, target_adjacency)
# print('__________________')
# print('Linear coeff on QPU:', target_h)
# print('Quadratic coeff on QPU:', target_J)
# print('__________________')

# Set parameters for calculations. num_reas is a number of experiments
runs = 4
response = sampler.sample_ising(target_h,
							target_J, 
							num_reads=runs, 
							answer_mode='histogram',
							annealing_time = 1000)
#Get a SampleSet with spins and energies (WARNING: results are for the task, embedded on QPU)
# print('Resulting spins on QPU:')
# print(response)
      "\n\nEmbedding:\n")
for key, val in embedding.items():
    print(key, ":\t", val)

# Running on the QPU

# To see exactly how often each chain is breaking in a standard run on the QPU, we will first run our Ising problem on the QPU using the basic `embed_ising` method.
# We will use the optional parameter `chain_strength` within `embed_ising`, and will set this value to $2.0$ for our comparison.

# To use this tool, we provide the graph minor embedding discovered above to create and embedded version.
# Then send this embedded ising problem over to the QPU using `sample_ising`.

from dwave.embedding import embed_ising
embedded_h, embedded_J = embed_ising(h,
                                     J,
                                     embedding,
                                     dwave_sampler.adjacency,
                                     chain_strength=2.0)
reads = 1000
fixed_response = dwave_sampler.sample_ising(embedded_h,
                                            embedded_J,
                                            num_reads=reads)
energies = fixed_response.record.energy
print("QPU call complete using",
      fixed_response.info['timing']['qpu_access_time'] / 1000000.0,
      "seconds of QPU time.\n")

# Next we will look at the frequency that chains broke in our samples.
# This will be our metric to explore the advantages of `VirtualGraphComposite`.

from dwave.embedding import chain_break_frequency