Initialization#
from juniqutils import save_ising_file, load_ising_file
import json
import dimod
import dwave
from dwave.system import DWaveSampler,EmbeddingComposite
dwave.system.__version__, dimod.__version__
('1.32.0', '0.12.20')
Verification#
TSP_DATA = json.load(open('tsp_data.json'))
CITIES = TSP_DATA['CITIES']
Exact solver#
problem = load_ising_file('../problems/001.ising')
bqm = dimod.BQM.from_ising(problem['hs'], problem['Js'], problem['OFFSET'])
result = dimod.ExactSolver().sample(bqm).to_pandas_dataframe().sort_values('energy').reset_index(drop=True)
result
| x0,0 | x0,1 | x0,2 | x0,3 | x1,0 | x1,1 | x1,2 | x1,3 | x2,0 | x2,1 | x2,2 | x2,3 | x3,0 | x3,1 | x3,2 | x3,3 | energy | num_occurrences | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | -1 | -1 | 1 | -1 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | -1 | 1 | -1 | -1 | 0.5922 | 1 |
| 1 | -1 | -1 | 1 | -1 | 1 | -1 | -1 | -1 | -1 | 1 | -1 | -1 | -1 | -1 | -1 | 1 | 0.5922 | 1 |
| 2 | -1 | 1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | -1 | -1 | -1 | -1 | -1 | 1 | -1 | 0.5922 | 1 |
| 3 | -1 | 1 | -1 | -1 | -1 | -1 | -1 | 1 | -1 | -1 | 1 | -1 | 1 | -1 | -1 | -1 | 0.5922 | 1 |
| 4 | 1 | -1 | -1 | -1 | -1 | -1 | 1 | -1 | -1 | -1 | -1 | 1 | -1 | 1 | -1 | -1 | 0.5922 | 1 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 65531 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | 1 | 68.9884 | 1 |
| 65532 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | 1 | 1 | 1 | 68.9884 | 1 |
| 65533 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | 1 | 1 | 68.9884 | 1 |
| 65534 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | -1 | 68.9884 | 1 |
| 65535 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 79.7472 | 1 |
65536 rows × 18 columns
n = problem['METADATA']['n']
tourmatrix = result.loc[0][:n**2].to_numpy().reshape((n,n))
tourmatrix
array([[-1., -1., 1., -1.],
[ 1., -1., -1., -1.],
[-1., -1., -1., 1.],
[-1., 1., -1., -1.]])
for t in range(n):
for i in range(n):
if tourmatrix[i,t] == 1:
print(f'At time {t}, we are in city {i} ({CITIES[i]})')
At time 0, we are in city 1 (Madrid)
At time 1, we are in city 3 (Paris)
At time 2, we are in city 0 (Berlin)
At time 3, we are in city 2 (Rome)
print(result.loc[0].energy / problem['METADATA']['scale'])
print(problem['METADATA']['optimal_cost'])
59.21999999999896
59.22
QPU#
sampler = DWaveSampler(profile='defaults')
solver = EmbeddingComposite(sampler)
print(f'Selected QPU {sampler.properties["chip_id"]} with {len(sampler.nodelist)} qubits')
Selected QPU Advantage_system4.1 with 5627 qubits
problem = load_ising_file('../problems/002.ising')
bqm = dimod.BQM.from_ising(problem['hs'], problem['Js'], problem['OFFSET'])
result = solver.sample(bqm,num_reads=1000).to_pandas_dataframe()
result
| x0,0 | x0,1 | x0,2 | x0,3 | x0,4 | x1,0 | x1,1 | x1,2 | x1,3 | x1,4 | ... | x3,3 | x3,4 | x4,0 | x4,1 | x4,2 | x4,3 | x4,4 | chain_break_fraction | energy | num_occurrences | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | -1 | -1 | -1 | -1 | 1 | -1 | 1 | -1 | -1 | -1 | ... | -1 | -1 | -1 | -1 | -1 | 1 | -1 | 0.0 | 0.6232 | 3 |
| 1 | -1 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | -1 | ... | -1 | -1 | 1 | -1 | -1 | -1 | -1 | 0.0 | 0.6232 | 17 |
| 2 | -1 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | ... | -1 | -1 | -1 | -1 | 1 | -1 | -1 | 0.0 | 0.6232 | 9 |
| 3 | -1 | -1 | -1 | -1 | 1 | -1 | -1 | 1 | -1 | -1 | ... | 1 | -1 | 1 | -1 | -1 | -1 | -1 | 0.0 | 0.6232 | 14 |
| 4 | -1 | -1 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | ... | 1 | -1 | -1 | 1 | -1 | -1 | -1 | 0.0 | 0.6232 | 8 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 396 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | ... | 1 | -1 | -1 | 1 | -1 | -1 | 1 | 0.0 | 5.2155 | 1 |
| 397 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | ... | 1 | -1 | 1 | -1 | -1 | -1 | 1 | 0.0 | 5.2164 | 1 |
| 398 | -1 | -1 | -1 | 1 | -1 | -1 | -1 | -1 | 1 | -1 | ... | -1 | -1 | 1 | -1 | 1 | -1 | -1 | 0.0 | 5.2710 | 1 |
| 399 | 1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | 1 | 1 | ... | -1 | -1 | -1 | 1 | 1 | -1 | -1 | 0.0 | 5.3011 | 1 |
| 400 | -1 | -1 | -1 | -1 | 1 | 1 | -1 | 1 | -1 | -1 | ... | -1 | -1 | -1 | 1 | -1 | 1 | -1 | 0.0 | 5.6011 | 1 |
401 rows × 28 columns
# the states with energy 0.6232 are the ground sate, corresponding to the optimal cost
problem['METADATA']['optimal_cost']
62.32