Exemple #1
0
    def test_default(self):
        """Test default device specification.
        """
        qubits = [
            Qubit(0, DriveChannel(0), MeasureChannel(0), AcquireChannel(0)),
            Qubit(1, DriveChannel(1), MeasureChannel(1), AcquireChannel(1))
        ]
        registers = [RegisterSlot(i) for i in range(2)]
        mem_slots = [MemorySlot(i) for i in range(2)]
        spec = DeviceSpecification(qubits, registers, mem_slots)

        self.assertEqual(spec.q[0].drive, DriveChannel(0))
        self.assertEqual(spec.q[1].acquire, AcquireChannel(1))
        self.assertEqual(spec.mem[0], MemorySlot(0))
        self.assertEqual(spec.c[1], RegisterSlot(1))
Exemple #2
0
    def test_default(self):
        """Test default register slot.
        """
        register_slot = RegisterSlot(123)

        self.assertEqual(register_slot.index, 123)
        self.assertEqual(register_slot.name, 'c123')
Exemple #3
0
    def test_default(self):
        """Test default register slot."""
        register_slot = RegisterSlot(123)

        self.assertEqual(register_slot.index, 123)
        self.assertEqual(register_slot.name, "c123")
        self.assertTrue(isinstance(register_slot, ClassicalIOChannel))
Exemple #4
0
    def sample_schedule(self):
        """Generate a sample schedule that includes the most common elements of
           pulse schedules."""
        gp0 = library.gaussian(duration=20, amp=1.0, sigma=1.0)
        gp1 = library.gaussian(duration=20, amp=-1.0, sigma=2.0)
        gs0 = library.gaussian_square(duration=20, amp=-1.0, sigma=2.0, risefall=3)

        sched = Schedule(name='test_schedule')
        sched = sched.append(gp0(DriveChannel(0)))
        sched = sched.insert(0, Play(library.Constant(duration=60, amp=0.2 + 0.4j),
                                     ControlChannel(0)))
        sched = sched.insert(60, ShiftPhase(-1.57, DriveChannel(0)))
        sched = sched.insert(60, SetFrequency(8.0, DriveChannel(0)))
        sched = sched.insert(60, SetPhase(3.14, DriveChannel(0)))
        sched = sched.insert(70, ShiftFrequency(4.0e6, DriveChannel(0)))
        sched = sched.insert(30, Play(gp1, DriveChannel(1)))
        sched = sched.insert(60, Play(gp0, ControlChannel(0)))
        sched = sched.insert(60, Play(gs0, MeasureChannel(0)))
        sched = sched.insert(90, ShiftPhase(1.57, DriveChannel(0)))
        sched = sched.insert(90, Acquire(10,
                                         AcquireChannel(1),
                                         MemorySlot(1),
                                         RegisterSlot(1)))
        sched = sched.append(Delay(100, DriveChannel(0)))
        sched = sched + sched
        sched |= Snapshot("snapshot_1", "snap_type") << 60
        sched |= Snapshot("snapshot_2", "snap_type") << 120
        return sched
    def sample_schedule(self):
        """Generate a sample schedule that includes the most common elements of
           pulse schedules."""
        gp0 = pulse_lib.gaussian(duration=20, amp=1.0, sigma=1.0)
        gp1 = pulse_lib.gaussian(duration=20, amp=-1.0, sigma=2.0)
        gs0 = pulse_lib.gaussian_square(duration=20,
                                        amp=-1.0,
                                        sigma=2.0,
                                        risefall=3)

        fc_pi_2 = FrameChange(phase=1.57)
        acquire = Acquire(10)
        delay = Delay(100)
        sched = Schedule()
        sched = sched.append(gp0(DriveChannel(0)))
        sched = sched.insert(
            0,
            pulse_lib.ConstantPulse(duration=60,
                                    amp=0.2 + 0.4j)(ControlChannel(0)))
        sched = sched.insert(60, FrameChange(phase=-1.57)(DriveChannel(0)))
        sched = sched.insert(60, SetFrequency(8.0, DriveChannel(0)))
        sched = sched.insert(30, gp1(DriveChannel(1)))
        sched = sched.insert(60, gp0(ControlChannel(0)))
        sched = sched.insert(60, gs0(MeasureChannel(0)))
        sched = sched.insert(90, fc_pi_2(DriveChannel(0)))
        sched = sched.insert(
            90, acquire(AcquireChannel(1), MemorySlot(1), RegisterSlot(1)))
        sched = sched.append(delay(DriveChannel(0)))
        sched = sched + sched
        sched |= Snapshot("snapshot_1", "snap_type") << 60
        sched |= Snapshot("snapshot_2", "snap_type") << 120
        return sched
Exemple #6
0
    def test_acquire(self):
        """Test converted qobj from AcquireInstruction."""
        converter = InstructionToQobjConverter(PulseQobjInstruction,
                                               meas_level=2)
        command = Acquire(duration=10)
        instruction = command([AcquireChannel(0)], [MemorySlot(0)],
                              [RegisterSlot(0)])

        valid_qobj = PulseQobjInstruction(name='acquire',
                                          t0=0,
                                          duration=10,
                                          qubits=[0],
                                          memory_slot=[0],
                                          register_slot=[0])

        self.assertEqual(converter(0, instruction), valid_qobj)

        # test without register
        instruction = command([AcquireChannel(0)], [MemorySlot(0)])

        valid_qobj = PulseQobjInstruction(name='acquire',
                                          t0=0,
                                          duration=10,
                                          qubits=[0],
                                          memory_slot=[0])

        self.assertEqual(converter(0, instruction), valid_qobj)
Exemple #7
0
    def test_acquire(self):
        """Test converted qobj from AcquireInstruction."""
        cmd = Acquire(
            10, Discriminator(name='test_disc', params={'test_params': 1.0}),
            Kernel(name='test_kern', params={'test_params': 'test'}))
        instruction = cmd([AcquireChannel(i) for i in range(self.n_qubits)],
                          [MemorySlot(i) for i in range(self.n_qubits)],
                          [RegisterSlot(i) for i in range(self.n_qubits)])

        qobj = PulseQobjInstruction(
            name='acquire',
            t0=0,
            duration=10,
            qubits=[0, 1],
            memory_slot=[0, 1],
            register_slot=[0, 1],
            kernels=[
                QobjMeasurementOption(name='test_kern',
                                      params={'test_params': 'test'})
            ],
            discriminators=[
                QobjMeasurementOption(name='test_disc',
                                      params={'test_params': 1.0})
            ])
        converted_instruction = self.converter(qobj)

        self.assertEqual(converted_instruction.timeslots,
                         instruction.timeslots)
        self.assertEqual(converted_instruction.instructions[0][-1].command,
                         cmd)
Exemple #8
0
    def test_can_create_valid_schedule(self):
        """Test valid schedule creation without error."""
        gp0 = library.gaussian(duration=20, amp=0.7, sigma=3)
        gp1 = library.gaussian(duration=20, amp=0.7, sigma=3)

        sched = Schedule()
        sched = sched.append(Play(gp0, self.config.drive(0)))
        sched = sched.insert(60, ShiftPhase(-1.57, self.config.drive(0)))
        sched = sched.insert(30, Play(gp1, self.config.drive(0)))
        sched = sched.insert(60, Play(gp0, self.config.control([0, 1])[0]))
        sched = sched.insert(80, Snapshot("label", "snap_type"))
        sched = sched.insert(90, ShiftPhase(1.57, self.config.drive(0)))
        sched = sched.insert(
            90, Acquire(10, self.config.acquire(0), MemorySlot(0), RegisterSlot(0))
        )
        self.assertEqual(0, sched.start_time)
        self.assertEqual(100, sched.stop_time)
        self.assertEqual(100, sched.duration)
        new_sched = Schedule()
        new_sched = new_sched.append(sched)
        new_sched = new_sched.append(sched)
        self.assertEqual(0, new_sched.start_time)
        self.assertEqual(200, new_sched.stop_time)
        self.assertEqual(200, new_sched.duration)
        ids = set()
        for _, inst in sched.instructions:
            self.assertFalse(inst.id in ids)
            ids.add(inst.id)
Exemple #9
0
    def test_deprecated_acquire(self):
        """Test converted qobj from AcquireInstruction."""
        converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2)
        with self.assertWarns(DeprecationWarning):
            command = Acquire(duration=10)
        with self.assertWarns(DeprecationWarning):
            instruction = command(AcquireChannel(0),
                                  MemorySlot(0),
                                  RegisterSlot(0))

        valid_qobj = PulseQobjInstruction(
            name='acquire',
            t0=0,
            duration=10,
            qubits=[0],
            memory_slot=[0],
            register_slot=[0]
        )

        self.assertEqual(converter(0, instruction), valid_qobj)

        # test without register
        with self.assertWarns(DeprecationWarning):
            instruction = command(AcquireChannel(0), MemorySlot(0))

        valid_qobj = PulseQobjInstruction(
            name='acquire',
            t0=0,
            duration=10,
            qubits=[0],
            memory_slot=[0]
        )

        self.assertEqual(converter(0, instruction), valid_qobj)
Exemple #10
0
    def test_can_create_valid_schedule(self):
        """Test valid schedule creation without error."""
        gp0 = pulse_lib.gaussian(duration=20, amp=0.7, sigma=3)
        gp1 = pulse_lib.gaussian(duration=20, amp=0.7, sigma=3)

        fc_pi_2 = FrameChange(phase=1.57)
        acquire = Acquire(10)

        sched = Schedule()
        sched = sched.append(gp0(self.config.drive(0)))
        sched = sched.insert(
            0,
            PersistentValue(value=0.2 + 0.4j)(self.config.control(0)))
        sched = sched.insert(60,
                             FrameChange(phase=-1.57)(self.config.drive(0)))
        sched = sched.insert(30, gp1(self.config.drive(0)))
        sched = sched.insert(60, gp0(self.config.control(0)))
        sched = sched.insert(80, Snapshot("label", "snap_type"))
        sched = sched.insert(90, fc_pi_2(self.config.drive(0)))
        sched = sched.insert(
            90, acquire(self.config.acquire(0), MemorySlot(0),
                        RegisterSlot(0)))
        self.assertEqual(0, sched.start_time)
        self.assertEqual(100, sched.stop_time)
        self.assertEqual(100, sched.duration)
        new_sched = Schedule()
        new_sched = new_sched.append(sched)
        new_sched = new_sched.append(sched)
        self.assertEqual(0, new_sched.start_time)
        self.assertEqual(200, new_sched.stop_time)
        self.assertEqual(200, new_sched.duration)
 def test_truncate_acquisition(self):
     sched = Schedule()
     acquire = Acquire(30)
     sched = sched.insert(
         0, acquire(AcquireChannel(1), MemorySlot(1), RegisterSlot(1)))
     # Check ValueError is not thrown
     sched.draw(plot_range=(0, 15))
Exemple #12
0
    def test_pad_no_delay_on_classical_io_channels(self):
        """Test padding does not apply to classical IO channels."""
        delay = 10
        sched = (
            Delay(delay, MemorySlot(0)).shift(20)
            + Delay(delay, RegisterSlot(0)).shift(10)
            + Delay(delay, SnapshotChannel())
        )

        ref_sched = (
            Delay(delay, MemorySlot(0)).shift(20)
            + Delay(delay, RegisterSlot(0)).shift(10)
            + Delay(delay, SnapshotChannel())
        )

        self.assertEqual(transforms.pad(sched, until=15), ref_sched)
Exemple #13
0
 def test_truncate_acquisition(self):
     sched = Schedule(name='test_schedule')
     sched = sched.insert(0, Acquire(30, AcquireChannel(1),
                                     MemorySlot(1),
                                     RegisterSlot(1)))
     # Check ValueError is not thrown
     pulse_drawer(sched, plot_range=(0, 15))
    def test_can_create_valid_schedule(self):
        """Test valid schedule creation without error."""
        gp0 = pulse_lib.gaussian(duration=20, amp=0.7, sigma=3)
        gp1 = pulse_lib.gaussian(duration=20, amp=0.7, sigma=3)

        sched = Schedule()
        sched = sched.append(Play(gp0, self.config.drive(0)))
        with self.assertWarns(DeprecationWarning):
            sched = sched.insert(
                0,
                PersistentValue(value=0.2 + 0.4j)(self.config.control([0,
                                                                       1])[0]))
        sched = sched.insert(60, ShiftPhase(-1.57, self.config.drive(0)))
        sched = sched.insert(30, Play(gp1, self.config.drive(0)))
        sched = sched.insert(60, Play(gp0, self.config.control([0, 1])[0]))
        sched = sched.insert(80, Snapshot("label", "snap_type"))
        sched = sched.insert(90, ShiftPhase(1.57, self.config.drive(0)))
        sched = sched.insert(
            90,
            Acquire(10, self.config.acquire(0), MemorySlot(0),
                    RegisterSlot(0)))  # TODO: this shouldn't raise a warning
        self.assertEqual(0, sched.start_time)
        self.assertEqual(100, sched.stop_time)
        self.assertEqual(100, sched.duration)
        new_sched = Schedule()
        new_sched = new_sched.append(sched)
        new_sched = new_sched.append(sched)
        self.assertEqual(0, new_sched.start_time)
        self.assertEqual(200, new_sched.stop_time)
        self.assertEqual(200, new_sched.duration)
        ids = set()
        for _, inst in sched.instructions:
            self.assertFalse(inst.id in ids)
            ids.add(inst.id)
Exemple #15
0
    def test_schedule_with_acquire_for_back_and_forward_compatibility(self):
        """Test schedule with acquire for back and forward compatibility."""
        acquire = Acquire(10)
        cmds = [
            acquire(AcquireChannel(0), MemorySlot(0)),
            acquire([AcquireChannel(0)], MemorySlot(0)),
            acquire(AcquireChannel(0), [MemorySlot(0)]),
            acquire([AcquireChannel(0)], mem_slots=[MemorySlot(0)]),
            acquire(AcquireChannel(0), MemorySlot(0), [RegisterSlot(0)]),
            acquire(AcquireChannel(0), MemorySlot(0), reg_slot=RegisterSlot(0))
        ]
        for cmd in cmds:
            mixed_schedule = Schedule()
            mixed_schedule = mixed_schedule.insert(10, cmd)

            self.assertEqual(len(mixed_schedule.instructions), 1)
            self.assertTrue(MemorySlot(0) in mixed_schedule.channels)
 def setUp(self):
     self.device = DeviceSpecification(qubits=[
         Qubit(0,
               drive_channels=[DriveChannel(0, 1.2)],
               acquire_channels=[AcquireChannel(0)])
     ],
                                       registers=[RegisterSlot(0)],
                                       mem_slots=[MemorySlot(0)])
    def test_default(self):
        """Test default device specification.
        """
        spec = PulseChannelSpec(n_qubits=2, n_control=0, n_registers=2)

        self.assertEqual(spec.drives[0], DriveChannel(0))
        self.assertEqual(spec.acquires[1], AcquireChannel(1))
        self.assertEqual(spec.memoryslots[0], MemorySlot(0))
        self.assertEqual(spec.registers[1], RegisterSlot(1))
Exemple #18
0
    def test_schedule_with_acquire_for_back_and_forward_compatibility(self):
        """Test schedule with acquire for back and forward compatibility."""
        dur = 10
        with self.assertWarns(DeprecationWarning):
            # mem_slots and reg_slots are deprecated kwargs
            cmds = [
                Acquire(dur, AcquireChannel(0), MemorySlot(0)),
                Acquire(dur, [AcquireChannel(0)], MemorySlot(0)),
                Acquire(dur, AcquireChannel(0), [MemorySlot(0)]),
                Acquire(dur, [AcquireChannel(0)], mem_slots=[MemorySlot(0)]),
                Acquire(dur, AcquireChannel(0), MemorySlot(0), [RegisterSlot(0)]),
                Acquire(dur, AcquireChannel(0), MemorySlot(0), reg_slot=RegisterSlot(0))
            ]
        for cmd in cmds:
            mixed_schedule = Schedule()
            mixed_schedule = mixed_schedule.insert(dur, cmd)

            self.assertEqual(len(mixed_schedule.instructions), 1)
            self.assertTrue(MemorySlot(0) in mixed_schedule.channels)
    def setUp(self):
        self.linear = SamplePulse(np.arange(0, 0.01), name='linear')
        self.pulse_library = [
            PulseLibraryItem(name=self.linear.name,
                             samples=self.linear.samples.tolist())
        ]

        self.converter = QobjToInstructionConverter(self.pulse_library,
                                                    buffer=0)

        self.device = DeviceSpecification(
            qubits=[
                Qubit(0, DriveChannel(0), MeasureChannel(0),
                      AcquireChannel(0)),
                Qubit(1, DriveChannel(1), MeasureChannel(1),
                      AcquireChannel(1)),
            ],
            registers=[RegisterSlot(0), RegisterSlot(1)],
            mem_slots=[MemorySlot(0), MemorySlot(1)])
Exemple #20
0
    def test_schedule_with_acquire_on_single_qubit(self):
        """Test schedule with acquire on single qubit."""
        sched_single = Schedule()
        for i in range(self.config.n_qubits):
            sched_single = sched_single.insert(10, Acquire(10, self.config.acquire(i),
                                                           mem_slot=MemorySlot(i),
                                                           reg_slot=RegisterSlot(i)))

        self.assertEqual(len(sched_single.instructions), 2)
        self.assertEqual(len(sched_single.channels), 6)
Exemple #21
0
    def test_schedule_with_acquire_on_multiple_qubits(self):
        """Test schedule with acquire on multiple qubits."""
        sched_multiple = Schedule()
        qubits = [self.config.acquire(i) for i in range(self.config.n_qubits)]
        mem_slots = [MemorySlot(i) for i in range(self.config.n_qubits)]
        reg_slots = [RegisterSlot(i) for i in range(self.config.n_qubits)]
        with self.assertWarns(DeprecationWarning):
            sched_multiple = sched_multiple.insert(10, Acquire(10, qubits, mem_slots, reg_slots))

        self.assertEqual(len(sched_multiple.instructions), 1)
        self.assertEqual(len(sched_multiple.channels), 6)
Exemple #22
0
 def setUp(self):
     self.device = DeviceSpecification(
         qubits=[
             Qubit(0, DriveChannel(0), MeasureChannel(0), AcquireChannel(0))
         ],
         registers=[
             RegisterSlot(0)
         ],
         mem_slots=[
             MemorySlot(0)
         ]
     )
 def setUp(self):
     qubits = [
         Qubit(0,
               drive_channels=[DriveChannel(0, 1.2)],
               control_channels=[ControlChannel(0)]),
         Qubit(1,
               drive_channels=[DriveChannel(1, 3.4)],
               acquire_channels=[AcquireChannel(1)])
     ]
     registers = [RegisterSlot(i) for i in range(2)]
     mem_slots = [MemorySlot(i) for i in range(2)]
     self.two_qubit_device = DeviceSpecification(qubits, registers,
                                                 mem_slots)
    def test_creation_from_backend_with_zero_u_channels(self):
        """Test creation of device specification from backend.
        """
        backend = FakeOpenPulse2Q()

        device = PulseChannelSpec.from_backend(backend)

        self.assertEqual(device.drives[0], DriveChannel(0))
        self.assertEqual(device.controls[0], ControlChannel(0))
        self.assertEqual(device.measures[0], MeasureChannel(0))
        self.assertEqual(device.acquires[0], AcquireChannel(0))
        self.assertEqual(device.registers[0], RegisterSlot(0))
        self.assertEqual(device.memoryslots[0], MemorySlot(0))
Exemple #25
0
    def setUp(self):
        @functional_pulse
        def linear(duration, slope, intercept):
            x = np.linspace(0, duration - 1, duration)
            return slope * x + intercept

        self.linear = linear

        qubits = [Qubit(0, DriveChannel(0), AcquireChannel(0), MeasureChannel(0),
                        control_channels=[ControlChannel(0)]),
                  Qubit(1, DriveChannel(1), MeasureChannel(0), AcquireChannel(1))]
        registers = [RegisterSlot(i) for i in range(2)]
        mem_slots = [MemorySlot(i) for i in range(2)]
        self.two_qubit_device = DeviceSpecification(qubits, registers, mem_slots)
Exemple #26
0
    def setUp(self):
        super().setUp()

        qubits = [
            Qubit(0,
                  DriveChannel(0),
                  AcquireChannel(0),
                  MeasureChannel(0),
                  control_channels=[ControlChannel(0)]),
            Qubit(1, DriveChannel(1), MeasureChannel(0), AcquireChannel(1))
        ]
        registers = [RegisterSlot(i) for i in range(2)]
        mem_slots = [MemorySlot(i) for i in range(2)]
        self.two_qubit_device = DeviceSpecification(qubits, registers,
                                                    mem_slots)
 def setUp(self):
     self.schedule = Schedule()
     self.qubits = [
         Qubit(0,
               drive_channel=DriveChannel(0),
               control_channels=[ControlChannel(0)],
               measure_channel=MeasureChannel(0),
               acquire_channel=AcquireChannel(0)),
         Qubit(1,
               drive_channel=DriveChannel(1),
               acquire_channel=AcquireChannel(1),
               measure_channel=MeasureChannel(1))
     ]
     self.registers = [RegisterSlot(i) for i in range(2)]
     self.mem_slots = [MemorySlot(i) for i in range(2)]
     self.device = DeviceSpecification(self.qubits, self.registers,
                                       self.mem_slots)
Exemple #28
0
    def test_can_create_valid_schedule_with_syntax_sugar(self):
        """Test that in place operations on schedule are still immutable
           and return equivalent schedules."""
        gp0 = pulse_lib.gaussian(duration=20, amp=0.7, sigma=3)
        gp1 = pulse_lib.gaussian(duration=20, amp=0.5, sigma=3)
        acquire = Acquire(10)

        sched = Schedule()
        sched += gp0(self.config.drive(0))
        sched |= PersistentValue(value=0.2 + 0.4j)(self.config.control(0))
        sched |= ShiftPhase(-1.57, self.config.drive(0)) << 60
        sched |= gp1(self.config.drive(0)) << 30
        sched |= gp0(self.config.control(0)) << 60
        sched |= Snapshot("label", "snap_type") << 60
        sched |= ShiftPhase(1.57, self.config.drive(0)) << 90
        sched |= acquire(self.config.acquire(0), MemorySlot(0),
                         RegisterSlot(0)) << 90
        sched += sched
    def test_acquire(self):
        """Test converted qobj from Acquire."""
        schedule = Schedule()
        for i in range(self.num_qubits):
            schedule |= Acquire(
                10,
                AcquireChannel(i),
                MemorySlot(i),
                RegisterSlot(i),
                kernel=Kernel(name="test_kern", test_params="test"),
                discriminator=Discriminator(name="test_disc", test_params=1.0),
            )

        qobj = PulseQobjInstruction(
            name="acquire",
            t0=0,
            duration=10,
            qubits=[0, 1],
            memory_slot=[0, 1],
            register_slot=[0, 1],
            kernels=[
                QobjMeasurementOption(name="test_kern",
                                      params={"test_params": "test"})
            ],
            discriminators=[
                QobjMeasurementOption(name="test_disc",
                                      params={"test_params": 1.0})
            ],
        )
        converted_instruction = self.converter(qobj)

        self.assertEqual(converted_instruction.start_time, 0)
        self.assertEqual(converted_instruction.duration, 10)
        self.assertEqual(converted_instruction.instructions[0][-1].duration,
                         10)
        self.assertEqual(
            converted_instruction.instructions[0][-1].kernel.params,
            {"test_params": "test"})
        self.assertEqual(converted_instruction.instructions[1][-1].channel,
                         AcquireChannel(1))
    def test_acquire(self):
        """Test converted qobj from AcquireInstruction."""
        converter = InstructionToQobjConverter(PulseQobjInstruction,
                                               meas_level=2)
        instruction = Acquire(10, AcquireChannel(0), MemorySlot(0),
                              RegisterSlot(0))
        valid_qobj = PulseQobjInstruction(name="acquire",
                                          t0=0,
                                          duration=10,
                                          qubits=[0],
                                          memory_slot=[0],
                                          register_slot=[0])
        self.assertEqual(converter(0, instruction), valid_qobj)

        # without register
        instruction = Acquire(10, AcquireChannel(0), MemorySlot(0))
        valid_qobj = PulseQobjInstruction(name="acquire",
                                          t0=0,
                                          duration=10,
                                          qubits=[0],
                                          memory_slot=[0])
        self.assertEqual(converter(0, instruction), valid_qobj)