def test_svd_on_averaged(self): """Use IQ data gathered from the hardware.""" # This data is primarily oriented along the real axis with a slight tilt. # There is a large offset in the imaginary dimension when comparing qubits # 0 and 1. The data below is averaged IQ data on two qubits. iq_data = [ [[-6.20601501e14, -1.33257051e15], [-1.70921324e15, -4.05881657e15]], [[-5.80546502e14, -1.33492509e15], [-1.65094637e15, -4.05926942e15]], [[-4.04649069e14, -1.33191056e15], [-1.29680377e15, -4.03604815e15]], [[-2.22203874e14, -1.30291309e15], [-8.57663429e14, -3.97784973e15]], [[-2.92074029e13, -1.28578530e15], [-9.78824053e13, -3.92071056e15]], [[1.98056981e14, -1.26883024e15], [3.77157017e14, -3.87460328e15]], [[4.29955888e14, -1.25022995e15], [1.02340118e15, -3.79508679e15]], [[6.38981344e14, -1.25084614e15], [1.68918514e15, -3.78961044e15]], [[7.09988897e14, -1.21906634e15], [1.91914171e15, -3.73670664e15]], [[7.63169115e14, -1.20797552e15], [2.03772603e15, -3.74653863e15]], ] self.create_experiment_data(iq_data) iq_svd = SVD() iq_svd.train(np.asarray([datum["memory"] for datum in self.iq_experiment.data()])) np.testing.assert_array_almost_equal( iq_svd.parameters.main_axes[0], np.array([0.99633018, 0.08559302]) ) np.testing.assert_array_almost_equal( iq_svd.parameters.main_axes[1], np.array([0.99627747, 0.0862044]) )
def test_simple_data(self): """ A simple setting where the IQ data of qubit 0 is oriented along (1,1) and the IQ data of qubit 1 is oriented along (1,-1). """ iq_data = [[[0.0, 0.0], [0.0, 0.0]], [[1.0, 1.0], [-1.0, 1.0]], [[-1.0, -1.0], [1.0, -1.0]]] self.create_experiment(iq_data) iq_svd = SVD() iq_svd.train([datum["memory"] for datum in self.iq_experiment.data()]) # qubit 0 IQ data is oriented along (1,1) self.assertTrue( np.allclose(iq_svd._main_axes[0], np.array([-1, -1]) / np.sqrt(2))) # qubit 1 IQ data is oriented along (1, -1) self.assertTrue( np.allclose(iq_svd._main_axes[1], np.array([-1, 1]) / np.sqrt(2))) processed, _ = iq_svd(np.array([[1, 1], [1, -1]])) expected = np.array([-1, -1]) / np.sqrt(2) self.assertTrue(np.allclose(processed, expected)) processed, _ = iq_svd(np.array([[2, 2], [2, -2]])) self.assertTrue(np.allclose(processed, expected * 2)) # Check that orthogonal data gives 0. processed, _ = iq_svd(np.array([[1, -1], [1, 1]])) expected = np.array([0, 0]) self.assertTrue(np.allclose(processed, expected))
def test_on_single_shot(self): """Test the SVD node on single shot data.""" # The data has the shape iq_data = [ # Circuit no. 1, 5 shots [ [[-84858304.0, -111158232.0]], [[-92671216.0, -74032944.0]], [[-74049176.0, -22372804.0]], [[-87495592.0, -72437616.0]], [[-52787048.0, -63746976.0]], ], # Circuit no. 2, 5 shots [ [[-70452328.0, -91318008.0]], [[-82281464.0, -72478736.0]], [[-107760368.0, -77817680.0]], [[-47410012.0, -48451952.0]], [[68308432.0, -72074976.0]], ], # Circuit no. 3, 5 shots [ [[47855768.0, -52185604.0]], [[-64009220.0, -79507104.0]], [[51899032.0, -80737864.0]], [[118873272.0, -43621036.0]], [[24438894.0, -84970704.0]], ], ] self.create_experiment_data(iq_data, single_shot=True) iq_svd = SVD() iq_svd.train( np.asarray( [datum["memory"] for datum in self.iq_experiment.data()])) processed_data = iq_svd(np.array(iq_data)) # Test the output of the axis self.assertEqual(len(iq_svd.parameters.main_axes), 1) self.assertTrue( np.allclose(iq_svd.parameters.main_axes[0], [0.92727304, 0.37438577])) # Test the output data self.assertEqual(processed_data.shape, (3, 5, 1)) test_values = np.array(processed_data[0].flatten(), dtype=float) expected = np.array( [-0.4982860, -0.4383349, -0.10852355, -0.38971727, -0.07045186], dtype=float) self.assertTrue(np.allclose(test_values, expected, atol=1e-06)) # Test in a data processor, will catch, e.g., unumpy issues data_processor = DataProcessor("memory", [SVD()]) data_processor.train(self.iq_experiment.data()) processed_data = data_processor(self.iq_experiment.data()) self.assertEqual(processed_data.shape, (3, 5, 1))
def test_simple_data(self): """ A simple setting where the IQ data of qubit 0 is oriented along (1,1) and the IQ data of qubit 1 is oriented along (1,-1). """ iq_data = [[[0.0, 0.0], [0.0, 0.0]], [[1.0, 1.0], [-1.0, 1.0]], [[-1.0, -1.0], [1.0, -1.0]]] self.create_experiment(iq_data) iq_svd = SVD() iq_svd.train( np.asarray( [datum["memory"] for datum in self.iq_experiment.data()])) # qubit 0 IQ data is oriented along (1,1) np.testing.assert_array_almost_equal(iq_svd.parameters.main_axes[0], np.array([-1, -1]) / np.sqrt(2)) # qubit 1 IQ data is oriented along (1, -1) np.testing.assert_array_almost_equal(iq_svd.parameters.main_axes[1], np.array([-1, 1]) / np.sqrt(2)) # This is n_circuit = 1, n_slot = 2, the input shape should be [1, 2, 2] # Then the output shape will be [1, 2] by reducing the last dimension processed_data = iq_svd(np.array([[[1, 1], [1, -1]]])) np.testing.assert_array_almost_equal( unp.nominal_values(processed_data), np.array([[-1, -1]]) / np.sqrt(2), ) processed_data = iq_svd(np.array([[[2, 2], [2, -2]]])) np.testing.assert_array_almost_equal( unp.nominal_values(processed_data), 2 * np.array([[-1, -1]]) / np.sqrt(2), ) # Check that orthogonal data gives 0. processed_data = iq_svd(np.array([[[1, -1], [1, 1]]])) np.testing.assert_array_almost_equal( unp.nominal_values(processed_data), np.array([[0, 0]]), )