From d418bd1a574302c228e3c7b25cf0a916d0109fb2 Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Fri, 12 Mar 2021 23:33:26 +1300 Subject: [PATCH 1/9] two simple test fixes --- src/qeda/qast.py | 2 +- src/tests/qast_tests.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qeda/qast.py b/src/qeda/qast.py index b234346..35f77a8 100755 --- a/src/qeda/qast.py +++ b/src/qeda/qast.py @@ -6,7 +6,7 @@ config = CONFIG # Import gate primitives -from qeda.qast_primitives import Int, Char, Real, String, BinaryOp, Component, UQGate, CQGate, CustomQGate +from qeda.qast_primitives import End, Int, Char, Real, String, BinaryOp, Component, UQGate, CQGate, CustomQGate class OpenQASM(): diff --git a/src/tests/qast_tests.py b/src/tests/qast_tests.py index 713594f..1c13688 100644 --- a/src/tests/qast_tests.py +++ b/src/tests/qast_tests.py @@ -172,7 +172,7 @@ def test_cu3_gate(self): def test_measurement(self): self.assertEqual(self.measure(0).eval(), - (qast.LIB, 'MEASURE')) + (qast.LIB, 'Measure')) if __name__ == '__main__': From 3432af8eefb6478f74572dff143ef28e3d919ee2 Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Fri, 12 Mar 2021 23:34:11 +1300 Subject: [PATCH 2/9] start github workflow for running tests --- .github/workflows/tests.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..c8bce4e --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,24 @@ +name: tests +on: push +jobs: + python_tests: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.6, 3.8] + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: Run existing unittests + run: | + cd src + python3 -m tests.lexer_tests + python3 -m tests.parser_tests + python3 -m tests.qast_tests From 2177d021f0af0e6c7ca0a17be9fb5efa2780faea Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Fri, 12 Mar 2021 23:40:13 +1300 Subject: [PATCH 3/9] move requirements.txt to root and alpha sort --- src/requirements.txt => requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/requirements.txt => requirements.txt (100%) diff --git a/src/requirements.txt b/requirements.txt similarity index 100% rename from src/requirements.txt rename to requirements.txt index 77763f7..b3ca633 100644 --- a/src/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ +numpy pykicad rply -numpy From 7b189ba8b8806e405ce9f2323ae7f11a26bfadf6 Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Sat, 13 Mar 2021 00:55:00 +1300 Subject: [PATCH 4/9] renable qast tests which pass, separate + skip controlled gates for now --- src/qeda/qast_primitives.py | 1 - src/tests/qast_tests.py | 76 +++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/qeda/qast_primitives.py b/src/qeda/qast_primitives.py index bba8654..0df9b55 100755 --- a/src/qeda/qast_primitives.py +++ b/src/qeda/qast_primitives.py @@ -98,7 +98,6 @@ def _add_qcomponent(self, qid): QCODE[qid].append(self.footprint) except Exception as e: print("exception", e) - pass def eval(self): return self.footprint, QCODE diff --git a/src/tests/qast_tests.py b/src/tests/qast_tests.py index 1c13688..5bb0140 100644 --- a/src/tests/qast_tests.py +++ b/src/tests/qast_tests.py @@ -1,5 +1,6 @@ import unittest from tests.context import qast +from qeda.qast import CustomQGate class QAST_Tests(unittest.TestCase): @@ -11,8 +12,6 @@ def setUp(self): self.binop = qast.BinaryOp self.component = qast.Component self.uq_gate = qast.UQGate - self.cq_gate = qast.CQGate - self.custom_q_gate = qast.CustomQGate self.h = qast.H self.i = qast.I self.s = qast.S @@ -25,14 +24,6 @@ def setUp(self): self.rx = qast.RX self.ry = qast.RY self.rz = qast.RZ - self.cx = qast.CX - self.cy = qast.CYGate - self.cz = qast.CZGate - self.ch = qast.CHGate - self.ccx = qast.CCXGate - self.crz = qast.CRZGate - self.cu1 = qast.CU1Gate - self.cu3 = qast.CU3Gate self.measure = qast.Measure def test_end(self): @@ -51,25 +42,11 @@ def test_binop(self): pass def test_component(self): - self.assertEqual(self.component().eval(), None) + self.assertEqual(self.component().eval(), (None, {})) def test_uq_gate(self): self.assertEqual(self.uq_gate(1).eval(), None) - def test_cq_gate(self): - control = 1 - targets = [2, 3, 4, 5] - self.assertEqual(self.cq_gate(control=control, targets=targets).eval(), - (control, targets, None)) - - def test_custom_q_gate(self): - self.assertEqual(self.custom_q_gate('test').eval(), - (None, None, None)) - control = 1 - targets = [2, 3, 4] - self.assertEqual(self.custom_q_gate('test', control, targets).eval(), - (control, targets, None)) - def test_h_gate(self): self.assertEqual(self.h(0).eval(), (qast.LIB, 'HGate')) @@ -114,6 +91,40 @@ def test_rz_gate(self): self.assertEqual(self.rz(0).eval(), (qast.LIB, 'RZGate')) + def test_measurement(self): + self.assertEqual(self.measure(0).eval(), + (qast.LIB, 'Measure')) + + +class testCustomGates(unittest.TestCase): + def test_CustomQGate(self): + self.assertEqual(CustomQGate('test').eval(), + (None, None, None)) + control = 1 + targets = [2, 3, 4] + self.assertEqual(CustomQGate('test', control, targets).eval(), + (control, targets, None)) + + +@unittest.skip("Commit 8965482: 'macro component footprints ... needs a rewrite for controlled and custom gates.") +class testControlledGates(unittest.TestCase): + def setUp(self): + self.cq_gate = qast.CQGate + self.cx = qast.CX + self.cy = qast.CYGate + self.cz = qast.CZGate + self.ch = qast.CHGate + self.ccx = qast.CCXGate + self.crz = qast.CRZGate + self.cu1 = qast.CU1Gate + self.cu3 = qast.CU3Gate + + def test_cq_gate(self): + control = 1 + targets = [2, 3, 4, 5] + self.assertEqual(self.cq_gate(control=control, targets=targets).eval(), + (control, targets, None)) + def test_cx_gate(self): control = 1 target = 2 @@ -124,16 +135,19 @@ def test_cx_gate(self): def test_cy_gate(self): control = 1 target = 2 - self.assertEqual(self.cy(control, target).eval(), + self.assertEqual(self.cy(control, target).eval()[0], (control, target, (qast.LIB, 'CYGate'))) def test_cz_gate(self): control = 1 target = 2 - self.assertEqual(self.cz(control, target).eval(), - (control, target, - (qast.LIB, 'CZGate'))) + result = self.cz(control, target).eval() + # TODO: Not sure what the expectations should be here... + # Global QCODE is accumulating gates in a list against control + self.assertEqual(len(result), 2) + self.assertIn(control, result[1]) + self.assertEqual(result[0], (qast.LIB, 'CZGate')) def test_ch_gate(self): control = 1 @@ -170,10 +184,6 @@ def test_cu3_gate(self): (control, target, (qast.LIB, 'CU3Gate'))) - def test_measurement(self): - self.assertEqual(self.measure(0).eval(), - (qast.LIB, 'Measure')) - if __name__ == '__main__': unittest.main() From 91d9d1b2d368fef95afbf4e1ec10227d03f67dcb Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Thu, 4 Mar 2021 18:03:47 +1300 Subject: [PATCH 5/9] allow adder.qasm to be processed --- src/qeda/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qeda/parser.py b/src/qeda/parser.py index fbc49ae..69b5d13 100755 --- a/src/qeda/parser.py +++ b/src/qeda/parser.py @@ -121,7 +121,7 @@ def uop(p): if len(p) == 5: return CX(p[1], p[3]) else: - return CX(p[0][0], p[0][1]) + return CX(p[1][0], p[1][1]) elif p[0].name == 'ID': V('a', p) if p[0].value.lower() == 'h': From bbc5e624688da2b4194c9db5a360f6667361a7b6 Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Sat, 13 Mar 2021 01:51:45 +1300 Subject: [PATCH 6/9] convert type checks to isinstance() --- src/qeda/qast_primitives.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/qeda/qast_primitives.py b/src/qeda/qast_primitives.py index 0df9b55..2c74fd3 100755 --- a/src/qeda/qast_primitives.py +++ b/src/qeda/qast_primitives.py @@ -10,9 +10,6 @@ class End(): - def __init__(self): - global QCODE - def eval(self): return QCODE, 0 @@ -63,8 +60,6 @@ class Component: """ def __init__(self): global QCODE - global SEL - global LIB self.footprint = None self.qid = None @@ -77,7 +72,7 @@ def _add_qcomponent(self, qid): """Adds the components to master dictionary""" try: # If the qid is of type Number we need to convert to integer - if type(qid) != type(1): + if not isinstance(qid, int): qid = qid.eval() # If component is applied to all qubits if qid == -1: @@ -87,7 +82,6 @@ def _add_qcomponent(self, qid): elif qid == -2: for key, value in QCODE.items(): QCODE[key].append(self.footprint) - pass # If the qubit isn't already listed, add new qubit # qcode = { 0: [], 1: [],...} elif qid not in QCODE: @@ -134,12 +128,12 @@ def _set_target(self, target, indicator): def set_targets(self): indicator = self.footprint + 'TGT' - if 'Int' in str(type(self.targets)): + if isinstance(self.targets, Int): self.targets.eval() self._set_target(self.targets, indicator) else: for target in self.targets: - if type(target) != type(int): + if not isinstance(target, int): target.eval() self._set_target(target, indicator) # def eval(self): From 5e8b5169128cb2a1b03c65d7c246e7adf50222ae Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Sat, 13 Mar 2021 01:59:51 +1300 Subject: [PATCH 7/9] Fixed HGate: was an exact copy of Y --- src/kicad/library/QMacro.pretty/HGate.kicad_mod | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/kicad/library/QMacro.pretty/HGate.kicad_mod b/src/kicad/library/QMacro.pretty/HGate.kicad_mod index 5cad769..1af55ee 100644 --- a/src/kicad/library/QMacro.pretty/HGate.kicad_mod +++ b/src/kicad/library/QMacro.pretty/HGate.kicad_mod @@ -1,9 +1,9 @@ -(module Y-Gate (layer F.Cu) (tedit 5E37F349) - (tags "Macro, Y, Pauli, PauliY, Quantum") - (fp_text reference Y** (at 0 0.5) (layer F.SilkS) +(module H-Gate (layer F.Cu) (tedit 5E37F349) + (tags "Macro, H, Hadamard, Quantum") + (fp_text reference H** (at 0 0.5) (layer F.SilkS) (effects (font (size 1 1) (thickness 0.15))) ) - (fp_text value Y-Gate (at 0 7.62) (layer F.Fab) + (fp_text value H-Gate (at 0 7.62) (layer F.Fab) (effects (font (size 1 1) (thickness 0.15))) ) (fp_line (start -7.62 -6.35) (end 7.62 -6.35) (layer F.SilkS) (width 0.15)) From a80f5cf35fc2cfa5d11a32991cf8471d3d0c0abe Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Sat, 13 Mar 2021 02:41:12 +1300 Subject: [PATCH 8/9] correct ZGate labeling --- src/kicad/library/QMacro.pretty/ZGate.kicad_mod | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/kicad/library/QMacro.pretty/ZGate.kicad_mod b/src/kicad/library/QMacro.pretty/ZGate.kicad_mod index 5cad769..dd51494 100644 --- a/src/kicad/library/QMacro.pretty/ZGate.kicad_mod +++ b/src/kicad/library/QMacro.pretty/ZGate.kicad_mod @@ -1,9 +1,9 @@ -(module Y-Gate (layer F.Cu) (tedit 5E37F349) - (tags "Macro, Y, Pauli, PauliY, Quantum") - (fp_text reference Y** (at 0 0.5) (layer F.SilkS) +(module Z-Gate (layer F.Cu) (tedit 5E37F349) + (tags "Macro, Z, Pauli, PauliZ, Quantum") + (fp_text reference Z** (at 0 0.5) (layer F.SilkS) (effects (font (size 1 1) (thickness 0.15))) ) - (fp_text value Y-Gate (at 0 7.62) (layer F.Fab) + (fp_text value Z-Gate (at 0 7.62) (layer F.Fab) (effects (font (size 1 1) (thickness 0.15))) ) (fp_line (start -7.62 -6.35) (end 7.62 -6.35) (layer F.SilkS) (width 0.15)) From 64cd05a010e259960aa9928a825da702b2a9d21a Mon Sep 17 00:00:00 2001 From: Charles Horn Date: Sat, 13 Mar 2021 17:40:21 +1300 Subject: [PATCH 9/9] correct kicad tags --- src/kicad/library/QMacro.pretty/CYGate.kicad_mod | 2 +- src/kicad/library/QMacro.pretty/CZGate.kicad_mod | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kicad/library/QMacro.pretty/CYGate.kicad_mod b/src/kicad/library/QMacro.pretty/CYGate.kicad_mod index 2d54606..02131ac 100644 --- a/src/kicad/library/QMacro.pretty/CYGate.kicad_mod +++ b/src/kicad/library/QMacro.pretty/CYGate.kicad_mod @@ -1,5 +1,5 @@ (module CY-Gate (layer F.Cu) (tedit 5E49C1FF) - (tags "Macro, CX, Quantum, Controlled") + (tags "Macro, CY, Quantum, Controlled") (fp_text reference CY** (at 0 5.715) (layer F.SilkS) (effects (font (size 1 1) (thickness 0.15))) ) diff --git a/src/kicad/library/QMacro.pretty/CZGate.kicad_mod b/src/kicad/library/QMacro.pretty/CZGate.kicad_mod index c45bded..06f2c18 100644 --- a/src/kicad/library/QMacro.pretty/CZGate.kicad_mod +++ b/src/kicad/library/QMacro.pretty/CZGate.kicad_mod @@ -1,5 +1,5 @@ (module CZ-Gate (layer F.Cu) (tedit 5E49C1EA) - (tags "Macro, CX, Quantum, Controlled") + (tags "Macro, CZ, Quantum, Controlled") (fp_text reference CZ** (at 0 5.715) (layer F.SilkS) (effects (font (size 1 1) (thickness 0.15))) )