Repository navigation
Support for physical qubit and ParsedCircuit structure - #21
Open
thierry-martinez wants to merge 13 commits into
Open
thierry-martinez wants to merge 13 commits into
thierry-martinez wants to merge 13 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.
`CONDINSTR` now takes its `domain` as first argument, before the instructions. The uv source for graphix temporarily points to the PR branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Measurement outcomes are now referred to in domains by the index of the measurement, following Graphix's convention for CONDINSTR in TeamGraphix/graphix#644. The new option `discard_measurements` (default `True`) controls whether measured qubits are discarded; with `discard_measurements=False`, qubits remain active after measurement, as in OpenQASM. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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} |
pranav97nair
approved these changes
Nov 24, 2026
|
|
||
| Methods `parse_str` and `parse_file` return a `ParsedCircuit` | ||
| instance. This dataclass exposes the Graphix circuit itself through | ||
| the `circuit` attribute. The other attributes are mapping from the |
There was a problem hiding this comment.
Suggested change
| the `circuit` attribute. The other attributes are mapping from the | |
| the `circuit` attribute. The other attributes are mappings from the |
thierry-martinez
added a commit
to TeamGraphix/graphix
that referenced
this pull request
Nov 26, 2026
* Update for TeamGraphix/graphix-qasm-parser#21 This commit makes the code compatible with the new `ParsedCircuit` structure. * Add missing `.circuit` in test introduced in #644 * Fix repo URL
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 PR relies on #23 to be compatible with TeamGraphix/graphix#644, which has already been merged. PR #23 should be approved and merged before merging this one.
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.