Skip to content

Commit

Permalink
Revert "add qfasm exceptions"
Browse files Browse the repository at this point in the history
This reverts commit 275794b.
  • Loading branch information
chensgit169 authored and Zhaoyilunnn committed Aug 25, 2023
1 parent b5bd691 commit 3fe0eec
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 33 deletions.
93 changes: 64 additions & 29 deletions src/quafu/qfasm/qfasm_convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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):
Expand Down Expand Up @@ -40,39 +41,33 @@ 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(r"\d+", qbs)[0])
num = int(re.findall("\d+", qbs)[0])
reg = QuantumRegister(num)
qc.qregs.append(reg)
elif operations == "creg":
pass
elif operations == "measure":
qbs = operations_qbs[1]
indstr = re.findall(r"\d+", qbs)
indstr = re.findall("\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]
Expand All @@ -88,16 +83,16 @@ def qasm2_to_quafu_qc(qc: QuantumCircuit, openqasm: str):
qc.barrier(inds)

else:
sp_op = operations.split("(") # parameters
gatename = sp_op[0].lower()
sp_op = operations.split("(")
gatename = sp_op[0]
if gatename == "delay":
paras = sp_op[1].strip("()")
duration = int(re.findall(r"\d+", paras)[0])
duration = int(re.findall("\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(r"\d+", paras)[0])
duration = int(re.findall("\d+", paras)[0])
unit = re.findall("[a-z]+", paras)[0]
qc.xy(min(inds), max(inds), duration, unit)
else:
Expand All @@ -108,18 +103,44 @@ def qasm2_to_quafu_qc(qc: QuantumCircuit, openqasm: str):
eval(parai, {"pi": pi}) for parai in parastr
]

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])
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])
elif gatename == "cp":
qc.cp(inds[0], inds[1], paras[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 == "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 == "ccx":
qc.toffoli(inds[0], inds[1], inds[2])
elif gatename == "cswap":
Expand All @@ -134,9 +155,12 @@ 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 in ["rxx", "ryy", "rzz"]:
funcs = {"rxx": qc.rxx, "ryy": qc.ryy, "rzz": qc.rzz}
funcs[gatename](inds[0], inds[1], paras[0])
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])
else:
print(
"Warning: Operations %s may be not supported by QuantumCircuit class currently."
Expand All @@ -146,7 +170,18 @@ 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:
import warnings
warnings.warn("All operations after measurement will be removed for executing on experiment")
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)

return qc
print(matches)
2 changes: 1 addition & 1 deletion src/quafu/qfasm/qfasm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions src/quafu/qfasm/qfasmlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

class QfasmLexer(object):
def __init__(self):
self.lexer = None
self.data = None
self.build()

def input(self, data):
Expand Down Expand Up @@ -93,7 +91,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):
Expand Down
1 change: 1 addition & 0 deletions src/quafu/tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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):
Expand Down

0 comments on commit 3fe0eec

Please sign in to comment.