-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcqc.c
679 lines (616 loc) · 17.5 KB
/
cqc.c
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include "cqc.h"
/*
* cqc_init
*
* Create and return a cqc_ctx handle.
*
* Arguments:
* app_id ID to use for this application
*/
cqc_ctx *cqc_init(uint16_t app_id)
{
/* Initialize CQC Data structure */
cqc_ctx *cqc = (cqc_ctx *)malloc(sizeof(cqc_ctx));
memset(cqc, 0x00, sizeof(cqc_ctx));
cqc->app_id = app_id;
return cqc;
}
/*
* cqc_connect
*
* Connect to CQC backend.
*
* Arguments:
*
* hostname hostname of the CQC backend
* portno port of the CQC backend
*/
int cqc_connect(cqc_ctx *cqc, char *hostname, int portno)
{
/* Set up connection details */
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("ERROR opening socket");
return CQC_LIB_ERR;
}
struct hostent *server;
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
return CQC_LIB_ERR;
}
struct sockaddr_in serv_addr;
memset((char *) &serv_addr, 0x00, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memmove((char *)&serv_addr.sin_addr.s_addr,
(char *)server->h_addr_list[0],
server->h_length);
serv_addr.sin_port = htons(portno);
/* Now connect to the server */
if (connect(sock,
(struct sockaddr*)&serv_addr,
sizeof(serv_addr)) < 0) {
perror("ERROR - Cannot connect");
return CQC_LIB_ERR;
}
cqc->sockfd = sock;
return CQC_LIB_OK;
}
/*
* cqc_close
*
* Disconnect from the backend.
*/
void cqc_close(cqc_ctx *cqc)
{
close(cqc->sockfd);
}
/*
* cqc_destroy
*
* Free the resources allocated for this cqc_ctx handle (including itself).
*/
void cqc_destroy(cqc_ctx *cqc)
{
free(cqc);
}
/* Utility function to provide string messages for CQC errors. */
static void cqc_error(uint8_t type)
{
switch(type) {
case CQC_ERR_GENERAL:
fprintf(stderr,"CQC ERROR: General error.\n");
break;
case CQC_ERR_NOQUBIT:
fprintf(stderr,"CQC ERROR: No more qubits available.\n");
break;
case CQC_ERR_UNSUPP:
fprintf(stderr,"CQC ERROR: Command not supported.\n");
break;
case CQC_ERR_TIMEOUT:
fprintf(stderr,"CQC ERROR: Timeout.\n");
break;
case CQC_ERR_INUSE:
fprintf(stderr,"CQC ERROR: Qubit already in use.\n");
break;
case CQC_ERR_UNKNOWN:
fprintf(stderr,"CQC ERROR: Unknown qubit ID.");
break;
default:
fprintf(stderr,"CQC ERROR: Unknown error type.\n");
}
return;
}
/*
* send_cqc_header
*
* Prepare and send the top-level CQC header.
*
* Arguments:
* type the message type
* len length of subsequent headers
*/
int send_cqc_header(cqc_ctx *cqc, uint8_t type, uint32_t len)
{
cqcHeader cqcH;
cqcH.version = CQC_VERSION;
cqcH.type = type;
cqcH.app_id = htons(cqc->app_id);
cqcH.length = htonl(len);
/* Send message to the server */
int n = write(cqc->sockfd, &cqcH, CQC_HDR_LENGTH);
if (n < 0) {
perror("ERROR writing to socket");
return CQC_LIB_ERR;
}
return CQC_LIB_OK;
}
/*
* send_cqc_cmd
*
* Build and send the CQC header and the command header.
*
* Arguments:
* command command identifier to be sent
* qubit_id identifier of qubit on which to perform this command
* notify whether to request a DONE upon completion
* action set if there are more actions to execute when done
* block block other instructions until this command is done
* length length of any headers that are to follow
*/
int send_cqc_cmd(cqc_ctx *cqc,
uint8_t command,
uint16_t qubit_id,
bool notify,
bool action,
bool block,
uint32_t length)
{
/* Send CQC message indicating a command */
int rc = send_cqc_header(cqc, CQC_TP_COMMAND, CQC_CMD_HDR_LENGTH + length);
if (rc != CQC_LIB_OK) {
fprintf(stderr, "Failed to send CQC header.\n");
return CQC_LIB_ERR;
}
/* Prepare message for the specific command */
cmdHeader cmdH;
memset(&cmdH, 0x00, sizeof(cmdH));
cmdH.qubit_id = htons(qubit_id);
cmdH.instr = command;
if (notify) {
cmdH.options = cmdH.options | CQC_OPT_NOTIFY;
}
if (action) {
cmdH.options = cmdH.options | CQC_OPT_ACTION;
}
if (block) {
cmdH.options = cmdH.options | CQC_OPT_BLOCK;
}
/* Send message to the server */
int n = write(cqc->sockfd, &cmdH, CQC_CMD_HDR_LENGTH);
if (n < 0) {
perror("ERROR writing to socket");
return CQC_LIB_ERR;
}
return CQC_LIB_OK;
}
/*
* cqc_hello
*
* Sends a HELLO message to the CQC backend.
*/
int cqc_hello(cqc_ctx *cqc)
{
return send_cqc_header(cqc, CQC_TP_HELLO, 0);
}
/*
* cqc_simple_cmd
*
* Executes a simple CQC command (not requiring any additional details).
*
* Arguments:
* command command identifier to be sent
* qubit_id identifier of qubit on which to perform this command
* notify whether to request a DONE upon completion
*/
int cqc_simple_cmd(cqc_ctx *cqc,
uint8_t command,
uint16_t qubit_id,
bool notify)
{
return send_cqc_cmd(cqc, command, qubit_id, notify, false, notify, 0);
}
/*
* cqc_send
*
* Send a qubit to a remote node.
*
* Arguments:
* qubit_id qubit to send
* remote_app_id app id on the remote node to send to
* remote_node address of remote node (IPv4)
* remote_port port for classical control info
*/
int cqc_send(cqc_ctx *cqc,
uint16_t qubit_id,
uint16_t remote_app_id,
uint16_t remote_port,
uint32_t remote_node)
{
int rc = send_cqc_cmd(cqc,
CQC_CMD_SEND,
qubit_id,
true,
false,
false,
CQC_COMM_HDR_LENGTH);
if (rc != CQC_LIB_OK) {
fprintf(stderr, "Failed to send CQC command.\n");
return CQC_LIB_ERR;
}
/* Prepare message for the specific command */
commHeader commH;
memset(&commH, 0x00, sizeof(commH));
commH.remote_app_id = htons(remote_app_id);
commH.remote_port = htons(remote_port);
commH.remote_node = htonl(remote_node);
/* Send message to the server */
int n = write(cqc->sockfd, &commH, CQC_COMM_HDR_LENGTH);
if (n < 0) {
perror("ERROR writing to socket");
return CQC_LIB_ERR;
}
return CQC_LIB_OK;
}
/*
* cqc_recv
*
* Receive a qubit.
*
* Arguments:
* qubit_id (OUT) ID assigned to the received qubit
*/
int cqc_recv(cqc_ctx *cqc, uint16_t *qubit_id)
{
/* Send out request to receive a qubit */
int n = cqc_simple_cmd(cqc, CQC_CMD_RECV, 0, 0);
if (n < 0) {
perror("ERROR - Cannot send receive request");
return CQC_LIB_ERR;
}
/* Now read CQC Header from server response */
cqcHeader reply;
memset(&reply, 0x00, sizeof(reply));
n = read(cqc->sockfd, &reply, CQC_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get reply header");
return CQC_LIB_ERR;
}
if(reply.type != CQC_TP_RECV) {
fprintf(stderr,"ERROR: Expected RECV");
return CQC_LIB_ERR;
}
/* Then read qubit id from notify header */
qubitHeader note;
memset(¬e, 0x00, sizeof(note));
n = read(cqc->sockfd, ¬e, CQC_QUBIT_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get measurement outcome");
return CQC_LIB_ERR;
}
*qubit_id = ntohs(note.qubit_id);
return CQC_LIB_OK;
}
/*
* cqc_measure
*
* Measure a specific qubit. This will block until the reply is received.
*
* Arguments:
* qubit_id qubit to measure
* meas_out (OUT) measurement outcome
*/
int cqc_measure(cqc_ctx *cqc, uint16_t qubit_id, uint8_t *meas_out)
{
/* Send command to perform measurement */
int rc = send_cqc_cmd(cqc,
CQC_CMD_MEASURE,
qubit_id,
false,
false,
false,
CQC_ASSIGN_HDR_LENGTH);
if (rc != CQC_LIB_OK) {
fprintf(stderr, "Failed to send CQC command.\n");
return CQC_LIB_ERR;
}
/* Send the assign header */
assignHeader assign;
memset(&assign, 0x00, sizeof(assign));
int n = write(cqc->sockfd, &assign, CQC_ASSIGN_HDR_LENGTH);
if (n < 0) {
perror("ERROR writing to socket");
return CQC_LIB_ERR;
}
/* Now read CQC Header from server response */
cqcHeader reply;
memset(&reply, 0x00, sizeof(reply));
n = read(cqc->sockfd, &reply, CQC_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get reply header");
return CQC_LIB_ERR;
}
if(reply.type != CQC_TP_MEASOUT) {
fprintf(stderr,"ERROR: Expected MEASOUT, got %u\n", reply.type);
return CQC_LIB_ERR;
}
/* Then read measurement outcome from notify header */
measoutHeader note;
memset(¬e, 0x00, sizeof(note));
n = read(cqc->sockfd, ¬e, CQC_MEASOUT_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get measurement outcome");
return CQC_LIB_ERR;
}
*meas_out = note.meas_out;
return CQC_LIB_OK;
}
/*
* cqc_wait_until_done
*
* Receive certain number of DONE commands before proceeding.
*
* Arguments:
* reps number of replies to wait for
*/
int cqc_wait_until_done(cqc_ctx *cqc, unsigned int reps)
{
for(int _ = 0; _ < reps; ++_) {
/* Now read CQC Header from server response */
cqcHeader reply;
memset(&reply, 0x00, sizeof(reply));
int n = read(cqc->sockfd, &reply, CQC_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get reply header");
return CQC_LIB_ERR;
}
/* Check whether an error occured */
if(reply.type >= CQC_ERR_GENERAL) {
cqc_error(reply.type);
return CQC_LIB_ERR;
}
/* Otherwise check whether it's done */
if(reply.type != CQC_TP_DONE) {
fprintf(stderr,"Unexpected reply of type %d\n", reply.type);
return CQC_LIB_ERR;
}
}
return CQC_LIB_OK;
}
/*
* cqc_wait_until_newok
*
* Wait until qubit creation is confirmed.
*
* Arguments:
* qubit_id (OUT) ID of created qubit
*/
int cqc_wait_until_newok(cqc_ctx *cqc, uint16_t *qubit_id)
{
/* Now read CQC Header from server response */
cqcHeader reply;
memset(&reply, 0x00, sizeof(reply));
int n = read(cqc->sockfd, &reply, CQC_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get reply header");
return CQC_LIB_ERR;
}
/* Check whether an error occured */
if(reply.type >= CQC_ERR_GENERAL) {
cqc_error(reply.type);
return CQC_LIB_ERR;
}
/* Otherwise check whether it's done */
if(reply.type != CQC_TP_NEW_OK) {
fprintf(stderr,"Unexpected reply of type %d, expected %d\n",
reply.type, CQC_TP_NEW_OK);
return CQC_LIB_ERR;
}
/* Then read qubit id from notify header */
qubitHeader note;
memset(¬e, 0x00, sizeof(note));
n = read(cqc->sockfd, ¬e, CQC_QUBIT_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get measurement outcome");
return CQC_LIB_ERR;
}
*qubit_id = ntohs(note.qubit_id);
return CQC_LIB_OK;
}
/*
* cqc_twoqubit
*
* Execute local two qubit gate.
*
* Arguments:
* command command id to execute
* qubit1 number of the first qubit
* qubit2 number of the second qubit
*/
int cqc_twoqubit(cqc_ctx *cqc,
uint8_t command,
uint16_t qubit1,
uint16_t qubit2)
{
int rc = send_cqc_cmd(cqc,
command,
qubit1,
false,
false,
true,
CQC_QUBIT_HDR_LENGTH);
if (rc != CQC_LIB_OK) {
fprintf(stderr, "Failed to send CQC command.\n");
return CQC_LIB_ERR;
}
/* Prepare message for the specific command */
qubitHeader qubitH;
memset(&qubitH, 0x00, sizeof(qubitH));
qubitH.qubit_id = htons(qubit2);
/* Send message to the server */
int n = write(cqc->sockfd, &qubitH, CQC_QUBIT_HDR_LENGTH);
if (n < 0) {
perror("ERROR writing to socket");
return CQC_LIB_ERR;
}
return CQC_LIB_OK;
}
/*
* cqc_ntohll
*
* Convert 64-bit value to host order. Linux needs endian.h, but cannot use
* endian.h on a mac so this is the portable solution.
*/
static uint64_t cqc_ntohll(const uint64_t input)
{
uint8_t val[8];
val[0] = input >> 56;
val[1] = input >> 48;
val[2] = input >> 40;
val[3] = input >> 32;
val[4] = input >> 24;
val[5] = input >> 16;
val[6] = input >> 8;
val[7] = input >> 0;
return *(uint64_t *)val;
}
/*
* cqc_ntoh_epr_hdr
*
* Convert EPR header from network to host order.
*/
static void cqc_ntoh_epr_hdr(entanglementHeader *ent_info)
{
ent_info->node_A = ntohl(ent_info->node_A);
ent_info->port_A = ntohs(ent_info->port_A);
ent_info->app_id_A = ntohs(ent_info->app_id_A);
ent_info->node_B = ntohl(ent_info->node_B);
ent_info->port_B = ntohs(ent_info->port_B);
ent_info->app_id_B = ntohs(ent_info->app_id_B);
ent_info->id_AB = ntohl(ent_info->id_AB);
ent_info->timestamp = cqc_ntohll(ent_info->timestamp);
ent_info->tog = cqc_ntohll(ent_info->tog);
ent_info->goodness = ntohs(ent_info->goodness);
}
/*
* cqc_epr
*
* Generate EPR pair with remote node.
*
* Arguments:
* remote_app_id app id on the remote node
* remote_node address of remote node to receive from (IPv4)
* remote_port port for classical control info
* qubit_id (OUT) ID of local qubit of the EPR pair
* ent_info (OUT) etanglement information
*/
int cqc_epr(cqc_ctx *cqc,
uint16_t remote_app_id,
uint16_t remote_port,
uint32_t remote_node,
uint16_t *qubit_id,
entanglementHeader *ent_info)
{
int rc = send_cqc_cmd(cqc,
CQC_CMD_EPR,
0,
false,
false,
true,
CQC_COMM_HDR_LENGTH);
if (rc != CQC_LIB_OK) {
fprintf(stderr, "Failed to send CQC command.\n");
return CQC_LIB_ERR;
}
/* Prepare message for the specific command */
commHeader commH;
memset(&commH, 0x00, sizeof(commH));
commH.remote_app_id = htons(remote_app_id);
commH.remote_port = htons(remote_port);
commH.remote_node = htonl(remote_node);
/* Send message to the server */
int n = write(cqc->sockfd, &commH, CQC_COMM_HDR_LENGTH);
if (n < 0) {
perror("ERROR writing to socket");
return CQC_LIB_ERR;
}
/* Now read CQC Header from server response */
cqcHeader reply;
memset(&reply, 0x00, sizeof(reply));
n = read(cqc->sockfd, &reply, CQC_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get reply header");
return CQC_LIB_ERR;
}
if (reply.type != CQC_TP_EPR_OK) {
fprintf(stderr, "ERROR: Expected EPR_OK, got %u\n", reply.type);
return CQC_LIB_ERR;
}
/* Then read qubit id from notify header */
qubitHeader note;
memset(¬e, 0x00, sizeof(note));
n = read(cqc->sockfd, ¬e, CQC_QUBIT_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get measurement outcome");
return CQC_LIB_ERR;
}
*qubit_id = ntohs(note.qubit_id);
/* Read the entanglement header */
memset(ent_info, 0x00, sizeof(*ent_info));
n = read(cqc->sockfd, ent_info, CQC_ENT_INFO_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get entanglement info header");
return CQC_LIB_ERR;
}
cqc_ntoh_epr_hdr(ent_info);
return CQC_LIB_OK;
}
/*
* cqc_epr_recv
*
* Receive EPR pair.
*
* qubit_id (OUT) ID of local qubit of the EPR pair
* ent_info (OUT) etanglement information
*/
int cqc_epr_recv(cqc_ctx *cqc,
uint16_t *qubit_id,
entanglementHeader *ent_info)
{
int rc = send_cqc_cmd(cqc,
CQC_CMD_EPR_RECV,
0,
false,
false,
true,
0);
if (rc != CQC_LIB_OK) {
fprintf(stderr, "Failed to send CQC command.\n");
return CQC_LIB_ERR;
}
/* Now read CQC Header from server response */
cqcHeader reply;
memset(&reply, 0x00, sizeof(reply));
int n = read(cqc->sockfd, &reply, CQC_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get reply header");
return CQC_LIB_ERR;
}
if (reply.type != CQC_TP_EPR_OK) {
fprintf(stderr, "ERROR: Expected EPR_OK, got %u\n", reply.type);
return CQC_LIB_ERR;
}
/* Then read qubit id from notify header */
qubitHeader note;
memset(¬e, 0x00, sizeof(note));
n = read(cqc->sockfd, ¬e, CQC_QUBIT_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get measurement outcome");
return CQC_LIB_ERR;
}
*qubit_id = ntohs(note.qubit_id);
/* Read the entanglement header */
memset(ent_info, 0x00, sizeof(*ent_info));
n = read(cqc->sockfd, ent_info, CQC_ENT_INFO_HDR_LENGTH);
if (n < 0) {
perror("ERROR - cannot get entanglement info header");
return CQC_LIB_ERR;
}
cqc_ntoh_epr_hdr(ent_info);
return CQC_LIB_OK;
}