dwavesystems / dwavebinarycsp

Map constraint satisfaction problems with binary variables to binary quadratic models.
https://docs.ocean.dwavesys.com/projects/binarycsp/en/latest
Apache License 2.0
21 stars 28 forks source link

"get_penalty_model" in stitch-function does not allow graph size larger than 8 #120

Closed annaehhh closed 1 year ago

annaehhh commented 1 year ago

Description Using dwavebinarycsp.stitch() , if a graph with more than 8 nodes is entered into the function and max_graph_size is extended to 9 an error occurs in the "generate"-function included in the "get_penalty_model"-function. This allows to only build BQMs with small number of connections between variables.

image

Steps To Reproduce Execute the Jupyter Notebook below using the sample data provided in the repository. As input enter for example: departure: Hamburg destination: Kiel https://github.com/annaehhh/Quantum-Computing/blob/main/ferrylines.ipynb

Expected Behavior Build a BQM like the method does for smaller graph sizes. (in the above code, enter departure: Hamburg, destination: Bremerhaven)

Environment

Additional Context Problem seems to be known already, as in the code of generation.py the following comment was found. (Line 178)

image
annaehhh commented 1 year ago

Using the following the solution, the maximum graph size can be extended to 32.

in generation.py modify the function as follows:

def all_possible(num_variables: int) -> np.ndarray: """Create an array of all possible spin configurations.""" assert 0 <= num_variables < 1 << 32

changed: now max graph size of 32

a = np.unpackbits(np.arange(1 << num_variables, dtype=np.uint32).reshape(1 << num_variables, 1).view(np.uint8),
                 axis=1, count=num_variables, bitorder='little').astype(np.int32)
a *= 2
a -= 1
return a

in addition, change dtype of matrix A to np.int32 (as below): A = np.empty((1 << len(graph), len(indexer)), dtype=np.int32)

Updated example on: https://github.com/annaehhh/Quantum-Computing/blob/main/ferrylines.ipynb