-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add qbraid_qir.qiskit module for Qiskit to QIR conversion #271
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
07af47d
feat: add qbraid_qir.qiskit module for Qiskit to QIR conversion
eidrynbot a44cba2
fix: update license headers with proper MIT attribution format
eidrynbot 9f30903
docs: link CHANGELOG to tracking issue #272
eidrynbot b69c5d9
fix: resolve CI lint errors and add missing qiskit extra to test work…
eidrynbot 8cb78ec
feat: refactor qiskit visitor to map-based dispatch, add transpile fl…
TheGupta2012 ff5d4e2
fix: tests to be compatible with pyqir 0.11.x
TheGupta2012 d307221
fix: address CodeRabbit review comments on PR #271
TheGupta2012 7879d6f
Merge branch 'main' into feature/qiskit-qir-port
TheGupta2012 ed3fd37
fix: address remaining CodeRabbit review comments (round 2)
TheGupta2012 6353236
fix: add type hints to abstract visitor, bind record_output to visite…
TheGupta2012 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,8 @@ | ||
| :orphan: | ||
|
|
||
| qbraid_qir.qiskit | ||
| ================== | ||
|
|
||
| .. automodule:: qbraid_qir.qiskit | ||
| :undoc-members: | ||
| :show-inheritance: | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
|
TheGupta2012 marked this conversation as resolved.
|
This file contains hidden or 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,37 @@ | ||
| # Third Party Notices | ||
|
|
||
| This software includes code derived from third party sources. | ||
|
TheGupta2012 marked this conversation as resolved.
|
||
|
|
||
| ## qiskit-qir | ||
|
|
||
| The `qbraid_qir.qiskit` module contains code derived from [microsoft/qiskit-qir](https://github.com/microsoft/qiskit-qir). | ||
|
|
||
| ``` | ||
| MIT License | ||
|
|
||
| Copyright (c) Microsoft Corporation. | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE | ||
| ``` | ||
|
|
||
| The qiskit-qir codebase has been adapted to: | ||
| - Work with Qiskit 2.x (updated for new QuantumCircuit API) | ||
| - Follow qbraid-qir conventions and coding style | ||
| - Support both pyqir 0.10.x and 0.12+ (typed and opaque pointers) | ||
| - Integrate with the qbraid-qir module structure | ||
|
TheGupta2012 marked this conversation as resolved.
|
||
This file contains hidden or 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,59 @@ | ||
| # Copyright 2026 qBraid | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # pylint: disable=line-too-long | ||
| # Portions of this module are adapted from microsoft/qiskit-qir | ||
| # (https://github.com/microsoft/qiskit-qir), with modifications by qBraid. | ||
| # The original MIT license notice is reproduced in NOTICE.md. | ||
| # pylint: enable=line-too-long | ||
|
|
||
| """ | ||
| Module containing Qiskit QIR functionality. | ||
|
|
||
| .. currentmodule:: qbraid_qir.qiskit | ||
|
|
||
| Functions | ||
| ----------- | ||
|
|
||
| .. autosummary:: | ||
| :toctree: ../stubs/ | ||
|
|
||
| qiskit_to_qir | ||
|
|
||
|
|
||
| Classes | ||
| --------- | ||
|
|
||
| .. autosummary:: | ||
| :toctree: ../stubs/ | ||
|
|
||
| QiskitModule | ||
| BasicQiskitVisitor | ||
|
|
||
| Exceptions | ||
| ----------- | ||
|
|
||
| .. autosummary:: | ||
| :toctree: ../stubs/ | ||
|
|
||
| QiskitConversionError | ||
|
|
||
| """ | ||
|
|
||
| from .convert import qiskit_to_qir | ||
| from .elements import QiskitModule | ||
| from .exceptions import QiskitConversionError | ||
| from .visitor import BasicQiskitVisitor | ||
|
|
||
| __all__ = ["qiskit_to_qir", "QiskitModule", "QiskitConversionError", "BasicQiskitVisitor"] | ||
|
TheGupta2012 marked this conversation as resolved.
|
||
This file contains hidden or 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,102 @@ | ||
| # Copyright 2026 qBraid | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # pylint: disable=line-too-long | ||
| # Portions of this module are adapted from microsoft/qiskit-qir | ||
| # (https://github.com/microsoft/qiskit-qir), with modifications by qBraid. | ||
| # The original MIT license notice is reproduced in NOTICE.md. | ||
| # pylint: enable=line-too-long | ||
|
|
||
| """ | ||
| Module containing Qiskit to QIR conversion functions. | ||
|
|
||
| """ | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from pyqir import Context, Module, qir_module | ||
| from qiskit.circuit import QuantumCircuit | ||
| from qiskit.compiler import transpile as qiskit_transpile | ||
|
|
||
| from .elements import QiskitModule, generate_module_id | ||
| from .exceptions import QiskitConversionError | ||
| from .maps import QISKIT_BASIS_GATES | ||
| from .visitor import BasicQiskitVisitor | ||
|
|
||
|
|
||
| def qiskit_to_qir( | ||
| circuit: QuantumCircuit, | ||
| name: Optional[str] = None, | ||
| transpile: bool = False, | ||
| **kwargs, | ||
| ) -> Module: | ||
| """ | ||
| Converts a Qiskit QuantumCircuit to a PyQIR module. | ||
|
|
||
| Args: | ||
| circuit: The Qiskit QuantumCircuit to convert. | ||
| name: Identifier for created QIR module. Auto-generated if not provided. | ||
| transpile: If True, transpile the circuit to the supported basis gate set | ||
| before conversion. This enables conversion of circuits containing | ||
| gates not directly supported in QIR. Defaults to False. | ||
|
|
||
| Keyword Args: | ||
| initialize_runtime (bool): Whether to perform quantum runtime environment initialization, | ||
| default `True`. | ||
| record_output (bool): Whether to record output calls for registers, default `True`. | ||
| emit_barrier_calls (bool): Whether to emit barrier calls in the QIR, default `False`. | ||
|
|
||
| Returns: | ||
| The QIR ``pyqir.Module`` representation of the input Qiskit circuit. | ||
|
|
||
| Raises: | ||
| TypeError: If the input is not a valid Qiskit QuantumCircuit. | ||
| ValueError: If the input circuit is empty. | ||
| QiskitConversionError: If the conversion fails. | ||
|
|
||
| Example: | ||
| >>> from qiskit import QuantumCircuit | ||
| >>> from qbraid_qir.qiskit import qiskit_to_qir | ||
| >>> | ||
| >>> qc = QuantumCircuit(2, 2) | ||
| >>> qc.h(0) | ||
| >>> qc.cx(0, 1) | ||
| >>> qc.measure([0, 1], [0, 1]) | ||
| >>> | ||
| >>> module = qiskit_to_qir(qc, name="bell") | ||
| >>> ir = str(module) | ||
| """ | ||
| if not isinstance(circuit, QuantumCircuit): | ||
| raise TypeError("Input quantum program must be of type qiskit.QuantumCircuit.") | ||
|
|
||
| if len(circuit.data) == 0: | ||
| raise ValueError("Input quantum circuit must consist of at least one operation.") | ||
|
|
||
| if transpile: | ||
| circuit = qiskit_transpile(circuit, basis_gates=QISKIT_BASIS_GATES) | ||
|
|
||
| if name is None: | ||
| name = generate_module_id(circuit) | ||
|
|
||
| llvm_module = qir_module(Context(), name) | ||
| module = QiskitModule.from_circuit(circuit, llvm_module) | ||
|
|
||
| visitor = BasicQiskitVisitor(**kwargs) | ||
| module.accept(visitor) | ||
|
|
||
| error = llvm_module.verify() | ||
| if error is not None: | ||
| raise QiskitConversionError(error) | ||
|
|
||
| return llvm_module |
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.
Uh oh!
There was an error while loading. Please reload this page.