Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Enable Github actions tests #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
24 changes: 24 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/requirements.txt → requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
numpy
pykicad
rply
numpy
8 changes: 4 additions & 4 deletions src/kicad/library/QMacro.pretty/HGate.kicad_mod
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/qeda/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
2 changes: 1 addition & 1 deletion src/qeda/qast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
13 changes: 3 additions & 10 deletions src/qeda/qast_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@


class End():
def __init__(self):
global QCODE

def eval(self):
return QCODE, 0

Expand Down Expand Up @@ -63,8 +60,6 @@ class Component:
"""
def __init__(self):
global QCODE
global SEL
global LIB
self.footprint = None
self.qid = None

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -98,7 +92,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
Expand Down Expand Up @@ -135,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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure about this one. The original type(int) is <class 'type'> , I'm assuming this is a mistake and the condition is supposed to be if target is not a Python int (as opposed to a qast_primitives.Int / qast.Int)

target.eval()
self._set_target(target, indicator)
# def eval(self):
Expand Down
76 changes: 43 additions & 33 deletions src/tests/qast_tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from tests.context import qast
from qeda.qast import CustomQGate


class QAST_Tests(unittest.TestCase):
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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'))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()