dwavesystems / dwave-system

An API for easily incorporating the D-Wave system as a sampler, either directly or through Leap's cloud-based hybrid samplers
https://docs.ocean.dwavesys.com/
Apache License 2.0
87 stars 62 forks source link

CQM fails with AttributeError: 'ConstrainedQuadraticModel' object has no attribute 'quadratic' #463

Open yurivict opened 2 years ago

yurivict commented 2 years ago

Description This program:

import dimod
import dwave.inspector
import dwave.system

cqm = dimod.generators.random_knapsack(20)
print("cqm="+str(cqm))

sampler = dwave.system.EmbeddingComposite(dwave.system.DWaveSampler())
print("sampler="+str(sampler))

sampleset = sampler.sample(cqm, num_reads=100)
dwave.inspector.show(sampleset)

fails:

$ python3.9 dwave-knapsak.py 
cqm=Constrained quadratic model: 20 variables, 1 constraints, 40 biases

Objective
  -24*Binary('x_0') - 22*Binary('x_1') - 27*Binary('x_2') - 26*Binary('x_3') - 25*Binary('x_4') - 27*Binary('x_5') - 11*Binary('x_6') - 25*Binary('x_7') - 13*Binary('x_8') - 26*Binary('x_9') - 25*Binary('x_10') - 26*Binary('x_11') - 11*Binary('x_12') - 12*Binary('x_13') - 14*Binary('x_14') - 24*Binary('x_15') - 15*Binary('x_16') - 19*Binary('x_17') - 25*Binary('x_18') - 22*Binary('x_19')

Constraints
  capacity: -181 + 18*Binary('x_0') + 23*Binary('x_1') + 16*Binary('x_2') + 10*Binary('x_3') + 21*Binary('x_4') + 11*Binary('x_5') + 17*Binary('x_6') + 23*Binary('x_7') + 19*Binary('x_8') + 10*Binary('x_9') + 26*Binary('x_10') + 12*Binary('x_11') + 14*Binary('x_12') + 12*Binary('x_13') + 28*Binary('x_14') + 16*Binary('x_15') + 17*Binary('x_16') + 26*Binary('x_17') + 27*Binary('x_18') + 17*Binary('x_19') <= 0

Bounds

sampler=<dwave.system.composites.embedding.EmbeddingComposite object at 0x82bf44a30>
Traceback (most recent call last):
  File "/usr/home/yuri/quantum-computing/dwave/dwave-knapsak.py", line 11, in <module>
    sampleset = sampler.sample(cqm, num_reads=100)
  File "/home/yuri/.local/lib/python3.9/site-packages/dwave/system/composites/embedding.py", line 221, in sample
    source_edgelist = list(bqm.quadratic) + [(v, v) for v in bqm.linear]
AttributeError: 'ConstrainedQuadraticModel' object has no attribute 'quadratic'

Since sampler.sample didn't complain about type mismatch I assume that it does accept CQM and this is a bug triggered by CQM only having linear terms in it, and not having quadratic terms.

To Reproduce n/a

Expected behavior n/a

Environment:

py39-dwave-cloud-client-0.10.1 DWave: REST interface to communicate with D-Wave Solver API servers
py39-dwave-greedy-0.2.5        DWave: Greedy binary quadratic model solvers
py39-dwave-hybrid-0.6.8        DWave: Hybrid asynchronous decomposition sampler prototype framework
py39-dwave-inspector-0.3.0     DWave: Problem Inspector
py39-dwave-neal-0.6.0.d0       DWave: Simulated annealing sampler for general Ising model graphs
py39-dwave-networkx-0.8.12     DWave: Extension of the NetworkX Python package for graphs
py39-dwave-ocean-sdk-5.3.0     DWave: Meta-package for D-Wave's Ocean tools
py39-dwave-preprocessing-0.4.0_1 DWave: Preprocessing tools to aid in solving binary quadratic models
py39-dwave-samplers-1.0.0.d1   DWave: Classical algorithms for solving binary quadratic models
py39-dwave-system-1.15.0       DWave: API for incorporating the D-Wave system as a sampler
py39-dwave-tabu-0.4.5          DWave: Tabu solver for QUBO/Ising problems
py39-dwavebinarycsp-0.2.0      DWave: Map CSP with binary variables to binary quadratic models
arcondello commented 2 years ago

The EmbeddingComposite and the DWaveSampler only accept BQMs, not CQMs or DQMs. If you want to solve CQMs you should use the LeapHybridCQMSampler.

We should probably raise a better error message, I agree.