-
Notifications
You must be signed in to change notification settings - Fork 162
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
Add transpiler tests #1323
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
98
test/unit/transpiler/passes/basis/test_convert_id_to_delay.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
test/unit/transpiler/passes/scheduling/control_flow_test_case.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
"""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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?There was a problem hiding this comment.
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