@@ -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
0 commit comments