def test_correct_1q_depolarization(self):
        """Compute 2Q EPG with 1Q depolarization correction."""
        analysis_1qrb_q0 = rb.RBAnalysis()
        analysis_1qrb_q0.set_options(outcome="0",
                                     gate_error_ratio={
                                         "x": 2,
                                         "h": 1,
                                         "s": 0
                                     })
        result_q0 = analysis_1qrb_q0.run(self.expdata_1qrb_q0,
                                         replace_results=False)
        self.assertExperimentDone(result_q0)

        analysis_1qrb_q1 = rb.RBAnalysis()
        analysis_1qrb_q1.set_options(outcome="0",
                                     gate_error_ratio={
                                         "x": 2,
                                         "h": 1,
                                         "s": 0
                                     })
        result_q1 = analysis_1qrb_q1.run(self.expdata_1qrb_q1,
                                         replace_results=False)
        self.assertExperimentDone(result_q1)

        analysis_2qrb = rb.RBAnalysis()
        analysis_2qrb.set_options(
            outcome="00",
            epg_1_qubit=result_q0.analysis_results() +
            result_q1.analysis_results(),
        )
        result_2qrb = analysis_2qrb.run(self.expdata_2qrb)
        self.assertExperimentDone(result_2qrb)

        cx_epg = result_2qrb.analysis_results("EPG_cx")
        self.assertAlmostEqual(cx_epg.value.n, 0.08 * 0.75, delta=0.006)
    def test_2q_epg(self):
        """Compute 2Q EPG without correction.

        Since 1Q gates are designed to have comparable EPG with CX gate,
        this will overestimate the error of CX gate.
        """
        analysis = rb.RBAnalysis()
        analysis.set_options(outcome="00")
        result = analysis.run(self.expdata_2qrb, replace_results=False)
        self.assertExperimentDone(result)

        cx_epg = result.analysis_results("EPG_cx")

        self.assertGreater(cx_epg.value.n, 0.08 * 0.75)
    def test_no_epg(self):
        """Calculate no EPGs."""
        analysis = rb.RBAnalysis()
        analysis.set_options(outcome="0", gate_error_ratio=None)
        result = analysis.run(self.expdata_1qrb_q0, replace_results=False)
        self.assertExperimentDone(result)

        with self.assertRaises(DbExperimentEntryNotFound):
            result.analysis_results("EPG_s")

        with self.assertRaises(DbExperimentEntryNotFound):
            result.analysis_results("EPG_h")

        with self.assertRaises(DbExperimentEntryNotFound):
            result.analysis_results("EPG_x")
    def test_with_custom_epg_ratio(self):
        """Calculate no EPGs with custom EPG ratio dictionary."""
        analysis = rb.RBAnalysis()
        analysis.set_options(outcome="0",
                             gate_error_ratio={
                                 "x": 2,
                                 "h": 1,
                                 "s": 0
                             })
        result = analysis.run(self.expdata_1qrb_q0, replace_results=False)
        self.assertExperimentDone(result)

        h_epg = result.analysis_results("EPG_h")
        x_epg = result.analysis_results("EPG_x")

        self.assertAlmostEqual(x_epg.value.n, 0.04 * 0.5, delta=0.005)
        self.assertAlmostEqual(h_epg.value.n, 0.02 * 0.5, delta=0.005)
    def test_default_epg_ratio(self):
        """Calculate EPG with default ratio dictionary. H and X have the same ratio."""
        analysis = rb.RBAnalysis()
        analysis.set_options(outcome="0")
        result = analysis.run(self.expdata_1qrb_q0, replace_results=False)
        self.assertExperimentDone(result)

        s_epg = result.analysis_results("EPG_s")
        h_epg = result.analysis_results("EPG_h")
        x_epg = result.analysis_results("EPG_x")

        self.assertEqual(s_epg.value.n, 0.0)

        # H and X gate EPG are assumed to be the same, so this underestimate X and overestimate H
        self.assertEqual(h_epg.value.n, x_epg.value.n)
        self.assertLess(x_epg.value.n, 0.04 * 0.5)
        self.assertGreater(h_epg.value.n, 0.02 * 0.5)
 def test_analysis_config(self):
     """ "Test converting analysis to and from config works"""
     analysis = rb.RBAnalysis()
     loaded = rb.RBAnalysis.from_config(analysis.config())
     self.assertNotEqual(analysis, loaded)
     self.assertEqual(analysis.config(), loaded.config())