From 275794b3f2ee29c4d228a69ffe773dda2633b5f1 Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Tue, 22 Aug 2023 20:27:24 +0800 Subject: [PATCH] add qfasm exceptions --- src/quafu/qfasm/qfasm_convertor.py | 93 ++++++++++-------------------- src/quafu/qfasm/qfasm_parser.py | 2 +- src/quafu/qfasm/qfasmlex.py | 4 +- src/quafu/tasks/tasks.py | 1 - 4 files changed, 33 insertions(+), 67 deletions(-) diff --git a/src/quafu/qfasm/qfasm_convertor.py b/src/quafu/qfasm/qfasm_convertor.py index f4414191..6adfa919 100644 --- a/src/quafu/qfasm/qfasm_convertor.py +++ b/src/quafu/qfasm/qfasm_convertor.py @@ -2,7 +2,6 @@ from quafu.dagcircuits.circuit_dag import node_to_gate from quafu.dagcircuits.instruction_node import InstructionNode from quafu.circuits import QuantumCircuit, QuantumRegister -from quafu.elements.quantum_element import Instruction def qasm_to_circuit(qasm): @@ -41,33 +40,39 @@ def qasm2_to_quafu_qc(qc: QuantumCircuit, openqasm: str): import re qc.openqasm = openqasm - # lines = self.openqasm.strip("\n").splitlines(";") + lines = qc.openqasm.splitlines() lines = [line for line in lines if line] + lines = [line for line in lines if not line.startswith("//")] # annotations + + # init qc qc.gates = [] qc.measures = {} measured_qubits = [] global_valid = True + + # proceed line by line for line in lines[2:]: if line: operations_qbs = line.split(" ", 1) operations = operations_qbs[0] if operations == "qreg": qbs = operations_qbs[1] - num = int(re.findall("\d+", qbs)[0]) + num = int(re.findall(r"\d+", qbs)[0]) reg = QuantumRegister(num) qc.qregs.append(reg) elif operations == "creg": pass elif operations == "measure": qbs = operations_qbs[1] - indstr = re.findall("\d+", qbs) + indstr = re.findall(r"\d+", qbs) inds = [int(indst) for indst in indstr] mb = inds[0] cb = inds[1] qc.measures[mb] = cb measured_qubits.append(mb) else: + # apply some kind of instruction qbs = operations_qbs[1] indstr = re.findall("\d+", qbs) inds = [int(indst) for indst in indstr] @@ -83,16 +88,16 @@ def qasm2_to_quafu_qc(qc: QuantumCircuit, openqasm: str): qc.barrier(inds) else: - sp_op = operations.split("(") - gatename = sp_op[0] + sp_op = operations.split("(") # parameters + gatename = sp_op[0].lower() if gatename == "delay": paras = sp_op[1].strip("()") - duration = int(re.findall("\d+", paras)[0]) + duration = int(re.findall(r"\d+", paras)[0]) unit = re.findall("[a-z]+", paras)[0] qc.delay(inds[0], duration, unit) elif gatename == "xy": paras = sp_op[1].strip("()") - duration = int(re.findall("\d+", paras)[0]) + duration = int(re.findall(r"\d+", paras)[0]) unit = re.findall("[a-z]+", paras)[0] qc.xy(min(inds), max(inds), duration, unit) else: @@ -103,44 +108,18 @@ def qasm2_to_quafu_qc(qc: QuantumCircuit, openqasm: str): eval(parai, {"pi": pi}) for parai in parastr ] - if gatename == "cx": - qc.cnot(inds[0], inds[1]) - elif gatename == "cy": - qc.cy(inds[0], inds[1]) - elif gatename == "cz": - qc.cz(inds[0], inds[1]) + if gatename in ["cx", "cy", "cz", "swap"]: + funcs = {"cx": qc.cnot, "cy": qc.cy, "cz": qc.cz, "swap": qc.swap} + funcs[gatename](inds[0], inds[1]) elif gatename == "cp": qc.cp(inds[0], inds[1], paras[0]) - elif gatename == "swap": - qc.swap(inds[0], inds[1]) - elif gatename == "rx": - qc.rx(inds[0], paras[0]) - elif gatename == "ry": - qc.ry(inds[0], paras[0]) - elif gatename == "rz": - qc.rz(inds[0], paras[0]) - elif gatename == "p": - qc.p(inds[0], paras[0]) - elif gatename == "x": - qc.x(inds[0]) - elif gatename == "y": - qc.y(inds[0]) - elif gatename == "z": - qc.z(inds[0]) - elif gatename == "h": - qc.h(inds[0]) - elif gatename == "id": - qc.id(inds[0]) - elif gatename == "s": - qc.s(inds[0]) - elif gatename == "sdg": - qc.sdg(inds[0]) - elif gatename == "t": - qc.t(inds[0]) - elif gatename == "tdg": - qc.tdg(inds[0]) - elif gatename == "sx": - qc.sx(inds[0]) + elif gatename in ["rx", "ry", "rz", "p"]: + funcs = {"rx": qc.rx, "ry": qc.ry, "rz": qc.rz, "p": qc.p} + funcs[gatename](inds[0], paras[0]) + elif gatename in ["x", "y", "z", "h", "id", "s", "sdg", "t", "tdg", "sx"]: + func = {"x": qc.x, "y": qc.y, "z": qc.z, "h": qc.h, "id": qc.id, + "s": qc.s, "sdg": qc.sdg, "t": qc.t, "tdg": qc.tdg, "sx": qc.sx} + func[gatename](inds[0]) elif gatename == "ccx": qc.toffoli(inds[0], inds[1], inds[2]) elif gatename == "cswap": @@ -155,12 +134,9 @@ def qasm2_to_quafu_qc(qc: QuantumCircuit, openqasm: str): qc.rz(inds[0], paras[2]) qc.ry(inds[0], paras[0]) qc.rz(inds[0], paras[1]) - elif gatename == "rxx": - qc.rxx(inds[0], inds[1], paras[0]) - elif gatename == "ryy": - qc.ryy(inds[0], inds[1], paras[0]) - elif gatename == "rzz": - qc.rzz(inds[0], inds[1], paras[0]) + elif gatename in ["rxx", "ryy", "rzz"]: + funcs = {"rxx": qc.rxx, "ryy": qc.ryy, "rzz": qc.rzz} + funcs[gatename](inds[0], inds[1], paras[0]) else: print( "Warning: Operations %s may be not supported by QuantumCircuit class currently." @@ -170,18 +146,7 @@ def qasm2_to_quafu_qc(qc: QuantumCircuit, openqasm: str): if not qc.measures: qc.measures = dict(zip(range(qc.num), range(qc.num))) if not global_valid: - print( - "Warning: All operations after measurement will be removed for executing on experiment" - ) - - -if __name__ == '__main__': - import re - - pattern = r"[a-z]" - - text = "Hello, world! This is a test." - - matches = re.findall(pattern, text) + import warnings + warnings.warn("All operations after measurement will be removed for executing on experiment") - print(matches) + return qc diff --git a/src/quafu/qfasm/qfasm_parser.py b/src/quafu/qfasm/qfasm_parser.py index 9f97974c..5a0aa348 100644 --- a/src/quafu/qfasm/qfasm_parser.py +++ b/src/quafu/qfasm/qfasm_parser.py @@ -209,7 +209,7 @@ def p_expression_none(self, p): p[0] = p[1] def p_expression_term(self, p): - "expression : term" + """expression : term""" p[0] = p[1] def p_expression_m(self, p): diff --git a/src/quafu/qfasm/qfasmlex.py b/src/quafu/qfasm/qfasmlex.py index a12497e9..c5e73297 100644 --- a/src/quafu/qfasm/qfasmlex.py +++ b/src/quafu/qfasm/qfasmlex.py @@ -9,6 +9,8 @@ class QfasmLexer(object): def __init__(self): + self.lexer = None + self.data = None self.build() def input(self, data): @@ -91,7 +93,7 @@ def t_error(self, t): print("Illegal character '%s'" % t.value[0]) def t_newline(self, t): - r"\n+" + r"""\n+""" t.lexer.lineno += len(t.value) def build(self, **kwargs): diff --git a/src/quafu/tasks/tasks.py b/src/quafu/tasks/tasks.py index ffadf7ca..e6028be4 100644 --- a/src/quafu/tasks/tasks.py +++ b/src/quafu/tasks/tasks.py @@ -39,7 +39,6 @@ class Task(object): priority (int): priority level of the task submit_history (dict): circuit submitted with this task backend (dict): quantum backend that execute the task. - """ def __init__(self, user: User = None):