Example #1
0
    def test_json_trained(self):
        """Check if the trained node is serializable."""
        node = SVD()
        node.set_parameters(main_axes=np.array([[1.0, 2.0]]),
                            scales=[1.0],
                            i_means=[2.0],
                            q_means=[3.0])
        self.assertRoundTripSerializable(node, check_func=self.json_equiv)

        loaded_node = json.loads(json.dumps(node, cls=ExperimentEncoder),
                                 cls=ExperimentDecoder)
        self.assertTrue(loaded_node.is_trained)
Example #2
0
    def test_svd_error(self):
        """Test the error formula of the SVD."""
        # This is n_circuit = 1, n_slot = 1, the input shape should be [1, 1, 2]
        # Then the output shape will be [1, 1] by reducing the last dimension

        iq_svd = SVD()
        iq_svd.set_parameters(main_axes=np.array([[1.0, 0.0]]),
                              scales=[1.0],
                              i_means=[0.0],
                              q_means=[0.0])

        # Since the axis is along the real part the imaginary error is irrelevant.
        processed_data = iq_svd(
            unp.uarray(nominal_values=[[[1.0, 0.2]]], std_devs=[[[0.2, 0.1]]]))
        np.testing.assert_array_almost_equal(
            unp.nominal_values(processed_data), np.array([[1.0]]))
        np.testing.assert_array_almost_equal(unp.std_devs(processed_data),
                                             np.array([[0.2]]))

        # Since the axis is along the real part the imaginary error is irrelevant.
        processed_data = iq_svd(
            unp.uarray(nominal_values=[[[1.0, 0.2]]], std_devs=[[[0.2, 0.3]]]))
        np.testing.assert_array_almost_equal(
            unp.nominal_values(processed_data), np.array([[1.0]]))
        np.testing.assert_array_almost_equal(unp.std_devs(processed_data),
                                             np.array([[0.2]]))

        # Tilt the axis to an angle of 36.9... degrees
        iq_svd.set_parameters(main_axes=np.array([[0.8, 0.6]]))

        processed_data = iq_svd(
            unp.uarray(nominal_values=[[[1.0, 0.0]]], std_devs=[[[0.2, 0.3]]]))
        cos_ = np.cos(np.arctan(0.6 / 0.8))
        sin_ = np.sin(np.arctan(0.6 / 0.8))
        np.testing.assert_array_almost_equal(
            unp.nominal_values(processed_data),
            np.array([[cos_]]),
        )
        np.testing.assert_array_almost_equal(
            unp.std_devs(processed_data),
            np.array([[np.sqrt((0.2 * cos_)**2 + (0.3 * sin_)**2)]]),
        )
Example #3
0
    def test_distorted_iq_data(self):
        """Test if uncertainty can consider correlation.

        SVD projects IQ data onto I-axis, and input different data sets that
        have the same mean and same variance but squeezed along different axis.
        """
        svd_node = SVD()
        svd_node.set_parameters(
            main_axes=np.array([[1, 0]]), scales=[1.0], i_means=[0.0], q_means=[0.0]
        )

        processor = DataProcessor("memory", [AverageData(axis=1), svd_node])

        dist_i_axis = {"memory": [[[-1, 0]], [[-0.5, 0]], [[0.0, 0]], [[0.5, 0]], [[1, 0]]]}
        dist_q_axis = {"memory": [[[0, -1]], [[0, -0.5]], [[0, 0.0]], [[0, 0.5]], [[0, 1]]]}

        out_i = processor(dist_i_axis)
        self.assertAlmostEqual(out_i[0].nominal_value, 0.0)
        self.assertAlmostEqual(out_i[0].std_dev, 0.31622776601683794)

        out_q = processor(dist_q_axis)
        self.assertAlmostEqual(out_q[0].nominal_value, 0.0)
        self.assertAlmostEqual(out_q[0].std_dev, 0.0)