def test_001_t (self):
        src = analog.sig_source_c(32000, analog.GR_COS_WAVE,
                                  12500, 1)

        detector = inspector.signal_detector_cvf(32000,  1024,
                    firdes.WIN_BLACKMAN_hARRIS, -80, 0.6, False, 0.5, 0.0001)
        dst1 = blocks.null_sink(gr.sizeof_float*1024)
        dst2 = blocks.null_sink(gr.sizeof_float*1024)
        msg_dst = blocks.message_debug()
        self.tb.connect(src, detector)
        self.tb.connect((detector, 0), dst1)
        self.tb.connect((detector, 1), dst2)
        self.tb.msg_connect((detector, 'map_out'), (msg_dst, 'store'))
        self.tb.start()
        time.sleep(0.5)
        self.tb.stop()
        self.tb.wait()

        msg = msg_dst.get_message(1)
        res_vector = numpy.empty([0, 2])
        for i in range(pmt.length(msg)):
            row = pmt.vector_ref(msg, i)
            res_vector = numpy.vstack((res_vector, numpy.array(
                [pmt.f32vector_ref(row, 0), pmt.f32vector_ref(row, 1)]
            )))

        self.assertAlmostEqual(12500.0, res_vector[0][0], delta=100)
        self.assertAlmostEqual(0.0, res_vector[0][1], delta=200)
    def msg_handler_analyzed_data_in(self, msg):
        print("in msg_handler_analyzed_data_in")
        self.lock()
        gate_params = pmt.vector_ref(msg, 0)
        gate_type_PMT = pmt.dict_ref(
            gate_params,
            pmt.from_float(
                quantum_gate_param_type.quantum_gate_param_type.GATE_TYPE),
            pmt.PMT_NIL)
        if (pmt.eq(gate_type_PMT, pmt.PMT_NIL)):
            return

        gate_type = pmt.to_float(gate_type_PMT)
        print("gate_params.gate_type=" + str(gate_type))

        qubit_id_PMT = pmt.dict_ref(
            gate_params,
            pmt.from_float(
                quantum_gate_param_type.quantum_gate_param_type.QUBIT_ID),
            pmt.PMT_NIL)
        if (pmt.eq(qubit_id_PMT, pmt.PMT_NIL)):
            return
        qubit_id = pmt.to_float(qubit_id_PMT)
        print("gate_params.qubit_id=" + str(qubit_id))

        if (gate_type == quantum_gate_type.quantum_gate_type.X):
            print("in msg_handler_analyzed_data_in X gate")
            #回路を作る
            RO_STATE = self._qubit_stat_map[qubit_id]
            if (float(RO_STATE.angle) == 0.0):
                RO_STATE.angle = 180.0
            else:
                RO_STATE.angle = 0.0
            self._qubit_stat_map[qubit_id] = RO_STATE
        elif (gate_type == quantum_gate_type.quantum_gate_type.RO):
            print("in msg_handler_analyzed_data_in RO")
            #回路を実行する
            RO_STATE = self._qubit_stat_map[qubit_id]
            SIM_msg = pmt.make_dict()
            SIM_msg = pmt.dict_add(
                SIM_msg,
                pmt.from_float(
                    quantum_qubit_param_type.quantum_qubit_param_type.ID),
                pmt.from_float(qubit_id))
            SIM_msg = pmt.dict_add(
                SIM_msg,
                pmt.from_float(
                    quantum_qubit_param_type.quantum_qubit_param_type.ANGLE),
                pmt.from_float(float(RO_STATE.angle)))
            SIM_msg = pmt.dict_add(
                SIM_msg,
                pmt.from_float(
                    quantum_qubit_param_type.quantum_qubit_param_type.STATE),
                pmt.from_float(quantum_qubit_RO_state_type.
                               quantum_qubit_RO_state_type.START))
            self.message_port_pub(pmt.intern('simulated_data'), SIM_msg)
            RO_STATE.state = quantum_qubit_RO_state_type.quantum_qubit_RO_state_type.START
            self._qubit_stat_map[qubit_id] = RO_STATE
        self.unlock()
Beispiel #3
0
    def test_000(self):
        num_msgs = 10
        msg_interval = 1000
        msg_list = []
        for i in range(num_msgs):
            msg_list.append(pmt.from_long(i))

        # Create vector source with dummy data to trigger messages
        src_data = []
        for i in range(num_msgs * msg_interval):
            src_data.append(float(i))
        src = blocks.vector_source_f(src_data, False)
        msg_gen = message_generator(msg_list, msg_interval)
        msg_cons = message_consumer()

        # Connect vector source to message gen
        self.tb.connect(src, msg_gen)

        # Connect message generator to message consumer
        self.tb.msg_connect(msg_gen, 'out_port', msg_cons, 'in_port')

        # Verify that the messgae port query functions work
        self.assertEqual(
            pmt.symbol_to_string(pmt.vector_ref(msg_gen.message_ports_out(),
                                                0)), 'out_port')
        self.assertEqual(
            pmt.symbol_to_string(pmt.vector_ref(msg_cons.message_ports_in(),
                                                0)), 'in_port')

        # Run to verify message passing
        self.tb.start()

        # Wait for all messages to be sent
        while msg_gen.msg_ctr < num_msgs:
            time.sleep(0.5)
        self.tb.stop()
        self.tb.wait()

        # Verify that the message consumer got all the messages
        self.assertEqual(num_msgs, len(msg_cons.msg_list))
        for i in range(num_msgs):
            self.assertTrue(pmt.equal(msg_list[i], msg_cons.msg_list[i]))
    def test_000(self):
        num_msgs = 10
        msg_interval = 1000
        msg_list = []
        for i in range(num_msgs):
            msg_list.append(pmt.from_long(i))

        # Create vector source with dummy data to trigger messages
        src_data = []
        for i in range(num_msgs*msg_interval):
            src_data.append(float(i))
        src = blocks.vector_source_f(src_data, False)
        msg_gen = message_generator(msg_list, msg_interval)
        msg_cons = message_consumer()
        
        # Connect vector source to message gen
        self.tb.connect(src, msg_gen)
        
        # Connect message generator to message consumer
        self.tb.msg_connect(msg_gen, 'out_port', msg_cons, 'in_port')

        # Verify that the messgae port query functions work
        self.assertEqual(pmt.symbol_to_string(pmt.vector_ref(
                    msg_gen.message_ports_out(), 0)), 'out_port')
        self.assertEqual(pmt.symbol_to_string(pmt.vector_ref(
                    msg_cons.message_ports_in(), 0)), 'in_port')
        
        # Run to verify message passing
        self.tb.start()

        # Wait for all messages to be sent
        while msg_gen.msg_ctr < num_msgs:
            time.sleep(0.5)
        self.tb.stop()
        self.tb.wait()               
        
        # Verify that the message consumer got all the messages
        self.assertEqual(num_msgs, len(msg_cons.msg_list))
        for i in range(num_msgs):
            self.assertTrue(pmt.equal(msg_list[i], msg_cons.msg_list[i]))
    def occ_handler_method(self, msg):
        occvec = []

        for i in range(0, self.num_split):
            occvec.append(pmt.to_double(pmt.vector_ref(msg, i)))

        pmt_to_send = pmt.make_dict()

        curtime = strftime("%Y-%m-%d %H:%M:%S", gmtime())
        attributes = pmt.make_dict()
        attributes = pmt.dict_add(attributes,
                                  pmt.string_to_symbol("center_freq"),
                                  pmt.from_double(self.center_freq))
        attributes = pmt.dict_add(attributes, pmt.string_to_symbol("occ"),
                                  pmt.init_f32vector(self.num_split, occvec))
        attributes = pmt.dict_add(attributes,
                                  pmt.string_to_symbol("bandwidth"),
                                  pmt.from_double(self.bw))
        attributes = pmt.dict_add(attributes, pmt.string_to_symbol("timetag"),
                                  pmt.intern(curtime))
        attributes = pmt.dict_add(
            attributes, pmt.string_to_symbol("noise_floor"),
            pmt.init_f32vector(self.num_split, self.noise_floor))
        attributes = pmt.dict_add(attributes, pmt.string_to_symbol("nodeid"),
                                  pmt.from_long(self.nodeid))
        attributes = pmt.dict_add(attributes, pmt.string_to_symbol("latitude"),
                                  pmt.from_double(self.latitude))
        attributes = pmt.dict_add(attributes,
                                  pmt.string_to_symbol("longitude"),
                                  pmt.from_double(self.longitude))

        command = pmt.make_dict()
        command = pmt.dict_add(command, pmt.string_to_symbol("table"),
                               pmt.string_to_symbol("spectruminfo"))
        command = pmt.dict_add(command, pmt.string_to_symbol("attributes"),
                               attributes)

        pmt_to_send = pmt.dict_add(pmt_to_send, pmt.string_to_symbol("INSERT"),
                                   command)

        serialized_pmt = pmt.serialize_str(pmt_to_send)

        #print pmt_to_send

        UDP_IP = self.host
        UDP_PORT = self.port

        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.sendto(serialized_pmt, (UDP_IP, UDP_PORT))
    def test_001_t(self):
        src1 = analog.sig_source_c(32000, analog.GR_COS_WAVE, 12500, 3)

        src2 = analog.sig_source_c(32000, analog.GR_COS_WAVE, 9800, 3)

        add = blocks.add_cc()
        detector = inspector.signal_detector_cvf(32000, 4096,
                                                 firdes.WIN_BLACKMAN_hARRIS,
                                                 -80, 0.6, False, 0.5, 0.001)
        dst1 = blocks.null_sink(gr.sizeof_float * 4096)
        msg_dst = blocks.message_debug()

        # connections
        self.tb.connect(src1, (add, 0))
        self.tb.connect(src2, (add, 1))
        self.tb.connect(add, detector)
        self.tb.connect((detector, 0), dst1)
        self.tb.msg_connect((detector, 'map_out'), (msg_dst, 'store'))
        self.tb.start()
        time.sleep(0.5)
        self.tb.stop()
        self.tb.wait()

        # take most recent message
        msg = msg_dst.get_message(msg_dst.num_messages() - 1)
        res_vector = numpy.empty([0, 2])
        for i in range(pmt.length(msg)):
            row = pmt.vector_ref(msg, i)
            res_vector = numpy.vstack(
                (res_vector,
                 numpy.array(
                     [pmt.f32vector_ref(row, 0),
                      pmt.f32vector_ref(row, 1)])))

        self.assertAlmostEqual(9800.0, res_vector[0][0], delta=50)
        self.assertAlmostEqual(0.0, res_vector[0][1], delta=100)
        self.assertAlmostEqual(12500.0, res_vector[1][0], delta=50)
        self.assertAlmostEqual(0.0, res_vector[1][1], delta=100)
    def test_001_t (self):
        src1 = analog.sig_source_c(32000, analog.GR_COS_WAVE,
                                  12500, 3)

        src2 = analog.sig_source_c(32000, analog.GR_COS_WAVE,
                                   9800, 3)

        add = blocks.add_cc()
        detector = inspector.signal_detector_cvf(32000,  4096,
                    firdes.WIN_BLACKMAN_hARRIS, -80, 0.6, False, 0.5, 0.001)
        dst1 = blocks.null_sink(gr.sizeof_float*4096)
        msg_dst = blocks.message_debug()

        #connections
        self.tb.connect(src1, (add, 0))
        self.tb.connect(src2, (add, 1))
        self.tb.connect(add, detector)
        self.tb.connect((detector, 0), dst1)
        self.tb.msg_connect((detector, 'map_out'), (msg_dst, 'store'))
        self.tb.start()
        time.sleep(0.5)
        self.tb.stop()
        self.tb.wait()

        #take most recent message
        msg = msg_dst.get_message(msg_dst.num_messages()-1)
        res_vector = numpy.empty([0, 2])
        for i in range(pmt.length(msg)):
            row = pmt.vector_ref(msg, i)
            res_vector = numpy.vstack((res_vector, numpy.array(
                [pmt.f32vector_ref(row, 0), pmt.f32vector_ref(row, 1)]
            )))

        self.assertAlmostEqual(9800.0, res_vector[0][0], delta=50)
        self.assertAlmostEqual(0.0, res_vector[0][1], delta=100)
        self.assertAlmostEqual(12500.0, res_vector[1][0], delta=50)
        self.assertAlmostEqual(0.0, res_vector[1][1], delta=100)
  def __init__(self, buf, len, debug, d_mac_id, d_seq_nr, no_self_loop): 

	self.debug 	= debug
	self.buf   	= buf
        self.len   	= len
	self.d_seq_nr	= d_seq_nr
	self.d_mac_id	= d_mac_id
	self.no_self_loop  = no_self_loop

	if self.no_self_loop:
	        #Insert an id here to check for self routing. This makes the packet non standard.
	        d_msg.insert(0, self.d_mac_id)	
		d_msg.insert(1, 11 + self.len + 2) 

		#FCF
		d_msg.insert(2,0x41)
		d_msg.insert(3,0x88)

		#seq nr
		d_msg.insert(4,++self.d_seq_nr)

		#addr info
		d_msg.insert(5,0xcd)
		d_msg.insert(6,0xab)
		d_msg.insert(7,0xff)
		d_msg.insert(8,0xff)
		d_msg.insert(9,0x40)
		d_msg.insert(10,0xe8)
	        #Copy the data here.
		if (pmt.is_vector(buf)): 
			for i in range(pmt.length(buf)):	
				d_msg.insert(10+i,pmt.to_long(pmt.vector_ref(buf,i)))
		elif (pmt.is_uniform_vector(buf)):
			d_msg.extend(pmt.u8vector_elements(buf))	
		else:
			bufString = pmt.symbol_to_string(buf)
			#print "pmt.symbol_to_string(buf): ", bufString
			#print "pmt.length(buf): ", pmt.length(buf)
			bytes = map(ord,bufString)
			#print "map(ord,buf): ", bytes
			d_msg.extend(bytes)

	        #Compute the CRC over the whole packet (excluding the CRC bytes themselves)
		crc = crc16(d_msg, self.len + 11) 
	        #if self.debug: print  "#### CRC at Transmission: #### ", crc.get_crc() 
       
       		#CRC goes on the end.
		d_msg.insert(11+ self.len, crc.get_crc() & 0xFF)
		d_msg.insert(12+ self.len, crc.get_crc() >> 8)

		d_msg_len = 11  + self.len + 2
		print 
		print
		if self.debug: print "d_msg: ", d_msg
		print
		print
	else:
	        #Preamble length + CRC length ( CRC at the end)
		d_msg.insert(0, 10 + self.len  + 2)

		#FCF
		d_msg.insert(1,0x41)
		d_msg.insert(2,0x88)

		#seq nr
		d_msg.insert(3,++self.d_seq_nr)

		#addr info
		d_msg.insert(4,0xcd)
		d_msg.insert(5,0xab)
		d_msg.insert(6,0xff)
		d_msg.insert(7,0xff)
		d_msg.insert(8,0x40)
		d_msg.insert(9,0xe8)

	        #Copy the data here.
		d_msg.extend(pmt.u8vector_elements(buf))
	        #Compute the CRC over the whole packet (excluding the CRC bytes themselves)
	        crc = crc16(d_msg, self.len + 10)
		if self.debug: print  "#### CRC at Transmission: #### ", crc.get_crc() 

		d_msg.insert(10 + self.len, crc.get_crc() & 0xFF)
		d_msg.insert(11 + self.len, crc.get_crc() >> 8)

	        d_msg_len = 10  + self.len + 2 # Preamble + Data + CRC

	if self.debug: print  " msg len ", d_msg_len, " len ", self.len, "\n"
    def msg_handler_analyzed_data_in_circuit(self, msg):
        print("in msg_handler_analyzed_data_in")
        self.lock()
        gate_params = pmt.vector_ref(msg, 0)
        gate_type_PMT = pmt.dict_ref(
            gate_params,
            pmt.from_float(
                quantum_gate_param_type.quantum_gate_param_type.GATE_TYPE),
            pmt.PMT_NIL)
        if (pmt.eq(gate_type_PMT, pmt.PMT_NIL)):
            return

        gate_type = pmt.to_float(gate_type_PMT)
        print("gate_params.gate_type=" + str(gate_type))

        qubit_id_PMT = pmt.dict_ref(
            gate_params,
            pmt.from_float(
                quantum_gate_param_type.quantum_gate_param_type.QUBIT_ID),
            pmt.PMT_NIL)
        if (pmt.eq(qubit_id_PMT, pmt.PMT_NIL)):
            return
        qubit_id = pmt.to_float(qubit_id_PMT)
        print("gate_params.qubit_id=" + str(qubit_id))

        if (gate_type == quantum_gate_type.quantum_gate_type.X):
            print("in msg_handler_analyzed_data_in X gate")
            self._qubit_circuit.add_gate("RX",
                                         targets=qubit_id,
                                         arg_value=pi,
                                         index=this._qcircuit_index_cnt)

        elif (gate_type == quantum_gate_type.quantum_gate_type.Y):
            print("in msg_handler_analyzed_data_in Y gate")
            self._qubit_circuit.add_gate("RY",
                                         targets=qubit_id,
                                         arg_value=pi,
                                         index=this._qcircuit_index_cnt)

        elif (gate_type == quantum_gate_type.quantum_gate_type.Z):
            print("in msg_handler_analyzed_data_in Z gate")
            self._qubit_circuit.add_gate("RZ",
                                         targets=qubit_id,
                                         arg_value=pi,
                                         index=this._qcircuit_index_cnt)

        elif (gate_type == quantum_gate_type.quantum_gate_type.H):
            print("in msg_handler_analyzed_data_in H gate")
            self._qubit_circuit.add_gate("hadamard_transform",
                                         targets=qubit_id,
                                         index=this._qcircuit_index_cnt)

        elif (gate_type == quantum_gate_type.quantum_gate_type.S):
            print("in msg_handler_analyzed_data_in S gate")
            self._qubit_circuit.add_gate("RZ",
                                         targets=qubit_id,
                                         arg_value=pi / 2,
                                         index=this._qcircuit_index_cnt)

        elif (gate_type == quantum_gate_type.quantum_gate_type.T):
            print("in msg_handler_analyzed_data_in T gate")
            self._qubit_circuit.add_gate("RZ",
                                         targets=qubit_id,
                                         arg_value=pi / 4,
                                         index=this._qcircuit_index_cnt)

        elif (gate_type == quantum_gate_type.quantum_gate_type.INIT):
            print("in msg_handler_analyzed_data_in INIT gate")
            pass

        elif (gate_type == quantum_gate_type.quantum_gate_type.CNOT):
            print("in msg_handler_analyzed_data_in CNOT gate")
            self._qubit_circuit.add_gate("CNOT", controls=[0], targets=[1])

        elif (gate_type == quantum_gate_type.quantum_gate_type.JUNC):
            print("in msg_handler_analyzed_data_in JUNC gate")
            pass

        elif (gate_type == quantum_gate_type.quantum_gate_type.RO):
            print("in msg_handler_analyzed_data_in RO")
            #回路を実行する
            RO_STATE = self._qubit_stat_map[qubit_id]
            SIM_msg = pmt.make_dict()
            SIM_msg = pmt.dict_add(
                SIM_msg,
                pmt.from_float(
                    quantum_qubit_param_type.quantum_qubit_param_type.ID),
                pmt.from_float(qubit_id))
            SIM_msg = pmt.dict_add(
                SIM_msg,
                pmt.from_float(
                    quantum_qubit_param_type.quantum_qubit_param_type.ANGLE),
                pmt.from_float(float(RO_STATE.angle)))
            SIM_msg = pmt.dict_add(
                SIM_msg,
                pmt.from_float(
                    quantum_qubit_param_type.quantum_qubit_param_type.STATE),
                pmt.from_float(quantum_qubit_RO_state_type.
                               quantum_qubit_RO_state_type.START))
            self.message_port_pub(pmt.intern('simulated_data'), SIM_msg)
            RO_STATE.state = quantum_qubit_RO_state_type.quantum_qubit_RO_state_type.START
            self._qubit_stat_map[qubit_id] = RO_STATE
        self.unlock()
def pmt_to_tracked_peaks(pmt_vector):
    return [
        tracked_peak.from_pmt(pmt.vector_ref(pmt_vector, i))
        for i in range(pmt.length(pmt_vector))
    ]
 def gps_handler_method(self, msg):
     self.latitude = pmt.to_double(pmt.vector_ref(msg, 0))
     self.longitude = pmt.to_double(pmt.vector_ref(msg, 1))
    def noise_floor_handler_method(self, msg):
        nfvec = []

        for i in range(0, self.num_split):
            nfvec.append(pmt.to_double(pmt.vector_ref(msg, i)))
        self.noise_floor = copy.copy(nfvec)