API Reference¶
The starting point for PlanqTN Python API is the planqtn
package.
PlanqTN is a library for creating and analyzing tensor network quantum error correction codes.
To build tensor network codes manually, use the planqtn.TensorNetwork class and the planqtn.StabilizerCodeTensorEnumerator class for nodes alongside with the planqtn.Legos module for predefined parity check matrices.
Example
Put together a tensor network from stabilizer code tensors and compute the weight enumerator polynomial.
>>> from planqtn import TensorNetwork
>>> from planqtn import StabilizerCodeTensorEnumerator
>>> from planqtn import Legos
>>> # Create tensor network from stabilizer code tensors
>>> nodes = [StabilizerCodeTensorEnumerator(tensor_id="z0", h=Legos.z_rep_code(3)),
... StabilizerCodeTensorEnumerator(tensor_id="x1", h=Legos.x_rep_code(3)),
... StabilizerCodeTensorEnumerator(tensor_id="z2", h=Legos.z_rep_code(3))]
>>> tn = TensorNetwork(nodes)
>>> # Add traces to define contraction pattern
>>> tn.self_trace("z0", "x1", [0], [0])
>>> tn.self_trace("x1", "z2", [1], [0])
>>> # Compute weight enumerator polynomial
>>> wep = tn.stabilizer_enumerator_polynomial()
>>> print(wep)
{0:1, 2:2, 3:8, 4:13, 5:8}
To build tensor network codes automatically, you can use classes in the planqtn.networks module, which contain universal tensor network layouts for stabilizer codes as well for specific codes.
Example
Generate the tensor network for the 5x5 rotated surface code and calculate the weight enumerator polynomial.
>>> from planqtn.networks import RotatedSurfaceCodeTN
>>> tn = RotatedSurfaceCodeTN(5)
>>> for power, coeff in tn.stabilizer_enumerator_polynomial().items():
... print(f"{power}: {coeff}")
0: 1
2: 8
4: 72
6: 534
8: 3715
10: 25816
12: 158448
14: 782532
16: 2726047
18: 5115376
20: 5136632
22: 2437206
24: 390829
Legos
¶
Collection of predefined quantum error correction tensor "legos".
This class provides a library of pre-defined stabilizer code tensors and quantum operations that can be used as building blocks for quantum error correction codes. Each lego represents a specific quantum code or operation with its associated parity check matrix.
The class includes various types of tensors:
- Encoding tensors for specific quantum codes (
[[6,0,3]]
,[[5,1,2]]
, etc.) - Repetition codes for basic error correction
- Stopper tensors for terminating tensor networks
- Identity and Hadamard operations
- Well-known codes like the Steane code and Quantum Reed-Muller codes
Example
>>> from planqtn.symplectic import sprint
>>> # Get the Hadamard tensor
>>> Legos.h
GF([[1, 0, 0, 1],
[0, 1, 1, 0]], order=2)
>>> # Get the stopper_x tensor
>>> Legos.stopper_x
GF([[1, 0]], order=2)
>>> # Get the stopper_z tensor
>>> Legos.stopper_z
GF([[0, 1]], order=2)
>>> # Get a Z-repetition code with distance 3
>>> # and print it in a nice symplectic format
>>> sprint(Legos.z_rep_code(d=3))
___|11_
___|_11
111|___
Source code in planqtn/legos.py
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 |
|
encoding_tensor_512 = GF2([[1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 0], [1, 1, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 0, 1]])
class-attribute
instance-attribute
¶
the [[5,1,2]] subspace tensor of the [[4,2,2]] code, i.e. with the logical leg, leg 5 traced out with the identity stopper from the [[6,0,3]] encoding tensor.
encoding_tensor_512_x = GF2([[1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 1, 0, 0, 0, 0, 0]])
class-attribute
instance-attribute
¶
the X-only version of the planqtn.Legos.encoding_tensor_512
encoding_tensor_512_z = GF2([[0, 0, 0, 0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1, 1, 0, 1]])
class-attribute
instance-attribute
¶
the Z-only version of the planqtn.Legos.encoding_tensor_512
h = GF2([[1, 0, 0, 1], [0, 1, 1, 0]])
class-attribute
instance-attribute
¶
the Hadamard tensor
identity = GF2([[1, 1, 0, 0], [0, 0, 1, 1]])
class-attribute
instance-attribute
¶
the identity tensor is the Bell state, the |00> + |11> state
stopper_i = GF2([Pauli.I.to_gf2()])
class-attribute
instance-attribute
¶
the identity stopper tensor, which is the free qubit subspace, corresponds to the Pauli I operator.
stopper_x = GF2([Pauli.X.to_gf2()])
class-attribute
instance-attribute
¶
the X-type stopper tensor, the |+> state, corresponds to the Pauli X operator.
stopper_y = GF2([Pauli.Y.to_gf2()])
class-attribute
instance-attribute
¶
the Y-type stopper tensor, the |+i> state, corresponds to the Pauli Y operator.
stopper_z = GF2([Pauli.Z.to_gf2()])
class-attribute
instance-attribute
¶
the Z-type stopper tensor, the |0> state, corresponds to the Pauli Z operator.
x_rep_code(d=3)
staticmethod
¶
Generate an X-type repetition code parity check matrix.
Creates a repetition code that protects against phase-flip errors using X-type stabilizers. The code has distance d and encodes 1 logical qubit in d physical qubits. It is also the X-spider in the ZX-calculus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
int
|
Distance of the repetition code (default: 3). |
3
|
Returns:
Name | Type | Description |
---|---|---|
GF2 |
GF2
|
Parity check matrix for the X-repetition code. |
Source code in planqtn/legos.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
z_rep_code(d=3)
staticmethod
¶
Generate a Z-type repetition code parity check matrix.
Creates a repetition code that protects against bit-flip errors using Z-type stabilizers. The code has distance d and encodes 1 logical qubit in d physical qubits. It is also the Z-spider in the ZX-calculus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
int
|
Distance of the repetition code (default: 3). |
3
|
Returns:
Name | Type | Description |
---|---|---|
GF2 |
GF2
|
Parity check matrix for the Z-repetition code. |
Source code in planqtn/legos.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
StabilizerCodeTensorEnumerator
¶
Tensor enumerator for a stabilizer code.
Source code in planqtn/stabilizer_tensor_enumerator.py
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 |
|
__init__(h, tensor_id=0, legs=None, coset_flipped_legs=None, annotation=None)
¶
Construct a stabilizer code tensor enumerator.
A StabilizerCodeTensorEnumerator
is basically an object oriented wrapper around
a parity check matrix. It supports self-tracing, as well as tensor product, and conjoining
of with other StabilizerCodeTensorEnumerator
instances. As such, it is the building block
of tensor networks in the TensorNetwork class.
The class also supports the enumeration of the scalar stabilizer weight enumerator of the
code via brute force. There can be legs left open, in which case the weight enumerator
becomes a tensor weight enumerator. Weight truncation is supported for approximate
enumeration. Coset support is represented by coset_flipped_legs
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h
|
GF2
|
The parity check matrix. |
required |
tensor_id
|
TensorId
|
The ID of the tensor. |
0
|
legs
|
Optional[List[TensorLeg]]
|
The legs of the tensor. |
None
|
coset_flipped_legs
|
Optional[List[Tuple[Tuple[Any, int], GF2]]]
|
The coset flipped legs of the tensor. |
None
|
annotation
|
Optional[LegoAnnotation]
|
The annotation of the tensor for hints for visualization in PlanqTN Studio. |
None
|
Raises:
Type | Description |
---|---|
AssertionError
|
If the legs are not valid. |
Source code in planqtn/stabilizer_tensor_enumerator.py
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 |
|
conjoin(other, legs1, legs2)
¶
Creates a new tensor enumerator by conjoining two of them.
Creates a new tensor enumerator by contracting the specified legs between this tensor and another tensor. The legs of the other tensor will become the legs of the new tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
StabilizerCodeTensorEnumerator
|
The other tensor enumerator to conjoin with. |
required |
legs1
|
Sequence[int | TensorLeg]
|
Legs from this tensor to contract. |
required |
legs2
|
Sequence[int | TensorLeg]
|
Legs from the other tensor to contract. |
required |
Returns:
Name | Type | Description |
---|---|---|
StabilizerCodeTensorEnumerator |
StabilizerCodeTensorEnumerator
|
The conjoined tensor enumerator. |
Source code in planqtn/stabilizer_tensor_enumerator.py
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 |
|
is_stabilizer(op)
¶
Check if an operator is a stabilizer of this code.
Determines whether the given operator commutes with all stabilizers of the code by checking if op * omega * h^T = 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
op
|
GF2
|
Operator to check (as GF2 vector). |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if op is a stabilizer, False otherwise. |
Source code in planqtn/stabilizer_tensor_enumerator.py
213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
self_trace(legs1, legs2)
¶
Perform self-tracing by contracting pairs of legs within this tensor.
Contracts pairs of legs within the same tensor, effectively performing a partial trace operation. The legs are paired up and contracted together.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
legs1
|
Sequence[int | TensorLeg]
|
First set of legs to contract (must match length of legs2). |
required |
legs2
|
Sequence[int | TensorLeg]
|
Second set of legs to contract (must match length of legs1). |
required |
Returns:
Name | Type | Description |
---|---|---|
StabilizerCodeTensorEnumerator |
StabilizerCodeTensorEnumerator
|
New tensor with contracted legs removed. |
Raises:
Type | Description |
---|---|
AssertionError
|
If legs1 and legs2 have different lengths. |
Source code in planqtn/stabilizer_tensor_enumerator.py
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 |
|
set_tensor_id(tensor_id)
¶
Set the tensor ID and update all legs to use the new ID.
Updates the tensor_id attribute and modifies all legs that reference the old tensor_id to use the new one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor_id
|
TensorId
|
New tensor ID to assign to this tensor. |
required |
Source code in planqtn/stabilizer_tensor_enumerator.py
196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
stabilizer_enumerator_polynomial(open_legs=(), verbose=False, progress_reporter=DummyProgressReporter(), truncate_length=None)
¶
Compute the stabilizer enumerator polynomial.
Note that this is a brute force method, and is not efficient for large codes, use it with the planqtn.progress_reporter.TqdmProgressReporter to get time estimates. If open_legs is empty, returns the scalar stabilizer enumerator polynomial. If open_legs is not empty, returns a sparse tensor with non-zero values on the open legs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_legs
|
Sequence[TensorLeg]
|
List of legs to leave open. |
()
|
verbose
|
bool
|
Whether to print verbose output. |
False
|
progress_reporter
|
ProgressReporter
|
Progress reporter to use. |
DummyProgressReporter()
|
truncate_length
|
Optional[int]
|
Maximum weight to truncate the enumerator at. |
None
|
Returns:
Name | Type | Description |
---|---|---|
wep |
Union[TensorEnumerator, UnivariatePoly]
|
The stabilizer weight enumerator polynomial. |
Source code in planqtn/stabilizer_tensor_enumerator.py
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 |
|
tensor_with(other)
¶
Create the tensor product with another tensor enumerator.
Computes the tensor product of this tensor with another tensor enumerator. The resulting tensor has the combined parity check matrix and all legs from both tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
StabilizerCodeTensorEnumerator
|
The other tensor enumerator to tensor with. |
required |
Returns:
Name | Type | Description |
---|---|---|
StabilizerCodeTensorEnumerator |
StabilizerCodeTensorEnumerator
|
The tensor product of the two tensors. |
Source code in planqtn/stabilizer_tensor_enumerator.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
trace_with_stopper(stopper, traced_leg)
¶
Trace this tensor with a stopper tensor on the specified leg.
Contracts this tensor with a stopper tensor (representing a measurement or boundary condition) on the specified leg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stopper
|
GF2
|
The stopper tensor to contract with (as a 1x2 GF2 matrix). |
required |
traced_leg
|
int | TensorLeg
|
The leg to contract with the stopper. |
required |
Returns:
Name | Type | Description |
---|---|---|
StabilizerCodeTensorEnumerator |
StabilizerCodeTensorEnumerator
|
New tensor with the stopper contraction applied. |
Source code in planqtn/stabilizer_tensor_enumerator.py
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
|
with_coset_flipped_legs(coset_flipped_legs)
¶
Create a new tensor enumerator with coset-flipped legs.
Creates a copy of this tensor enumerator with the specified coset-flipped legs. This is used for coset weight enumerator calculations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coset_flipped_legs
|
List[Tuple[TensorLeg, GF2]]
|
List of (leg, coset_error) pairs specifying which legs have coset errors applied. |
required |
Returns:
Name | Type | Description |
---|---|---|
StabilizerCodeTensorEnumerator |
StabilizerCodeTensorEnumerator
|
New tensor enumerator with coset-flipped legs. |
Source code in planqtn/stabilizer_tensor_enumerator.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
TensorNetwork
¶
A tensor network for contracting stabilizer code tensor enumerators.
Source code in planqtn/tensor_network.py
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 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 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 |
|
__init__(nodes, truncate_length=None)
¶
Construct a tensor network.
This class represents a tensor network composed of
StabilizerCodeTensorEnumerator
nodes that can be contracted together to compute stabilizer enumerator polynomials.
The trace ordering can be left to use the original manual ordering or use automated,
hyperoptimized contraction ordering using the cotengra
library.
The tensor network maintains a collection of nodes (tensors) and traces (contraction operations between nodes). It can compute weight enumerator polynomials for stabilizer codes by contracting the network according to the specified traces.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Union[Iterable[StabilizerCodeTensorEnumerator], Dict[TensorId, StabilizerCodeTensorEnumerator]]
|
Dictionary mapping tensor IDs to
|
required |
truncate_length
|
Optional[int]
|
Optional maximum length for truncating enumerator polynomials. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the nodes have inconsistent indexing. |
ValueError
|
If there are colliding index values in the nodes. |
Source code in planqtn/tensor_network.py
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 |
|
analyze_traces(cotengra=False, each_step=False, details=False, **cotengra_opts)
¶
Analyze the trace operations and optionally optimize the contraction path.
Analyzes the current trace schedule and can optionally use cotengra to find an optimal contraction path. This is useful for understanding the computational complexity of the tensor network contraction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cotengra
|
bool
|
If True, use cotengra to optimize the contraction path. |
False
|
each_step
|
bool
|
If True, print details for each contraction step. |
False
|
details
|
bool
|
If True, print detailed analysis information. |
False
|
**cotengra_opts
|
Any
|
Additional options to pass to cotengra. |
{}
|
Returns:
Type | Description |
---|---|
Tuple[ContractionTree, int]
|
Tuple[ctg.ContractionTree, int]: The contraction tree and total cost. |
Source code in planqtn/tensor_network.py
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 |
|
conjoin_nodes(verbose=False, progress_reporter=DummyProgressReporter())
¶
Conjoin all nodes in the tensor network according to the trace schedule.
Executes all the trace operations defined in the tensor network to produce
a single tensor enumerator. This tensor enumerator will have the conjoined parity check
matrix. However, running weight enumerator calculation on this conjoined node would use the
brute force method, and as such would be typically more expensive than using the
stabilizer_enumerator_polynomial
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verbose
|
bool
|
If True, print verbose output during contraction. |
False
|
progress_reporter
|
ProgressReporter
|
Progress reporter for tracking the contraction process. |
DummyProgressReporter()
|
Returns:
Name | Type | Description |
---|---|---|
StabilizerCodeTensorEnumerator |
StabilizerCodeTensorEnumerator
|
The contracted tensor enumerator. |
Source code in planqtn/tensor_network.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 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 599 600 601 602 603 604 |
|
n_qubits()
¶
Get the total number of qubits in the tensor network.
Returns the total number of qubits represented by this tensor network. This is an abstract method that must be implemented by subclasses that have a representation for qubits.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Total number of qubits. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
This method must be implemented by subclasses. |
Source code in planqtn/tensor_network.py
192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
qubit_to_node_and_leg(q)
¶
Map a qubit index to its corresponding node and leg.
This method maps a global qubit index to the specific node and leg that represents that qubit in the tensor network. This is an abstract method that must be implemented by subclasses that have a representation for qubits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
int
|
Global qubit index. |
required |
Returns:
Name | Type | Description |
---|---|---|
node_id |
TensorId
|
Node ID and leg that represent the qubit. |
leg |
TensorLeg
|
Leg that represent the qubit. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
This method must be implemented by subclasses. |
Source code in planqtn/tensor_network.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
self_trace(node_idx1, node_idx2, join_legs1, join_legs2)
¶
Add a trace operation between two nodes in the tensor network.
Defines a contraction between two nodes by specifying which legs to join. This operation is added to the trace schedule and will be executed when the tensor network is contracted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_idx1
|
TensorId
|
ID of the first node to trace. |
required |
node_idx2
|
TensorId
|
ID of the second node to trace. |
required |
join_legs1
|
Sequence[int | TensorLeg]
|
Legs from the first node to contract. |
required |
join_legs2
|
Sequence[int | TensorLeg]
|
Legs from the second node to contract. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the weight enumerator has already been computed. |
Source code in planqtn/tensor_network.py
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 |
|
set_coset(coset_error)
¶
Set the coset error for the tensor network.
Sets the coset error that will be used for coset weight enumerator calculations.
The coset error should follow the qubit numbering defined in
qubit_to_node_and_leg
which maps the index
to a node ID. Both qubit_to_node_and_leg
and n_qubits
are abstract classes, and thus this method
can only be called on a subclass that implements these methods, see the
planqtn.networks
module for examples.
There are two possible ways to pass the coset_error:
- a tuple of two lists of qubit indices, one for the
Z
errors and one for theX
errors - a
galois.GF2
array of length2 * tn.n_qubits()
for thetn
tensor network. This is a symplectic operator representation on then
qubits of the tensor network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coset_error
|
GF2 | Tuple[List[int], List[int]]
|
The coset error specification. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the coset error has the wrong number of qubits. |
Source code in planqtn/tensor_network.py
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 |
|
set_truncate_length(truncate_length)
¶
Set the truncation length for weight enumerator polynomials.
Sets the maximum weight to keep in weight enumerator polynomials. This affects all subsequent computations and resets any cached results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
truncate_length
|
int
|
Maximum weight to keep in enumerator polynomials. |
required |
Source code in planqtn/tensor_network.py
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 |
|
stabilizer_enumerator(verbose=False, progress_reporter=DummyProgressReporter())
¶
Compute the stabilizer weight enumerator.
Computes the weight enumerator polynomial and returns it as a dictionary mapping weights to coefficients. This is a convenience method that calls stabilizer_enumerator_polynomial() and extracts the dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verbose
|
bool
|
If True, print verbose output. |
False
|
progress_reporter
|
ProgressReporter
|
Progress reporter for tracking computation. |
DummyProgressReporter()
|
Returns:
Type | Description |
---|---|
Dict[int, int]
|
Dict[int, int]: Weight enumerator as a dictionary mapping weights to counts. |
Source code in planqtn/tensor_network.py
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 |
|
stabilizer_enumerator_polynomial(open_legs=(), verbose=False, progress_reporter=DummyProgressReporter(), cotengra=True)
¶
Returns the reduced stabilizer enumerator polynomial for the tensor network.
If open_legs is not empty, then the returned tensor enumerator polynomial is a dictionary of tensor keys to UnivariatePoly objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_legs
|
Sequence[TensorLeg]
|
The legs that are open in the tensor network. If empty, the result is a
scalar weightenumerator polynomial of type |
()
|
verbose
|
bool
|
If True, print verbose output. |
False
|
progress_reporter
|
ProgressReporter
|
The progress reporter to use, defaults to no progress reporting
( |
DummyProgressReporter()
|
cotengra
|
bool
|
If True, use cotengra to contract the tensor network, otherwise use the order the traces were constructed. |
True
|
Returns:
Name | Type | Description |
---|---|---|
TensorEnumerator |
TensorEnumerator | UnivariatePoly
|
The reduced stabilizer enumerator polynomial for the tensor network. |
Source code in planqtn/tensor_network.py
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
|
traces_to_dot()
¶
Print the tensor network traces in DOT format.
Prints the traces (contractions) between nodes in a format that can be used to visualize the tensor network structure. Each trace is printed as a directed edge between nodes.
Source code in planqtn/tensor_network.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
UnivariatePoly
¶
A class for univariate integer polynomials.
Source code in planqtn/poly.py
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 |
|
__init__(d=None)
¶
Construct a univariate integer polynomial.
This class represents univariate polynomials as a dictionary mapping powers to coefficients. It's specifically designed for weight enumerator polynomials, where coefficients are typically integers.
The class provides basic polynomial operations like addition, multiplication, normalization, and MacWilliams dual computation. It also supports truncation and homogenization for bivariate polynomials.
Attributes:
Name | Type | Description |
---|---|---|
dict |
Dictionary mapping integer powers to integer coefficients. |
|
num_vars |
Number of variables (always 1 for univariate). |
Raises:
Type | Description |
---|---|
ValueError
|
If the input is not a dictionary or a UnivariatePoly. |
Example
>>> # Create a polynomial: 1 + 3x + 2x^2
>>> poly = UnivariatePoly({0: 1, 1: 3, 2: 2})
>>> # Add polynomials
>>> result = poly + UnivariatePoly({1: 1, 3: 1})
>>> # Multiply by scalar
>>> scaled = poly * 2
>>> # Get minimum weight term
>>> min_weight, coeff = poly.minw()
>>> min_weight
0
>>> coeff
1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
Optional[Union[UnivariatePoly, Dict[int, int]]]
|
The dictionary of powers and coefficients. |
None
|
Source code in planqtn/poly.py
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 |
|
add_inplace(other)
¶
Add another polynomial to this one in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
UnivariatePoly
|
The polynomial to add to this one. |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
If the polynomials have different numbers of variables. |
Source code in planqtn/poly.py
236 237 238 239 240 241 242 243 244 245 246 247 |
|
is_scalar()
¶
Check if the polynomial is a scalar (constant term only).
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the polynomial has only a constant term (power 0). |
Source code in planqtn/poly.py
228 229 230 231 232 233 234 |
|
items()
¶
Yield items from the polynomial.
Yields:
Type | Description |
---|---|
Tuple[Any, int]
|
Tuple[Any, int]: A tuple of the power and coefficient. |
Source code in planqtn/poly.py
279 280 281 282 283 284 285 |
|
leading_order_poly()
¶
Get the polynomial containing only the minimum weight term.
Returns:
Name | Type | Description |
---|---|---|
UnivariatePoly |
UnivariatePoly
|
A new polynomial with only the minimum weight term. |
Source code in planqtn/poly.py
266 267 268 269 270 271 272 273 274 |
|
macwilliams_dual(n, k, to_normalizer=True)
¶
Convert this weight enumerator polynomial to its MacWilliams dual.
The MacWilliams duality theorem relates the weight enumerator polynomial of a code to that of its dual code. This method implements the transformation A(z) -> B(z) = (1 + z)^n * A((1 - z)/(1 + z)) / 2^k.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
int
|
Length of the code. |
required |
k
|
int
|
Dimension of the code. |
required |
to_normalizer
|
bool
|
If True, compute the normalizer enumerator polynomial. If False, compute the weight enumerator polynomial. This affects the normalization factors. |
True
|
Returns:
Name | Type | Description |
---|---|---|
UnivariatePoly |
UnivariatePoly
|
The MacWilliams dual weight enumerator polynomial. |
Source code in planqtn/poly.py
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 |
|
minw()
¶
Get the minimum weight term and its coefficient.
Returns:
Type | Description |
---|---|
Tuple[Any, int]
|
Tuple containing the minimum power and its coefficient. |
Source code in planqtn/poly.py
256 257 258 259 260 261 262 263 264 |
|
normalize(verbose=False)
¶
Normalize the polynomial by dividing by the constant term if it's greater than 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verbose
|
bool
|
If True, print normalization information. |
False
|
Returns:
Name | Type | Description |
---|---|---|
UnivariatePoly |
UnivariatePoly
|
The normalized polynomial. |
Source code in planqtn/poly.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
truncate_inplace(n)
¶
Truncate the polynomial to terms with power <= n in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
int
|
Maximum power to keep in the polynomial. |
required |
Source code in planqtn/poly.py
355 356 357 358 359 360 361 |
|
Stabilizer tensor enumerator module.
The unit of the tensor network is a stabilizer code encoding tensor (quantum lego), represented by
the StabilizerCodeTensorEnumerator
defined by a parity check matrix.
The main methods are:
- stabilizer_enumerator_polynomial
: Brute force calculation of the stabilizer enumerator
polynomial for the stabilizer code.
- trace_with_stopper
: Traces the lego leg with a stopper.
- conjoin
: Conjoins two lego pieces into a new lego piece.
- self_trace
: Traces a leg with itself.
- with_coset_flipped_legs
: Adds coset flipped legs to the lego piece.
- tensor_with
: Tensor product of two lego pieces.
TensorId = str | int | Tuple[int, int]
module-attribute
¶
The tensor id can be a string, an integer, or a tuple of two integers.
TensorLeg = Tuple[TensorId, int]
module-attribute
¶
The tensor leg is a tuple of a tensor id and a leg index.
TensorEnumeratorKey = Tuple[int, ...]
module-attribute
¶
The tensor enumerator key is a tuple of integers.
TensorEnumerator = Dict[TensorEnumeratorKey, UnivariatePoly]
module-attribute
¶
The tensor enumerator is a dictionary of tuples of integers and univariate polynomials.
The planqtn.networks
package¶
The planqtn.networks
module contains layouts for tensor network networks.
For universally applicable tensor network layouts, see:
- planqtn.networks.CssTannerCodeTN
- planqtn.networks.StabilizerMeasurementStatePrepTN
- planqtn.networks.StabilizerTannerCodeTN
For specific networks, see:
- planqtn.networks.CompassCodeDualSurfaceCodeLayoutTN for any compass code using the dual surface code layout.
- planqtn.networks.SurfaceCodeTN for unrotated surface code family
- planqtn.networks.RotatedSurfaceCodeTN for the rotated surface code family
CompassCodeDualSurfaceCodeLayoutTN
¶
Bases: SurfaceCodeTN
A tensor network representation of compass codes using dual surface code layout.
This class implements a compass code using the dual doubled surface code equivalence described by Cao & Lackey in the expansion pack paper. The compass code is constructed by applying gauge operations to a surface code based on a coloring pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coloring
|
ndarray
|
Array specifying the coloring pattern for the compass code. |
required |
lego
|
Callable[[TensorId], GF2]
|
Function that returns the lego tensor for each node. |
lambda node: encoding_tensor_512
|
coset_error
|
Optional[GF2]
|
Optional coset error for weight enumerator calculations. |
None
|
truncate_length
|
Optional[int]
|
Optional maximum weight for truncating enumerators. |
None
|
Source code in planqtn/networks/compass_code.py
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 |
|
__init__(coloring, *, lego=lambda node: Legos.encoding_tensor_512, coset_error=None, truncate_length=None)
¶
Create a square compass code based on the coloring.
Creates a compass code using the dual doubled surface code equivalence described by Cao & Lackey in the expansion pack paper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coloring
|
ndarray
|
Array specifying the coloring pattern for the compass code. |
required |
lego
|
Callable[[TensorId], GF2]
|
Function that returns the lego tensor for each node. |
lambda node: encoding_tensor_512
|
coset_error
|
Optional[GF2]
|
Optional coset error for weight enumerator calculations. |
None
|
truncate_length
|
Optional[int]
|
Optional maximum weight for truncating enumerators. |
None
|
Source code in planqtn/networks/compass_code.py
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 |
|
CssTannerCodeTN
¶
Bases: TensorNetwork
A tensor network representation of CSS codes using Tanner graph structure.
This class constructs a tensor network from X and Z parity check matrices (Hx and Hz), representing a CSS code. The tensor network connects qubit tensors to check tensors according to the non-zero entries in the parity check matrices.
Source code in planqtn/networks/css_tanner_code.py
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 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 |
|
__init__(hx, hz)
¶
Construct a CSS code tensor network from X and Z parity check matrices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hx
|
ndarray
|
X-type parity check matrix. |
required |
hz
|
ndarray
|
Z-type parity check matrix. |
required |
Source code in planqtn/networks/css_tanner_code.py
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 |
|
n_qubits()
¶
Get the total number of qubits in the tensor network.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Total number of qubits represented by this tensor network. |
Source code in planqtn/networks/css_tanner_code.py
181 182 183 184 185 186 187 |
|
qubit_to_node_and_leg(q)
¶
Map a qubit index to its corresponding node and leg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
int
|
Global qubit index. |
required |
Returns:
Type | Description |
---|---|
Tuple[TensorId, TensorLeg]
|
Tuple[TensorId, TensorLeg]: Node ID and leg that represent the qubit. |
Source code in planqtn/networks/css_tanner_code.py
189 190 191 192 193 194 195 196 197 198 |
|
RotatedSurfaceCodeTN
¶
Bases: TensorNetwork
A tensor network representation of rotated surface codes.
This class constructs a tensor network for a rotated surface code of distance d. The rotated surface code has a checkerboard pattern of X and Z stabilizers, with appropriate boundary conditions for the rotated geometry.
Source code in planqtn/networks/rotated_surface_code.py
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 134 135 136 |
|
__init__(d, lego=lambda i: Legos.encoding_tensor_512, coset_error=None, truncate_length=None)
¶
Construct a rotated surface code tensor network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
int
|
Distance of the surface code. |
required |
lego
|
Callable[[TensorId], GF2]
|
Function that returns the lego tensor for each node. |
lambda i: encoding_tensor_512
|
coset_error
|
Optional[GF2]
|
Optional coset error for weight enumerator calculations. |
None
|
truncate_length
|
Optional[int]
|
Optional maximum weight for truncating enumerators. |
None
|
Source code in planqtn/networks/rotated_surface_code.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 |
|
n_qubits()
¶
Get the total number of qubits in the tensor network.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Total number of qubits represented by this tensor network. |
Source code in planqtn/networks/rotated_surface_code.py
130 131 132 133 134 135 136 |
|
qubit_to_node_and_leg(q)
¶
Map a qubit index to its corresponding node and leg.
The rotated surface code uses column major ordering.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
int
|
Global qubit index. |
required |
Returns:
Type | Description |
---|---|
Tuple[TensorId, TensorLeg]
|
Tuple[TensorId, TensorLeg]: Node ID and leg that represent the qubit. |
Source code in planqtn/networks/rotated_surface_code.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
StabilizerMeasurementStatePrepTN
¶
Bases: TensorNetwork
Measurement-based state preparation circuit layout.
A universal tensor network layout based on the measurement-based state preparation circuit layout described in the following work:
Cao, ChunJun, Michael J. Gullans, Brad Lackey, and Zitao Wang. 2024. “Quantum Lego Expansion Pack: Enumerators from Tensor Networks.” PRX Quantum 5 (3): 030313. https://doi.org/10.1103/PRXQuantum.5.030313.
Source code in planqtn/networks/stabilizer_measurement_state_prep.py
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 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 |
|
__init__(parity_check_matrix)
¶
Construct a stabilizer measurement state preparation tensor network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parity_check_matrix
|
ndarray
|
The parity check matrix of the stabilizer code. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the parity check matrix is not symplectic. |
NotImplementedError
|
If Y stabilizers are in the parity check matrix. It is not implemented yet. |
Source code in planqtn/networks/stabilizer_measurement_state_prep.py
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 |
|
n_qubits()
¶
Get the total number of qubits in the tensor network.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Total number of qubits represented by this tensor network. |
Source code in planqtn/networks/stabilizer_measurement_state_prep.py
239 240 241 242 243 244 245 |
|
qubit_to_node_and_leg(q)
¶
Map a qubit index to its corresponding node and leg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
int
|
Global qubit index. |
required |
Returns:
Type | Description |
---|---|
Tuple[TensorId, TensorLeg]
|
Tuple[TensorId, TensorLeg]: Node ID and leg that represent the qubit. |
Source code in planqtn/networks/stabilizer_measurement_state_prep.py
247 248 249 250 251 252 253 254 255 256 |
|
StabilizerTannerCodeTN
¶
Bases: TensorNetwork
A tensor network representation of stabilizer codes using Tanner graph structure.
This class constructs a tensor network from a parity check matrix H, where each row of H represents a stabilizer generator and each column represents a qubit. The tensor network is built by connecting check tensors to qubit tensors according to the non-zero entries in the parity check matrix.
Source code in planqtn/networks/stabilizer_tanner_code.py
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
__init__(h)
¶
Construct a stabilizer Tanner code tensor network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h
|
ndarray
|
Parity check matrix in symplectic form (must have even number of columns). |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the parity check matrix is not symplectic. |
Source code in planqtn/networks/stabilizer_tanner_code.py
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 |
|
n_qubits()
¶
Get the total number of qubits in the tensor network.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Total number of qubits represented by this tensor network. |
Source code in planqtn/networks/stabilizer_tanner_code.py
128 129 130 131 132 133 134 |
|
qubit_to_node_and_leg(q)
¶
Map a qubit index to its corresponding node and leg.
Returns the tensor and leg for the given qubit index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
int
|
Global qubit index. |
required |
Returns:
Name | Type | Description |
---|---|---|
TensorId
|
Node ID: node id for the tensor in the network |
|
Leg |
TensorLeg
|
leg that represent the qubit. |
Source code in planqtn/networks/stabilizer_tanner_code.py
136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
SurfaceCodeTN
¶
Bases: TensorNetwork
A tensor network layout for the surface code.
Source code in planqtn/networks/surface_code.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 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 |
|
__init__(d, lego=lambda i: Legos.encoding_tensor_512, coset_error=None, truncate_length=None)
¶
Construct a surface code tensor network.
The numbering convention is as follows for the tensor ids (row, column):
(0,0) (0,2) (0,4)
(1,1) (1,3)
(2,0) (2,2) (2,4)
(3,1) (3,3)
(4,0) (4,2) (4,4)
Cao, ChunJun, Michael J. Gullans, Brad Lackey, and Zitao Wang. 2024. “Quantum Lego Expansion Pack: Enumerators from Tensor Networks.” PRX Quantum 5 (3): 030313. https://doi.org/10.1103/PRXQuantum.5.030313.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
int
|
The number of qubits in the surface code. |
required |
lego
|
Callable[[TensorId], GF2]
|
The lego to use for the surface code. |
lambda i: encoding_tensor_512
|
coset_error
|
Optional[GF2]
|
The coset error to use for the surface code. |
None
|
truncate_length
|
Optional[int]
|
The truncate length to use for the surface code. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the distance is less than 2. |
Source code in planqtn/networks/surface_code.py
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 |
|
n_qubits()
¶
Get the total number of qubits in the tensor network.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Total number of qubits represented by this tensor network. |
Source code in planqtn/networks/surface_code.py
226 227 228 229 230 231 232 |
|
qubit_to_node_and_leg(q)
¶
Map a qubit index to its corresponding node and leg.
Returns the tensor and leg for the given qubit index. We follow row-major ordering, i.e. for this layout:
(0,0) (0,2) (0,4)
(1,1) (1,3)
(2,0) (2,2) (2,4)
(3,1) (3,3)
(4,0) (4,2) (4,4)
0 1 2
3 4
5 6 7
8 9
10 11 12
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q
|
int
|
Global qubit index. |
required |
Returns:
Name | Type | Description |
---|---|---|
TensorId
|
Node ID: node id for the tensor in the network |
|
Leg |
TensorLeg
|
leg that represent the qubit. |
Source code in planqtn/networks/surface_code.py
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 |
|
Utilities¶
The planqtn.progress_reporter
package¶
Progress reporter interface and implementations for calculations.
The main class is ProgressReporter
which is an abstract base class for all progress reporters.
The main methods are:
- iterate: Iterates over an iterable and reports progress on every item.
- enter_phase: Starts a new phase.
- exit_phase: Ends the current phase.
The main implementations are:
- TqdmProgressReporter: A progress reporter that
uses
tqdm
to report progress in the terminal. - DummyProgressReporter: A progress reporter that does nothing.
This is the main mechanism for reporting progress back to PlanqTN Studio UI from the backend jobs in realtime.
DummyProgressReporter
¶
Bases: ProgressReporter
A no-op progress reporter that does nothing.
This implementation provides a null progress reporter that can be used when progress reporting is not needed. It implements all required methods but performs no actual reporting, making it useful as a default or for testing purposes or creating a silent mode for scripts to run.
Source code in planqtn/progress_reporter.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
handle_result(result)
¶
Handle progress result (no-op for dummy reporter).
The dummy reporter ignores all progress results, making it useful when progress reporting is not needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result
|
Dict[str, Any]
|
Progress result dictionary (ignored). |
required |
Source code in planqtn/progress_reporter.py
393 394 395 396 397 398 399 400 401 |
|
IterationState
¶
State tracking information for a single iteration phase.
This class tracks the progress and timing information for a single iteration or calculation phase. It maintains statistics like current progress, timing, and performance metrics that can be used for progress reporting and analysis.
Attributes:
Name | Type | Description |
---|---|---|
desc |
str
|
Description of the current iteration phase. |
total_size |
int
|
Total number of items to process in this iteration. |
current_item |
int
|
Current item being processed (0-indexed). |
start_time |
float
|
Timestamp when the iteration started. |
end_time |
float | None
|
Timestamp when the iteration ended (None if not finished). |
duration |
float | None
|
Total duration of the iteration in seconds (None if not finished). |
avg_time_per_item |
float | None
|
Average time per item in seconds (None if no items processed). |
Source code in planqtn/progress_reporter.py
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 |
|
end()
¶
Mark the iteration as completed.
Sets the end time and calculates the final duration and average time per item statistics.
Source code in planqtn/progress_reporter.py
83 84 85 86 87 88 89 90 91 |
|
to_dict()
¶
Convert the IterationState to a dictionary for JSON serialization.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary representation of the iteration state suitable for JSON serialization. |
Source code in planqtn/progress_reporter.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
update(current_item=None)
¶
Update the iteration state with progress information.
Updates the current item count, recalculates duration, and updates the average time per item. If no current_item is provided, increments the current item by 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_item
|
int | None
|
New current item index. If None, increments by 1. |
None
|
Source code in planqtn/progress_reporter.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
IterationStateEncoder
¶
Bases: JSONEncoder
Custom JSON encoder for IterationState objects.
This encoder extends the standard JSON encoder to handle IterationState objects by converting them to dictionaries using their to_dict() method. This enables JSON serialization of progress reporting data.
Source code in planqtn/progress_reporter.py
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 |
|
__call__(o)
¶
Encode an object to JSON string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
o
|
Any
|
Object to encode. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
JSON string representation of the object. |
Source code in planqtn/progress_reporter.py
141 142 143 144 145 146 147 148 149 150 |
|
default(o)
¶
Convert IterationState objects to dictionaries for JSON serialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
o
|
Any
|
Object to encode. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
Dictionary representation if o is an IterationState, otherwise delegates to parent class. |
Source code in planqtn/progress_reporter.py
127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
ProgressReporter
¶
Bases: ABC
Abstract base class for progress reporting in calculations.
This class provides a framework for reporting progress during long-running calculations. It supports nested iteration phases and can be composed with other progress reporters. The main mechanism for reporting progress back to PlanqTN Studio UI from backend jobs in realtime.
Subclasses should implement the handle_result
method to define how progress
information is processed (e.g., displayed, logged, or sent to a UI).
Attributes:
Name | Type | Description |
---|---|---|
sub_reporter |
Optional nested progress reporter for composition. |
|
iterator_stack |
list[IterationState]
|
Stack of active iteration states for nested phases. |
iteration_report_frequency |
Minimum time interval between progress reports. |
Source code in planqtn/progress_reporter.py
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 |
|
__init__(sub_reporter=None, iteration_report_frequency=0.0)
¶
Initialize the progress reporter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sub_reporter
|
Optional[ProgressReporter]
|
Optional nested progress reporter for composition. |
None
|
iteration_report_frequency
|
float
|
Minimum time interval between progress reports in seconds. If 0.0, reports on every iteration. |
0.0
|
Source code in planqtn/progress_reporter.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
enter_phase(desc)
¶
Enter a new calculation phase with progress tracking.
Creates a context manager for tracking a single-step phase. This is useful for marking the beginning and end of calculation phases that don't involve iteration but should still be tracked for progress reporting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
desc
|
str
|
Description of the phase. |
required |
Returns:
Type | Description |
---|---|
_GeneratorContextManager[Any, None, None]
|
Context manager that can be used with 'with' statement. |
Source code in planqtn/progress_reporter.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
exit_phase()
¶
Exit the current calculation phase.
Removes the current iteration state from the stack, effectively ending the current phase. This is typically called automatically when using the context manager from enter_phase().
Source code in planqtn/progress_reporter.py
301 302 303 304 305 306 307 308 |
|
handle_result(result)
abstractmethod
¶
Handle progress result data.
This hook method must be implemented by subclasses to define how progress information is processed. The result dictionary contains iteration state and metadata about the current progress.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result
|
Dict[str, Any]
|
Dictionary containing progress information including: - iteration: IterationState object or dict - level: Nesting level of the current iteration - Additional metadata specific to the implementation |
required |
Source code in planqtn/progress_reporter.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
iterate(iterable, desc, total_size)
¶
Start a new iteration phase with progress reporting.
Creates a generator that yields items from the iterable while tracking progress and reporting it at regular intervals. The iteration state is maintained on a stack to support nested iterations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable
|
Iterable
|
The iterable to iterate over. |
required |
desc
|
str
|
Description of the iteration phase. |
required |
total_size
|
int
|
Total number of items to process. |
required |
Yields:
Type | Description |
---|---|
Any
|
Items from the iterable. |
Source code in planqtn/progress_reporter.py
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 |
|
log_result(result)
¶
Log progress result and propagate to sub-reporter.
Converts IterationState objects to dictionaries for serialization, calls the handle_result method, and propagates the result to any nested sub-reporter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result
|
Dict[str, Any]
|
Dictionary containing progress information. |
required |
Source code in planqtn/progress_reporter.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
TqdmProgressReporter
¶
Bases: ProgressReporter
Progress reporter that displays progress using tqdm
progress bars.
This implementation uses the tqdm
library to display progress bars in the
terminal. It's useful for command-line applications and provides visual
feedback during long-running calculations.
Attributes:
Name | Type | Description |
---|---|---|
file |
Output stream for the progress bars (default: sys.stdout). |
|
mininterval |
Minimum time interval between progress bar updates. |
Source code in planqtn/progress_reporter.py
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 |
|
__init__(file=sys.stdout, mininterval=None, sub_reporter=None)
¶
Initialize the tqdm
progress reporter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
TextIO
|
Output stream for progress bars (default: sys.stdout). |
stdout
|
mininterval
|
float | None
|
Minimum time interval between updates in seconds. If None, uses 2 seconds for large iterations (>100k items) or 0.1 seconds for smaller ones. |
None
|
sub_reporter
|
Optional[ProgressReporter]
|
Optional nested progress reporter for composition. |
None
|
Source code in planqtn/progress_reporter.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
handle_result(result)
¶
Handle progress result (no-op for tqdm
reporter).
The tqdm
reporter doesn't need to handle results separately since
the progress is displayed through the tqdm
progress bar.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result
|
Dict[str, Any]
|
Progress result dictionary (ignored). |
required |
Source code in planqtn/progress_reporter.py
373 374 375 376 377 378 379 380 381 |
|
iterate(iterable, desc, total_size)
¶
Iterate with tqdm
progress bar display.
Overrides the parent iterate method to wrap the iteration with a tqdm
progress bar that provides visual feedback in the terminal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable
|
Iterable
|
The iterable to iterate over. |
required |
desc
|
str
|
Description for the progress bar. |
required |
total_size
|
int
|
Total number of items to process. |
required |
Yields:
Type | Description |
---|---|
Any
|
Items from the iterable. |
Source code in planqtn/progress_reporter.py
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 |
|
The planqtn.symplectic
package¶
Symplectic operations and utilities.
omega(n)
¶
Create a symplectic operator for the omega matrix over GF(2).
For n the omega matrix is:
[0 I_n
]
[I_n
0]
where I_n
is the n x n identity matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
int
|
The number of qubits. |
required |
Returns:
Type | Description |
---|---|
GF2
|
The symplectic operator for the omega matrix. |
Source code in planqtn/symplectic.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
replace_with_op_on_indices(indices, op, target)
¶
Replace target symplectic operator's operations with op on the given indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
indices
|
List[int]
|
Indices to replace on. |
required |
op
|
GF2
|
The operator to replace with. |
required |
target
|
GF2
|
The target operator. |
required |
Returns:
Type | Description |
---|---|
GF2
|
The replaced operator. |
Source code in planqtn/symplectic.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
sconcat(*ops)
¶
Concatenate symplectic operators.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*ops
|
Tuple[int, ...]
|
The symplectic operators to concatenate. |
()
|
Returns:
Type | Description |
---|---|
Tuple[int, ...]
|
The concatenated symplectic operator. |
Source code in planqtn/symplectic.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
sprint(h, end='\n')
¶
Print a symplectic matrix in string format.
Prints the string representation of the symplectic matrix to stdout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h
|
GF2
|
Parity check matrix in GF2. |
required |
end
|
str
|
String to append at the end (default: newline). |
'\n'
|
Source code in planqtn/symplectic.py
179 180 181 182 183 184 185 186 187 188 |
|
sslice(op, indices)
¶
Slice a symplectic operator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
op
|
GF2
|
The symplectic operator. |
required |
indices
|
List[int] | slice | ndarray
|
The indices to slice. |
required |
Returns:
Type | Description |
---|---|
GF2
|
The sliced symplectic operator. |
Raises:
Type | Description |
---|---|
ValueError
|
If the indices are of invalid type (neither list, np.ndarray, or slice). |
Source code in planqtn/symplectic.py
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 |
|
sstr(h)
¶
Convert a symplectic matrix to a string representation.
Creates a human-readable string representation of a symplectic matrix where X and Z parts are separated by a '|' character. Uses '_' for 0 and '1' for 1 to make the pattern more visible.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h
|
GF2
|
Parity check matrix in GF2. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
String representation of the matrix. |
Source code in planqtn/symplectic.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
symp_to_str(vec, swapxz=False)
¶
Convert a symplectic operator to a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vec
|
GF2
|
The symplectic operator. |
required |
swapxz
|
bool
|
Whether to swap X and Z. |
False
|
Returns:
Type | Description |
---|---|
str
|
The string representation of the symplectic operator. |
Source code in planqtn/symplectic.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
sympl_to_pauli_repr(op)
¶
Convert a symplectic operator to a Pauli operator representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
op
|
GF2
|
The symplectic operator. |
required |
Returns:
Type | Description |
---|---|
Tuple[int, ...]
|
The Pauli operator representation of the symplectic operator. |
Source code in planqtn/symplectic.py
69 70 71 72 73 74 75 76 77 78 79 |
|
weight(op, skip_indices=())
¶
Calculate the weight of a symplectic operator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
op
|
GF2
|
The symplectic operator. |
required |
skip_indices
|
Sequence[int]
|
Indices to skip. |
()
|
Returns:
Type | Description |
---|---|
int
|
The weight of the symplectic operator. |
Source code in planqtn/symplectic.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
The planqtn.linalg
package¶
Linear algebra utilities.
gauss(mx, noswaps=False, col_subset=None)
¶
Perform Gauss elimination on a GF2 matrix.
Performs row reduction on a GF2 matrix to bring it to row echelon form. Optionally can restrict elimination to a subset of columns and control whether row swaps are preserved.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mx
|
GF2
|
Input GF2 matrix to eliminate. |
required |
noswaps
|
bool
|
If True, undo row swaps at the end. |
False
|
col_subset
|
Iterable[int] | None
|
Subset of columns to perform elimination on. |
None
|
Returns:
Name | Type | Description |
---|---|---|
GF2 |
GF2
|
Matrix in row echelon form. |
Raises:
Type | Description |
---|---|
ValueError
|
If the matrix is not of GF2 type. |
Source code in planqtn/linalg.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 |
|
gauss_row_augmented(mx)
¶
Perform Gauss elimination on a row-augmented matrix.
Creates a row-augmented matrix by appending the identity matrix to the right of the input matrix, then performs Gauss elimination. This is useful for computing matrix inverses and kernels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mx
|
GF2
|
Input GF2 matrix. |
required |
Returns:
Name | Type | Description |
---|---|---|
GF2 |
GF2
|
Row-augmented matrix after Gauss elimination. |
Source code in planqtn/linalg.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
invert(mx)
¶
Invert a square GF2 matrix.
Computes the inverse of a square GF2 matrix using row-augmented Gauss elimination. The matrix must be non-singular (full rank) for the inverse to exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mx
|
GF2
|
Square GF2 matrix to invert. |
required |
Returns:
Name | Type | Description |
---|---|---|
GF2 |
GF2
|
The inverse of the input matrix. |
Raises:
Type | Description |
---|---|
ValueError
|
If the matrix is not GF2 type, not square, or singular. |
Source code in planqtn/linalg.py
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 |
|
right_kernel(mx)
¶
Compute the right kernel (nullspace) of a GF2 matrix.
Computes a basis for the right kernel of the matrix, which consists of all vectors v such that mx * v = 0. Uses row-augmented Gauss elimination on the transpose of the matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mx
|
GF2
|
Input GF2 matrix. |
required |
Returns:
Name | Type | Description |
---|---|---|
GF2 |
GF2
|
Matrix whose rows form a basis for the right kernel. |
Source code in planqtn/linalg.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|