Skip to content

Commit 3a8ab39

Browse files
committed
Allow to teleport qubits from Java node to Python node
1 parent 97c3412 commit 3a8ab39

4 files changed

Lines changed: 129 additions & 25 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.gluonhq.strange.cqc;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.net.Socket;
6+
7+
public class AppSession {
8+
9+
private Socket socket;
10+
private OutputStream os;
11+
12+
public AppSession() {
13+
14+
}
15+
16+
public OutputStream connect(String host, int port) throws IOException {
17+
System.err.println("Connecting to " + host + ":" + port);
18+
Socket socket = new Socket(host, port);
19+
System.err.println("socket created: " + socket);
20+
this.socket = socket;
21+
this.os = socket.getOutputStream();
22+
return this.os;
23+
}
24+
25+
}

src/main/java/com/gluonhq/strange/cqc/CQCSession.java

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,56 +38,75 @@ public void sendHello() throws IOException {
3838

3939
public int createQubit() throws IOException {
4040
sendCqcHeader(Protocol.CQC_TP_COMMAND, 4);
41-
sendCommandHeader((short) 0, Protocol.CQC_CMD_NEW, (byte) 0);
41+
byte option = Protocol.CQC_OPT_NOTIFY | Protocol.CQC_OPT_BLOCK;
42+
sendCommandHeader((short) 0, Protocol.CQC_CMD_NEW, option);
43+
System.err.println("readmessage");
4244
ResponseMessage msg = readMessage();
45+
System.err.println("readmessage again, expect TP_DONE");
46+
ResponseMessage done = readMessage();
47+
System.err.println("that was a message of type "+done.getType());
4348
return msg.getQubitId();
4449
}
4550

46-
public int createEPR(String name, short port) throws IOException {
51+
// TODO return an EPR (create that class first)
52+
public ResponseMessage createEPR(String name, short port) throws IOException {
4753
sendCqcHeader(Protocol.CQC_TP_COMMAND, 12);
48-
sendCommandHeader((short) 0, Protocol.CQC_CMD_EPR, (byte) 0);
54+
byte option = Protocol.CQC_OPT_NOTIFY | Protocol.CQC_OPT_BLOCK;
55+
sendCommandHeader((short) 0, Protocol.CQC_CMD_EPR, option);
4956
sendCommunicationHeader((short)0, port, 127*256*256*256+1);
57+
System.err.println("readmessage");
5058
ResponseMessage msg = readMessage();
51-
return msg.getQubitId();
59+
System.err.println("readmessage again, expect TP_DONE");
60+
ResponseMessage done = readMessage();
61+
System.err.println("that was a message of type "+done.getType());
62+
return msg;
5263
}
5364

5465
public void applyGate(Gate gate) throws IOException {
55-
sendCqcHeader(Protocol.CQC_TP_COMMAND, 4);
5666
int qid = gate.getMainQubitIndex();
5767
byte cmdByte = 0;
68+
int len = 4;
5869
if (gate instanceof X) {
5970
cmdByte = Protocol.CQC_CMD_X;
6071
} else if (gate instanceof Hadamard) {
6172
cmdByte = Protocol.CQC_CMD_H;
6273
} else if (gate instanceof Cnot) {
6374
cmdByte = Protocol.CQC_CMD_CNOT;
75+
len = 6;
6476
}
65-
System.err.println("Send command to apply gate");
66-
sendCommandHeader((short) qid, cmdByte, (byte) 0);
67-
System.err.println("Sent command done, read reply");
68-
ResponseMessage msg = readMessage();
69-
System.err.println("reply done");
77+
sendCqcHeader(Protocol.CQC_TP_COMMAND, len);
7078

79+
byte option = Protocol.CQC_OPT_NOTIFY | Protocol.CQC_OPT_BLOCK;
80+
System.err.println("Send command to apply gate");
81+
sendCommandHeader((short) qid, cmdByte, option);
82+
if (gate instanceof Cnot) {
83+
sendExtraQubitHeader((short) ((Cnot) gate).getSecondQubit());
84+
}
85+
System.err.println("wait for TP_DONE");
86+
ResponseMessage done = readMessage();
87+
System.err.println("that was a message of type "+done.getType());
88+
if (done.getType() != Protocol.CQC_TP_DONE) {
89+
System.err.println("That wasn't TP_DONE!");
90+
throw new IOException("Got message of type "+done.getType()+" instead of TP_DONE");
91+
}
92+
}
7193

94+
public boolean measure(int qid) throws IOException {
95+
sendCqcHeader(Protocol.CQC_TP_COMMAND, 4);
96+
byte option = Protocol.CQC_OPT_BLOCK;
97+
sendCommandHeader((short) qid, Protocol.CQC_CMD_MEASURE, option);
98+
System.err.println("send measure command, read response");
99+
ResponseMessage done = readMessage();
100+
System.err.println("got measure response");
101+
return done.getMeasurement();
72102
}
73103

74104
public ResponseMessage readMessage() throws IOException {
75105
if (is == null) {
106+
System.err.println("IS NULL!!!\n");
76107
is = socket.getInputStream();
77108
}
78109
ResponseMessage answer = new ResponseMessage(is);
79-
// byte[] msg = new byte[256];
80-
// DataInputStream dis = new DataInputStream(is);
81-
// byte protocol = dis.readByte();
82-
// int len = is.read(msg);
83-
// for (int i = 0; i < len; i ++) {
84-
// System.err.println("b["+i+"] = "+msg[i]);
85-
// }
86-
// System.err.println("Response from CQC has length "+len);
87-
// if (len < 8) {
88-
// throw new IOException ("Can't read message if it has less than 8 bytes (got "+len+")");
89-
// }
90-
// ResponseMessage answer = new ResponseMessage(msg, len);
91110
return answer;
92111
}
93112

@@ -127,6 +146,12 @@ private void sendCommunicationHeader(short appId, short port, int host) throws I
127146
dos.flush();
128147
}
129148

149+
private void sendExtraQubitHeader(short extraQubit) throws IOException {
150+
DataOutputStream dos = new DataOutputStream(os);
151+
dos.writeShort(extraQubit);
152+
dos.flush();
153+
}
154+
130155
private void sendCommand(byte command, short qubit_id, boolean notify, boolean action, boolean block, int length) throws IOException {
131156
sendCqcHeader(Protocol.CQC_TP_COMMAND, length);
132157

src/main/java/com/gluonhq/strange/cqc/Protocol.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ public class Protocol {
66

77
public static final byte CQC_TP_HELLO = 0x0;
88
public static final byte CQC_TP_COMMAND = 0x1;
9+
public static final byte CQC_TP_DONE = 0x4;
910
public static final byte CQC_TP_EPR_OK = 0x6;
11+
public static final byte CQC_TP_MEASOUT = 0x7;
1012
public static final byte CQC_TP_NEW_OK = 0xA;
1113

1214
public static final byte CQC_CMD_NEW = 0x1;
15+
public static final byte CQC_CMD_MEASURE = 0x2;
1316
public static final byte CQC_CMD_EPR = 0x7;
1417
public static final byte CQC_CMD_EPR_RECV = 0x8;
1518
public static final byte CQC_CMD_X = 0xA;
1619
public static final byte CQC_CMD_H = 0x11;
17-
public static final byte CQC_CMD_CNOT = 0x13;
20+
public static final byte CQC_CMD_CNOT = 0x14;
21+
22+
// OPTIONS
23+
public static final byte CQC_OPT_NOTIFY = 0x1;
24+
public static final byte CQC_OPT_ACTION = 0x2;
25+
public static final byte CQC_OPT_BLOCK = 0x4;
26+
1827

1928
}

src/main/java/com/gluonhq/strange/cqc/ResponseMessage.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.gluonhq.strange.cqc;
22

33
import java.io.DataInputStream;
4+
import java.io.IOException;
45
import java.io.InputStream;
56

67
public class ResponseMessage {
78

89
private InputStream is;
910
byte type;
1011
private short qubitId;
12+
private byte measurement;
13+
14+
private short eprOtherPort;
1115

1216
public ResponseMessage(InputStream is) {
1317
this.is = is;
@@ -22,30 +26,71 @@ public byte getType() {
2226
return type;
2327
}
2428

29+
public boolean getMeasurement() {
30+
return (measurement != 0x0);
31+
}
32+
2533
public short getQubitId() {
2634
return this.qubitId;
2735
}
2836

37+
public short getEprOtherPort() {
38+
return eprOtherPort;
39+
}
40+
2941
private void parseInputStream() {
3042
try {
3143
DataInputStream dis = new DataInputStream(is);
3244
byte protocol = dis.readByte();
45+
if (protocol != Protocol.VERSION) {
46+
System.err.println("Wrong protocol version: "+protocol);
47+
throw new IOException("wrong protocol from server response");
48+
}
3349
byte cmd = dis.readByte();
3450
System.err.println("CMD = "+cmd);
51+
this.type = cmd;
3552
short appId = dis.readShort();
3653
int len = dis.readInt();
3754
System.err.println("len = "+len);
3855
if (cmd == Protocol.CQC_TP_NEW_OK) {
3956
short qid = dis.readShort();
4057
System.err.println("qid = "+qid);
4158
this.qubitId = qid;
42-
} else {
43-
System.err.println("payload len = "+len);
59+
} else if (cmd == Protocol.CQC_TP_EPR_OK) {
60+
short qid = dis.readShort();
61+
System.err.println("qid = "+qid);
62+
this.qubitId = qid;
63+
parseEntangledHeader(dis, len -2);
64+
} else if (cmd == Protocol.CQC_TP_MEASOUT) {
65+
this.measurement = dis.readByte();
66+
System.err.println("got measurement: "+this.measurement);
67+
}
68+
else {
69+
System.err.println("ignore payload len = "+len);
4470
byte[] payload = new byte[len];
4571
dis.read(payload);
4672
}
4773
} catch (Throwable t) {
4874
t.printStackTrace();
4975
}
5076
}
77+
78+
private void parseEntangledHeader(DataInputStream dis,int expected) throws IOException {
79+
int myIp = dis.readInt();
80+
short myPort = dis.readShort();
81+
short myAppId = dis.readShort();
82+
83+
int otherId = dis.readInt();
84+
this.eprOtherPort = dis.readShort();
85+
short otherAppId = dis.readShort();
86+
87+
int eaid = dis.readInt();
88+
long timestamp = dis.readLong();
89+
long tog = dis.readLong();
90+
short goodness = dis.readShort();
91+
byte df = dis.readByte();
92+
byte unused = dis.readByte();
93+
// byte[] payload = new byte[expected];
94+
// dis.read(payload);
95+
}
5196
}

0 commit comments

Comments
 (0)