Beispiel #1
0
    def test_memory_int_out(self):
        """Test that memory bitstrings are extracted properly without header."""
        raw_memory = ['0x0', '0x0', '0x2', '0x2', '0x2', '0x2', '0x2']
        data = models.ExperimentResultData(memory=raw_memory)
        exp_result = models.ExperimentResult(shots=14, success=True, meas_level=2,
                                             memory=True, data=data)
        result = Result(results=[exp_result], **self.base_result_args)

        with self.assertRaises(Exception) as context:
            result.get_memory(99)
        self.assertEqual('Result for experiment "99" could not be found.',
                         context.exception.message)
    def test_meas_level_0_single(self):
        """Test measurement level 0 single result."""
        # 3 qubits
        raw_memory = [
            [[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]],
             [[1.0, 0.0], [1.0, 0.0], [1.0, 0.0]]],
            [[[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]],
             [[1.0, 0.0], [1.0, 0.0], [1.0, 0.0]]],
        ]
        processed_memory = np.array(
            [[[1.0j, 1.0j, 1.0j], [1.0, 1.0, 1.0]],
             [[1.0j, 1.0j, 1.0j], [1.0, 1.0, 1.0]]],
            dtype=np.complex_,
        )
        data = models.ExperimentResultData(memory=raw_memory)
        exp_result = models.ExperimentResult(shots=2,
                                             success=True,
                                             meas_level=0,
                                             meas_return="single",
                                             data=data)
        result = Result(results=[exp_result], **self.base_result_args)
        memory = result.get_memory(0)

        self.assertEqual(memory.shape, (2, 2, 3))
        self.assertEqual(memory.dtype, np.complex_)
        np.testing.assert_almost_equal(memory, processed_memory)
    def test_memory_counts_header(self):
        """Test that memory bitstrings are extracted properly with header."""
        raw_memory = ["0x0", "0x0", "0x2", "0x2", "0x2", "0x2", "0x2"]
        no_header_processed_memory = [
            "0 0 00",
            "0 0 00",
            "0 0 10",
            "0 0 10",
            "0 0 10",
            "0 0 10",
            "0 0 10",
        ]
        data = models.ExperimentResultData(memory=raw_memory)
        exp_result_header = QobjExperimentHeader(creg_sizes=[["c0", 2],
                                                             ["c0", 1],
                                                             ["c1", 1]],
                                                 memory_slots=4)
        exp_result = models.ExperimentResult(shots=14,
                                             success=True,
                                             meas_level=2,
                                             memory=True,
                                             data=data,
                                             header=exp_result_header)
        result = Result(results=[exp_result], **self.base_result_args)

        self.assertEqual(result.get_memory(0), no_header_processed_memory)
Beispiel #4
0
    def test_memory_counts_no_header(self):
        """Test that memory bitstrings are extracted properly without header."""
        raw_memory = ['0x0', '0x0', '0x2', '0x2', '0x2', '0x2', '0x2']
        no_header_processed_memory = [bin(int(bs[2:], 16))[2:] for bs in raw_memory]
        data = models.ExperimentResultData(memory=raw_memory)
        exp_result = models.ExperimentResult(shots=14, success=True, meas_level=2,
                                             memory=True, data=data)
        result = Result(results=[exp_result], **self.base_result_args)

        self.assertEqual(result.get_memory(0), no_header_processed_memory)
Beispiel #5
0
 def _parse_result(self, result: Result) -> List[str]:
     measurements: List[str] = []
     if self._requires_memory:
         for e in range(self._experiments):
             measurements += result.get_memory(e)
     else:
         cts = result.get_counts()
         counts: List[Counts] = [cts] if isinstance(cts, list) else cts
         for c in counts:
             measurements += [k for k, v in c.items() if v == 1]
     return [reverse_endian(m) for m in measurements]
 def _parse_result(self, result: Result) -> str:
     measurements: List[str] = []
     if self._memory:
         for e in range(self._experiments):
             measurements += result.get_memory(e)
     else:
         cts = result.get_counts()
         counts: List[Counts] = [cts] if type(cts) != list else cts
         for c in counts:
             measurements += [k for k, v in c.items() if v == 1]
     bitstring: str = ""
     for m in measurements:
         bitstring += m
     return bitstring
Beispiel #7
0
    def test_meas_level_1_avg(self):
        """Test measurement level 1 average result."""
        # 3 qubits
        raw_memory = [[0., 1.], [1., 0.], [0.5, 0.5]]
        processed_memory = np.array([1.j, 1., 0.5+0.5j], dtype=np.complex_)
        data = models.ExperimentResultData(memory=raw_memory)
        exp_result = models.ExperimentResult(shots=2, success=True, meas_level=1,
                                             meas_return='avg', data=data)
        result = Result(results=[exp_result], **self.base_result_args)
        memory = result.get_memory(0)

        self.assertEqual(memory.shape, (3,))
        self.assertEqual(memory.dtype, np.complex_)
        np.testing.assert_almost_equal(memory, processed_memory)