Skip to content

Commit

Permalink
Merge pull request #150 from dlyongemallo/reset_parser_state
Browse files Browse the repository at this point in the history
In qasmparser.py, reset the parser state between calls to `parse`.
  • Loading branch information
jvdwetering authored Sep 6, 2023
2 parents dcb21f0 + c675e66 commit 71458b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pyzx/circuit/qasmparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def __init__(self) -> None:
self.circuit: Optional[Circuit] = None

def parse(self, s: str, strict:bool=True) -> Circuit:
self.gates = []
self.customgates = {}
self.registers = {}
self.qubit_count = 0
self.circuit = None
lines = s.splitlines()
r = []
#strip comments
Expand Down Expand Up @@ -62,7 +67,6 @@ def parse(self, s: str, strict:bool=True) -> Circuit:
data = data[:i] + data[j+1:]
#parse the regular commands
commands = [s.strip() for s in data.split(";") if s.strip()]
gates: List[Gate] = []
for c in commands:
self.gates.extend(self.parse_command(c, self.registers))

Expand Down
17 changes: 16 additions & 1 deletion tests/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,20 @@ def test_verify_equality_permutation_option(self):
self.assertTrue(c1.verify_equality(c2,up_to_swaps=True))
self.assertFalse(c1.verify_equality(c2,up_to_swaps=False))

def test_parser_state_reset(self):
from pyzx.circuit.qasmparser import QASMParser
s = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
h q[0];
"""
p = QASMParser()
c1 = p.parse(s)
c2 = p.parse(s)
self.assertEqual(c2.qubits, 1)
self.assertEqual(len(c2.gates), 1)
self.assertTrue(c1.verify_equality(c2))

if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 71458b0

Please sign in to comment.