Repository navigation
Support for physical qubit and ParsedCircuit structure - #21
Open
thierry-martinez wants to merge 4 commits into
Open
thierry-martinez wants to merge 4 commits into
thierry-martinez wants to merge 4 commits into
Conversation
This commit adds support for physical qubits and changes the return types of `parse_str` and `parse_file` result to `ParsedCircuit`, which contains mappings from the symbols declared in the OpenQASM document to the indices of the corresponding qubits.
matulni
self-requested a review
November 3, 2026 09:40
matulni
approved these changes
Nov 4, 2026
matulni
left a comment
Contributor
There was a problem hiding this comment.
Thank you for this nice change! Made some minor suggestions, otherwise LGTM!
| value = visitor.env[identifier] | ||
| if isinstance(value, _Array): | ||
| if isinstance(value.values[0], _Bit): | ||
| bits[identifier] = tuple(value.index for value in value.values if isinstance(value, _Bit)) |
Contributor
There was a problem hiding this comment.
I understand that here isinstance is done just to inform the typechecker that value has the attribute index, but it can also read as if we wanted to filter the _Bit instances from the value array.
I suggested the following to improve readability:
Suggested change
| bits[identifier] = tuple(value.index for value in value.values if isinstance(value, _Bit)) | |
| bits[identifier] = tuple(cast(_Bit, v).index for v in value.values) |
Tests and mypy pass, but needs importing from typing import cast
| if isinstance(value.values[0], _Bit): | ||
| bits[identifier] = tuple(value.index for value in value.values if isinstance(value, _Bit)) | ||
| else: | ||
| qubits[identifier] = tuple(value.index for value in value.values if isinstance(value, _Qubit)) |
Contributor
There was a problem hiding this comment.
Suggested change
| qubits[identifier] = tuple(value.index for value in value.values if isinstance(value, _Qubit)) | |
| qubits[identifier] = tuple(cast(_Qubit, v).index for v in value.values) |
Comment on lines
+53
to
+60
| s = """ | ||
| include "qelib1.inc"; | ||
| qubit q; | ||
| rz(5*pi/4) q; | ||
| """ | ||
| parser = OpenQASMParser() | ||
| parsed = parser.parse_str(s) | ||
| print(parsed.qubits) # {'q': 0} |
Contributor
There was a problem hiding this comment.
Suggested change
| s = """ | |
| include "qelib1.inc"; | |
| qubit q; | |
| rz(5*pi/4) q; | |
| """ | |
| parser = OpenQASMParser() | |
| parsed = parser.parse_str(s) | |
| print(parsed.qubits) # {'q': 0} | |
| s = """ | |
| include "qelib1.inc"; | |
| qubit q; | |
| rz(5*pi/4) q; | |
| cx q, $55; | |
| bit b; | |
| b = measure q; | |
| """ | |
| parser = OpenQASMParser() | |
| parsed = parser.parse_str(s) | |
| print(parsed.circuit.instruction) # [RZ(0, 5pi/4), CNOT(0, 1), M(0, Axis.Z)] | |
| print(parsed.qubits) # {'q': 0} | |
| print(parsed.physical_qubits) # {55: 1} | |
| print(parsed.bits) # {'b': 0} |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This commit adds support for physical qubits and changes the return types of
parse_strandparse_fileresult toParsedCircuit, which contains mappings from the symbols declared in the OpenQASM document to the indices of the corresponding qubits.