Skip to content

Commit

Permalink
Improve QASM parser to handle multi-character register names
Browse files Browse the repository at this point in the history
  • Loading branch information
contra-bit committed Dec 17, 2024
1 parent 541e1e5 commit f4c5023
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pyzx/circuit/qasmparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,18 @@ def extract_command_parts(self, c: str) -> Tuple[str,List[Fraction],List[str]]:
def parse_command(self, c: str, registers: Dict[str,Tuple[int,int]]) -> List[Gate]:
gates: List[Gate] = []
name, phases, args = self.extract_command_parts(c)
if name in ("barrier","creg","measure", "id"): return gates
if name in ("barrier","creg", "id"): return gates
if name == "measure":
target, result_bit = args[0].split(' -> ')
# Extract the register name and index separately for both target and result
target_reg, target_idx = target.split('[')
result_reg, result_idx = result_bit.split('[')
# Remove the trailing ']' and convert to int
target_qbit = int(target_idx[:-1])
result_bit = int(result_idx[:-1])
gate = Measurement(target_qbit, result_bit)
gates.append(gate)
return gates
if name in ("opaque", "if"):
raise TypeError("Unsupported operation {}".format(c))
if name == "qreg":
Expand All @@ -154,9 +165,12 @@ def parse_command(self, c: str, registers: Dict[str,Tuple[int,int]]) -> List[Gat
dim = 1
for a in args:
if "[" in a:
# Split at the first '[' to handle multi-character register names
regname, valp = a.split("[",1)
# Remove the trailing ']' before converting to int
val = int(valp[:-1])
if regname not in registers: raise TypeError("Invalid register {}".format(regname))
if regname not in registers:
raise TypeError("Invalid register {}".format(regname))
qubit_values.append([registers[regname][0]+val])
else:
if is_range:
Expand Down

0 comments on commit f4c5023

Please sign in to comment.