Skip to content

Commit

Permalink
Fix to carry over calibrations with __iadd__ and __add__ (#5910)
Browse files Browse the repository at this point in the history
* * Added fix to carry over calibrations with __iadd__ and __add__.
* Added corresponding test.

* * Added reno.

* * Lint fix.

* * Switched to add_calibration.

* * Removed the redundent comments.
  • Loading branch information
eggerdj authored Mar 1, 2021
1 parent 2533392 commit cb21109
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
10 changes: 10 additions & 0 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,11 @@ def combine(self, rhs):
for instruction_context in itertools.chain(self.data, rhs.data):
circuit._append(*instruction_context)
circuit.global_phase = self.global_phase + rhs.global_phase

for gate, cals in rhs.calibrations.items():
for key, sched in cals.items():
circuit.add_calibration(gate, qubits=key[0], schedule=sched, params=key[1])

return circuit

def extend(self, rhs):
Expand Down Expand Up @@ -610,6 +615,11 @@ def extend(self, rhs):
for instruction_context in data:
self._append(*instruction_context)
self.global_phase += rhs.global_phase

for gate, cals in rhs.calibrations.items():
for key, sched in cals.items():
self.add_calibration(gate, qubits=key[0], schedule=sched, params=key[1])

return self

def compose(self, other, qubits=None, clbits=None, front=False, inplace=False):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Quantum circuit Calibrations are now carried over when the operators += and +
are used.
51 changes: 51 additions & 0 deletions test/python/circuit/test_calibrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Test calibrations in quantum circuits."""

import unittest

from qiskit.pulse import Schedule
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import RZXGate
from qiskit.test import QiskitTestCase


class TestCalibrations(QiskitTestCase):
"""Test composition of two circuits."""

def test_iadd(self):
"""Test that __iadd__ keeps the calibrations."""
qc_cal = QuantumCircuit(2)
qc_cal.rzx(0.5, 0, 1)
qc_cal.add_calibration(RZXGate, (0, 1), params=[0.5], schedule=Schedule())

qc = QuantumCircuit(2)
qc += qc_cal

self.assertEqual(qc.calibrations[RZXGate], {((0, 1), (0.5,)): Schedule(name="test")})
self.assertEqual(qc_cal.calibrations, qc.calibrations)

def test_add(self):
"""Test that __add__ keeps the calibrations."""
qc_cal = QuantumCircuit(2)
qc_cal.rzx(0.5, 0, 1)
qc_cal.add_calibration(RZXGate, (0, 1), params=[0.5], schedule=Schedule())

qc = QuantumCircuit(2) + qc_cal

self.assertEqual(qc.calibrations[RZXGate], {((0, 1), (0.5,)): Schedule(name="test")})
self.assertEqual(qc_cal.calibrations, qc.calibrations)


if __name__ == '__main__':
unittest.main()

0 comments on commit cb21109

Please sign in to comment.