Models
The models module contains the QBMBase abstract base class, which holds the visible and hidden units, weights, biases, and random number generator, and the BQRBM class which implements the bound-based quantum restricted Boltzmann machine on top of it.
A BQRBM is trained with the train() method and sampled from with the sample() method, using either the simulation or the annealer backend depending on how it was instantiated.
See the Getting Started page for a complete example, and the Annealer guide for setting up the annealer backend.
QBMBase
Bases: ABC
Abstract base class for Quantum Boltzmann Machines
Theory based on Quantum Boltzmann Machine by Amin et al. https://journals.aps.org/prx/abstract/10.1103/PhysRevX.8.021050
Source code in src/qbm/models/QBMBase.py
__init__(V_train, n_hidden, seed)
Initializes the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V_train
|
ndarray
|
Training data. |
required |
n_hidden
|
int
|
Number of hidden units. |
required |
seed
|
int | None
|
Seed for the random number generator. |
required |
Source code in src/qbm/models/QBMBase.py
BQRBM
Bases: QBMBase
Bound-based Quantum Restricted Boltzmann Machine
Source code in src/qbm/models/BQRBM.py
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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
J
property
Ising J values. Correspond to w_ij = -β * B_freeze * J_ij
h
property
Ising h values. Correspond to b_i = -β * B_freeze * h_i
__init__(V_train, n_hidden, A_freeze, B_freeze, beta_initial=1.0, beta_range=[0.1, 10], annealer_params=None, simulation_params=None, seed=0)
Initializes the model.
Note
Exactly one of annealer_params or simulation_params must be provided. Whichever is provided determines whether the samples are generated by the annealer or the simulation, respectively.
The simulation works by exact computation of ρ(s, T) = e^{-β * H(s)} / Z. H(s) is determined by A_freeze and B_freeze.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V_train
|
ndarray
|
Training data set of visible vectors, shape (n_samples, n_visible). Values must be in {+1, -1}, or in {0, 1} which are converted to {+1, -1}. |
required |
n_hidden
|
int
|
Number of hidden units. |
required |
A_freeze
|
float
|
Value of A(s) at the freeze out point. Used for Γ = β * A_freeze. Units in GHz. |
required |
B_freeze
|
float
|
Value of B(s) at the freeze out point. Used for b_i = -β * B_freeze * h_i and w_ij = -β * B_freeze * J_ij. Units in GHz. |
required |
beta_initial
|
float
|
Initial value of the effective β. Units in 1/GHz. |
1.0
|
beta_range
|
Sequence[float | int]
|
Range of allowed β values, used for making sure β is not updated to an infeasible value (e.g. negative). |
[0.1, 10]
|
annealer_params
|
AnnealerParams | None
|
Dictionary with keys: - "schedule": List of (t, s) tuples defining the anneal schedule. - "embedding": Dict mapping the logical to physical qubits. - "relative_chain_strength" [optional]: Relative chain strength value. - "qpu_params" [optional]: Parameters dict to unpack to DWaveSampler(), e.g. {"region": "na-west-1", "solver": "Advantage_system4.1"} |
None
|
simulation_params
|
Mapping[str, Any] | None
|
Dictionary with keys: - "beta": Effective β that the simulation generates samples at. - "h_range" [optional]: Allowed range of h values, defaults to (-inf, inf). - "J_range" [optional]: Allowed range of J values, defaults to (-inf, inf). |
None
|
seed
|
int | None
|
Seed for the random number generator. Used for random minibatches, as well as the exact sampler. |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither or both of annealer_params and simulation_params are provided, if a required key is missing from the provided params dict, if beta_initial is not positive, if beta_range does not satisfy 0 < min < max, or if the training data values are not in {-1, +1}. |
Source code in src/qbm/models/BQRBM.py
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 | |
load(file_path, initialize_annealer=True)
staticmethod
Loads the BQRBM model at file_path. Necessary because of pickling issues with the qpu and sampler objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the model to load. Must be a Path object or a string with ".pkl" file extension. |
required |
initialize_annealer
|
bool
|
If True and has attribute self.annealer_params, then will call self._initialize_annealer(). |
True
|
Returns:
| Type | Description |
|---|---|
BQRBM
|
BQRBM instance loaded from the file path. |
Source code in src/qbm/models/BQRBM.py
sample(n_samples, answer_mode='raw', use_gauge=True, binary=False)
Generate samples using the model, either exact or from the annealer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Number of samples to generate (num_reads param in sample_ising). |
required |
answer_mode
|
str
|
"raw" or "histogram". |
'raw'
|
use_gauge
|
bool
|
If True will use a random gauge transformation (recommended for more robust sample generation). |
True
|
binary
|
bool
|
If true will convert the state vector values from {+1, -1} to {0, 1}. |
False
|
Returns:
| Type | Description |
|---|---|
SampleOutput
|
Dictionary (exact) or Ocean SDK SampleSet object (annealer). |
Source code in src/qbm/models/BQRBM.py
save(file_path, reinitialize_annealer=True)
Saves the BQRBM model at file_path. Necessary because of pickling issues with the qpu and sampler objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to save the model to. Must be a Path object or a string with ".pkl" file extension. |
required |
reinitialize_annealer
|
bool
|
If True and has attribute self.annealer_params, then will call self._initialize_annealer() after saving. |
True
|
Source code in src/qbm/models/BQRBM.py
train(n_epochs=100, learning_rate=0.1, learning_rate_beta=0.1, mini_batch_size=10, n_samples=10000, callback=None)
Fits the model to the training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_epochs
|
int
|
Number of epochs to train for. |
100
|
learning_rate
|
float | Sequence[float] | ndarray
|
Learning rate. If a list or array, then it will represent the learning rate over the epochs, must be of length n_epochs. |
0.1
|
learning_rate_beta
|
float | Sequence[float] | ndarray
|
Learning rate for the effective temperature. If a list or array, then it will represent the learning rate over the epochs, must be of length n_epochs. Note: It might be useful to use a larger learning_rate_beta in the beginning to help the model find a good temperature, then drop it after a number of epochs. |
0.1
|
mini_batch_size
|
int
|
Size of the mini-batches. |
10
|
n_samples
|
int
|
Number of samples to generate after every epoch. Used for computing β gradient, as well as the callback. |
10000
|
callback
|
Callback | None
|
A function called at the end of each epoch. It takes the arguments (model, samples), and returns a dictionary with required keys ["value", "print"], where the "print" value is a string to be printed at the end of each epoch. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If learning_rate or learning_rate_beta is a sequence whose length is not n_epochs. |
Source code in src/qbm/models/BQRBM.py
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 | |