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

Add transpiler tests #1323

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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.
98 changes: 98 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,98 @@
# 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.test import QiskitTestCase
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,
)

# pylint: disable=invalid-name


class TestConvertIdToDelay(QiskitTestCase):
"""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.
35 changes: 35 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,35 @@
# 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 import QiskitTestCase
from qiskit.test._canonical import canonicalize_control_flow


class ControlFlowTestCase(QiskitTestCase):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think QiskitTestCase is used anywhere else in this repo. It does provide the benefit of turning deprecation warnings into errors, so perhaps that should be used more widely (obviously not this PR).

But for now perhaps use IBMTestCase to keep things consistent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should qiskit.test be removed entirely as part of #1292?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes i'll use IBMTestCase in this PR and then remove all of qiskit.test in #1292

"""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
Loading