def test_roundtrip_serializable(self):
     """Test round trip JSON serialization"""
     exp = rb.InterleavedRB(interleaved_element=SXGate(),
                            qubits=(0, ),
                            lengths=[10, 20, 30],
                            seed=123)
     self.assertRoundTripSerializable(exp, self.json_equiv)
    def test_interleaved_structure(self, interleaved_element, qubits, length):
        """Verifies that when generating an interleaved circuit, it will be
        identical to the original circuit up to additions of
        barrier and interleaved element between any two Cliffords.
        """
        exp = rb.InterleavedRB(interleaved_element=interleaved_element,
                               qubits=qubits,
                               lengths=[length],
                               num_samples=1)

        circuits = exp.circuits()
        c_std = circuits[0]
        c_int = circuits[1]
        if c_std.metadata["interleaved"]:
            c_std, c_int = c_int, c_std
        num_cliffords = c_std.metadata["xval"]
        std_idx = 0
        int_idx = 0
        for _ in range(num_cliffords):
            # barrier
            self.assertEqual(c_std[std_idx][0].name, "barrier")
            self.assertEqual(c_int[int_idx][0].name, "barrier")
            # clifford
            self.assertEqual(c_std[std_idx + 1], c_int[int_idx + 1])
            # for interleaved circuit: barrier + interleaved element
            self.assertEqual(c_int[int_idx + 2][0].name, "barrier")
            self.assertEqual(c_int[int_idx + 3][0].name,
                             interleaved_element.name)
            std_idx += 2
            int_idx += 4
 def test_experiment_config(self):
     """Test converting to and from config works"""
     exp = rb.InterleavedRB(interleaved_element=SXGate(),
                            qubits=(0, ),
                            lengths=[10, 20, 30],
                            seed=123)
     loaded_exp = rb.InterleavedRB.from_config(exp.config())
     self.assertNotEqual(exp, loaded_exp)
     self.assertTrue(self.json_equiv(exp, loaded_exp))
    def test_interleaving_delay(self):
        """Test delay instruction can be interleaved."""
        # See qiskit-experiments/#727 for details
        interleaved_element = Delay(10, unit="us")
        exp = rb.InterleavedRB(
            interleaved_element,
            qubits=[0],
            lengths=[1],
            num_samples=1,
        )
        # Not raises an error
        _, int_circ = exp.circuits()

        # barrier, clifford, barrier, "delay", barrier, ...
        self.assertEqual(int_circ.data[3][0], interleaved_element)
 def test_expdata_serialization(self):
     """Test serializing experiment data works."""
     exp = rb.InterleavedRB(
         interleaved_element=SXGate(),
         qubits=(0, ),
         lengths=list(range(1, 200, 50)),
         seed=123,
         backend=self.backend,
     )
     exp.set_transpile_options(**self.transpiler_options)
     expdata = exp.run()
     self.assertExperimentDone(expdata)
     self.assertRoundTripSerializable(expdata,
                                      check_func=self.experiment_data_equiv)
     self.assertRoundTripPickle(expdata,
                                check_func=self.experiment_data_equiv)
    def test_interleaving_circuit_with_delay(self):
        """Test circuit with delay can be interleaved."""
        delay_qc = QuantumCircuit(2)
        delay_qc.delay(10, [0], unit="us")
        delay_qc.x(1)

        exp = rb.InterleavedRB(interleaved_element=delay_qc,
                               qubits=[1, 2],
                               lengths=[1],
                               seed=123,
                               num_samples=1)
        _, int_circ = exp.circuits()

        qc = QuantumCircuit(2)
        qc.x(1)
        expected_inversion = Clifford(
            int_circ.data[1][0]).compose(qc).adjoint()
        # barrier, clifford, barrier, "interleaved circuit", barrier, inversion, ...
        self.assertEqual(expected_inversion, Clifford(int_circ.data[5][0]))
    def test_two_qubit(self):
        """Test two qubit IRB."""
        exp = rb.InterleavedRB(
            interleaved_element=CXGate(),
            qubits=(0, 1),
            lengths=list(range(1, 30, 3)),
            seed=123,
            backend=self.backend,
        )
        exp.set_transpile_options(**self.transpiler_options)
        self.assertAllIdentity(exp.circuits())

        expdata = exp.run()
        self.assertExperimentDone(expdata)

        # Since this is interleaved, we can directly compare values, i.e. n_gpc = 1
        epc = expdata.analysis_results("EPC")
        epc_expected = 3 / 4 * self.p2q
        self.assertAlmostEqual(epc.value.n,
                               epc_expected,
                               delta=0.1 * epc_expected)