Models - Library - Linen
Flax Linen-based neural network models for quantum device characterization.
inspeqtor.models.library.linen
inspeqtor.models.library.linen.WoModel
Source code in src/inspeqtor/v1/models/linen.py
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 | |
inspeqtor.models.library.linen.UnitaryModel
Source code in src/inspeqtor/v1/models/linen.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
inspeqtor.models.library.linen.UnitarySPAMModel
Source code in src/inspeqtor/v1/models/linen.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
inspeqtor.models.library.linen.train_model
train_model(
key: ndarray,
train_data: DataBundled,
val_data: DataBundled,
test_data: DataBundled,
model: Module,
optimizer: GradientTransformation,
loss_fn: Callable,
callbacks: list[Callable] = [],
NUM_EPOCH: int = 1000,
model_params: VariableDict | None = None,
opt_state: OptState | None = None,
)
Train the BlackBox model
Examples:
>>> # The number of epochs break down
... NUM_EPOCH = 150
... # Total number of iterations as 90% of data is used for training
... # 10% of the data is used for testing
... total_iterations = 9 * NUM_EPOCH
... # The step for optimizer if set to 8 * NUM_EPOCH (should be less than total_iterations)
... step_for_optimizer = 8 * NUM_EPOCH
... optimizer = get_default_optimizer(step_for_optimizer)
... # The warmup steps for the optimizer
... warmup_steps = 0.1 * step_for_optimizer
... # The cool down steps for the optimizer
... cool_down_steps = total_iterations - step_for_optimizer
... total_iterations, step_for_optimizer, warmup_steps, cool_down_steps
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
ndarray
|
Random key |
required |
model
|
Module
|
The model to be used for training |
required |
optimizer
|
GradientTransformation
|
The optimizer to be used for training |
required |
loss_fn
|
Callable
|
The loss function to be used for training |
required |
callbacks
|
list[Callable]
|
list of callback functions. Defaults to []. |
[]
|
NUM_EPOCH
|
int
|
The number of epochs. Defaults to 1_000. |
1000
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
The model parameters, optimizer state, and the histories |
Source code in src/inspeqtor/v1/models/linen.py
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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | |
inspeqtor.models.library.linen.make_predictive_fn
make_predictive_fn(
adapter_fn: adapter_fn_type,
model: Module,
model_params: Any,
)
Source code in src/inspeqtor/v1/models/linen.py
504 505 506 507 508 509 510 511 512 513 514 | |
inspeqtor.models.library.linen.create_step
create_step(
optimizer: GradientTransformation,
loss_fn: Callable[..., ndarray]
| Callable[..., Tuple[ndarray, Any]],
has_aux: bool = False,
)
The create_step function creates a training step function and a test step function.
loss_fn should have the following signature:
def loss_fn(params: jaxtyping.PyTree, *args) -> jnp.ndarray:
...
return loss_value
params is the parameters to be optimized, and args are the inputs for the loss function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
GradientTransformation
|
|
required |
loss_fn
|
Callable[[PyTree, ...], ndarray]
|
Loss function, which takes in the model parameters, inputs, and targets, and returns the loss value. |
required |
has_aux
|
bool
|
Whether the loss function return aux data or not. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
|
typing.Any: train_step, test_step |
Source code in src/inspeqtor/v1/models/linen.py
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 | |
inspeqtor.models.library.linen.make_loss_fn
make_loss_fn(
adapter_fn: Callable,
model: Module,
evaluate_fn: Callable[
[ndarray, ndarray, ndarray], ndarray
],
)
summary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictive_fn
|
Callable
|
Function for calculating expectation value from the model |
required |
model
|
Module
|
Flax linen Blackbox part of the graybox model. |
required |
evaluate_fn
|
Callable[[ndarray, ndarray], ndarray, ndarray]
|
Take in predicted and experimental expectation values and ideal unitary and return loss value |
required |
Source code in src/inspeqtor/v1/models/linen.py
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 | |