Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

In qasmparser.py, reset the parser state between calls to parse. #150

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()