Skip to content

Commit

Permalink
fix: add support for some pyquafu gate
Browse files Browse the repository at this point in the history
  • Loading branch information
beizhansl committed Mar 23, 2024
1 parent 16ecbab commit d058041
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
21 changes: 21 additions & 0 deletions quafu/qfasm/qelib1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,24 @@ gate c4x a,b,c,d,e
c3x a,b,c,d;
c3sqrtx a,b,c,e;
}

// add gate supported by pyquafu
// cnot
gate cnot c,t {}
// mcx mcy mcz need more support in parser
// ryy
gate ryy(theta) a,b {}
// cs
gate cs a,b {}
// ct
gate ct a,b {}
// sy
gate sy a {}
// w
gate w a {}
// sw
gate sw a {}
// Toffoli
gate toffoli a,b,c {}
// Fredkin
gate fredkin a,b,c {}
13 changes: 6 additions & 7 deletions quafu/qfasm/qfasm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(self, filepath: str = None, debug=False):
self.stdgate = list(gate_classes.keys())
# extent keyword(the )
self.stdgate.extend(["U", "CX"])
self.mulctrl = ["mcx", "mcz", "mcy"]
self.parser = yacc.yacc(module=self, debug=debug)
# when there is reset/op after measure/if, set to false
self.executable_on_backend = True
Expand Down Expand Up @@ -148,11 +149,7 @@ def updateSymtab(self, symtabnode: SymtabNode):
def handle_gateins(self, gateins: GateInstruction):
gate_list = []
# end of recurse
if gateins.name in self.stdgate and gateins.name not in [
"reset",
"barrier",
"measure",
]:
if gateins.name in self.stdgate and gateins.name not in self.nuop:
args = []
# add qubits to args, it's might be a qubit or a qreg
for qarg in gateins.qargs:
Expand Down Expand Up @@ -188,6 +185,8 @@ def handle_gateins(self, gateins: GateInstruction):
gate_list.append(gate_classes["rz"](*[*oneargs, gateins.cargs[2]]))
gate_list.append(gate_classes["ry"](*[*oneargs, gateins.cargs[0]]))
gate_list.append(gate_classes["rz"](*[*oneargs, gateins.cargs[1]]))
if gateins.name in self.mulctrl:
gate_list.append(gate_classes[gateins.name](oneargs[:-1], oneargs[-1]))
else:
# add carg to args if there is
if gateins.cargs is not None and len(gateins.cargs) > 0:
Expand Down Expand Up @@ -373,7 +372,7 @@ def check_measure_bit(self, gateins: GateInstruction):
def check_qargs(self, gateins: GateInstruction):
# check gatename declared
qargslist = []
if gateins.name not in self.nuop:
if gateins.name not in self.nuop and gateins.name not in self.mulctrl:
if gateins.name not in self.global_symtab:
raise ParserError(
f"The gate {gateins.name} is undefined at line {gateins.lineno} file {gateins.filename}"
Expand Down Expand Up @@ -419,7 +418,7 @@ def check_qargs(self, gateins: GateInstruction):
def check_cargs(self, gateins: GateInstruction):
# check that cargs belongs to unary (they must be int or float)
# cargs is different from CREG
if gateins.name not in self.nuop:
if gateins.name not in self.nuop and gateins.name not in self.mulctrl:
if gateins.name not in self.global_symtab:
raise ParserError(
f"The gate {gateins.name} is undefined at line {gateins.lineno} file {gateins.filename}"
Expand Down

0 comments on commit d058041

Please sign in to comment.