def test_barriers_in_middle(self):
     """As a follow on to `test_can_add_gates_into_free_space`, similar issues
     arose for barriers, specifically.
     """
     qr = QuantumRegister(2)
     qc = QuantumCircuit(qr)
     for i in range(2):
         qc.u2(0, 0, [qr[i]])
         qc.barrier(qr[i])
         qc.u1(3.14, [qr[i]])
         qc.barrier(qr[i])
         qc.u2(0, 0, [qr[i]])
     sched = schedule(qc, self.backend, method="alap")
     expected = Schedule(self.inst_map.get('u2', [0], 0, 0),
                         self.inst_map.get('u2', [1], 0, 0),
                         (28, self.inst_map.get('u1', [0], 3.14)),
                         (28, self.inst_map.get('u1', [1], 3.14)),
                         (28, self.inst_map.get('u2', [0], 0, 0)),
                         (28, self.inst_map.get('u2', [1], 0, 0)))
     for actual, expected in zip(sched.instructions, expected.instructions):
         self.assertEqual(actual[0], expected[0])
         self.assertEqual(actual[1].command, expected[1].command)
         self.assertEqual(actual[1].channels, expected[1].channels)
    def test_multiple_measure_in_3Q(self):
        """Test multiple measure, user memslot mapping, 3Q."""
        backend = FakeOpenPulse3Q()
        cmd_def = backend.defaults().build_cmd_def()
        q = QuantumRegister(3)
        c = ClassicalRegister(5)
        qc = QuantumCircuit(q, c)
        qc.measure(q[0], c[2])
        qc.measure(q[0], c[4])
        sched = schedule(qc, backend)

        acquire = Acquire(duration=10)
        expected = Schedule(
            cmd_def.get('measure', [0, 1, 2]).filter(channels=[MeasureChannel(0)]),
            acquire(AcquireChannel(0), MemorySlot(2)),
            acquire(AcquireChannel(1), MemorySlot(0)),
            acquire(AcquireChannel(2), MemorySlot(1)),
            (10, cmd_def.get('measure', [0, 1, 2]).filter(channels=[MeasureChannel(0)])),
            (10, acquire(AcquireChannel(0), MemorySlot(4))),
            (10, acquire(AcquireChannel(1), MemorySlot(0))),
            (10, acquire(AcquireChannel(2), MemorySlot(1)))
        )
        self.assertEqual(sched.instructions, expected.instructions)
 def test_alap_pass(self):
     """Test ALAP scheduling."""
     q = QuantumRegister(2)
     c = ClassicalRegister(2)
     qc = QuantumCircuit(q, c)
     qc.append(U2Gate(3.14, 1.57), [q[0]])
     qc.append(U2Gate(0.5, 0.25), [q[1]])
     qc.barrier(q[1])
     qc.append(U2Gate(0.5, 0.25), [q[1]])
     qc.barrier(q[0], [q[1]])
     qc.cx(q[0], q[1])
     qc.measure(q, c)
     sched = schedule(qc, self.backend)
     # X pulse on q0 should end at the start of the CNOT
     expected = Schedule(
         (28, self.inst_map.get('u2', [0], 3.14, 1.57)),
         self.inst_map.get('u2', [1], 0.5, 0.25),
         (28, self.inst_map.get('u2', [1], 0.5, 0.25)),
         (56, self.inst_map.get('cx', [0, 1])),
         (78, self.inst_map.get('measure', [0, 1])))
     for actual, expected in zip(sched.instructions, expected.instructions):
         self.assertEqual(actual[0], expected[0])
         self.assertEqual(actual[1], expected[1])
 def test_3q_schedule(self):
     """Test a schedule that was recommended by David McKay :D """
     backend = FakeOpenPulse3Q()
     inst_map = backend.defaults().instruction_schedule_map
     q = QuantumRegister(3)
     c = ClassicalRegister(3)
     qc = QuantumCircuit(q, c)
     qc.cx(q[0], q[1])
     qc.append(U2Gate(0.778, 0.122), [q[2]])
     qc.append(U3Gate(3.14, 1.57, 0), [q[0]])
     qc.append(U2Gate(3.14, 1.57), [q[1]])
     qc.cx(q[1], q[2])
     qc.append(U2Gate(0.778, 0.122), [q[2]])
     sched = schedule(qc, backend)
     expected = Schedule(inst_map.get('cx', [0, 1]),
                         (22, inst_map.get('u2', [1], 3.14, 1.57)),
                         (22, inst_map.get('u2', [2], 0.778, 0.122)),
                         (24, inst_map.get('cx', [1, 2])),
                         (44, inst_map.get('u3', [0], 3.14, 1.57, 0)),
                         (46, inst_map.get('u2', [2], 0.778, 0.122)))
     for actual, expected in zip(sched.instructions, expected.instructions):
         self.assertEqual(actual[0], expected[0])
         self.assertEqual(actual[1], expected[1])
    def test_measure_combined(self):
        """
        Test to check for measure on the same qubit which generated another measure schedule.

        The measures on different qubits are combined, but measures on the same qubit
        adds another measure to the schedule.
        """
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.u2(3.14, 1.57, q[0])
        qc.cx(q[0], q[1])
        qc.measure(q[0], c[0])
        qc.measure(q[1], c[1])
        qc.measure(q[1], c[1])
        sched = schedule(qc, self.backend, method="as_soon_as_possible")
        expected = Schedule(
            self.inst_map.get('u2', [0], 3.14, 1.57),
            (28, self.inst_map.get('cx', [0, 1])),
            (50, self.inst_map.get('measure', [0, 1])),
            (60, self.inst_map.get('measure', [0, 1]).filter(channels=[MeasureChannel(1)])),
            (60, Acquire(10, AcquireChannel(0), MemorySlot(0))),
            (60, Acquire(10, AcquireChannel(1), MemorySlot(1))))
        self.assertEqual(sched.instructions, expected.instructions)
 def test_asap_pass(self):
     """Test ASAP scheduling."""
     q = QuantumRegister(2)
     c = ClassicalRegister(2)
     qc = QuantumCircuit(q, c)
     qc.u2(3.14, 1.57, q[0])
     qc.u2(0.5, 0.25, q[1])
     qc.barrier(q[1])
     qc.u2(0.5, 0.25, q[1])
     qc.barrier(q[0], q[1])
     qc.cx(q[0], q[1])
     qc.measure(q, c)
     sched = schedule(qc, self.backend, method="as_soon_as_possible")
     # X pulse on q0 should start at t=0
     expected = Schedule(
         self.inst_map.get('u2', [0], 3.14, 1.57),
         self.inst_map.get('u2', [1], 0.5, 0.25),
         (28, self.inst_map.get('u2', [1], 0.5, 0.25)),
         (56, self.inst_map.get('cx', [0, 1])),
         (78, self.inst_map.get('measure', [0, 1])))
     for actual, expected in zip(sched.instructions, expected.instructions):
         self.assertEqual(actual[0], expected[0])
         self.assertEqual(actual[1].command, expected[1].command)
         self.assertEqual(actual[1].channels, expected[1].channels)
示例#7
0
 def test_3q_schedule(self):
     """Test a schedule that was recommended by David McKay :D """
     backend = FakeOpenPulse3Q()
     cmd_def = backend.defaults().build_cmd_def()
     q = QuantumRegister(3)
     c = ClassicalRegister(3)
     qc = QuantumCircuit(q, c)
     qc.cx(q[0], q[1])
     qc.u2(0.778, 0.122, q[2])
     qc.u3(3.14, 1.57, 0., q[0])
     qc.u2(3.14, 1.57, q[1])
     qc.cx(q[1], q[2])
     qc.u2(0.778, 0.122, q[2])
     sched = schedule(qc, backend)
     expected = Schedule(cmd_def.get('cx', [0, 1]),
                         (22, cmd_def.get('u2', [1], 3.14, 1.57)),
                         (46, cmd_def.get('u2', [2], 0.778, 0.122)),
                         (50, cmd_def.get('cx', [1, 2])),
                         (72, cmd_def.get('u2', [2], 0.778, 0.122)),
                         (74, cmd_def.get('u3', [0], 3.14, 1.57)))
     for actual, expected in zip(sched.instructions, expected.instructions):
         self.assertEqual(actual[0], expected[0])
         self.assertEqual(actual[1].command, expected[1].command)
         self.assertEqual(actual[1].channels, expected[1].channels)
 def test_asap_pass(self):
     """Test ASAP scheduling."""
     q = QuantumRegister(2)
     c = ClassicalRegister(2)
     qc = QuantumCircuit(q, c)
     qc.append(U2Gate(3.14, 1.57), [q[0]])
     qc.append(U2Gate(0.5, 0.25), [q[1]])
     qc.barrier(q[1])
     qc.append(U2Gate(0.5, 0.25), [q[1]])
     qc.barrier(q[0], q[1])
     qc.cx(q[0], q[1])
     qc.measure(q, c)
     sched = schedule(qc, self.backend, method="as_soon_as_possible")
     # X pulse on q0 should start at t=0
     expected = Schedule(
         self.inst_map.get("u2", [0], 3.14, 1.57),
         self.inst_map.get("u2", [1], 0.5, 0.25),
         (2, self.inst_map.get("u2", [1], 0.5, 0.25)),
         (4, self.inst_map.get("cx", [0, 1])),
         (26, self.inst_map.get("measure", [0, 1])),
     )
     for actual, expected in zip(sched.instructions, expected.instructions):
         self.assertEqual(actual[0], expected[0])
         self.assertEqual(actual[1], expected[1])
 def time_instruction_to_schedule(self, _, __):
     schedule(self.qc, self.backend, inst_map=self.add_inst_map)
示例#10
0
                                 two_level=True)

builtin_instructions = backend.defaults().instruction_schedule_map
grape_inst_map = QOCInstructionScheduleMap.from_inst_map(
    grape_optimizer, builtin_instructions, ['measure'])

# %%
circ = QuantumCircuit(1, 1)
# circ.x(1)
circ.x(0)
# circ.cnot(0,1)
circ.measure(0, 0)
# circ.measure(1,1)

# %%
grape_schedule = qiskit.schedule(circ,
                                 inst_map=grape_inst_map,
                                 meas_map=backend.configuration().meas_map)
grape_schedule.draw(plot_range=[0, 200])
# %%
real_qobj = assemble(grape_schedule,
                     backend=backend,
                     meas_level=2,
                     meas_return='single',
                     shots=1024)
real_job = backend.run(real_qobj)
# %%
job_monitor(real_job)
# %%
qubit_distribution(real_job.result().get_counts())
示例#11
0
    def test_rzx_calibration_builder(self):
        """Test whether RZXCalibrationBuilderNoEcho scales pulses correctly."""

        # Define a circuit with one RZX gate and an angle theta.
        theta = pi / 3
        rzx_qc = circuit.QuantumCircuit(2)
        rzx_qc.rzx(theta / 2, 1, 0)

        # Verify that there are no calibrations for this circuit yet.
        self.assertEqual(rzx_qc.calibrations, {})

        # apply the RZXCalibrationBuilderNoEcho.
        pass_ = RZXCalibrationBuilderNoEcho(
            instruction_schedule_map=self.backend.defaults(
            ).instruction_schedule_map)
        cal_qc = PassManager(pass_).run(rzx_qc)
        rzx_qc_duration = schedule(cal_qc, self.backend).duration

        # Check that the calibrations contain the correct instructions
        # and pulses on the correct channels.
        rzx_qc_instructions = cal_qc.calibrations["rzx"][((1, 0),
                                                          (theta /
                                                           2, ))].instructions
        self.assertEqual(rzx_qc_instructions[0][1].channel, DriveChannel(0))
        self.assertTrue(isinstance(rzx_qc_instructions[0][1], Play))
        self.assertTrue(
            isinstance(rzx_qc_instructions[0][1].pulse, GaussianSquare))
        self.assertEqual(rzx_qc_instructions[1][1].channel, DriveChannel(1))
        self.assertTrue(isinstance(rzx_qc_instructions[1][1], Delay))
        self.assertEqual(rzx_qc_instructions[2][1].channel, ControlChannel(1))
        self.assertTrue(isinstance(rzx_qc_instructions[2][1], Play))
        self.assertTrue(
            isinstance(rzx_qc_instructions[2][1].pulse, GaussianSquare))

        # Calculate the duration of one scaled Gaussian square pulse from the CX gate.
        cx_sched = self.inst_map.get("cx", qubits=(1, 0))

        crs = []
        for time, inst in cx_sched.instructions:

            # Identify the CR pulses.
            if isinstance(inst, Play) and not isinstance(inst, ShiftPhase):
                if isinstance(inst.channel, ControlChannel):
                    crs.append((time, inst))

        pulse_ = crs[0][1].pulse
        amp = pulse_.amp
        width = pulse_.width
        sigma = pulse_.sigma
        n_sigmas = (pulse_.duration - width) / sigma
        sample_mult = 16

        gaussian_area = abs(amp) * sigma * np.sqrt(2 * np.pi) * erf(n_sigmas)
        area = gaussian_area + abs(amp) * width
        target_area = abs(theta) / (np.pi / 2.0) * area
        width = (target_area - gaussian_area) / abs(amp)
        duration = round(
            (width + n_sigmas * sigma) / sample_mult) * sample_mult

        # Check whether the durations of the RZX pulse and
        # the scaled CR pulse from the CX gate match.
        self.assertEqual(rzx_qc_duration, duration)
示例#12
0
    grape_optimizer, builtin_instructions, ['measure'])

# %%
import numpy as np
circ = QuantumCircuit(2, 2)
# circ.u3(np.pi, 0, np.pi, 0)
# circ.h(0)
circ.x(0)
circ.x(1)
# circ.cnot(1,0)
circ.measure(0, 0)
# circ.measure(1, 1)

# %%
# transpiled_circ = qiskit.transpile(circ,backend)
grape_schedule = qiskit.schedule(circ,
                                 inst_map=builtin_instructions,
                                 meas_map=backend.configuration().meas_map)
grape_schedule.draw(plot_range=[0, 800])
# %%
real_qobj = assemble(grape_schedule,
                     backend=backend,
                     meas_level=2,
                     meas_return='single',
                     shots=1024)
real_job = backend.run(real_qobj)
# %%
job_monitor(real_job)
# %%
qubit_distribution(real_job.result().get_counts())
# %%
def create_qpt_experiment(target_circuits: List[QuantumCircuit],
                          control: int,
                          target: int,
                          backend: BaseBackend,
                          mit_readout: bool = True,
                          inst_map: InstructionScheduleMap = None,
                          basis_gate: List[str] = None,
                          sanity_check: bool = False,
                          shots: int = 2048,
                          return_schedule=False)\
        -> Tuple[Union[PulseQobj, Schedule], List[List[QuantumCircuit]], List[str]]:
    """ Create circuits and schedules for QPT.

    Args:
        target_circuits: List of target circuits for QPT experiment.
        control: index of control qubit.
        target: index of target qubit.
        backend: Target quantum system.
        mit_readout: If use readout mitigation.
        inst_map: instruction mapping object.
        basis_gate: basis gates.
        sanity_check: check memory slot mapping of generated qobj.
        shots: Number of shots.
        return_schedule: set ``True`` when return schedule object instead of qobj.

    Returns:
        Qobj, Schedules, Quantum circuits, Measurement labels

    Additional Information:
        Bit ordering is little endian as a convention of computational science community,
        as the rest of qiskit does. When you measure the CR process tomography of q0 and q1,
        you will observe XZ (ZX) interaction when q0 (q1) is control qubit.
    """
    qubits = sorted([control, target])

    back_config = backend.configuration()
    back_defaults = backend.defaults()

    if inst_map is None:
        inst_map = back_defaults.circuit_instruction_map

    if basis_gate is None:
        basis_gate = back_config.basis_gates

    if isinstance(target_circuits, QuantumCircuit):
        target_circuits = [target_circuits]

    exp_circs = []

    # create the measurement circuits for error mitigation, optional
    qr = target_circuits[0].qregs[0]
    if mit_readout:
        meas_circs, meas_labels = mit.complete_meas_cal(qubit_list=qubits,
                                                        qr=qr,
                                                        circlabel='mcal')
        exp_circs.extend(meas_circs)
    else:
        meas_labels = []

    # create qpt circuit
    qpt_qcs_list = []
    for target_circuit in target_circuits:
        # extract quantum registers from target circuit
        qr = target_circuit.qregs[0]
        qr0 = qr[qubits[0]]
        qr1 = qr[qubits[1]]
        qpt_qcs = tomo.process_tomography_circuits(target_circuit,
                                                   measured_qubits=[qr0, qr1])
        qpt_qcs_list.append(qpt_qcs)
        exp_circs.extend(qpt_qcs)

    # transpile
    exp_circs = qiskit.transpile(exp_circs, backend, basis_gates=basis_gate)

    # schedule with measure alignment
    exp_scheds = align_measures(qiskit.schedule(exp_circs,
                                                backend=backend,
                                                inst_map=inst_map),
                                inst_map=inst_map)

    if return_schedule:
        return exp_scheds, qpt_qcs_list, meas_labels

    # assemble pulse qobj
    qobj = qiskit.assemble(exp_scheds,
                           backend=backend,
                           meas_level=2,
                           shots=shots)

    # sanity check
    if sanity_check:
        for experiment in qobj.experiments:
            for inst in experiment.instructions:
                if inst.name == 'acquire':
                    memory_slot_map = inst.memory_slot
                    if memory_slot_map[qubits[0]] != __reserved_registers[0] or \
                            memory_slot_map[qubits[0]] != __reserved_registers[1]:
                        warnings.warn('Wrong memory slots are assigned. '
                                      'QPT fitter may return invalid result.')

        assert len(qobj.experiments) <= back_config.max_experiments

    return qobj, qpt_qcs_list, meas_labels