-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdwc4.py
More file actions
59 lines (50 loc) · 2.07 KB
/
dwc4.py
File metadata and controls
59 lines (50 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# BusBar500kV, 09-Feb-2019
# D-Wave Challenge 4
import dwavebinarycsp
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite
import dimod
shots=100
# Solving as a QUBO Problem method 1 using converting to BQM
j01=1
h0=-0.2
h1=-0.2
linear = {1: h0, 2: h1}
quadratic = {(1, 2): j01}
bqm = dimod.BinaryQuadraticModel(linear, quadratic, 0.0, dimod.BINARY)
sampler = EmbeddingComposite(DWaveSampler())
response = sampler.sample(bqm, num_reads=shots)
print('\n************ QUBO - Results - Method 1 using converting to BQM **************')
for res in response.data(['sample', 'energy', 'num_occurrences']):
print('|s1 = %s |s2 = %s | Energy = %f | Probability = %f %% ' % (res.sample[1],res.sample[2],
res.energy, res.num_occurrences*100/shots))
# Solving as a QUBO Problem - Method 2 using automatic embedding
j01=1
h0=-0.2
h1=-0.2
linear = {('s0','s0'): h0, ('s1','s1'): h1}
quadratic = {('s0','s1'): j01}
Q=dict(linear)
Q.update(quadratic)
response = EmbeddingComposite(DWaveSampler()).sample_qubo(Q,num_reads=shots)
print('\n************ QUBO - Results - Method 2 using automatic embedding ************** \n')
for res in response.data(['sample', 'energy', 'num_occurrences']):
print('|s1 = %s |s2 = %s | Energy = %f | Probability = %f %% ' % (res.sample['s0'],res.sample['s1'],
res.energy, res.num_occurrences*100/shots))
# Solving as a QUBO Problem - Method 3 using manual embedding
j01=1
h0=-0.2
h1=-0.2
q1=1
q2=0
biases = {(q1,q1): h0, (q2,q2): h1}
coupler_strengths = {(q1,q2): j01}
Q=dict(biases)
Q.update(coupler_strengths)
sampler = DWaveSampler()
response = sampler.sample_qubo(Q, num_reads=shots)
print('\n************ QUBO - Results - Method 3 using manual embedding ************** \n')
print('Unit cell status :',DWaveSampler().nodelist[0:8])
for res in response.data(['sample', 'energy', 'num_occurrences']):
print('|s1 = %s |s2 = %s | Energy = %f | Probability = %f %% ' % (res.sample[q1],res.sample[q2],
res.energy, res.num_occurrences*100/shots))