Skip to content

Commit

Permalink
Allow to teleport qubits from Java node to Python node
Browse files Browse the repository at this point in the history
  • Loading branch information
johanvos committed Nov 5, 2019
1 parent 97c3412 commit 3a8ab39
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 25 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/gluonhq/strange/cqc/AppSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.gluonhq.strange.cqc;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class AppSession {

private Socket socket;
private OutputStream os;

public AppSession() {

}

public OutputStream connect(String host, int port) throws IOException {
System.err.println("Connecting to " + host + ":" + port);
Socket socket = new Socket(host, port);
System.err.println("socket created: " + socket);
this.socket = socket;
this.os = socket.getOutputStream();
return this.os;
}

}
69 changes: 47 additions & 22 deletions src/main/java/com/gluonhq/strange/cqc/CQCSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,75 @@ public void sendHello() throws IOException {

public int createQubit() throws IOException {
sendCqcHeader(Protocol.CQC_TP_COMMAND, 4);
sendCommandHeader((short) 0, Protocol.CQC_CMD_NEW, (byte) 0);
byte option = Protocol.CQC_OPT_NOTIFY | Protocol.CQC_OPT_BLOCK;
sendCommandHeader((short) 0, Protocol.CQC_CMD_NEW, option);
System.err.println("readmessage");
ResponseMessage msg = readMessage();
System.err.println("readmessage again, expect TP_DONE");
ResponseMessage done = readMessage();
System.err.println("that was a message of type "+done.getType());
return msg.getQubitId();
}

public int createEPR(String name, short port) throws IOException {
// TODO return an EPR (create that class first)
public ResponseMessage createEPR(String name, short port) throws IOException {
sendCqcHeader(Protocol.CQC_TP_COMMAND, 12);
sendCommandHeader((short) 0, Protocol.CQC_CMD_EPR, (byte) 0);
byte option = Protocol.CQC_OPT_NOTIFY | Protocol.CQC_OPT_BLOCK;
sendCommandHeader((short) 0, Protocol.CQC_CMD_EPR, option);
sendCommunicationHeader((short)0, port, 127*256*256*256+1);
System.err.println("readmessage");
ResponseMessage msg = readMessage();
return msg.getQubitId();
System.err.println("readmessage again, expect TP_DONE");
ResponseMessage done = readMessage();
System.err.println("that was a message of type "+done.getType());
return msg;
}

public void applyGate(Gate gate) throws IOException {
sendCqcHeader(Protocol.CQC_TP_COMMAND, 4);
int qid = gate.getMainQubitIndex();
byte cmdByte = 0;
int len = 4;
if (gate instanceof X) {
cmdByte = Protocol.CQC_CMD_X;
} else if (gate instanceof Hadamard) {
cmdByte = Protocol.CQC_CMD_H;
} else if (gate instanceof Cnot) {
cmdByte = Protocol.CQC_CMD_CNOT;
len = 6;
}
System.err.println("Send command to apply gate");
sendCommandHeader((short) qid, cmdByte, (byte) 0);
System.err.println("Sent command done, read reply");
ResponseMessage msg = readMessage();
System.err.println("reply done");
sendCqcHeader(Protocol.CQC_TP_COMMAND, len);

byte option = Protocol.CQC_OPT_NOTIFY | Protocol.CQC_OPT_BLOCK;
System.err.println("Send command to apply gate");
sendCommandHeader((short) qid, cmdByte, option);
if (gate instanceof Cnot) {
sendExtraQubitHeader((short) ((Cnot) gate).getSecondQubit());
}
System.err.println("wait for TP_DONE");
ResponseMessage done = readMessage();
System.err.println("that was a message of type "+done.getType());
if (done.getType() != Protocol.CQC_TP_DONE) {
System.err.println("That wasn't TP_DONE!");
throw new IOException("Got message of type "+done.getType()+" instead of TP_DONE");
}
}

public boolean measure(int qid) throws IOException {
sendCqcHeader(Protocol.CQC_TP_COMMAND, 4);
byte option = Protocol.CQC_OPT_BLOCK;
sendCommandHeader((short) qid, Protocol.CQC_CMD_MEASURE, option);
System.err.println("send measure command, read response");
ResponseMessage done = readMessage();
System.err.println("got measure response");
return done.getMeasurement();
}

public ResponseMessage readMessage() throws IOException {
if (is == null) {
System.err.println("IS NULL!!!\n");
is = socket.getInputStream();
}
ResponseMessage answer = new ResponseMessage(is);
// byte[] msg = new byte[256];
// DataInputStream dis = new DataInputStream(is);
// byte protocol = dis.readByte();
// int len = is.read(msg);
// for (int i = 0; i < len; i ++) {
// System.err.println("b["+i+"] = "+msg[i]);
// }
// System.err.println("Response from CQC has length "+len);
// if (len < 8) {
// throw new IOException ("Can't read message if it has less than 8 bytes (got "+len+")");
// }
// ResponseMessage answer = new ResponseMessage(msg, len);
return answer;
}

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

private void sendExtraQubitHeader(short extraQubit) throws IOException {
DataOutputStream dos = new DataOutputStream(os);
dos.writeShort(extraQubit);
dos.flush();
}

private void sendCommand(byte command, short qubit_id, boolean notify, boolean action, boolean block, int length) throws IOException {
sendCqcHeader(Protocol.CQC_TP_COMMAND, length);

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/gluonhq/strange/cqc/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ public class Protocol {

public static final byte CQC_TP_HELLO = 0x0;
public static final byte CQC_TP_COMMAND = 0x1;
public static final byte CQC_TP_DONE = 0x4;
public static final byte CQC_TP_EPR_OK = 0x6;
public static final byte CQC_TP_MEASOUT = 0x7;
public static final byte CQC_TP_NEW_OK = 0xA;

public static final byte CQC_CMD_NEW = 0x1;
public static final byte CQC_CMD_MEASURE = 0x2;
public static final byte CQC_CMD_EPR = 0x7;
public static final byte CQC_CMD_EPR_RECV = 0x8;
public static final byte CQC_CMD_X = 0xA;
public static final byte CQC_CMD_H = 0x11;
public static final byte CQC_CMD_CNOT = 0x13;
public static final byte CQC_CMD_CNOT = 0x14;

// OPTIONS
public static final byte CQC_OPT_NOTIFY = 0x1;
public static final byte CQC_OPT_ACTION = 0x2;
public static final byte CQC_OPT_BLOCK = 0x4;


}
49 changes: 47 additions & 2 deletions src/main/java/com/gluonhq/strange/cqc/ResponseMessage.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.gluonhq.strange.cqc;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ResponseMessage {

private InputStream is;
byte type;
private short qubitId;
private byte measurement;

private short eprOtherPort;

public ResponseMessage(InputStream is) {
this.is = is;
Expand All @@ -22,30 +26,71 @@ public byte getType() {
return type;
}

public boolean getMeasurement() {
return (measurement != 0x0);
}

public short getQubitId() {
return this.qubitId;
}

public short getEprOtherPort() {
return eprOtherPort;
}

private void parseInputStream() {
try {
DataInputStream dis = new DataInputStream(is);
byte protocol = dis.readByte();
if (protocol != Protocol.VERSION) {
System.err.println("Wrong protocol version: "+protocol);
throw new IOException("wrong protocol from server response");
}
byte cmd = dis.readByte();
System.err.println("CMD = "+cmd);
this.type = cmd;
short appId = dis.readShort();
int len = dis.readInt();
System.err.println("len = "+len);
if (cmd == Protocol.CQC_TP_NEW_OK) {
short qid = dis.readShort();
System.err.println("qid = "+qid);
this.qubitId = qid;
} else {
System.err.println("payload len = "+len);
} else if (cmd == Protocol.CQC_TP_EPR_OK) {
short qid = dis.readShort();
System.err.println("qid = "+qid);
this.qubitId = qid;
parseEntangledHeader(dis, len -2);
} else if (cmd == Protocol.CQC_TP_MEASOUT) {
this.measurement = dis.readByte();
System.err.println("got measurement: "+this.measurement);
}
else {
System.err.println("ignore payload len = "+len);
byte[] payload = new byte[len];
dis.read(payload);
}
} catch (Throwable t) {
t.printStackTrace();
}
}

private void parseEntangledHeader(DataInputStream dis,int expected) throws IOException {
int myIp = dis.readInt();
short myPort = dis.readShort();
short myAppId = dis.readShort();

int otherId = dis.readInt();
this.eprOtherPort = dis.readShort();
short otherAppId = dis.readShort();

int eaid = dis.readInt();
long timestamp = dis.readLong();
long tog = dis.readLong();
short goodness = dis.readShort();
byte df = dis.readByte();
byte unused = dis.readByte();
// byte[] payload = new byte[expected];
// dis.read(payload);
}
}

0 comments on commit 3a8ab39

Please sign in to comment.