jwoehr / nuqasm2 Goto Github PK
View Code? Open in Web Editor NEWNew OPENQASM2 translator
License: Apache License 2.0
New OPENQASM2 translator
License: Apache License 2.0
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.
I created a custom gate cu1mol(theta) a, b
in a file containing definitions of quantum gates.
`
OPENQASM 2.0;
include "qelib1.inc";
gate cxmol a, b {
h b;
cz a, b;
h b;
}
gate cu1mol(theta) a, b {
u1(theta/2) a;
cxmol a, b;
u1(-theta/2) b;
cxmol a, b;
u1(theta/2) b;
}
If I try to define in a qasm file a gate like
cu1mol(pi/2) q[1], q[0];nuqasm2 does not recognize
pi/2, thus having on the output file
u1(theta/2) q[1];instead of
u1(pi/2) q[1];`
Unrolling does not test for failure nor throw an exception when an op from the AST is in proper form but yet cannot be unrolled into a circuit.
E.g.,
// Here's an unknown op 'u(a,b,c)'
u(pi/2,pi/4,pi/15) q[0] ;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.