Skip to content

Support for physical qubit and ParsedCircuit structure - #21

Open
thierry-martinez wants to merge 4 commits into
TeamGraphix:mainfrom
thierry-martinez:physical_qubits
Open

thierry-martinez wants to merge 4 commits into
TeamGraphix:mainfrom
thierry-martinez:physical_qubits

Conversation

@thierry-martinez

Copy link
Copy Markdown
Collaborator

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.

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
matulni self-requested a review November 3, 2026 09:40

@matulni matulni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread README.md
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}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants