penaltymodel icon indicating copy to clipboard operation
penaltymodel copied to clipboard

More Elegant Fail for Cases with No Quadratic Biases

Open m3ller opened this issue 6 years ago • 0 comments

Current Problem When no quadratic biases are present in a penaltymodel, return an error that is more comprehensible to the end user. (Note: end user is likely to be calling the penaltymodel from stitch(..) and may not be able to understand the error from so deep in the penaltymodel stack)

import dwavebinarycsp as dbc
csp = dbc.ConstraintSatisfactionProblem(dbc.BINARY)                                     
csp.add_constraint(lambda x: x>0, ['a'])                                                
bqm = dbc.stitch(new_csp, min_classical_gap=3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-35-25eb8a1c9af4> in <module>
----> 1 new_bqm = dbc.stitch(new_csp, min_classical_gap=3)

~/envs/ocean3/lib/python3.7/site-packages/dwavebinarycsp/compilers/stitcher.py in stitch(csp, min_classical_gap, max_graph_size)
    177             # try to use the penaltymodel ecosystem
    178             try:
--> 179                 pmodel = pm.get_penalty_model(spec)
    180             except pm.ImpossiblePenaltyModel:
    181                 # hopefully adding more variables will make it possible

~/envs/ocean3/lib/python3.7/site-packages/penaltymodel/core/interface.py in get_penalty_model(specification)
     68         # asynchronously
     69         for cache in iter_caches():
---> 70             cache(pm)
     71 
     72         return pm

~/envs/ocean3/lib/python3.7/site-packages/penaltymodel/cache/interface.py in cache_penalty_model(penalty_model, database)
     94     # load into the database
     95     with conn as cur:
---> 96         insert_penalty_model(cur, penalty_model)
     97 
     98     # close the connection

~/envs/ocean3/lib/python3.7/site-packages/penaltymodel/cache/database_manager.py in insert_penalty_model(cur, penalty_model)
    478     insert_graph(cur, nodelist, edgelist, encoded_data)
    479     insert_feasible_configurations(cur, penalty_model.feasible_configurations, encoded_data)
--> 480     insert_ising_model(cur, nodelist, edgelist, linear, quadratic, offset, encoded_data)
    481 
    482     encoded_data['decision_variables'] = json.dumps(penalty_model.decision_variables, separators=(',', ':'))

~/envs/ocean3/lib/python3.7/site-packages/penaltymodel/cache/database_manager.py in insert_ising_model(cur, nodelist, edgelist, linear, quadratic, offset, encoded_data)
    277         encoded_data['offset'] = offset
    278     if 'max_quadratic_bias' not in encoded_data:
--> 279         encoded_data['max_quadratic_bias'] = max(itervalues(quadratic))
    280     if 'min_quadratic_bias' not in encoded_data:
    281         encoded_data['min_quadratic_bias'] = min(itervalues(quadratic))

ValueError: max() arg is an empty sequence

Proposed Solution Catch the case when no quadratics are provided.

m3ller avatar Jul 23 '19 17:07 m3ller