def test_integration(self, ix, iy, iz, zx, zy, zz):
        """Integration test for Hamiltonian tomography."""
        backend = AerSimulator(seed_simulator=123, shots=2000)
        backend._configuration.dt = 1e-9
        delta = 3e4

        sigma = 20
        t_off = np.sqrt(2 * np.pi) * sigma

        # Hack: transpiler calls qiskit.parallel but local object cannot be picked
        cr_gate = functools.partial(
            SimulatableCRGate,
            t_off=t_off,
            wix=2 * np.pi * ix,
            wiy=2 * np.pi * iy,
            wiz=2 * np.pi * iz,
            wzx=2 * np.pi * zx,
            wzy=2 * np.pi * zy,
            wzz=2 * np.pi * zz,
            dt=1e-9,
        )

        durations = np.linspace(0, 700, 50)
        expr = cr_hamiltonian.CrossResonanceHamiltonian(
            qubits=(0, 1),
            flat_top_widths=durations,
            sigma=sigma,
            risefall=2,
            cr_gate=cr_gate,
        )
        expr.backend = backend

        exp_data = expr.run()
        self.assertExperimentDone(exp_data, timeout=600)

        self.assertEqual(exp_data.analysis_results(0).quality, "good")

        # These values are computed from other analysis results in post hook.
        # Thus at least one of these values should be round-trip tested.
        res_ix = exp_data.analysis_results("omega_ix")
        self.assertAlmostEqual(res_ix.value.n, ix, delta=delta)
        self.assertRoundTripSerializable(res_ix.value,
                                         check_func=self.ufloat_equiv)
        self.assertEqual(res_ix.extra["unit"], "Hz")

        self.assertAlmostEqual(exp_data.analysis_results("omega_iy").value.n,
                               iy,
                               delta=delta)
        self.assertAlmostEqual(exp_data.analysis_results("omega_iz").value.n,
                               iz,
                               delta=delta)
        self.assertAlmostEqual(exp_data.analysis_results("omega_zx").value.n,
                               zx,
                               delta=delta)
        self.assertAlmostEqual(exp_data.analysis_results("omega_zy").value.n,
                               zy,
                               delta=delta)
        self.assertAlmostEqual(exp_data.analysis_results("omega_zz").value.n,
                               zz,
                               delta=delta)
    def test_integration(self, ix, iy, iz, zx, zy, zz):
        """Integration test for Hamiltonian tomography."""
        sigma = 20
        toff = np.sqrt(2 * np.pi) * sigma * 1e-9

        backend = CrossResonanceHamiltonianBackend(toff, ix, iy, iz, zx, zy,
                                                   zz)
        durations = np.linspace(0, 700, 50)
        expr = cr_hamiltonian.CrossResonanceHamiltonian(
            qubits=(0, 1), flat_top_widths=durations, sigma=sigma, risefall=2)
        exp_data = expr.run(backend, shots=1000)
        exp_data.block_for_results()

        self.assertAlmostEqual(
            exp_data.analysis_results("omega_ix").value.value, ix, delta=1e3)
        self.assertAlmostEqual(
            exp_data.analysis_results("omega_iy").value.value, iy, delta=1e3)
        self.assertAlmostEqual(
            exp_data.analysis_results("omega_iz").value.value, iz, delta=1e3)
        self.assertAlmostEqual(
            exp_data.analysis_results("omega_zx").value.value, zx, delta=1e3)
        self.assertAlmostEqual(
            exp_data.analysis_results("omega_zy").value.value, zy, delta=1e3)
        self.assertAlmostEqual(
            exp_data.analysis_results("omega_zz").value.value, zz, delta=1e3)
 def test_roundtrip_serializable(self):
     """Test round trip JSON serialization"""
     exp = cr_hamiltonian.CrossResonanceHamiltonian(
         qubits=[0, 1],
         flat_top_widths=[1000],
         amp=0.1,
         sigma=64,
         risefall=2,
     )
     self.assertRoundTripSerializable(exp, self.json_equiv)
    def test_circuit_generation_no_backend(self):
        """User can check experiment circuit without setting backend."""
        expr = cr_hamiltonian.CrossResonanceHamiltonian(
            qubits=(0, 1),
            flat_top_widths=[1000],
            amp=0.1,
            sigma=64,
            risefall=2,
        )

        # Not raise an error
        expr.circuits()
 def test_experiment_config(self):
     """Test converting to and from config works"""
     exp = cr_hamiltonian.CrossResonanceHamiltonian(
         qubits=[0, 1],
         flat_top_widths=[1000],
         amp=0.1,
         sigma=64,
         risefall=2,
     )
     loaded_exp = cr_hamiltonian.CrossResonanceHamiltonian.from_config(
         exp.config())
     self.assertNotEqual(exp, loaded_exp)
     self.assertTrue(self.json_equiv(exp, loaded_exp))
    def test_instance_with_backend_without_cr_gate(self):
        """Calling set backend method without setting cr gate."""
        backend = FakeBogota()
        setattr(
            backend.configuration(),
            "timing_constraints",
            {"granularity": 16},
        )

        # not raise an error
        exp = cr_hamiltonian.CrossResonanceHamiltonian(
            qubits=(0, 1),
            flat_top_widths=[1000],
            backend=backend,
        )
        ref_config = backend.configuration()
        self.assertEqual(exp._dt, ref_config.dt)

        # These properties are set when cr_gate is not provided
        self.assertEqual(exp._cr_channel, ref_config.control((0, 1))[0].index)
        self.assertEqual(exp._granularity,
                         ref_config.timing_constraints["granularity"])
    def test_integration(self, ix, iy, iz, zx, zy, zz):
        """Integration test for Hamiltonian tomography."""
        sigma = 20
        toff = np.sqrt(2 * np.pi) * sigma * 1e-9

        backend = CrossResonanceHamiltonianBackend(toff, ix, iy, iz, zx, zy,
                                                   zz)
        durations = np.linspace(0, 700, 50)
        expr = cr_hamiltonian.CrossResonanceHamiltonian(
            qubits=(0, 1), flat_top_widths=durations, sigma=sigma, risefall=2)
        exp_data = expr.run(backend, shots=2000)
        self.assertExperimentDone(exp_data, timeout=600)

        self.assertEqual(exp_data.analysis_results(0).quality, "good")

        # These values are computed from other analysis results in post hook.
        # Thus at least one of these values should be round-trip tested.
        res_ix = exp_data.analysis_results("omega_ix")
        self.assertAlmostEqual(res_ix.value.n, ix, delta=2e4)
        self.assertRoundTripSerializable(res_ix.value,
                                         check_func=self.ufloat_equiv)
        self.assertEqual(res_ix.extra["unit"], "Hz")

        self.assertAlmostEqual(exp_data.analysis_results("omega_iy").value.n,
                               iy,
                               delta=2e4)
        self.assertAlmostEqual(exp_data.analysis_results("omega_iz").value.n,
                               iz,
                               delta=2e4)
        self.assertAlmostEqual(exp_data.analysis_results("omega_zx").value.n,
                               zx,
                               delta=2e4)
        self.assertAlmostEqual(exp_data.analysis_results("omega_zy").value.n,
                               zy,
                               delta=2e4)
        self.assertAlmostEqual(exp_data.analysis_results("omega_zz").value.n,
                               zz,
                               delta=2e4)
    def test_circuit_generation_from_sec(self):
        """Test generated circuits when time unit is sec."""

        backend = CrossResonanceHamiltonianBackend()

        expr = cr_hamiltonian.CrossResonanceHamiltonian(
            qubits=(0, 1),
            flat_top_widths=[500],
            unit="ns",
            amp=0.1,
            sigma=20,
            risefall=2,
        )

        nearlest_16 = 576

        with pulse.build(default_alignment="left", name="cr") as ref_cr_sched:
            pulse.play(
                pulse.GaussianSquare(
                    nearlest_16,
                    amp=0.1,
                    sigma=20,
                    width=500,
                ),
                pulse.ControlChannel(0),
            )
            pulse.delay(nearlest_16, pulse.DriveChannel(0))
            pulse.delay(nearlest_16, pulse.DriveChannel(1))

        cr_gate = circuit.Gate("cr_gate", num_qubits=2, params=[500])
        expr_circs = expr.circuits(backend)

        x0_circ = QuantumCircuit(2, 1)
        x0_circ.append(cr_gate, [0, 1])
        x0_circ.h(1)
        x0_circ.measure(1, 0)

        x1_circ = QuantumCircuit(2, 1)
        x1_circ.x(0)
        x1_circ.append(cr_gate, [0, 1])
        x1_circ.h(1)
        x1_circ.measure(1, 0)

        y0_circ = QuantumCircuit(2, 1)
        y0_circ.append(cr_gate, [0, 1])
        y0_circ.sdg(1)
        y0_circ.h(1)
        y0_circ.measure(1, 0)

        y1_circ = QuantumCircuit(2, 1)
        y1_circ.x(0)
        y1_circ.append(cr_gate, [0, 1])
        y1_circ.sdg(1)
        y1_circ.h(1)
        y1_circ.measure(1, 0)

        z0_circ = QuantumCircuit(2, 1)
        z0_circ.append(cr_gate, [0, 1])
        z0_circ.measure(1, 0)

        z1_circ = QuantumCircuit(2, 1)
        z1_circ.x(0)
        z1_circ.append(cr_gate, [0, 1])
        z1_circ.measure(1, 0)

        ref_circs = [x0_circ, y0_circ, z0_circ, x1_circ, y1_circ, z1_circ]
        for c in ref_circs:
            c.add_calibration(cr_gate, (0, 1), ref_cr_sched)

        self.assertListEqual(expr_circs, ref_circs)
    def test_circuit_generation(self):
        """Test generated circuits."""
        backend = FakeBogota()

        # Add granularity to check duration optimization logic
        setattr(
            backend.configuration(),
            "timing_constraints",
            {"granularity": 16},
        )

        expr = cr_hamiltonian.CrossResonanceHamiltonian(
            qubits=(0, 1),
            flat_top_widths=[1000],
            amp=0.1,
            sigma=64,
            risefall=2,
        )
        expr.backend = backend

        nearlest_16 = 1248

        with pulse.build(default_alignment="left", name="cr") as ref_cr_sched:
            pulse.play(
                pulse.GaussianSquare(
                    nearlest_16,
                    amp=0.1,
                    sigma=64,
                    width=1000,
                ),
                pulse.ControlChannel(0),
            )
            pulse.delay(nearlest_16, pulse.DriveChannel(0))
            pulse.delay(nearlest_16, pulse.DriveChannel(1))

        cr_gate = cr_hamiltonian.CrossResonanceHamiltonian.CRPulseGate(
            width=1000)
        expr_circs = expr.circuits()

        x0_circ = QuantumCircuit(2, 1)
        x0_circ.append(cr_gate, [0, 1])
        x0_circ.h(1)
        x0_circ.measure(1, 0)

        x1_circ = QuantumCircuit(2, 1)
        x1_circ.x(0)
        x1_circ.append(cr_gate, [0, 1])
        x1_circ.h(1)
        x1_circ.measure(1, 0)

        y0_circ = QuantumCircuit(2, 1)
        y0_circ.append(cr_gate, [0, 1])
        y0_circ.sdg(1)
        y0_circ.h(1)
        y0_circ.measure(1, 0)

        y1_circ = QuantumCircuit(2, 1)
        y1_circ.x(0)
        y1_circ.append(cr_gate, [0, 1])
        y1_circ.sdg(1)
        y1_circ.h(1)
        y1_circ.measure(1, 0)

        z0_circ = QuantumCircuit(2, 1)
        z0_circ.append(cr_gate, [0, 1])
        z0_circ.measure(1, 0)

        z1_circ = QuantumCircuit(2, 1)
        z1_circ.x(0)
        z1_circ.append(cr_gate, [0, 1])
        z1_circ.measure(1, 0)

        ref_circs = [x0_circ, y0_circ, z0_circ, x1_circ, y1_circ, z1_circ]
        for c in ref_circs:
            c.add_calibration(cr_gate, (0, 1), ref_cr_sched)

        self.assertListEqual(expr_circs, ref_circs)