From 060cb70441421db336ada58bb0cd09f0236adb1d Mon Sep 17 00:00:00 2001 From: TsafrirA <113579969+TsafrirA@users.noreply.github.com> Date: Tue, 20 Feb 2024 05:54:02 +0200 Subject: [PATCH] Fix custom pulse instruction conversion to Qobj. (#11829) * Possible fix. * Update releasenotes/notes/fix-custom-pulse-qobj-conversion-5d6041b36356cfd1.yaml Co-authored-by: Will Shanks * release notes fix * Add test --------- Co-authored-by: Will Shanks --- qiskit/qobj/converters/pulse_instruction.py | 6 +++--- ...pulse-qobj-conversion-5d6041b36356cfd1.yaml | 7 +++++++ test/python/compiler/test_assembler.py | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/fix-custom-pulse-qobj-conversion-5d6041b36356cfd1.yaml diff --git a/qiskit/qobj/converters/pulse_instruction.py b/qiskit/qobj/converters/pulse_instruction.py index b8b96e7dc4b..77e811100f3 100644 --- a/qiskit/qobj/converters/pulse_instruction.py +++ b/qiskit/qobj/converters/pulse_instruction.py @@ -62,10 +62,10 @@ def from_instance( Raises: QiskitError: When pulse instance is not recognizable type. """ - try: + if isinstance(instance, library.SymbolicPulse): return cls(instance.pulse_type) - except ValueError as ex: - raise QiskitError(f"'{instance}' is not valid pulse type.") from ex + + raise QiskitError(f"'{instance}' is not valid pulse type.") @classmethod def to_type(cls, name: str) -> library.SymbolicPulse: diff --git a/releasenotes/notes/fix-custom-pulse-qobj-conversion-5d6041b36356cfd1.yaml b/releasenotes/notes/fix-custom-pulse-qobj-conversion-5d6041b36356cfd1.yaml new file mode 100644 index 00000000000..b3fd0e98004 --- /dev/null +++ b/releasenotes/notes/fix-custom-pulse-qobj-conversion-5d6041b36356cfd1.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixed a bug in the conversion of custom pulse instructions to the legacy :mod:`qiskit.qobj` format. The bug was introduced in Qiskit 1.0.0 + and caused conversion of instructions with custom pulse shapes to raise an error. After the fix, the conversion is + carried out correctly, and the custom pulse is converted to :class:`.pulse.Waveform` as it should. + Fixed `#11828 `__. diff --git a/test/python/compiler/test_assembler.py b/test/python/compiler/test_assembler.py index bb6aa6d47a9..630db1c0c1c 100644 --- a/test/python/compiler/test_assembler.py +++ b/test/python/compiler/test_assembler.py @@ -528,6 +528,24 @@ def test_pulse_gates_single_circ(self): self.assertEqual(len(lib), 2) self.assertTrue(all(len(item.samples) == 50 for item in lib)) + def test_custom_pulse_gates_single_circ(self): + """Test that we can add calibrations to circuits of pulses which are not in + qiskit.qobj.converters.pulse_instruction.ParametricPulseShapes""" + circ = QuantumCircuit(2) + circ.h(0) + + with pulse.build() as custom_h_schedule: + pulse.play(pulse.library.Triangle(50, 0.1, 0.2), pulse.DriveChannel(0)) + + circ.add_calibration("h", [0], custom_h_schedule) + + qobj = assemble(circ, FakeOpenPulse2Q()) + lib = qobj.config.pulse_library + self.assertEqual(len(lib), 1) + np.testing.assert_almost_equal( + lib[0].samples, pulse.library.Triangle(50, 0.1, 0.2).get_waveform().samples + ) + def test_pulse_gates_with_parameteric_pulses(self): """Test that pulse gates are assembled efficiently for backends that enable parametric pulses.