API Reference¶
This page lists the main modules and classes of sparse-qubo.
Public API (top-level package): Use sparse_qubo.create_constraint_dwave for D-Wave/dimod BQMs and sparse_qubo.create_constraint_amplify for Fixstars Amplify models. Constraint and network types are sparse_qubo.ConstraintType and sparse_qubo.NetworkType. The sections below document the implementation modules.
Core modules¶
sparse_qubo.core.constraint
¶
Constraint types and QUBO construction for equality/inequality constraints.
This module provides ConstraintType, get_initial_nodes, and get_constraint_qubo to build QUBOs from switching networks for use with D-Wave or Amplify.
ConstraintType
¶
Bases: StrEnum
Type of linear constraint on binary variables (sum of variables).
Source code in src/sparse_qubo/core/constraint.py
42 43 44 45 46 47 48 49 | |
get_constraint_qubo(variables, constraint_type, network_type=NetworkType.DIVIDE_AND_CONQUER, c1=None, c2=None, threshold=None, reverse=False, var_prefix=None)
¶
Build a QUBO for the given constraint.
Auxiliary variable prefixes
When var_prefix is None (the default), a unique prefix is assigned
internally (C0, C1, ...) so that merging multiple constraint QUBOs
into one BQM avoids name collisions. The counter increments on each call
and is not reset automatically; it resets only on process start or when
calling :func:reset_constraint_prefix_counter. See the Usage section
(Constraint prefix counter) in the documentation for details.
Source code in src/sparse_qubo/core/constraint.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
get_initial_nodes(variables, constraint_type, c1=None, c2=None, exponentiation=False)
¶
Build left and right VariableNode lists for a switching network from variable names and constraint type.
Used by get_constraint_qubo and by network implementations. If exponentiation is True, the right side is padded to a power-of-2 size (for Benes, Bitonic, OddEvenMergeSort).
Source code in src/sparse_qubo/core/constraint.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
reset_constraint_prefix_counter()
¶
Reset the internal constraint prefix counter.
The prefix counter is not reset automatically. It is reset only when:
- Process start: Restarting Python (e.g. re-running a script or restarting a Jupyter kernel) reloads the module and sets the counter to 0.
- Explicit call: Calling this function sets the counter back to 0.
Call this when you want the next constraint to use prefix C0 again
(e.g. when starting to build a new model in the same process). Mainly useful
for testing or reproducible variable names.
Source code in src/sparse_qubo/core/constraint.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
sparse_qubo.core.network
¶
Switching network types and base class for network implementations.
NetworkType enumerates available formulations. ISwitchingNetwork is the abstract base for networks that produce a list of Switch elements.
ISwitchingNetwork
¶
Bases: ABC
Abstract base for switching networks that produce a list of Switch elements.
Source code in src/sparse_qubo/core/network.py
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
generate_network(left_nodes, right_nodes, threshold=None, reverse=False)
classmethod
¶
Build the switching network, simplifying switches when nodes are fixed (ALWAYS_ZERO/ALWAYS_ONE).
Source code in src/sparse_qubo/core/network.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
NetworkType
¶
Bases: StrEnum
Identifier for each switching network (or naive) formulation.
Source code in src/sparse_qubo/core/network.py
14 15 16 17 18 19 20 21 22 23 24 | |
sparse_qubo.core.node
¶
Nodes and attributes for switching network construction.
VariableNode represents a binary variable with an optional NodeAttribute (ALWAYS_ZERO, ALWAYS_ONE, NOT_CARE, or ZERO_OR_ONE).
NodeAttribute
¶
Bases: Enum
Attribute of a binary variable in the switching network.
Source code in src/sparse_qubo/core/node.py
12 13 14 15 16 17 18 | |
VariableNode
¶
Bases: BaseModel
A binary variable with a name and an attribute.
Source code in src/sparse_qubo/core/node.py
21 22 23 24 25 | |
sparse_qubo.core.switch
¶
Switch elements and QUBO conversion for switching networks.
A Switch encodes a constraint that the sum of left variables plus left_constant equals the sum of right variables plus right_constant. Switch.to_qubo converts a list of Switch elements into a single QUBO (variables, linear, quadratic, constant).
QUBO
¶
Bases: BaseModel
QUBO representation: variables, linear and quadratic coefficients, and constant.
Source code in src/sparse_qubo/core/switch.py
19 20 21 22 23 24 25 | |
Switch
¶
Bases: BaseModel
Single switch: left and right variable sets and optional integer constants.
The constraint is: sum(left_nodes) + left_constant == sum(right_nodes) + right_constant.
Source code in src/sparse_qubo/core/switch.py
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
num_edges
property
¶
Number of quadratic terms if this switch were converted to QUBO alone.
num_variables
property
¶
Number of variables in this switch (left + right).
determine_layer_structure(switches)
classmethod
¶
Determine which layer each switch belongs to. Returns {layer_number: [switch_indices]} (0-indexed).
Source code in src/sparse_qubo/core/switch.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
left_node_to_switch(switches)
classmethod
¶
Map each left-side variable name to the index of the switch that contains it.
Source code in src/sparse_qubo/core/switch.py
103 104 105 106 107 108 109 110 | |
right_node_to_switch(switches)
classmethod
¶
Map each right-side variable name to the index of the switch that contains it.
Source code in src/sparse_qubo/core/switch.py
112 113 114 115 116 117 118 119 | |
to_qubo(switches)
classmethod
¶
Convert a list of Switch elements into a single QUBO (sum of (L + c_L - R - c_R)^2 terms).
Source code in src/sparse_qubo/core/switch.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
visualize_switches(switches, output_path, layout_type='network', layer_spacing=2.0, node_spacing=1.0)
classmethod
¶
Draw the relationship between variables and switches as a graph and save to output_path.
Source code in src/sparse_qubo/core/switch.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
Network implementations¶
sparse_qubo.networks.benes_network
¶
Implementation of Benes network
BenesNetwork
¶
Bases: ClosNetworkBase
Benes network implementation; requires power-of-2 variable count.
Source code in src/sparse_qubo/networks/benes_network.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
sparse_qubo.networks.bitonic_sort_network
¶
Implementation of Bitonic sort network
BitonicSortNetwork
¶
Bases: ISwitchingNetwork
Bitonic sort network; requires power-of-2 variable count.
Source code in src/sparse_qubo/networks/bitonic_sort_network.py
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
sparse_qubo.networks.bubble_sort_network
¶
Implementation of Bubble sort network
BubbleSortNetwork
¶
Bases: ISwitchingNetwork
Bubble sort network; works for any variable count.
Source code in src/sparse_qubo/networks/bubble_sort_network.py
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 | |
sparse_qubo.networks.clique_network
¶
Implementation of clique network: single all-to-all switch between left and right variables.
CliqueNetwork
¶
Bases: ISwitchingNetwork
Single switch connecting all left variables to all right variables (clique).
Source code in src/sparse_qubo/networks/clique_network.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
sparse_qubo.networks.clos_network_base
¶
Implementation of Clos network base: three-stage switching network with configurable switch sizes.
ClosNetworkBase
¶
Bases: ISwitchingNetwork, ABC
Base class for Clos-type networks (ingress, middle, egress stages).
Source code in src/sparse_qubo/networks/clos_network_base.py
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
sparse_qubo.networks.clos_network_max_degree
¶
Implementation of Clos network with maximum degree constraint: limits the size of each switch.
AdhocNetworkWithMinimumDegree
¶
Helper for small networks when max degree is large enough (single switch).
Source code in src/sparse_qubo/networks/clos_network_max_degree.py
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 | |
ClosNetworkWithMaxDegree
¶
Bases: ClosNetworkBase
Clos network that limits the maximum degree (switch size). Call reset_max_degree before use.
Source code in src/sparse_qubo/networks/clos_network_max_degree.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
reset_max_degree(new_max)
classmethod
¶
Set the maximum allowed degree (switch size) for the network.
Source code in src/sparse_qubo/networks/clos_network_max_degree.py
63 64 65 66 67 68 69 70 | |
sparse_qubo.networks.clos_network_minimum_edge
¶
Implementation of Clos network with minimum edge count: chooses (n, r) to minimize quadratic terms.
ClosNetworkMinimumEdge
¶
Bases: ClosNetworkBase
Clos network that minimizes the number of logical edges (quadratic terms).
Source code in src/sparse_qubo/networks/clos_network_minimum_edge.py
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 60 61 62 | |
sparse_qubo.networks.divide_and_conquer_network
¶
Implementation of Divide-and-conquer network: recursive division of the constraint.
DivideAndConquerNetwork
¶
Bases: ISwitchingNetwork
Divide-and-conquer switching network; uses BubbleSort when size is small or one-hot.
Source code in src/sparse_qubo/networks/divide_and_conquer_network.py
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
sparse_qubo.networks.oddeven_merge_sort_network
¶
Implementation of Odd-even merge sort network: Batcher's algorithm.
OddEvenMergeSortNetwork
¶
Bases: ISwitchingNetwork
Odd-even merge sort (Batcher) network; requires power-of-2 variable count.
Source code in src/sparse_qubo/networks/oddeven_merge_sort_network.py
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
D-Wave integration¶
Public entry point: sparse_qubo.create_constraint_dwave. Implementation module:
sparse_qubo.dwave.constraint
¶
D-Wave/dimod integration: build dimod BQMs from constraint types and network types.
Use create_constraint_dwave (from sparse_qubo) or constraint() from this module to obtain a BinaryQuadraticModel for use with D-Wave samplers or dimod.
constraint(variables, constraint_type, network_type=NetworkType.DIVIDE_AND_CONQUER, c1=None, c2=None, threshold=None)
¶
Build a dimod BQM for the given constraint using the specified network type (or NAIVE).
Source code in src/sparse_qubo/dwave/constraint.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
naive_constraint(variables, constraint_type, c1=None, c2=None)
¶
Encode the constraint as a single linear equality/inequality (no switching network).
Source code in src/sparse_qubo/dwave/constraint.py
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 | |
Fixstars Amplify integration¶
Public entry point: sparse_qubo.create_constraint_amplify. Implementation module:
sparse_qubo.fixstars_amplify.constraint
¶
Fixstars Amplify integration: build Amplify models from constraint types and network types.
Use create_constraint_amplify (from sparse_qubo) or constraint() from this module to obtain an amplify.Model for use with Amplify solvers.
constraint(variables, constraint_type, network_type=NetworkType.DIVIDE_AND_CONQUER, c1=None, c2=None, threshold=None)
¶
Build an Amplify model for the given constraint using the specified network type (or NAIVE).
Source code in src/sparse_qubo/fixstars_amplify/constraint.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
generate_amplify_model(variables, qubo)
¶
Build an Amplify model from a list of Amplify variables and a QUBO (linear + quadratic + constant).
Source code in src/sparse_qubo/fixstars_amplify/constraint.py
48 49 50 51 52 53 54 55 56 57 58 59 | |
naive_constraint(variables, constraint_type, c1=None, c2=None)
¶
Encode the constraint using Amplify's built-in one_hot/equal_to/less_equal/etc. (no switching network).
Source code in src/sparse_qubo/fixstars_amplify/constraint.py
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 | |