Пример #1
0
 def test_instructions(self):
     self.assertEqual(self.empty_target.instructions, [])
     ibm_expected = [
         (IGate(), (0, )),
         (IGate(), (1, )),
         (IGate(), (2, )),
         (IGate(), (3, )),
         (IGate(), (4, )),
         (RZGate(self.theta), (0, )),
         (RZGate(self.theta), (1, )),
         (RZGate(self.theta), (2, )),
         (RZGate(self.theta), (3, )),
         (RZGate(self.theta), (4, )),
         (SXGate(), (0, )),
         (SXGate(), (1, )),
         (SXGate(), (2, )),
         (SXGate(), (3, )),
         (SXGate(), (4, )),
         (XGate(), (0, )),
         (XGate(), (1, )),
         (XGate(), (2, )),
         (XGate(), (3, )),
         (XGate(), (4, )),
         (CXGate(), (3, 4)),
         (CXGate(), (4, 3)),
         (CXGate(), (3, 1)),
         (CXGate(), (1, 3)),
         (CXGate(), (1, 2)),
         (CXGate(), (2, 1)),
         (CXGate(), (0, 1)),
         (CXGate(), (1, 0)),
         (Measure(), (0, )),
         (Measure(), (1, )),
         (Measure(), (2, )),
         (Measure(), (3, )),
         (Measure(), (4, )),
     ]
     self.assertEqual(ibm_expected, self.ibm_target.instructions)
     ideal_sim_expected = [
         (UGate(self.theta, self.phi, self.lam), None),
         (RXGate(self.theta), None),
         (RYGate(self.theta), None),
         (RZGate(self.theta), None),
         (CXGate(), None),
         (ECRGate(), None),
         (CCXGate(), None),
         (Measure(), None),
     ]
     self.assertEqual(ideal_sim_expected,
                      self.ideal_sim_target.instructions)
Пример #2
0
 def test_update_from_instruction_schedule_map_add_instruction(self):
     target = Target()
     inst_map = InstructionScheduleMap()
     inst_map.add("sx", 0, self.custom_sx_q0)
     inst_map.add("sx", 1, self.custom_sx_q1)
     target.update_from_instruction_schedule_map(inst_map, {"sx": SXGate()})
     self.assertEqual(inst_map, target.instruction_schedule_map())
Пример #3
0
 def test_get_instructions_for_qargs(self):
     with self.assertRaises(KeyError):
         self.empty_target.operations_for_qargs((0, ))
     expected = [RZGate(self.theta), IGate(), SXGate(), XGate(), Measure()]
     res = self.ibm_target.operations_for_qargs((0, ))
     for gate in expected:
         self.assertIn(gate, res)
     expected = [ECRGate()]
     res = self.fake_backend_target.operations_for_qargs((1, 0))
     for gate in expected:
         self.assertIn(gate, res)
     expected = [CXGate()]
     res = self.fake_backend_target.operations_for_qargs((0, 1))
     self.assertEqual(expected, res)
     ideal_sim_expected = [
         UGate(self.theta, self.phi, self.lam),
         RXGate(self.theta),
         RYGate(self.theta),
         RZGate(self.theta),
         CXGate(),
         ECRGate(),
         CCXGate(),
         Measure(),
     ]
     for gate in ideal_sim_expected:
         self.assertIn(gate,
                       self.ideal_sim_target.operations_for_qargs(None))
 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)
Пример #5
0
 def __init__(self, qubit: int, backend: Optional[Backend] = None):
     """Initialize the experiment."""
     super().__init__(qubit, SXGate(), backend=backend)
     # Set default analysis options
     self.analysis.set_options(
         angle_per_gate=np.pi / 2,
         phase_offset=np.pi,
     )
 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))
Пример #7
0
    def _default_experiment_options(cls) -> Options:
        r"""Default values for the FineSXDrag experiment.

        Experiment Options:
            gate (Gate): FineSXDrag calibrates an SXGate.
        """
        options = super()._default_experiment_options()
        options.gate = SXGate()

        return options
Пример #8
0
 def __init__(self, qubit: int, backend: Optional[Backend] = None):
     """Initialize the experiment."""
     super().__init__([qubit], SXGate(), backend=backend)
     # Set default analysis options
     self.analysis.set_options(
         fixed_parameters={
             "angle_per_gate": np.pi / 2,
             "phase_offset": np.pi,
         }
     )
Пример #9
0
    def test_update_from_instruction_schedule_map_with_dt_set(self):
        inst_map = InstructionScheduleMap()
        with pulse.build(name="sx_q1") as custom_sx:
            pulse.play(pulse.Constant(1000, 0.2), pulse.DriveChannel(1))

        inst_map.add("sx", 0, self.custom_sx_q0)
        inst_map.add("sx", 1, custom_sx)
        self.pulse_target.dt = 1.0
        self.pulse_target.update_from_instruction_schedule_map(inst_map, {"sx": SXGate()})
        self.assertEqual(inst_map, self.pulse_target.instruction_schedule_map())
        self.assertEqual(self.pulse_target["sx"][(1,)].duration, 1000.0)
        self.assertIsNone(self.pulse_target["sx"][(1,)].error)
        self.assertIsNone(self.pulse_target["sx"][(0,)].error)
Пример #10
0
    def test_fine_sx_amp(self):
        """Test the fine SX amplitude."""

        exp = FineSXAmplitude(0)

        self.assertFalse(exp.experiment_options.add_sx)
        self.assertFalse(exp.experiment_options.add_xp_circuit)

        expected = [0, 1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 21, 23, 25]
        self.assertEqual(exp.experiment_options.repetitions, expected)
        self.assertEqual(exp.analysis.options.angle_per_gate, np.pi / 2)
        self.assertEqual(exp.analysis.options.phase_offset, np.pi)
        self.assertEqual(exp.experiment_options.gate, SXGate())
Пример #11
0
    def test_fine_sx_amp(self):
        """Test the fine SX amplitude."""

        exp = FineSXAmplitude(0)

        self.assertFalse(exp.experiment_options.add_cal_circuits)

        expected = [0, 1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 21, 23, 25]
        self.assertEqual(exp.experiment_options.repetitions, expected)
        self.assertDictEqual(
            exp.analysis.options.fixed_parameters,
            {"angle_per_gate": np.pi / 2, "phase_offset": np.pi},
        )
        self.assertEqual(exp.experiment_options.gate, SXGate())
 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)
Пример #13
0
 def setUp(self):
     super().setUp()
     self.pulse_target = Target(
         dt=3e-7, granularity=2, min_length=4, pulse_alignment=8, aquire_alignment=8
     )
     with pulse.build(name="sx_q0") as self.custom_sx_q0:
         pulse.play(pulse.Constant(100, 0.1), pulse.DriveChannel(0))
     with pulse.build(name="sx_q1") as self.custom_sx_q1:
         pulse.play(pulse.Constant(100, 0.2), pulse.DriveChannel(1))
     sx_props = {
         (0,): InstructionProperties(
             duration=35.5e-9, error=0.000413, calibration=self.custom_sx_q0
         ),
         (1,): InstructionProperties(
             duration=35.5e-9, error=0.000502, calibration=self.custom_sx_q1
         ),
     }
     self.pulse_target.add_instruction(SXGate(), sx_props)
Пример #14
0
 def test_operations(self):
     self.assertEqual(self.empty_target.operations, [])
     ibm_expected = [
         RZGate(self.theta),
         IGate(),
         SXGate(),
         XGate(),
         CXGate(),
         Measure()
     ]
     for gate in ibm_expected:
         self.assertIn(gate, self.ibm_target.operations)
     aqt_expected = [
         RZGate(self.theta),
         RXGate(self.theta),
         RYGate(self.theta),
         RGate(self.theta, self.phi),
         RXXGate(self.theta),
     ]
     for gate in aqt_expected:
         self.assertIn(gate, self.aqt_target.operations)
     fake_expected = [
         UGate(self.fake_backend._theta, self.fake_backend._phi,
               self.fake_backend._lam),
         CXGate(),
         Measure(),
         ECRGate(),
         RXGate(math.pi / 6),
         RXGate(self.fake_backend._theta),
     ]
     for gate in fake_expected:
         self.assertIn(gate, self.fake_backend_target.operations)
     ideal_sim_expected = [
         UGate(self.theta, self.phi, self.lam),
         RXGate(self.theta),
         RYGate(self.theta),
         RZGate(self.theta),
         CXGate(),
         ECRGate(),
         CCXGate(),
         Measure(),
     ]
     for gate in ideal_sim_expected:
         self.assertIn(gate, self.ideal_sim_target.operations)
Пример #15
0
    def _default_experiment_options(cls) -> Options:
        r"""Default values for the fine amplitude experiment.

        Experiment Options:
            gate (Gate): FineSXAmplitude calibrates an SXGate.
            add_cal_circuits (bool): If set to True then two circuits to calibrate 0 and 1 points
                will be added. This option is set to False by default for ``FineSXAmplitude``
                since the amplitude calibration can be achieved with two SX gates and this is
                included in the repetitions.
            repetitions (List[int]): By default the repetitions take on odd numbers for
                :math:`\pi/2` target angles as this ideally prepares states on the equator of
                the Bloch sphere. Note that the repetitions include two repetitions which
                plays the same role as including a circuit with an X gate.
        """
        options = super()._default_experiment_options()
        options.gate = SXGate()
        options.add_cal_circuits = False
        options.repetitions = [0, 1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 21, 23, 25]

        return options
Пример #16
0
    def setUp(self):
        super().setUp()
        self.target = Target()

        # U gate in qubit 0.
        self.theta = Parameter("theta")
        self.phi = Parameter("phi")
        self.lam = Parameter("lambda")
        u_props = {
            (0, ): InstructionProperties(duration=5.23e-8, error=0.00038115),
        }
        self.target.add_instruction(UGate(self.theta, self.phi, self.lam),
                                    u_props)

        # Rz gate in qubit 1.
        rz_props = {
            (1, ): InstructionProperties(duration=0.0, error=0),
        }
        self.target.add_instruction(RZGate(self.phi), rz_props)

        # X gate in qubit 1.
        x_props = {
            (1, ):
            InstructionProperties(duration=3.5555555555555554e-08,
                                  error=0.00020056469709026198),
        }
        self.target.add_instruction(XGate(), x_props)

        # SX gate in qubit 1.
        sx_props = {
            (1, ):
            InstructionProperties(duration=3.5555555555555554e-08,
                                  error=0.00020056469709026198),
        }
        self.target.add_instruction(SXGate(), sx_props)

        cx_props = {
            (0, 1): InstructionProperties(duration=5.23e-7, error=0.00098115),
            (1, 0): InstructionProperties(duration=4.52e-7, error=0.00132115),
        }
        self.target.add_instruction(CXGate(), cx_props)
    def test_single_qubit(self):
        """Test single qubit IRB."""
        exp = rb.InterleavedRB(
            interleaved_element=SXGate(),
            qubits=(0, ),
            lengths=list(range(1, 300, 30)),
            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 = 1 / 2 * self.p1q
        self.assertAlmostEqual(epc.value.n,
                               epc_expected,
                               delta=0.1 * epc_expected)
Пример #18
0
    def _default_experiment_options(cls) -> Options:
        r"""Default values for the fine amplitude experiment.

        Experiment Options:
            gate (Gate): FineSXAmplitude calibrates an SXGate.
            add_sx (bool): This option is False by default when calibrating gates with a target
                angle per gate of :math:`\pi/2` as it is not necessary in this case.
            add_xp_circuit (bool): This option is False by default when calibrating gates with
                a target angle per gate of :math:`\pi/2`.
            repetitions (List[int]): By default the repetitions take on odd numbers for
                :math:`\pi/2` target angles as this ideally prepares states on the equator of
                the Bloch sphere. Note that the repetitions include two repetitions which
                plays the same role as including a circuit with an X gate.
        """
        options = super()._default_experiment_options()
        options.gate = SXGate()
        options.add_sx = False
        options.add_xp_circuit = False
        options.repetitions = [0, 1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 21, 23, 25]

        return options
 def __init__(self, qubit: int):
     """Initialize the experiment."""
     super().__init__(qubit, SXGate())
Пример #20
0
 def __init__(self, qubit: int, backend: Optional[Backend] = None):
     """Initialize the experiment."""
     super().__init__(qubit, SXGate(), backend=backend)
Пример #21
0
    def setUp(self):
        super().setUp()
        self.fake_backend = FakeBackendV2()
        self.fake_backend_target = self.fake_backend.target
        self.theta = Parameter("theta")
        self.phi = Parameter("phi")
        self.ibm_target = Target()
        i_props = {
            (0, ): InstructionProperties(duration=35.5e-9, error=0.000413),
            (1, ): InstructionProperties(duration=35.5e-9, error=0.000502),
            (2, ): InstructionProperties(duration=35.5e-9, error=0.0004003),
            (3, ): InstructionProperties(duration=35.5e-9, error=0.000614),
            (4, ): InstructionProperties(duration=35.5e-9, error=0.006149),
        }
        self.ibm_target.add_instruction(IGate(), i_props)
        rz_props = {
            (0, ): InstructionProperties(duration=0, error=0),
            (1, ): InstructionProperties(duration=0, error=0),
            (2, ): InstructionProperties(duration=0, error=0),
            (3, ): InstructionProperties(duration=0, error=0),
            (4, ): InstructionProperties(duration=0, error=0),
        }
        self.ibm_target.add_instruction(RZGate(self.theta), rz_props)
        sx_props = {
            (0, ): InstructionProperties(duration=35.5e-9, error=0.000413),
            (1, ): InstructionProperties(duration=35.5e-9, error=0.000502),
            (2, ): InstructionProperties(duration=35.5e-9, error=0.0004003),
            (3, ): InstructionProperties(duration=35.5e-9, error=0.000614),
            (4, ): InstructionProperties(duration=35.5e-9, error=0.006149),
        }
        self.ibm_target.add_instruction(SXGate(), sx_props)
        x_props = {
            (0, ): InstructionProperties(duration=35.5e-9, error=0.000413),
            (1, ): InstructionProperties(duration=35.5e-9, error=0.000502),
            (2, ): InstructionProperties(duration=35.5e-9, error=0.0004003),
            (3, ): InstructionProperties(duration=35.5e-9, error=0.000614),
            (4, ): InstructionProperties(duration=35.5e-9, error=0.006149),
        }
        self.ibm_target.add_instruction(XGate(), x_props)
        cx_props = {
            (3, 4): InstructionProperties(duration=270.22e-9, error=0.00713),
            (4, 3): InstructionProperties(duration=305.77e-9, error=0.00713),
            (3, 1): InstructionProperties(duration=462.22e-9, error=0.00929),
            (1, 3): InstructionProperties(duration=497.77e-9, error=0.00929),
            (1, 2): InstructionProperties(duration=227.55e-9, error=0.00659),
            (2, 1): InstructionProperties(duration=263.11e-9, error=0.00659),
            (0, 1): InstructionProperties(duration=519.11e-9, error=0.01201),
            (1, 0): InstructionProperties(duration=554.66e-9, error=0.01201),
        }
        self.ibm_target.add_instruction(CXGate(), cx_props)
        measure_props = {
            (0, ): InstructionProperties(duration=5.813e-6, error=0.0751),
            (1, ): InstructionProperties(duration=5.813e-6, error=0.0225),
            (2, ): InstructionProperties(duration=5.813e-6, error=0.0146),
            (3, ): InstructionProperties(duration=5.813e-6, error=0.0215),
            (4, ): InstructionProperties(duration=5.813e-6, error=0.0333),
        }
        self.ibm_target.add_instruction(Measure(), measure_props)

        self.aqt_target = Target(description="AQT Target")
        rx_props = {
            (0, ): None,
            (1, ): None,
            (2, ): None,
            (3, ): None,
            (4, ): None,
        }
        self.aqt_target.add_instruction(RXGate(self.theta), rx_props)
        ry_props = {
            (0, ): None,
            (1, ): None,
            (2, ): None,
            (3, ): None,
            (4, ): None,
        }
        self.aqt_target.add_instruction(RYGate(self.theta), ry_props)
        rz_props = {
            (0, ): None,
            (1, ): None,
            (2, ): None,
            (3, ): None,
            (4, ): None,
        }
        self.aqt_target.add_instruction(RZGate(self.theta), rz_props)
        r_props = {
            (0, ): None,
            (1, ): None,
            (2, ): None,
            (3, ): None,
            (4, ): None,
        }
        self.aqt_target.add_instruction(RGate(self.theta, self.phi), r_props)
        rxx_props = {
            (0, 1): None,
            (0, 2): None,
            (0, 3): None,
            (0, 4): None,
            (1, 0): None,
            (2, 0): None,
            (3, 0): None,
            (4, 0): None,
            (1, 2): None,
            (1, 3): None,
            (1, 4): None,
            (2, 1): None,
            (3, 1): None,
            (4, 1): None,
            (2, 3): None,
            (2, 4): None,
            (3, 2): None,
            (4, 2): None,
            (3, 4): None,
            (4, 3): None,
        }
        self.aqt_target.add_instruction(RXXGate(self.theta), rxx_props)
        measure_props = {
            (0, ): None,
            (1, ): None,
            (2, ): None,
            (3, ): None,
            (4, ): None,
        }
        self.aqt_target.add_instruction(Measure(), measure_props)
        self.empty_target = Target()
        self.ideal_sim_target = Target(num_qubits=3,
                                       description="Ideal Simulator")
        self.lam = Parameter("lam")
        for inst in [
                UGate(self.theta, self.phi, self.lam),
                RXGate(self.theta),
                RYGate(self.theta),
                RZGate(self.theta),
                CXGate(),
                ECRGate(),
                CCXGate(),
                Measure(),
        ]:
            self.ideal_sim_target.add_instruction(inst, {None: None})
Пример #22
0
class TestJSON(QiskitExperimentsTestCase):
    """Test JSON encoder and decoder"""

    def test_roundtrip_experiment(self):
        """Test serializing an experiment"""

        obj = FakeExperiment([0])
        obj.set_transpile_options(optimization_level=3, basis_gates=["rx", "ry", "cz"])
        obj.set_run_options(shots=2000)
        self.assertRoundTripSerializable(obj, self.json_equiv)

    @ddt.data(SXGate(), RZXGate(0.4), Barrier(5), Measure())
    def test_roundtrip_gate(self, instruction):
        """Test round-trip serialization of a gate."""
        self.assertRoundTripSerializable(instruction)

    def test_custom_instruction(self):
        """Test the serialisation of a custom instruction."""

        class CustomInstruction(Instruction):
            """A custom instruction for testing."""

            def __init__(self, param: float):
                """Initialize the instruction."""
                super().__init__("test_inst", 2, 2, [param, 0.6])

        def compare_instructions(inst1, inst2):
            """Soft comparison of two instructions."""
            return inst1.soft_compare(inst2)

        self.assertRoundTripSerializable(CustomInstruction(0.123), check_func=compare_instructions)

    def test_roundtrip_quantum_circuit(self):
        """Test round-trip serialization of a circuits"""
        obj = QuantumVolume(4)
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_operator(self):
        """Test round-trip serialization of an Operator"""
        obj = qi.random_unitary(4, seed=10)
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_statevector(self):
        """Test round-trip serialization of a Statevector"""
        obj = qi.random_statevector(4, seed=10)
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_density_matrix(self):
        """Test round-trip serialization of a DensityMatrix"""
        obj = qi.random_density_matrix(4, seed=10)
        self.assertRoundTripSerializable(obj)

    @ddt.data("Choi", "SuperOp", "Kraus", "Stinespring", "PTM", "Chi")
    def test_roundtrip_quantum_channel(self, rep):
        """Test round-trip serialization of a DensityMatrix"""
        chan_cls = {
            "Choi": qi.Choi,
            "SuperOp": qi.SuperOp,
            "Kraus": qi.Kraus,
            "Stinespring": qi.Stinespring,
            "PTM": qi.PTM,
            "Chi": qi.Chi,
        }
        obj = chan_cls[rep](qi.random_quantum_channel(2, seed=10))
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_function(self):
        """Test roundtrip serialization of custom class object"""
        obj = custom_function
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_class_type(self):
        """Test roundtrip serialization of custom class"""
        obj = CustomClass
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_class_object(self):
        """Test roundtrip serialization of custom class object"""
        obj = CustomClass(123)
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_class_method(self):
        """Test roundtrip serialization of custom class object"""
        obj = CustomClass.class_method
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_custom_static_method(self):
        """Test roundtrip serialization of custom class object"""
        obj = CustomClass.static_method
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_main_function(self):
        """Test roundtrip serialization of __main__ custom class object"""
        import __main__ as main_mod

        main_mod.custom_function = custom_function
        main_mod.custom_function.__module__ = "__main__"
        obj = main_mod.custom_function
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_main_class_type(self):
        """Test roundtrip serialization of __main__ custom class"""
        import __main__ as main_mod

        main_mod.CustomClass = CustomClass
        main_mod.CustomClass.__module__ = "__main__"
        obj = main_mod.CustomClass
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_main_class_object(self):
        """Test roundtrip serialization of __main__ custom class object"""
        import __main__ as main_mod

        main_mod.CustomClass = CustomClass
        main_mod.CustomClass.__module__ = "__main__"
        obj = main_mod.CustomClass(123)
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_main_class_method(self):
        """Test roundtrip serialization of __main__ custom class object"""
        import __main__ as main_mod

        main_mod.CustomClass = CustomClass
        main_mod.CustomClass.__module__ = "__main__"
        obj = main_mod.CustomClass.class_method
        self.assertRoundTripSerializable(obj)

    def test_roundtrip_main_custom_static_method(self):
        """Test roundtrip serialization of __main__ custom class object"""
        import __main__ as main_mod

        main_mod.CustomClass = CustomClass
        main_mod.CustomClass.__module__ = "__main__"
        obj = main_mod.CustomClass.static_method
        self.assertRoundTripSerializable(obj)