def response_to_dict(response): results_dict = OrderedDict() for sample, energy in response.data(['sample', 'energy']): # Convert A and B from binary to decimal a, b = to_base_ten(sample) # Aggregate results by unique A and B values (ignoring internal circuit variables) if (a, b) not in results_dict: results_dict[(a, b)] = energy return results_dict
def response_to_dict(response): """ Function for converting the response to a dict of integer values """ results_dict = OrderedDict() for sample, energy, num_occurrences in response.data(['sample', 'energy', 'num_occurrences']): # print(sample, "Energy: ", energy, "Occurrences: ", num_occurrences) # Convert A and B from binary to decimal a, b = to_base_ten(sample) # Aggregate results by unique A and B values (ignoring internal circuit variables) if (a, b) not in results_dict: results_dict[(a, b)] = energy return results_dict
# Map back to the BQM's graph (nodes labeled "a0", "b0" etc,) response = dimod.unembed_response(response, embedding, source_bqm=bqm) print("\nThe solution in problem variables: \n", next(response.data(fields=['sample']))) # Sample(sample={'a0': 1, 'b0': 1, 'and0,1': 1, 'b1': 1, 'and0,2': 1, 'b2': 1, 'a1': 1, 'and1,0': 1, 'carry1,0': 1, 'and1,1': 1, 'carry1,1': 1, 'sum1,1': 0, 'and1,2': 1, 'a2': 0, 'and2,0': 0, 'carry2,0': 0, 'and2,1': 0, 'carry2,1': 1, 'sum2,1': 0, 'and2,2': 0, 'carry3,0': 0}) # ### Viewing the Solution # We need to convert back from binary numbers to integers. Because quantum computing is probabilistic, there is a slight chance that in many executions of this example, your execution might return an incorrect example. Rerunning the previous cell will most likely produce a correct answer. # In[ ]: from helpers.convert import to_base_ten # Select just just the first sample. sample = next(response.samples(n=1)) dict(sample) a, b = to_base_ten(sample) print("Given integer P={}, found factors a={} and b={}".format(P, a, b)) # Given integer P=21, found factors a=7 and b=3 # # Summary # This Jupyter Notebook showed how you can formulate a constraint satisfaction problem for solution on a quantum computer using Ocean software. We solved a factoring problem as an example of one proposed solution technique. # We considered two ways of formulating the factoring problem. Formulation A is intuitive and direct, but conversion of large integers to binary introduces (a) increasing weights per bit, $2^ma_m$, and (b) in the squaring of $(P-ab)$, terms of higher order that need to be reduced to quadratic. These affect performance. Formulation B, using binary gates, is a useful technique in general. The "modularity" of binary gates provides some benefits for minor-embedding: repeated small units that can be "tiled" onto the QPU's topology. # # Further Information # This section provides more information on binary multiplication, minimizing BQMs, sampling for solutions, and minor-embedding. # ## Binary Multiplication