-
Notifications
You must be signed in to change notification settings - Fork 18
Fix 217: Merge function for Qasm Modules #267
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
arunjmoorthy
wants to merge
6
commits into
main
Choose a base branch
from
arun/issue_217
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.
Open
Changes from all commits
Commits
Show all changes
6 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 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
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,126 @@ | ||||||
# 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. | ||||||
|
||||||
""" | ||||||
Unit tests for QasmModule.merge(). | ||||||
""" | ||||||
|
||||||
from pyqasm.entrypoint import loads | ||||||
from pyqasm.modules import QasmModule | ||||||
|
||||||
|
||||||
def _qasm3(qasm: str) -> QasmModule: | ||||||
return loads(qasm) | ||||||
|
||||||
|
||||||
def test_merge_basic_gates_and_offsets(): | ||||||
qasm_a = ( | ||||||
""" | ||||||
OPENQASM 3.0; | ||||||
include "stdgates.inc"; | ||||||
qubit[2] q; | ||||||
x q[0]; | ||||||
cx q[0], q[1]; | ||||||
""" | ||||||
) | ||||||
qasm_b = ( | ||||||
""" | ||||||
OPENQASM 3.0; | ||||||
include "stdgates.inc"; | ||||||
qubit[3] r; | ||||||
h r[0]; | ||||||
cx r[1], r[2]; | ||||||
""" | ||||||
) | ||||||
|
||||||
mod_a = _qasm3(qasm_a) | ||||||
mod_b = _qasm3(qasm_b) | ||||||
|
||||||
merged = mod_a.merge(mod_b) | ||||||
|
||||||
# Unrolled representation should have a single consolidated qubit declaration of size 5 | ||||||
text = str(merged) | ||||||
assert "qubit[5] __PYQASM_QUBITS__;" in text | ||||||
|
||||||
lines = [l.strip() for l in text.splitlines() if l.strip()] | ||||||
# Keep only gate lines for comparison; skip version/includes/declarations | ||||||
gate_lines = [ | ||||||
l | ||||||
for l in lines | ||||||
if l[0].isalpha() | ||||||
and not l.startswith("include") | ||||||
and not l.startswith("OPENQASM") | ||||||
and not l.startswith("qubit") | ||||||
] | ||||||
assert gate_lines[0].startswith("x __PYQASM_QUBITS__[0]") | ||||||
assert gate_lines[1].startswith("cx __PYQASM_QUBITS__[0], __PYQASM_QUBITS__[1]") | ||||||
assert any(l.startswith("h __PYQASM_QUBITS__[2]") for l in gate_lines) | ||||||
assert any(l.startswith("cx __PYQASM_QUBITS__[3], __PYQASM_QUBITS__[4]") for l in gate_lines) | ||||||
|
||||||
|
||||||
def test_merge_with_measurements_and_barriers(): | ||||||
# Module A: 1 qubit + classical 1; has barrier and measure | ||||||
qasm_a = ( | ||||||
"OPENQASM 3.0;\n" | ||||||
'include "stdgates.inc";\n' | ||||||
"qubit[1] qa; bit[1] ca;\n" | ||||||
"h qa[0];\n" | ||||||
"barrier qa;\n" | ||||||
"ca[0] = measure qa[0];\n" | ||||||
) | ||||||
# Module B: 2 qubits + classical 2 | ||||||
qasm_b = ( | ||||||
"OPENQASM 3.0;\n" | ||||||
'include "stdgates.inc";\n' | ||||||
"qubit[2] qb; bit[2] cb;\n" | ||||||
"x qb[1];\n" | ||||||
"cb[1] = measure qb[1];\n" | ||||||
) | ||||||
|
||||||
mod_a = _qasm3(qasm_a) | ||||||
mod_b = _qasm3(qasm_b) | ||||||
|
||||||
merged = mod_a.merge(mod_b) | ||||||
merged_text = str(merged) | ||||||
|
||||||
assert "qubit[3] __PYQASM_QUBITS__;" in merged_text | ||||||
assert "measure __PYQASM_QUBITS__[2];" in merged_text | ||||||
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. Why are we changing the structure of the measurement statements? I believe the merged qasm should be like - OPENQASM 3.0;
include "stdgates.inc";
qubit[3] __PYQASM_QUBITS__;
// Module A
bit[1] ca;
h __PYQASM_QUBITS__[0];
barrier __PYQASM_QUBITS__[0];
ca[0] = measure __PYQASM_QUBITS__[0];
// Module B
bit[2] cb;
x __PYQASM_QUBITS__[1];
cb[1] = measure __PYQASM_QUBITS__[1]; |
||||||
assert "barrier __PYQASM_QUBITS__" in merged_text | ||||||
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.
Suggested change
|
||||||
|
||||||
|
||||||
def test_merge_qasm2_with_qasm3(): | ||||||
qasm2 = ( | ||||||
"OPENQASM 2.0;\n" | ||||||
'include "qelib1.inc";\n' | ||||||
"qreg q[1];\n" | ||||||
"h q[0];\n" | ||||||
) | ||||||
qasm3 = ( | ||||||
"OPENQASM 3.0;\n" | ||||||
'include "stdgates.inc";\n' | ||||||
"qubit[2] r;\n" | ||||||
"x r[0];\n" | ||||||
) | ||||||
|
||||||
mod2 = loads(qasm2) | ||||||
mod3 = loads(qasm3) | ||||||
|
||||||
merged = mod2.merge(mod3) | ||||||
text = str(merged) | ||||||
# Since we are merging starting from a QASM2 module, the merged output | ||||||
# should remain in QASM2 syntax (qreg), not QASM3 (qubit). | ||||||
assert "OPENQASM 2.0;" in text | ||||||
assert 'include "qelib1.inc";' in text | ||||||
assert "qreg __PYQASM_QUBITS__[3];" in text | ||||||
assert "x __PYQASM_QUBITS__[1];" in text |
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.
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.
Can you use the verification functions from the
tests/utils.py
file? We have already implemented a lot of robust checks in that file which you are implementing from scratch