Skip to content

Commit

Permalink
add more CQC implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
johanvos committed Nov 5, 2019
1 parent f93aa9b commit 97c3412
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 27 deletions.
94 changes: 69 additions & 25 deletions src/main/java/com/gluonhq/strange/cqc/CQCSession.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package com.gluonhq.strange.cqc;

import com.gluonhq.strange.Gate;
import com.gluonhq.strange.gate.*;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class CQCSession {

private Socket socket;
private OutputStream os;
private InputStream is;
private short appId;
private final short appId;

public CQCSession() {
appId = getNextAppId();
}

public CQCSession(short id) {
appId = id;
}

public void connect(String host, int port) throws IOException {
System.err.println("Connecting to "+host+":"+port);
Socket socket = new Socket(host, port);
Expand All @@ -29,28 +36,58 @@ public void sendHello() throws IOException {
sendCqcHeader(Protocol.CQC_TP_HELLO, 0);
}

public void createQubit() throws IOException {
sendSimpleCommand(Protocol.CQC_CMD_NEW, (short)0);
public int createQubit() throws IOException {
sendCqcHeader(Protocol.CQC_TP_COMMAND, 4);
sendCommandHeader((short) 0, Protocol.CQC_CMD_NEW, (byte) 0);
ResponseMessage msg = readMessage();
return msg.getQubitId();
}

private void sendComman() {

public int createEPR(String name, short port) throws IOException {
sendCqcHeader(Protocol.CQC_TP_COMMAND, 12);
sendCommandHeader((short) 0, Protocol.CQC_CMD_EPR, (byte) 0);
sendCommunicationHeader((short)0, port, 127*256*256*256+1);
ResponseMessage msg = readMessage();
return msg.getQubitId();
}
public void createEPR(String name) throws IOException {
sendCommand(Protocol.CQC_CMD_EPR,(short)0,true,false, true, 12);
// sendSimpleCommand(Protocol.CQC_CMD_EPR, (short)0);

public void applyGate(Gate gate) throws IOException {
sendCqcHeader(Protocol.CQC_TP_COMMAND, 4);
int qid = gate.getMainQubitIndex();
byte cmdByte = 0;
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;
}
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");


}

public ResponseMessage readMessage() throws IOException {
if (is == null) {
is = socket.getInputStream();
}
byte[] msg = new byte[256];
int len = is.read(msg);
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);
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 All @@ -74,18 +111,25 @@ private void sendCqcHeader(byte type, int len) throws IOException {
dos.flush();
}

private void sendCommandHeader(short qubit_id, byte command, byte option) throws IOException {
DataOutputStream dos = new DataOutputStream(os);
dos.writeShort(qubit_id); // qubit_id
dos.writeByte(command);
dos.writeByte(option);
dos.flush();
}

private void sendCommunicationHeader(short appId, short port, int host) throws IOException {
DataOutputStream dos = new DataOutputStream(os);
dos.writeShort(appId);
dos.writeShort(port);
dos.writeInt(host);
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);
if (command == Protocol.CQC_CMD_EPR) {
DataOutputStream dos = new DataOutputStream(os);
dos.writeShort(0); // qubit_id
dos.writeByte(command);
dos.writeByte(0);
dos.writeShort(1);
dos.writeShort(2);
dos.writeInt(4);
dos.flush();
}


}

Expand Down
6 changes: 5 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,10 +6,14 @@ 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_EPR_OK = 0x6;
public static final byte CQC_TP_EPR_OK = 0x6;
public static final byte CQC_TP_NEW_OK = 0xA;

public static final byte CQC_CMD_NEW = 0x1;
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;

}
39 changes: 38 additions & 1 deletion src/main/java/com/gluonhq/strange/cqc/ResponseMessage.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package com.gluonhq.strange.cqc;

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

public class ResponseMessage {

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

public ResponseMessage(InputStream is) {
this.is = is;
parseInputStream();
}

public ResponseMessage(byte[] msg, int length) {
type = msg[1];
Expand All @@ -11,4 +21,31 @@ public ResponseMessage(byte[] msg, int length) {
public byte getType() {
return type;
}

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

private void parseInputStream() {
try {
DataInputStream dis = new DataInputStream(is);
byte protocol = dis.readByte();
byte cmd = dis.readByte();
System.err.println("CMD = "+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);
byte[] payload = new byte[len];
dis.read(payload);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}

0 comments on commit 97c3412

Please sign in to comment.