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

Multi-character register names not parsed correctly #3

Open
km-campbell opened this issue Oct 19, 2023 · 0 comments
Open

Multi-character register names not parsed correctly #3

km-campbell opened this issue Oct 19, 2023 · 0 comments

Comments

@km-campbell
Copy link

km-campbell commented Oct 19, 2023

Hi, firstly, thanks for the package! It has been a huge help to me.

I have found that when the names of qregs or cregs are multiple characters, only the last character of the name appears in the 'qreg_name' entry of elements in 'c_sect' with 'type' ASTType.QREG or ASTType.CREG. For example, a register called 'reg' would give return the value 'g' for the 'qreg_name' key.

This behaviour seems to stem from the line 233 to 235 of nuqasm2/qasmast.py (accessed 19/10/2023. The relevant lines are:

x = QTRegEx.REG_DECL.match(self.source)
self.qreg_name = x.group(1)
self.qreg_num = x.group(2)

One can replicate the issue without needing to run a specific file by modifying the above lines to read:

from nuqasm2.qasmast import QTRegEx

a_string = 'qreg reg[5]'
x = QTRegEx.REG_DECL.match(a_string)
print(x.group()) #gives 'qreg reg[5]'. This demonstrates that the matching is working as expected
qreg_name = x.group(1)
print(qreg_name) #gives 'g', rather than 'reg'

As I am new to regex, I personally fixed this using the 3rd-party pyparsing package by implementing the class:

import pyparsing as pp

class QasmParsingElement():
    l_sqr_brace = pp.Literal('[').suppress()
    r_sqr_brace = pp.Literal(']').suppress()
    idQasm = pp.Regex(r'[a-z][A-Za-z0-9]*') 
    nninteger = pp.Regex(r'[1-9]+[0-9]*|0')
    reg_index_slice = l_sqr_brace + nninteger + r_sqr_brace
    decl =  (pp.Keyword('qreg') + idQasm + reg_index_slice |
                 pp.Keyword('creg') + idQasm + reg_index_slice ) 

and then changing 233 to 235 of nuqasm2/qasmast.py to

parsed_line = QasmParsingElement.decl.parse_string(self.source)
self.qreg_name = parsed_line[1]
self.qreg_num = parsed_line[2]

However, obviously this does have the drawback of adding another dependency to the nuqasm2 package.

NOTE: The issue with cregs is identical.

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

No branches or pull requests

1 participant