1 2 3 4 5 6 7 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 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 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
//! # CQC Interface //! //! This module documents the [CQC Interface //! specification](https://softwarequtech.github.io/SimulaQron/html/CQCInterface.html) //! and defines the necessary constants and header structures. //! //! # CQC Header //! //! Every CQC message begins with a CQC header. //! //! ```text //! 0 1 2 3 //! 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | version | msg_type | app_id | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | length | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! version 1 byte CQC interface version. Current version is 2. //! msg_type 1 byte Message type. //! app_id 2 bytes Application ID. Return messages will be tagged //! appropriately. //! length 4 bytes Total length of the CQC instruction packet. //! ``` //! //! A CQC Command Header MUST follow the CQC Header for the following messages: //! //! - Command //! - Factory //! - GetTime //! //! ## CQC Header Message Types //! //! The supported message types. They are split into normal types (Tp) and //! error types (Err). //! //! ```text //! Type Name Meaning //! ---- ---- ------- //! 0 Hello Alive check. //! 1 Command Execute a command list. //! 2 Factory Start executing command list repeatedly. //! 3 Expire Qubit has expired. //! 4 Done Command execution done. //! 5 Recv Received qubit. //! 6 EprOk Created EPR pair. //! 7 MeasOut Measurement outcome. //! 8 GetTime Get creation time of qubit. //! 9 InfTime Inform about time. //! 10 NewOk Created new qubit. //! //! 20 General General purpose error (no details). //! 21 NoQubit No more qubits available. //! 22 Unsupp Command sequence not supported. //! 23 Timeout Timeout. //! 24 InUse Qubit already in use. //! 25 Unknown Unknown qubit ID. //! ``` //! //! # CQC Command Header //! //! A CQC Command Header identifies the specific instruction to execute, as //! well as the qubit ID on which to perform this instructions. //! //! A CQC Command Header MUST follow the CQC Header for the following messages: //! //! - Command //! - Factory //! - GetTime //! //! ```text //! 0 1 2 3 //! 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | qubit_id | instr | options | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! qubit_id 2 bytes Qubit ID to perform the operation on. //! instr 1 byte Instruction to perform. //! options 1 byte Options when executing the command. //! ``` //! //! ## Notify //! //! If the notify option bit is set, each of these commands return a CQC //! message Done indicating that execution has completed. Some commands also //! return additional messages, as described below: //! //! - New: Returns a NewOk reply followed by an Extra Qubit header with the //! qubit ID. //! - Measure(InPlace): Returns a MeasOut message followed by a Measurement //! Outcome header containing the measurement outcome. //! - Recv: Returns a Recv reply followed by an Extra Qubit header with the //! qubit ID. //! - Epr(Recv): Returns an EprOk reply by an Extra Qubit header and an //! Entanglement Information header. //! //! ## CQC Command Header Instruction Types //! //! The supported CQC instructions. //! //! ```text //! Type Name Meaning //! ---- ---- ------- //! 0 I Identity (do nothing, wait one step). //! 1 New Ask for a new qubit. //! 2 Measure Measure qubit. //! 3 MeasureInPlace Measure qubit in-place. //! 4 Reset Reset qubit to |0>. //! 5 Send Send qubit to another node. //! 6 Recv Ask to receive qubit. //! 7 Epr Create EPR pair with the specified node. //! 8 EprRecv Receive EPR pair. //! //! 10 X Pauli X. //! 11 Z Pauli Z. //! 12 Y Pauli Y. //! 13 T T Gate. //! 14 RotX Rotation over angle around X in pi/256 increments. //! 15 RotY Rotation over angle around Y in pi/256 increments. //! 16 RotZ Rotation over angle around Z in pi/256 increments. //! 17 H Hadamard Gate. //! 18 K K Gate - taking computational to Y eigenbasis. //! //! 20 Cnot CNOT Gate with this as control. //! 21 Cphase CPHASE Gate with this as control. //! ``` //! //! ## CQC Command Header options //! //! Command options are set as bit flags. //! //! ```text //! Flag Name Meaning //! ---- ---- ------- //! 0x01 Notify Send a notification when command completes. //! 0x02 Action On if there are actions to execute when done. //! 0x04 Block Block until command is done. //! 0x08 IfThen Execute command after done. //! ``` //! //! # CQC Sequence Header //! //! Additional header used to indicate size of a sequence. Used when sending //! multiple commands at once. It tells the backend how many more messages are //! coming. //! //! ```text //! 0 //! 0 1 2 3 4 5 6 7 //! +-+-+-+-+-+-+-+-+ //! | cmd_length | //! +-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! cmd_length 1 byte Length (in bytes) of messages to come. //! ``` //! //! # CQC Rotation Header //! //! Additional header used to define the rotation angle of a rotation gate. //! //! ```text //! 0 //! 0 1 2 3 4 5 6 7 //! +-+-+-+-+-+-+-+-+ //! | step | //! +-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! step 1 byte Angle step of rotation (increments of 1/256). //! ``` //! //! # CQC Extra Qubit Header //! //! Additional header used to send the qubit_id of a secondary qubit for two //! qubit gates. //! //! ```text //! 0 1 //! 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | qubit_id | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! qubit_id 2 bytes ID of the target qubit. //! ``` //! //! # CQC Communication Header //! //! Additional header used to send to which node to send information to. Used //! in send and EPR commands. //! //! ```text //! 0 1 2 3 //! 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | remote_app_id | remote_port | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | remote_node | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! remote_app_id 2 bytes Remote application ID. //! remote_port 2 bytes Port of the remote node for sending classical //! control info. //! remote_node 4 bytes IP of the remote node (IPv4). //! ``` //! //! # CQC Factory Header //! //! Additional header used to send factory information. Factory commands are //! used to tell the backend to do the following command or a sequence of //! commands multiple times. //! //! ```text //! 0 1 //! 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | num_iter | options | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! num_iter 1 byte Number of iterations to do the sequence. //! options 1 byte Options when executing the factory. //! ``` //! //! ## CQC Factory Header options //! //! Factory options are set as bit flags. //! //! ```text //! Flag Name Meaning //! ---- ---- ------- //! 0x01 Notify Send a notification when command completes. //! 0x04 Block Block until factory is done. //! ``` //! //! # CQC Measurement Outcome Header //! //! Additional header used to send the outcome of a measurement. //! //! ```text //! 0 //! 0 1 2 3 4 5 6 7 //! +-+-+-+-+-+-+-+-+ //! | meas_out | //! +-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! meas_out 1 byte Measurement outcome. //! ``` //! //! # CQC Time Info Header //! //! Additional header used to send time information in response to the GetTime //! command. //! //! ```text //! 0 1 2 3 //! 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | datetime | //! | | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! datetime 8 bytes Time of creation. //! ``` //! //! # CQC Entanglement Information Header //! //! When an EPR-pair is created the CQC Backend will return information about //! the entanglement which can be used in a entanglement management protocol. //! The entanglement information header contains information about the parties //! that share the EPR-pair, the time of creation, how good the entanglement is //! (goodness). Furthermore, the entanglement information header contain a //! entanglement ID (id_AB) which can be used to keep track of the entanglement //! in the network. The entanglement ID is incremented with respect to the //! pair of nodes and who initialized the entanglement (DF). For this reason //! the entanglement ID together with the nodes and the directionality flag //! gives a unique way to identify the entanglement in the network. //! //! ```text //! 0 1 2 3 //! 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | node_A | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | port_A | app_id_A | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | node_B | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | port_B | app_id_B | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | id_AB | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | timestamp | //! | | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | ToG | //! | | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! | goodness | DF | align | //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //! //! Field Length Meaning //! ----- ------ ------- //! node_A 4 bytes IP of this node. //! port_A 2 bytes Port of this node. //! app_id_A 2 bytes App ID of this node. //! node_B 4 bytes IP of other node. //! port_B 2 bytes Port of other node. //! app_id_B 2 bytes App ID of other node. //! id_AB 4 bytes Entanglement ID. //! timestamp 8 bytes Time of creation. //! ToG 8 bytes Time of goodness. //! goodness 2 bytes Goodness (estimate of the fidelity of state). //! DF 1 byte Directionality flag (0=Mid, 1=node_A, 2=node_B). //! align 1 byte 4 byte alignment. //! ``` extern crate serde; use self::serde::de; use std::fmt; use self::serde::de::Visitor; use self::serde::{Deserialize, Deserializer, Serialize, Serializer}; #[macro_use] mod macros; /// # CQC Version /// /// The current supported versions are: 2. /// The currently unsupported versions are: 0, 1. #[repr(u8)] #[derive(Copy, Clone, Debug, PartialEq)] pub enum Version { V2 = 2, } impl Version { /// Convert an 8-bit value to a version value. Returns `None` if the value /// does not correspond to a currently supported version. #[inline] pub fn get(value: u8) -> Option<Version> { let version = match value { 2 => Version::V2, _ => return None, }; Some(version) } } serde_enum_u8!(Version, VersionVisitor, "CQC version"); /// # CQC Header /// /// Every CQC message begins with a CQC header. /// /// ```text /// 0 1 2 3 /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | version | msg_type | app_id | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | length | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// version 1 byte CQC interface version. Current version is 0. /// msg_type 1 byte Message type. /// app_id 2 bytes Application ID. Return messages will be tagged /// appropriately. /// length 4 bytes Total length of the CQC instruction packet. /// ``` /// /// A CQC Command Header MUST follow the CQC Header for the following messages: /// /// - Command /// - Factory /// - GetTime #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct CqcHdr { pub version: Version, pub msg_type: MsgType, pub app_id: u16, pub length: u32, } def_len!(CqcHdr, 8); /// # CQC Header Message Types /// /// The supported message types. They are split into normal types (Tp) and /// error types (Err). /// /// ```text /// Type Name Meaning /// ---- ---- ------- /// 0 Hello Alive check. /// 1 Command Execute a command list. /// 2 Factory Start executing command list repeatedly. /// 3 Expire Qubit has expired. /// 4 Done Command execution done. /// 5 Recv Received qubit. /// 6 EprOk Created EPR pair. /// 7 MeasOut Measurement outcome. /// 8 GetTime Get creation time of qubit. /// 9 InfTime Inform about time. /// 10 NewOk Created new qubit. /// /// 20 General General purpose error (no details). /// 21 NoQubit No more qubits available. /// 22 Unsupp Command sequence not supported. /// 23 Timeout Timeout. /// 24 InUse Qubit already in use. /// 25 Unknown Unknown qubit ID. /// ``` #[derive(Copy, Clone, Debug, PartialEq)] pub enum MsgType { Tp(Tp), Err(Err), } impl From<MsgType> for u8 { fn from(msg_type: MsgType) -> Self { match msg_type { MsgType::Tp(val) => val as u8, MsgType::Err(val) => val as u8, } } } macro_rules! def_is_tp { ($tp: pat, $name: ident) => { #[inline] pub fn $name(&self) -> bool { match self { &MsgType::Tp($tp) => true, _ => false, } } } } macro_rules! def_is_err { ($tp: pat, $name: ident) => { #[inline] pub fn $name(&self) -> bool { match self { &MsgType::Err($tp) => true, _ => false, } } } } impl MsgType { #[inline] pub fn is_tp(&self) -> bool { match self { &MsgType::Tp(_) => true, &MsgType::Err(_) => false, } } #[inline] pub fn is_err(&self) -> bool { match self { &MsgType::Tp(_) => false, &MsgType::Err(_) => true, } } def_is_tp!(Tp::Hello, is_hello); def_is_tp!(Tp::Command, is_command); def_is_tp!(Tp::Factory, is_factory); def_is_tp!(Tp::Expire, is_expire); def_is_tp!(Tp::Done, is_done); def_is_tp!(Tp::Recv, is_recv); def_is_tp!(Tp::EprOk, is_epr_ok); def_is_tp!(Tp::MeasOut, is_measout); def_is_tp!(Tp::GetTime, is_get_time); def_is_tp!(Tp::InfTime, is_inf_time); def_is_tp!(Tp::NewOk, is_new_ok); def_is_err!(Err::General, is_err_general); def_is_err!(Err::NoQubit, is_err_noqubit); def_is_err!(Err::Unsupp, is_err_unsupp); def_is_err!(Err::Timeout, is_err_timeout); def_is_err!(Err::InUse, is_err_inuse); def_is_err!(Err::Unknown, is_err_unknown); /// Convert an 8-bit value to a message type. Returns `None` if the value /// does not correspond to a valid message type. #[inline] pub fn get(value: u8) -> Option<MsgType> { let msg_type = match value { 0 => MsgType::Tp(Tp::Hello), 1 => MsgType::Tp(Tp::Command), 2 => MsgType::Tp(Tp::Factory), 3 => MsgType::Tp(Tp::Expire), 4 => MsgType::Tp(Tp::Done), 5 => MsgType::Tp(Tp::Recv), 6 => MsgType::Tp(Tp::EprOk), 7 => MsgType::Tp(Tp::MeasOut), 8 => MsgType::Tp(Tp::GetTime), 9 => MsgType::Tp(Tp::InfTime), 10 => MsgType::Tp(Tp::NewOk), 20 => MsgType::Err(Err::General), 21 => MsgType::Err(Err::NoQubit), 22 => MsgType::Err(Err::Unsupp), 23 => MsgType::Err(Err::Timeout), 24 => MsgType::Err(Err::InUse), 25 => MsgType::Err(Err::Unknown), _ => return None, }; Some(msg_type) } } impl Serialize for MsgType { #[inline] fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { match self { &MsgType::Tp(tp) => serializer.serialize_u8(tp as u8), &MsgType::Err(err) => serializer.serialize_u8(err as u8), } } } deserialize_enum_u8!(MsgType, MsgTypeVisitor, "CQC message type"); /// # CQC Header Normal Message Types /// /// The supported normal message types. /// /// ```text /// Type Name Meaning /// ---- ---- ------- /// 0 Hello Alive check. /// 1 Command Execute a command list. /// 2 Factory Start executing command list repeatedly. /// 3 Expire Qubit has expired. /// 4 Done Command execution done. /// 5 Recv Received qubit. /// 6 EprOk Created EPR pair. /// 7 MeasOut Measurement outcome. /// 8 GetTime Get creation time of qubit. /// 9 InfTime Inform about time. /// 10 NewOk Created new qubit. /// ``` #[repr(u8)] #[derive(Copy, Clone, Debug, PartialEq)] pub enum Tp { Hello = 0, // Alive check. Command = 1, // Execute a command list. Factory = 2, // Start executing command list repeatedly. Expire = 3, // Qubit has expired. Done = 4, // Command execution done. Recv = 5, // Recevied qubit. EprOk = 6, // Created EPR pair. MeasOut = 7, // Measurement outcome. GetTime = 8, // Get creation time of qubit. InfTime = 9, // Inform about time. NewOk = 10, // Created new qubit. } /// # CQC Header Error Message Types /// /// The supported error message types. /// /// ```text /// Type Name Meaning /// ---- ---- ------- /// 20 General General purpose error (no details). /// 21 NoQubit No more qubits available. /// 22 Unsupp Command sequence not supported. /// 23 Timeout Timeout. /// 24 InUse Qubit already in use. /// 25 Unknown Unknown qubit ID. /// ``` #[repr(u8)] #[derive(Copy, Clone, Debug, PartialEq)] pub enum Err { General = 20, // General purpose error (no details. NoQubit = 21, // No more qubits available. Unsupp = 22, // Command sequence not supported. Timeout = 23, // Timeout. InUse = 24, // Qubit already in use. Unknown = 25, // Unknown qubit ID } /// # CQC Command Header /// /// A CQC Command Header identifies the specific instruction to execute, as /// well as the qubit ID on which to perform this instructions. /// /// A CQC Command Header MUST follow the CQC Header for the following messages: /// /// - Command /// - Factory /// - GetTime /// /// ```text /// 0 1 2 3 /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | qubit_id | instr | options | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// qubit_id 2 bytes Qubit ID to perform the operation on. /// instr 1 byte Instruction to perform. /// options 1 byte Options when executing the command. /// ``` /// /// ## Notify /// /// If the notify option bit is set, each of these commands return a CQC /// message Done indicating that execution has completed. Some commands also /// return additional messages, as described below: /// /// - New: Returns a NewOk reply followed by an Extra Qubit header with the /// qubit ID. /// - Measure(InPlace): Returns a MeasOut message followed by a Measurement /// Outcome header containing the measurement outcome. /// - Recv: Returns a Recv reply followed by an Extra Qubit header with the /// qubit ID. /// - Epr(Recv): Returns an EprOk reply by an Extra Qubit header and an /// Entanglement Information header. #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct CmdHdr { pub qubit_id: u16, pub instr: Cmd, pub options: CmdOpt, } def_len!(CmdHdr, 4); /// # CQC Command Header Instruction Types /// /// The supported CQC instructions. /// /// ```text /// Type Name Meaning /// ---- ---- ------- /// 0 I Identity (do nothing, wait one step). /// 1 New Ask for a new qubit. /// 2 Measure Measure qubit. /// 3 MeasureInPlace Measure qubit in-place. /// 4 Reset Reset qubit to |0>. /// 5 Send Send qubit to another node. /// 6 Recv Ask to receive qubit. /// 7 Epr Create EPR pair with the specified node. /// 8 EprRecv Receive EPR pair. /// /// 10 X Pauli X. /// 11 Z Pauli Z. /// 12 Y Pauli Y. /// 13 T T Gate. /// 14 RotX Rotation over angle around X in pi/256 increments. /// 15 RotY Rotation over angle around Y in pi/256 increments. /// 16 RotZ Rotation over angle around Z in pi/256 increments. /// 17 H Hadamard Gate. /// 18 K K Gate - taking computational to Y eigenbasis. /// /// 20 Cnot CNOT Gate with this as control. /// 21 Cphase CPHASE Gate with this as control. /// ``` #[repr(u8)] #[derive(Copy, Clone, Debug, PartialEq)] pub enum Cmd { I = 0, // Identity (do nothing, wait one step). New = 1, // Ask for a new qubit. Measure = 2, // Measure qubit. MeasureInplace = 3, // Measure qubit in-place. Reset = 4, // Reset qubit to |0>. Send = 5, // Send qubit to another node. Recv = 6, // Ask to receive qubit. Epr = 7, // Create EPR pair with the specified node. EprRecv = 8, // Receive EPR pair. X = 10, // Pauli X. Z = 11, // Pauli Z. Y = 12, // Pauli Y. T = 13, // T Gate. RotX = 14, // Rotation over angle around X in pi/256 increments. RotY = 15, // Rotation over angle around Y in pi/256 increments. RotZ = 16, // Rotation over angle around Z in pi/256 increments. H = 17, // Hadamard Gate. K = 18, // K Gate - taking computational to Y eigenbasis. Cnot = 20, // CNOT Gate with this as control. Cphase = 21, // CPHASE Gate with this as control. } impl Cmd { /// Convert an 8-bit value to a command type. Returns `None` if the value /// does not correspond to a valid command type. #[inline] pub fn get(value: u8) -> Option<Cmd> { let command = match value { 0 => Cmd::I, 1 => Cmd::New, 2 => Cmd::Measure, 3 => Cmd::MeasureInplace, 4 => Cmd::Reset, 5 => Cmd::Send, 6 => Cmd::Recv, 7 => Cmd::Epr, 8 => Cmd::EprRecv, 10 => Cmd::X, 11 => Cmd::Z, 12 => Cmd::Y, 13 => Cmd::T, 14 => Cmd::RotX, 15 => Cmd::RotY, 16 => Cmd::RotZ, 17 => Cmd::H, 18 => Cmd::K, 20 => Cmd::Cnot, 21 => Cmd::Cphase, _ => return None, }; Some(command) } } serde_enum_u8!(Cmd, CmdVisitor, "CQC instruction type"); bitflags! { /// # CQC Command Header options /// /// Command options are set as bit flags. /// /// ```text /// Flag Name Meaning /// ---- ---- ------- /// 0x01 Notify Send a notification when command completes. /// 0x02 Action On if there are actions to execute when done. /// 0x04 Block Block until command is done. /// 0x08 IfThen Execute command after done. /// ``` pub struct CmdOpt: u8 { const NOTIFY = 0x01; const ACTION = 0x02; const BLOCK = 0x04; const IFTHEN = 0x08; } } impl CmdOpt { def_set_flag!(CmdOpt, NOTIFY, set_notify); def_set_flag!(CmdOpt, ACTION, set_action); def_set_flag!(CmdOpt, BLOCK, set_block); def_set_flag!(CmdOpt, IFTHEN, set_ifthen); def_get_flag!(CmdOpt, NOTIFY, get_notify); def_get_flag!(CmdOpt, ACTION, get_action); def_get_flag!(CmdOpt, BLOCK, get_block); def_get_flag!(CmdOpt, IFTHEN, get_ifthen); } serde_option_u8!(CmdOpt, CmdOptVisitor, "command"); /// # CQC Sequence Header /// /// Additional header used to indicate size of a sequence. Used when sending /// multiple commands at once. It tells the backend how many more messages are /// coming. /// /// ```text /// 0 /// 0 1 2 3 4 5 6 7 /// +-+-+-+-+-+-+-+-+ /// | cmd_length | /// +-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// cmd_length 1 byte Length (in bytes) of messages to come. /// ``` #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct SeqHdr { pub cmd_length: u8, } def_len!(SeqHdr, 1); /// # CQC Rotation Header /// /// Additional header used to define the rotation angle of a rotation gate. /// /// ```text /// 0 /// 0 1 2 3 4 5 6 7 /// +-+-+-+-+-+-+-+-+ /// | step | /// +-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// step 1 byte Angle step of rotation (increments of 1/256). /// ``` #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct RotHdr { pub step: u8, } def_len!(RotHdr, 1); /// # CQC Extra Qubit Header /// /// Additional header used to send the qubit_id of a secondary qubit for two /// qubit gates. /// /// ```text /// 0 1 /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | qubit_id | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// qubit_id 2 bytes ID of the target qubit. /// ``` #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct QubitHdr { pub qubit_id: u16, } def_len!(QubitHdr, 2); /// # CQC Communication Header /// /// Additional header used to send to which node to send information to. Used /// in send and EPR commands. /// /// /// ```text /// 0 1 2 3 /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | remote_app_id | remote_port | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | remote_node | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// remote_app_id 2 bytes Remote application ID. /// remote_port 2 bytes Port of the remote node for sending classical /// control info. /// remote_node 4 bytes IP of the remote node (IPv4). /// ``` #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct CommHdr { pub remote_app_id: u16, pub remote_port: u16, pub remote_node: u32, } def_len!(CommHdr, 8); /// # CQC Factory Header /// /// Additional header used to send factory information. Factory commands are /// used to tell the backend to do the following command or a sequence of /// commands multiple times. /// /// ```text /// 0 1 /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | num_iter | options | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// num_iter 1 byte Number of iterations to do the sequence. /// options 1 byte Options when executing the factory. /// ``` #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct FactoryHdr { pub num_iter: u8, pub options: FactoryOpt, } def_len!(FactoryHdr, 2); bitflags! { /// # CQC Factory Header options /// /// Factory options are set as bit flags. /// /// ```text /// Flag Name Meaning /// ---- ---- ------- /// 0x01 Notify Send a notification when command completes. /// 0x04 Block Block until factory is done. /// ``` pub struct FactoryOpt: u8 { const NOTIFY = 0x01; const BLOCK = 0x04; } } impl FactoryOpt { def_set_flag!(FactoryOpt, NOTIFY, set_notify); def_set_flag!(FactoryOpt, BLOCK, set_block); def_get_flag!(FactoryOpt, NOTIFY, get_notify); def_get_flag!(FactoryOpt, BLOCK, get_block); } serde_option_u8!(FactoryOpt, FactoryOptVisitor, "factory"); /// # CQC Measurement Outcome Header /// /// Additional header used to send the outcome of a measurement. /// /// ```text /// 0 /// 0 1 2 3 4 5 6 7 /// +-+-+-+-+-+-+-+-+ /// | meas_out | /// +-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// meas_out 1 byte Measurement outcome. /// ``` #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct MeasOutHdr { pub meas_out: MeasOut, } def_len!(MeasOutHdr, 1); /// # CQC Measurement outcome /// /// There are only two possible outcome values: 0 or 1. #[repr(u8)] #[derive(Copy, Clone, Debug, PartialEq)] pub enum MeasOut { Zero = 0, One = 1, } impl MeasOut { /// Convert an 8-bit value to a measurement outcome value. Returns `None` /// if the value does not correspond to a valid outcome. #[inline] pub fn get(value: u8) -> Option<MeasOut> { let meas_out = match value { 0 => MeasOut::Zero, 1 => MeasOut::One, _ => return None, }; Some(meas_out) } } serde_enum_u8!(MeasOut, MeasOutVisitor, "Measurement Outcome"); /// # CQC Time Info Header /// /// Additional header used to send time information in response to the GetTime /// command. /// /// ```text /// 0 1 2 3 /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | datetime | /// | | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// datetime 8 bytes Time of creation. /// ``` #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct TimeInfoHdr { pub datetime: u64, } def_len!(TimeInfoHdr, 8); /// # CQC Entanglement Information Header /// /// When an EPR-pair is created the CQC Backend will return information about /// the entanglement which can be used in a entanglement management protocol. /// The entanglement information header contains information about the parties /// that share the EPR-pair, the time of creation, how good the entanglement is /// (goodness). Furthermore, the entanglement information header contain a /// entanglement ID (id_AB) which can be used to keep track of the entanglement /// in the network. The entanglement ID is incremented with respect to the /// pair of nodes and who initialized the entanglement (DF). For this reason /// the entanglement ID together with the nodes and the directionality flag /// gives a unique way to identify the entanglement in the network. /// /// ```text /// 0 1 2 3 /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | node_A | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | port_A | app_id_A | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | node_B | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | port_B | app_id_B | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | id_AB | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | timestamp | /// | | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | ToG | /// | | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | goodness | DF | align | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// /// Field Length Meaning /// ----- ------ ------- /// node_A 4 bytes IP of this node. /// port_A 2 bytes Port of this node. /// app_id_A 2 bytes App ID of this node. /// node_B 4 bytes IP of other node. /// port_B 2 bytes Port of other node. /// app_id_B 2 bytes App ID of other node. /// id_AB 4 bytes Entanglement ID. /// timestamp 8 bytes Time of creation. /// ToG 8 bytes Time of goodness. /// goodness 2 bytes Goodness (estimate of the fidelity of state). /// DF 1 byte Directionality flag (0=Mid, 1=node_A, 2=node_B). /// align 1 byte 4 byte alignment. /// ``` #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct EntInfoHdr { pub node_a: u32, pub port_a: u16, pub app_id_a: u16, pub node_b: u32, pub port_b: u16, pub app_id_b: u16, pub id_ab: u32, pub timestamp: u64, pub tog: u64, pub goodness: u16, pub df: u8, pub align: u8, } def_len!(EntInfoHdr, 40); // ---------------------------------------------------------------------------- // Tests. // ---------------------------------------------------------------------------- #[cfg(test)] mod tests { extern crate bincode; use self::bincode::serialize; use super::*; #[test] fn cqc_hdr_ser_size() { let cqc_hdr = CqcHdr { version: Version::V2, msg_type: MsgType::Tp(Tp::Hello), app_id: 0, length: 0, }; assert_eq!(serialize(&cqc_hdr).unwrap().len() as u32, cqc_hdr.len()); } #[test] fn cmd_hdr_ser_size() { let cmd_hdr = CmdHdr { qubit_id: 0, instr: Cmd::I, options: CmdOpt::empty(), }; assert_eq!(serialize(&cmd_hdr).unwrap().len() as u32, cmd_hdr.len()); } #[test] fn seq_hdr_ser_size() { let seq_hdr = SeqHdr { cmd_length: 0 }; assert_eq!(serialize(&seq_hdr).unwrap().len() as u32, seq_hdr.len()); } #[test] fn rot_hdr_ser_size() { let rot_hdr = RotHdr { step: 0 }; assert_eq!(serialize(&rot_hdr).unwrap().len() as u32, rot_hdr.len()); } #[test] fn qubit_hdr_ser_size() { let qubit_hdr = QubitHdr { qubit_id: 0 }; assert_eq!(serialize(&qubit_hdr).unwrap().len() as u32, qubit_hdr.len()); } #[test] fn comm_hdr_ser_size() { let comm_hdr = CommHdr { remote_app_id: 0, remote_node: 0, remote_port: 0, }; assert_eq!(serialize(&comm_hdr).unwrap().len() as u32, comm_hdr.len()); } #[test] fn factory_hdr_ser_size() { let factory_hdr = FactoryHdr { num_iter: 0, options: FactoryOpt::empty(), }; assert_eq!( serialize(&factory_hdr).unwrap().len() as u32, factory_hdr.len() ); } #[test] fn meas_out_hdr_ser_size() { let meas_out_hdr = MeasOutHdr { meas_out: MeasOut::Zero, }; assert_eq!( serialize(&meas_out_hdr).unwrap().len() as u32, meas_out_hdr.len() ); } #[test] fn time_info_hdr_ser_size() { let time_info_hdr = TimeInfoHdr { datetime: 0, }; assert_eq!( serialize(&time_info_hdr).unwrap().len() as u32, time_info_hdr.len() ); } #[test] fn ent_info_hdr_ser_size() { let ent_info_hdr = EntInfoHdr { node_a: 0, port_a: 0, app_id_a: 0, node_b: 0, port_b: 0, app_id_b: 0, id_ab: 0, timestamp: 0, tog: 0, goodness: 0, df: 0, align: 0, }; assert_eq!( serialize(&ent_info_hdr).unwrap().len() as u32, ent_info_hdr.len() ); } }