-
Notifications
You must be signed in to change notification settings - Fork 16
fix: support physical qubits in QASM3 to QIR conversion #290
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
Open
ryanhill1
wants to merge
5
commits into
main
Choose a base branch
from
fix/qasm3-physical-qubits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
71e9b34
fix: support physical qubits in QASM3 to QIR conversion
ryanhill1 d7ac111
refactor: take the physical qubit count from pyqasm instead of re-der…
ryanhill1 c012f4e
Merge branch 'main' into fix/qasm3-physical-qubits
TheGupta2012 7997378
add PR backrefs in CHANGELOG
TheGupta2012 ae7d1c5
refactor: drop redundant target check in _get_op_bits
ryanhill1 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,160 @@ | ||
| # Copyright 2025 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. | ||
|
|
||
| """ | ||
| Module containing unit tests for QASM3 programs that address physical (hardware) | ||
| qubits, e.g. "$0", rather than declaring a qubit register. | ||
|
|
||
| Physical qubits are valid OpenQASM 3 and are what Qiskit emits when a circuit is | ||
| transpiled against a backend (``qasm3.dumps(transpile(circuit, backend))``). They | ||
| carry their index in the identifier itself, so "$3" is hardware qubit 3 and maps | ||
| directly onto QIR qubit 3. | ||
|
|
||
| """ | ||
|
|
||
| import openqasm3.ast as qasm3_ast | ||
| import pytest | ||
|
|
||
| from qbraid_qir.qasm3 import qasm3_to_qir | ||
| from qbraid_qir.qasm3.exceptions import Qasm3ConversionError | ||
| from qbraid_qir.qasm3.visitor import QasmQIRVisitor | ||
| from tests.qir_utils import ( | ||
| check_attributes, | ||
| check_measure_op, | ||
| check_resets, | ||
| check_single_qubit_gate_op, | ||
| check_single_qubit_rotation_op, | ||
| check_two_qubit_gate_op, | ||
| ) | ||
|
|
||
|
|
||
| def test_physical_qubit_gates_and_measurement(): | ||
| """A bell pair on physical qubits lowers to QIR addressing qubits 0 and 1.""" | ||
| qasm3_string = """ | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| bit[2] meas; | ||
| h $0; | ||
| cx $0, $1; | ||
| meas[0] = measure $0; | ||
| meas[1] = measure $1; | ||
| """ | ||
| result = qasm3_to_qir(qasm3_string) | ||
| generated_qir = str(result).splitlines() | ||
|
|
||
| check_attributes(generated_qir, 2, 2) | ||
| check_single_qubit_gate_op(generated_qir, 1, [0], "h") | ||
| check_two_qubit_gate_op(generated_qir, 1, [[0, 1]], "cx") | ||
| check_measure_op(generated_qir, 2, [0, 1], [0, 1]) | ||
|
|
||
|
|
||
| def test_physical_qubit_index_is_preserved(): | ||
| """Physical qubit indices are hardware addresses, so "$3" stays qubit 3 and | ||
| the entry point must declare enough qubits to cover the highest index used.""" | ||
| qasm3_string = """ | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| bit[2] meas; | ||
| h $3; | ||
| cx $3, $7; | ||
| meas[0] = measure $3; | ||
| meas[1] = measure $7; | ||
| """ | ||
| result = qasm3_to_qir(qasm3_string) | ||
| generated_qir = str(result).splitlines() | ||
|
|
||
| # Highest index is 7, so the entry point requires 8 qubits. | ||
| check_attributes(generated_qir, 8, 2) | ||
| check_single_qubit_gate_op(generated_qir, 1, [3], "h") | ||
| check_two_qubit_gate_op(generated_qir, 1, [[3, 7]], "cx") | ||
| check_measure_op(generated_qir, 2, [3, 7], [0, 1]) | ||
|
|
||
|
|
||
| def test_qiskit_style_transpiled_program(): | ||
| """The shape Qiskit emits after transpiling against a backend: physical | ||
| qubits, a barrier, and register-indexed measurements.""" | ||
| qasm3_string = """ | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| bit[3] meas; | ||
| rz(pi / 2) $0; | ||
| sx $0; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should add the check for the single qubit op as well in the final qasm |
||
| cz $0, $1; | ||
| cz $1, $2; | ||
| barrier $0, $1, $2; | ||
| meas[0] = measure $0; | ||
| meas[1] = measure $1; | ||
| meas[2] = measure $2; | ||
| """ | ||
| result = qasm3_to_qir(qasm3_string) | ||
| generated_qir = str(result).splitlines() | ||
|
|
||
| check_attributes(generated_qir, 3, 3) | ||
| # pyqir emits pi/2 as its IEEE-754 hex form rather than a decimal literal. | ||
| check_single_qubit_rotation_op(generated_qir, 1, [0], ["0x3FF921FB54442D18"], "rz") | ||
| # QIR has no native "sx", so pyqasm decomposes it to h - s - h, all on qubit 0. | ||
| check_single_qubit_gate_op(generated_qir, 2, [0, 0], "h") | ||
| check_single_qubit_gate_op(generated_qir, 1, [0], "s") | ||
| check_two_qubit_gate_op(generated_qir, 2, [[0, 1], [1, 2]], "cz") | ||
| check_measure_op(generated_qir, 3, [0, 1, 2], [0, 1, 2]) | ||
|
|
||
|
|
||
| def test_physical_qubit_reset(): | ||
| """Reset on a physical qubit targets the same hardware index.""" | ||
| qasm3_string = """ | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| bit[1] meas; | ||
| h $2; | ||
| reset $2; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add check for |
||
| meas[0] = measure $2; | ||
| """ | ||
| result = qasm3_to_qir(qasm3_string) | ||
| generated_qir = str(result).splitlines() | ||
|
|
||
| check_attributes(generated_qir, 3, 1) | ||
| check_single_qubit_gate_op(generated_qir, 1, [2], "h") | ||
| check_resets(generated_qir, 1, [2]) | ||
| check_measure_op(generated_qir, 1, [2], [0]) | ||
|
|
||
|
|
||
| def test_measurement_without_target_raises_conversion_error(): | ||
| """'measure q;' parses but has no classical target to record into. It must | ||
| report that, not raise a bare AssertionError with an empty message.""" | ||
| qasm3_string = """ | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| qubit[2] q; | ||
| bit[2] c; | ||
| h q[0]; | ||
| measure q; | ||
| """ | ||
| with pytest.raises(Qasm3ConversionError, match="must be assigned to a classical bit"): | ||
| qasm3_to_qir(qasm3_string) | ||
|
|
||
|
|
||
| def test_unsupported_operand_raises_conversion_error(): | ||
| """An operand the visitor cannot lower raises a Qasm3ConversionError naming | ||
| the problem, never a bare AssertionError with an empty message.""" | ||
| visitor = QasmQIRVisitor() | ||
| operation = qasm3_ast.QuantumGate( | ||
| modifiers=[], | ||
| name=qasm3_ast.Identifier(name="h"), | ||
| arguments=[], | ||
| # Neither an IndexedIdentifier (virtual) nor a "$n" Identifier (physical). | ||
| qubits=[qasm3_ast.Identifier(name="q")], | ||
| ) | ||
|
|
||
| with pytest.raises(Qasm3ConversionError, match="Unsupported qubit operand"): | ||
| visitor._get_op_bits(operation) # pylint: disable=protected-access | ||
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.
As mentioned in the comment below, better to move the parsing semantics to pyqasm