Skip to content

Commit

Permalink
Wrote setup.py so we have a package.
Browse files Browse the repository at this point in the history
Added src/nisqai/utils with utility funcitons.

Minor changes to nisqai/layer ansatz classes.
  • Loading branch information
rmlarose committed Dec 23, 2018
1 parent 3210ca8 commit c1d8775
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 96 deletions.
147 changes: 62 additions & 85 deletions examples/one-qubit-classifier/one-qubit-classifier.ipynb

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,37 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import io
import os

from setuptools import setup, find_packages

# read the __version__ variable from nisqai/_version.py
exec(open("src/nisqai/_version.py").read())

# readme file as long description
long_description = ("======\n" +
"NISQAI\n" +
"======\n")
stream = io.open("README.md", encoding="utf-8")
stream.readline()
long_description += stream.read()

# read in requirements.txt
requirements = open("requirements.txt").readlines()
requirements = [r.strip() for r in requirements]

setup(
name="nisqai",
version=__version__,
author="The NISQAI Developers",
author_email="rlarose@umich.edu",
url="https://github.com/quantumai-lib/nisqai",
description="Library for artificial intelligence on NISQ computers.",
long_description=long_description,
install_requires=requirements,
license="Apache 2",
packages=find_packages(where="src"),
package_dir={"": "src"}
)
3 changes: 3 additions & 0 deletions src/nisqai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from nisqai.layer import *

from ._version import __version__
4 changes: 4 additions & 0 deletions src/nisqai/layer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ._product_ansatz import ProductAnsatz
from ._entangling_ansatz import EntanglingAnsatz
from ._alternating_ansatz import AlternatingAnsatz
2 changes: 1 addition & 1 deletion src/nisqai/layer/_alternating_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from _base_ansatz import BaseAnsatz, REAL_MEM_TYPE
from ._base_ansatz import BaseAnsatz, REAL_MEM_TYPE

class AlternatingAnsatz(BaseAnsatz):
"""Class for two-qubit alternating ansatz."""
Expand Down
4 changes: 4 additions & 0 deletions src/nisqai/layer/_base_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def __str__(self):
# probably want to write a TextDiagramDrawer class like in Cirq
return self.circuit.__str__()

def clear_circuit(self):
"""Clears all instructions in the circuit ansatz."""
self.circuit = Program()

def __add__(self, ansatz):
# TODO: make sure this works with all derived ansatz types
# e.g., make sure ProductAnsatz__sum__ returns a ProductAnsatz, etc.
Expand Down
24 changes: 24 additions & 0 deletions src/nisqai/layer/_entangling_ansatz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ._base_ansatz import BaseAnsatz

class EntanglingAnsatz(BaseAnsatz):
"""Class for entangling ansatz from Schuld et al.,
"Circuit-Centric Quantum Classifiers"
"""

def __init__(self, num_qubits):
super().__init__(num_qubits)

def write_circuit(self):
"""Writes operations into circuit."""
7 changes: 1 addition & 6 deletions src/nisqai/layer/_product_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module for generating and working with product ansatzes."""

from numpy import empty, pi

from pyquil import gates, Program

from _base_ansatz import BaseAnsatz, REAL_MEM_TYPE
from ._base_ansatz import BaseAnsatz, REAL_MEM_TYPE


class ProductAnsatz(BaseAnsatz):
Expand Down Expand Up @@ -46,8 +44,5 @@ def write_circuit(self):
gates.RZ(self.params[q, g], q)
)

def clear_circuit(self):
self.circuit = Program()

# TODO: make sure __sum__ works as intended for ProductAnsatz classe
# (see note in BaseAnsatz.__sum__())
2 changes: 1 addition & 1 deletion src/nisqai/layer/_test_alternating_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from _alternating_ansatz import AlternatingAnzatz
from ._alternating_ansatz import AlternatingAnsatz

def test_basic():
a = AlternatingAnsatz(4)
Expand Down
2 changes: 1 addition & 1 deletion src/nisqai/layer/_test_base_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from pyquil import gates

from _base_ansatz import BaseAnsatz
from ._base_ansatz import BaseAnsatz

def test_basic():
b = BaseAnsatz(4)
Expand Down
14 changes: 13 additions & 1 deletion src/nisqai/layer/_test_entangling_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.

from ._entangling_ansatz import EntanglingAnsatz


def test_basic():
e = EntanglingAnsatz(4)
assert e.num_qubits == 4


if __name__ == "__main__":
test_basic()
print("All unit tests for EntanglingAnsatz passed.")
4 changes: 3 additions & 1 deletion src/nisqai/layer/_test_product_ansatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from _product_ansatz import ProductAnsatz
from ._product_ansatz import ProductAnsatz

def test_basic():
p = ProductAnsatz(4)
assert p.num_qubits == 4

# TODO: add more test cases!!!


if __name__ == "__main__":
test_basic()
Expand Down
13 changes: 13 additions & 0 deletions src/nisqai/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ._program_utils import order
19 changes: 19 additions & 0 deletions src/nisqai/utils/_program_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pyquil.quil import percolate_declares

def order(program):
"""Orders Quil instructions into a nominal form."""
# TODO: define nominal form and add more ordering conditions
# right now, this just means all DECLARE statements are at the top
return percolate_declares(program)
25 changes: 25 additions & 0 deletions src/nisqai/utils/_test_program_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ._program_utils import order
from pyquil import Program, gates

def test_order_basic():
"""Tests an ordered program is in the nominal form."""
p = Program(gates.H(0))
p.declare("ro")
print(order(p))


if __name__ == "__main__":
test_order_basic()
print("All unit tests for program_utils passed.")

0 comments on commit c1d8775

Please sign in to comment.