예제 #1
0
    def test_multiple_instructions_with_different_parameters(self):
        """Test adding many instruction with different parameter binding."""
        backend = FakeAthens()
        backend.defaults().instruction_schedule_map.add("my_gate", (0, ),
                                                        self.my_gate_q0,
                                                        arguments=["P0"])

        qc = circuit.QuantumCircuit(1)
        qc.append(circuit.Gate("my_gate", 1, [1.0]), [0])
        qc.append(circuit.Gate("my_gate", 1, [2.0]), [0])
        qc.append(circuit.Gate("my_gate", 1, [3.0]), [0])

        transpiled_qc = transpile(qc,
                                  backend,
                                  basis_gates=["my_gate"],
                                  initial_layout=[0])

        my_gate_q0_1_0 = self.my_gate_q0.assign_parameters(
            {self.sched_param: 1.0}, inplace=False)
        my_gate_q0_2_0 = self.my_gate_q0.assign_parameters(
            {self.sched_param: 2.0}, inplace=False)
        my_gate_q0_3_0 = self.my_gate_q0.assign_parameters(
            {self.sched_param: 3.0}, inplace=False)

        ref_calibration = {
            "my_gate": {
                ((0, ), (1.0, )): my_gate_q0_1_0,
                ((0, ), (2.0, )): my_gate_q0_2_0,
                ((0, ), (3.0, )): my_gate_q0_3_0,
            }
        }
        self.assertDictEqual(transpiled_qc.calibrations, ref_calibration)
예제 #2
0
    def test_transpile_with_custom_gate(self):
        """Test providing non-basis gate."""
        backend = FakeAthens()
        backend.defaults().instruction_schedule_map.add("my_gate", (0, ),
                                                        self.my_gate_q0,
                                                        arguments=["P0"])
        backend.defaults().instruction_schedule_map.add("my_gate", (1, ),
                                                        self.my_gate_q1,
                                                        arguments=["P0"])

        qc = circuit.QuantumCircuit(2)
        qc.append(circuit.Gate("my_gate", 1, [1.0]), [0])
        qc.append(circuit.Gate("my_gate", 1, [2.0]), [1])

        transpiled_qc = transpile(qc,
                                  backend,
                                  basis_gates=["my_gate"],
                                  initial_layout=[0, 1])

        my_gate_q0_1_0 = self.my_gate_q0.assign_parameters(
            {self.sched_param: 1.0}, inplace=False)
        my_gate_q1_2_0 = self.my_gate_q1.assign_parameters(
            {self.sched_param: 2.0}, inplace=False)

        ref_calibration = {
            "my_gate": {
                ((0, ), (1.0, )): my_gate_q0_1_0,
                ((1, ), (2.0, )): my_gate_q1_2_0,
            }
        }
        self.assertDictEqual(transpiled_qc.calibrations, ref_calibration)
def instruction_to_gate(instruction):
    """Attempt to convert instruction to gate.

    Args:
        instruction (Instruction): instruction to convert

    Returns:
        Gate: instruction cast to gate

    Raises:
        QiskitError: Conversion fails if element of definition can't be converted.
    """
    import qiskit.circuit as circuit
    gate_spec_list = []
    if instruction.definition is not None:
        for instr_spec in instruction.definition:
            instr = instr_spec[0]
            if isinstance(instr, circuit.Gate):
                thisgate = instr
            elif isinstance(instr, circuit.Instruction):
                thisgate = instruction_to_gate(instr)
            else:
                raise QiskitError('One or more instructions in this instruction '
                                  'cannot be converted to a gate')
            gate_spec_list.append((thisgate, instr_spec[1], instr_spec[2]))
        gate = circuit.Gate(instruction.name, instruction.num_qubits, instruction.params)
        gate.definition = gate_spec_list
    elif instruction.num_clbits > 0:
        raise QiskitError('One or more instructions in this instruction '
                          'cannot be converted to a gate')
    else:
        gate = circuit.Gate(instruction.name, instruction.num_qubits, instruction.params)
    return gate
    def test_transpile_with_multiple_circuits(self):
        """Test transpile with multiple circuits with custom gate."""
        backend = FakeAthens()
        backend.defaults().instruction_schedule_map.add(
            "my_gate", (0,), self.my_gate_q0, arguments=["P0"]
        )

        params = [0.0, 1.0, 2.0, 3.0]
        circs = []
        for param in params:
            qc = circuit.QuantumCircuit(1)
            qc.append(circuit.Gate("my_gate", 1, [param]), [0])
            circs.append(qc)

        transpiled_qcs = transpile(circs, backend, basis_gates=["my_gate"], initial_layout=[0])

        for param, transpiled_qc in zip(params, transpiled_qcs):
            my_gate_q0_x = self.my_gate_q0.assign_parameters(
                {self.sched_param: param}, inplace=False
            )
            ref_calibration = {"my_gate": {((0,), (param,)): my_gate_q0_x}}
            self.assertDictEqual(transpiled_qc.calibrations, ref_calibration)
예제 #5
0
    def circuits(self) -> List[QuantumCircuit]:
        """Return a list of experiment circuits.

        Returns:
            A list of :class:`QuantumCircuit`.

        Raises:
            AttributeError: When the backend doesn't report the time resolution of waveforms.
        """
        opt = self.experiment_options

        try:
            dt_factor = self.backend.configuration().dt
        except AttributeError as ex:
            raise AttributeError("Backend configuration does not provide time resolution.") from ex

        # Parametrized duration cannot be used because total duration is computed
        # on the fly with granularity validation. This validation requires
        # duration value that is not a parameter expression.

        # Note that this experiment scans flat top width rather than total duration.
        expr_circs = list()
        for flat_top_width in np.asarray(opt.flat_top_widths, dtype=float):

            cr_gate = circuit.Gate(
                "cr_gate",
                num_qubits=2,
                params=[flat_top_width / self.__n_cr_pulses__],
            )

            for control_state in (0, 1):
                for meas_basis in ("x", "y", "z"):
                    tomo_circ = QuantumCircuit(2, 1)

                    # state prep
                    if control_state:
                        tomo_circ.x(0)

                    # add cross resonance
                    tomo_circ.compose(
                        other=self._build_cr_circuit(cr_gate),
                        qubits=[0, 1],
                        inplace=True,
                    )

                    # measure
                    if meas_basis == "x":
                        tomo_circ.h(1)
                    elif meas_basis == "y":
                        tomo_circ.sdg(1)
                        tomo_circ.h(1)
                    tomo_circ.measure(1, 0)

                    # add metadata
                    tomo_circ.metadata = {
                        "experiment_type": self.experiment_type,
                        "qubits": self.physical_qubits,
                        "xval": flat_top_width * dt_factor,  # in units of sec
                        "control_state": control_state,
                        "meas_basis": meas_basis,
                    }

                    # Create schedule and add it to the circuit.
                    # The flat top width and sigma are in units of dt
                    # width is divided by number of tones to keep total duration consistent
                    tomo_circ.add_calibration(
                        gate=cr_gate,
                        qubits=self.physical_qubits,
                        schedule=self._build_cr_schedule(
                            backend=self.backend,
                            flat_top_width=flat_top_width / self.__n_cr_pulses__,
                            sigma=opt.sigma,
                        ),
                    )

                    expr_circs.append(tomo_circ)

        return expr_circs
    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)