Skip to content

Commit

Permalink
Add transpiler tests (#1323)
Browse files Browse the repository at this point in the history
* Add transpiler tests

* remove qiskittestcase
  • Loading branch information
kt474 authored Jan 25, 2024
1 parent 8723ccf commit 385e10e
Show file tree
Hide file tree
Showing 9 changed files with 3,176 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test/unit/transpiler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2024.
#
# 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.
11 changes: 11 additions & 0 deletions test/unit/transpiler/passes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2024.
#
# 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.
11 changes: 11 additions & 0 deletions test/unit/transpiler/passes/basis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2024.
#
# 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.
99 changes: 99 additions & 0 deletions test/unit/transpiler/passes/basis/test_convert_id_to_delay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2024.
#
# 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 the conversion of Id gate operations to a delay."""

from qiskit.circuit import QuantumCircuit
from qiskit.transpiler.passmanager import PassManager

from qiskit_ibm_runtime.transpiler.passes.basis.convert_id_to_delay import (
ConvertIdToDelay,
)

from qiskit_ibm_runtime.transpiler.passes.scheduling.utils import (
DynamicCircuitInstructionDurations,
)

from .....ibm_test_case import IBMTestCase

# pylint: disable=invalid-name


class TestConvertIdToDelay(IBMTestCase):
"""Tests the ConvertIdToDelay pass"""

def setUp(self):
"""Setup."""
super().setUp()

self.durations = DynamicCircuitInstructionDurations([("sx", None, 160), ("x", None, 200)])

def test_id_gate(self):
"""Test if Id gate is converted a delay."""
qc = QuantumCircuit(1, 0)
qc.id(0)

pm = PassManager([ConvertIdToDelay(self.durations)])
transformed = pm.run(qc)

expected = QuantumCircuit(1, 0)
expected.delay(160, 0)

self.assertEqual(expected, transformed)

def test_id_gate_unit(self):
"""Test if Id gate is converted a delay with correct units."""
qc = QuantumCircuit(1, 0)
qc.id(0)

pm = PassManager([ConvertIdToDelay(self.durations, "x")])
transformed = pm.run(qc)

expected = QuantumCircuit(1, 0)
expected.delay(200, 0)

self.assertEqual(expected, transformed)

def test_c_if_id_gate(self):
"""Test if c_if Id gate is converted a c_if delay."""
qc = QuantumCircuit(1, 1)

with qc.if_test((0, 1)): # pylint: disable=not-context-manager
qc.id(0)

pm = PassManager([ConvertIdToDelay(self.durations)])
transformed = pm.run(qc)

expected = QuantumCircuit(1, 1)
with expected.if_test((0, 1)): # pylint: disable=not-context-manager
expected.delay(160, 0)

self.assertEqual(expected, transformed)

def test_if_test_id_gate(self):
"""Test if if_test Id gate is converted a if_test delay."""
qc = QuantumCircuit(1, 1)
with qc.if_test((0, 1)) as else_: # pylint: disable=not-context-manager
qc.id(0)
with else_: # pylint: disable=not-context-manager
qc.id(0)

pm = PassManager([ConvertIdToDelay(self.durations)])
transformed = pm.run(qc)

expected = QuantumCircuit(1, 1)
with expected.if_test((0, 1)) as else_: # pylint: disable=not-context-manager
expected.delay(160, 0)
with else_:
expected.delay(160, 0)

self.assertEqual(expected, transformed)
11 changes: 11 additions & 0 deletions test/unit/transpiler/passes/scheduling/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2024.
#
# 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.
36 changes: 36 additions & 0 deletions test/unit/transpiler/passes/scheduling/control_flow_test_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022, 2023.
#
# 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.

"""Enhanced test case for control flow circuits."""

from typing import Any, Optional

from qiskit import QuantumCircuit
from qiskit.test._canonical import canonicalize_control_flow

from .....ibm_test_case import IBMTestCase


class ControlFlowTestCase(IBMTestCase):
"""Test case that enforces control flow canonicalization of quantum circuits."""

def assertEqual( # pylint: disable=arguments-differ
self, first: Any, second: Any, msg: Optional[str] = None
) -> None:
"""Modify assertEqual to canonicalize the quantum circuit."""
if isinstance(first, QuantumCircuit):
first = canonicalize_control_flow(first)

if isinstance(second, QuantumCircuit):
second = canonicalize_control_flow(second)

super().assertEqual(first, second, msg=msg) # pylint: disable=no-value-for-parameter
Loading

0 comments on commit 385e10e

Please sign in to comment.