Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to carry over calibrations with __iadd__ and __add__ #5910

Merged
merged 6 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

# Add the calibrations
for gate, cals in rhs.calibrations.items():
circuit._calibrations[gate].update(cals)

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

# Add calibrations
for gate, cals in rhs.calibrations.items():
self._calibrations[gate].update(cals)

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()