예제 #1
0
 def test_lb_and_ub(self):
     bqm = BinaryQuadraticModel({
         0: 0.0,
         1: 0.0,
         2: 0.0,
         3: 0.0,
         4: 0.0
     }, {
         (0, 1): -2.0,
         (0, 2): -5.0,
         (0, 3): -2.0,
         (0, 4): -2.0,
         (1, 2): -2.0,
         (1, 3): -2.0,
         (1, 4): 4.0,
         (2, 3): -3.0,
         (2, 4): -5.0,
         (3, 4): -4.0
     }, 0, dimod.SPIN)
     sampler = ClipComposite(ExactSolver())
     solver = ExactSolver()
     response = sampler.sample(bqm, lower_bound=-1, upper_bound=1)
     response_exact = solver.sample(bqm)
     self.assertEqual(response.first.sample, response_exact.first.sample)
     self.assertAlmostEqual(response.first.energy,
                            response_exact.first.energy)
예제 #2
0
 def test_with_labels(self):
     bqm = BinaryQuadraticModel(
         {
             'a': 0.0,
             'b': 0.0,
             'c': 0.0,
             'd': 0.0,
             'e': 0.0
         }, {
             ('a', 'b'): -2.0,
             ('a', 'c'): -5.0,
             ('a', 'd'): -2.0,
             ('a', 'e'): -2.0,
             ('b', 'c'): -2.0,
             ('b', 'd'): -2.0,
             ('b', 'e'): 4.0,
             ('c', 'd'): -3.0,
             ('c', 'e'): -5.0,
             ('d', 'e'): -4.0
         }, 0, dimod.SPIN)
     sampler = ClipComposite(ExactSolver())
     solver = ExactSolver()
     response = sampler.sample(bqm, lower_bound=-1, upper_bound=1)
     response_exact = solver.sample(bqm)
     self.assertEqual(response.first.sample, response_exact.first.sample)
     self.assertAlmostEqual(response.first.energy,
                            response_exact.first.energy)
예제 #3
0
 def test_ub_only(self):
     bqm = BinaryQuadraticModel({
         0: 0.0,
         1: 0.0,
         2: 0.0,
         3: 0.0,
         4: 0.0
     }, {
         (0, 1): -2.0,
         (0, 2): -5.0,
         (0, 3): -2.0,
         (0, 4): -2.0,
         (1, 2): -2.0,
         (1, 3): -2.0,
         (1, 4): 4.0,
         (2, 3): -3.0,
         (2, 4): -5.0,
         (3, 4): -4.0
     }, 0, dimod.SPIN)
     with self.assertWarns(DeprecationWarning):
         sampler = ClipComposite(ExactSolver())
     solver = ExactSolver()
     response = sampler.sample(bqm, upper_bound=1)
     response_exact = solver.sample(bqm)
     self.assertEqual(response.first.sample,
                      response_exact.first.sample)
     self.assertAlmostEqual(response.first.energy,
                            response_exact.first.energy)
예제 #4
0
# In[ ]:


# Use a dimod test sampler that gives the BQM value for all values of its variables
from dimod import ExactSolver
sampler = ExactSolver()


# The next cell prints the BQM's values ("energy") in ascending order. Note that they are indeed lowest for valid assignments (values under "x1, x2, x3" match rows of our AND truth table) and higher for non-valid ones.

# In[ ]:


# Solve the BQM
solution = sampler.sample(and_bqm)
list(solution.data())


# Note: to understand the examples of this Jupyter Notebook, it's enough to understand that samplers such as the D-Wave system find solutions that minimize a BQM. If you want further details on that minimization (the "energy" here and "E" previously), see below under [Further Information](#Further-Information).

# # Factoring on the Quantum Computer
# This section solves a factoring problem as a CSP, following the same steps we used for the simple problem of the light circuit:

# 1. Express factoring as a CSP using Boolean logic operations.
# 2. Convert to a BQM.
# 3. Minimize the BQM.

# ## Step 1: Express Factoring as Multiplication Circuit
# We again start by expressing the problem with Boolean logic gates, in this case a multiplication circuit.
예제 #5
0
            J_a_b_dict['Z{}_{}'.format(i, j),
                       'Z{}_{}'.format(i, j_1)] = 2
        '''for i_1 in range(n):
            for k_1 in range(N):
                j_1 = -N/2+0.5 + k_1
                J_a_b_dict['Z{}_{}'.format(i, j),
                           'Z{}_{}'.format(i_1, j_1)] = theta_mass*(2*m[i]*m[i_1]) + theta_space*(2*t[i]*t[i_1]) + 2*m[i]*m[i_1]*(L/N*j-x_cg_min)*(L/N*j_1-x_cg_max) + \
                    theta_target_x*(2*m[i]*m[i_1]*(L/N*j-x_cg_t)*(L/N*j_1-x_cg_t))'''


#gamma = theta_mass*(W_p**2)+theta_space*(N**2) + W_e**2*(x_cg_e - x_cg_min)*(x_cg_e - x_cg_max) + theta_target_x*(W_e**2*(x_cg_e - x_cg_t)**2) +theta_one_to_one*(- N - n)
gamma = 0
BQM = dimod.BinaryQuadraticModel(h_a_dict, J_a_b_dict, gamma, 'BINARY')

sampler = ExactSolver()
sampled = sampler.sample(BQM)
#print(sampled)

#ising_model = BQM.to_ising()
#sampler = EmbeddingComposite(DWaveSampler())
#sampled = sampler.sample_ising(h = ising_model[0], J = ising_model[1], num_reads = 100)
print(sampled.slice(0, 10))

loaded_mass = []
loaded_coord = []
print()
for a in range(4):
    print("{} energy".format(a))
    for k in range(N):
        j = -N / 2 + 0.5 + k
        flag = False
예제 #6
0
def not_all_equal(q1, q2, q3):
    return not ((q1 == q2) and (q2 == q3))


csp = dwavebinarycsp.ConstraintSatisfactionProblem(vartype=dimod.Vartype.SPIN)
csp.add_constraint(not_all_equal, ['a', 'b', 'c'])
csp.add_constraint(not_all_equal, ['c', 'd', 'e'])

my_bqm = dwavebinarycsp.stitch(csp)

print(my_bqm)

print('Classical Solver')
sampler = ExactSolver()
response = sampler.sample(my_bqm)
for sample, energy, occurrences in response.data(
    ['sample', 'energy', 'num_occurrences']):
    print(list(sample.values()), 'Occurrences :', occurrences, 'Energy :',
          energy)

print('DWave Solver')
sampler = EmbeddingComposite(DWaveSampler())
response = sampler.sample(my_bqm, num_reads=5000)
for sample, energy, occurences in response.data(
    ['sample', 'energy', 'num_occurrences']):
    print(list(sample.values()), 'Occurrences:', occurences, 'Energy:', energy)

print('The correct answer is 18, while I said 10.')
print(
    'I got busted by the stochastic nature of the Quantum Solver, which does not give all the results, and not always the same ones.'
csp.fix_variable('B2', 0)
# the sum output of the circuit is 'S2'
csp.fix_variable('S2', 1)
# the carry output of the circuit is 'ORout'
csp.fix_variable('ORout', 1)

# convert the csp to a bqm. We specify that the energy gap between the valid configurations and
# the invalid ones must be at least 2.0
bqm = dwavebinarycsp.stitch(csp, min_classical_gap=2.0)

# set up any dimod solver. In this case we use the ExactSolver but any unstructured solver would
# work.
sampler = ExactSolver()

# we can determine the minimum and maximum number of faults that will induce this behavior
response = sampler.sample(bqm)
min_energy = min(response.data_vectors['energy'])

fault_counts = []
for sample, energy in response.data(['sample', 'energy']):
    if csp.check(sample):
        n_faults = sum(sample[v] for v in sample if 'fault' in v)
        fault_counts.append(n_faults)
    else:
        # if the CSP is not satisfied, the energy should be above ground
        assert energy > min_energy

print('Minimum number of faults: ', min(fault_counts))
print('Maximum number of faults: ', max(fault_counts))

# If, instead of the ground states corresponding to all possible fault configurations, we