Skip to content
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

Enhance QASM parser to support multi-character register names #279

Merged
merged 2 commits into from
Dec 19, 2024

Conversation

contra-bit
Copy link
Contributor

Prior to this patch, the QASM parser could only parse creg or qreg declarations with a length of 1 (e.g., q or c). This made it impossible to define or measure quantum registers with variable names longer than one letter.

For example, the parser would fail to parse a qreg named q1.

This patch addresses this limitation by enhancing the parser to handle multi-character register names. The key changes are:

  1. The parse_command method has been updated to handle the measure operation separately. It now correctly extracts the target register name and index, as well as the result register name and index, from the measure command.

  2. The logic for parsing qreg and creg declarations has been improved to support multi-character register names. The register name and index are now extracted separately, and the parser checks if the register name is valid before processing the declaration.

These changes can be tested by parsing the following QASM code:

zx.qasm("""OPENQASM 2.0;
include "qelib1.inc";
qreg q1[3];
creg c1[1];
creg c2[1];
h q1[1];
h q1[1];
x q1[2];
h q1[2];
id q1[2];
h q1[0];
cx q1[0], q1[2];
h q1[0];
measure q1[0] -> c2[0];
measure q1[1] -> c1[0];
""")

The updated parser should now be able to correctly parse the QASM code, including the qreg and measure operations with multi-character register names.

@contra-bit contra-bit force-pushed the fix/qasm-parser branch 2 times, most recently from f4c5023 to 57f3207 Compare December 17, 2024 18:58
@contra-bit
Copy link
Contributor Author

I am unsure why mpy complains. Target qbit and and result_bit are cast into ints.

These are the arguments from my example for Measurement

target_qbit 0 is a <class 'int'>
result 0 is a <class 'int'>
target_qbit 1 is a <class 'int'>
result 0 is a <class 'int'>

@jvdwetering
Copy link
Collaborator

You are reusing the variable name 'result_bit' which confuses mypy as you use it as str first and then as int.
And maybe you aren't importing the Measurement class?

@jvdwetering jvdwetering merged commit c8b45b6 into zxcalc:master Dec 19, 2024
3 checks passed
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