Skip to content

Commit

Permalink
Copy over initial braket-python-qdk implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Bolt🐾 committed Dec 13, 2019
1 parent 1110e20 commit 89f2915
Show file tree
Hide file tree
Showing 65 changed files with 6,870 additions and 6 deletions.
21 changes: 21 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[run]
parallel = True
branch = True
source =
braket
omit =
**/braket/ir/*

[paths]
source =
src/braket
.tox/*/lib/python*/site-packages/braket

[report]
show_missing = True

[html]
directory = build/coverage

[xml]
output = build/coverage/coverage.xml
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
*~
*#
*.swp
*.idea
*.DS_Store
build_files.tar.gz

.ycm_extra_conf.py
.tox
.python-version

__pycache__/
*.py[cod]
*$py.class
*.egg-info/
*.ipynb_checkpoints/

/.coverage
/.coverage.*
/.cache
/.pytest_cache
/.mypy_cache

/doc/_apidoc/
/build
/venv
18 changes: 18 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
repos:
- repo: local
hooks:
- id: tox
name: tox
stages: [push]
language: system
entry: tox
types: [python]
pass_filenames: false

- id: tox_integ_tests
name: tox integ tests
stages: [push]
language: system
entry: tox -e integ-tests
types: [python]
pass_filenames: false
145 changes: 139 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,146 @@
## My Project
**DO NOT SHARE OR TALK ABOUT THE CONTENTS OF THIS LIBRARY per the Amazon Beta NDA you signed.**

TODO: Fill this README out!
Amazon Braket Python SDK is an open source library for interacting with quantum devices on Amazon Braket.

Be sure to:
## Installation

* Change the title in this README
* Edit your repository description on GitHub
### Prerequisites
- Python 3.7.2+

### Steps

1. Get a confirmation email from Amazon Braket confirming you have access to the service.

2. (Optional) Recommended to work inside a virtual environment. You can skip this step if you don't care about mucking with your global python dependencies. See [virtualenv](https://virtualenv.pypa.io/en/stable/installation/) if you don't have it already installed.

```bash
mkdir braket
cd braket

virtualenv venv
source venv/bin/activate
```

2. Install [AWS CLI](https://github.com/aws/aws-cli#installation)

```bash
pip install awscli
```

3. [Configure AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) settings to have a profile that can access your AWS account. Once a profile has been created set the `AWS_PROFILE` such that all future steps can connect to your AWS account.

```bash
export AWS_PROFILE=YOUR_PROFILE_NAME
```

4. Install `braket-python-sdk` package.

```bash
git clone https://github.com/aws/braket-python-sdk.git
pip install -e braket-python-sdk
```

To install test dependencies for running tests locally run:
```bash
pip install -e "braket-python-sdk[test]"
```

5. Install latest Amazon Braket model in AWS CLI.

```bash
aws s3 cp s3://braket-external-assets-prod-us-west-2/models/aqx-2019-09-01.normal.json aqx-model.json
aws configure add-model --service-model "file://aqx-model.json" --service-name aqx
```

6. Create the necessary Amazon Braket resources in your AWS account.

This will create the required resources and a default S3 bucket, `braket-output-${AWS::AccountId}`, for storing Amazon Braket outputs. If you don't want to create a bucket and will create your own than drop the `--create-default-bucket` from the command below.
```bash
python tools/create_braket_resources.py --create-default-bucket
```

7. You can now call Amazon Braket from the `braket-python-sdk`.

```python
from braket.circuits import Circuit
from braket.aws import AwsQuantumSimulator

device = AwsQuantumSimulator("arn:aws:aqx:::quantum-simulator:aqx:qs1")
s3_folder = ("braket-output-AWS_ACCOUNT_ID", "folder-name")

bell = Circuit().h(0).cnot(0, 1)
print(device.run(bell, s3_folder).result().measurement_counts)
```

You should get output similar to...
```
Counter({'11': 50, '00': 50})
```

7. Install [Jupyter](https://jupyter.org/install) and create an braket kernel.
```
pip install jupyter ipykernel
python -m ipykernel install --user --name braket
```

8. You can now launch Jupyter and use the SDK within it.
```
jupyter notebook
```

## Sample Notebooks
TODO

## Documentation

First `cd` into the `doc` directory and run:
```bash
make html
```

Then open `BRAKET_SDK_ROOT/build/documentation/html/index.html` in a browser to view the docs.

## Testing

Make sure to install test dependencies first:
```
pip install -e "braket-python-sdk[test]"
```

### Unit Tests
```
tox -e unit-tests
```

To run an individual test
```bash
tox -e unit-tests -- -k 'your_test'
```

To run linters and doc generators and unit tests
```bash
tox
```

### Integration Tests

Set the `AWS_PROFILE`
```bash
export AWS_PROFILE=PROFILE_FROM_STEP_3
```

Create an S3 bucket in the same account as the `AWS_PROFILE` with the following naming convention `braket-sdk-integ-tests-{account_id}`.

Run the tests
```bash
tox -e integ-tests
```

To run an individual test
```bash
tox -e integ-tests -- -k 'your_test'
```

## License

This project is licensed under the Apache-2.0 License.

57 changes: 57 additions & 0 deletions bin/apply-header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os

"""
Run from the root of the git repository, will apply contents of header to the beginning of
all *.py files in the chosen directories. Script is idempotent, meaning it won't apply the
header to a file that already contains it.
Usage: python bin/apply-header.py
"""

HEADER = """# Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
"""

ROOT_DIRS = ["src", "test", "."]


def main():
for root_dir in ROOT_DIRS:
for root, dirs, files in os.walk(root_dir):
for py_file in python_files(files):
idempotent_prepend(os.path.join(root, py_file), HEADER)

# don't recurse "." directory, just look at local files
if root_dir == ".":
break


def python_files(files):
return [file for file in files if file.endswith("py")]


def idempotent_prepend(filename: str, new_content: str) -> None:
with open(filename, "r+") as file:
existing_content = file.read()

if existing_content.startswith(new_content):
print(f"Skipping {filename}, already contains the content.")
else:
print(f"Applying content to {filename}...")
file.seek(0, 0)
file.write(new_content + existing_content)


if __name__ == "__main__":
main()
20 changes: 20 additions & 0 deletions doc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = python -msphinx
SPHINXPROJ = braket-sdk
SOURCEDIR = .
BUILDDIR = ../build/documentation

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
30 changes: 30 additions & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Sphinx configuration."""
import datetime

import pkg_resources

# Sphinx configuration below.
project = "braket-sdk"
version = pkg_resources.require(project)[0].version
release = version
copyright = "{}, Amazon.com".format(datetime.datetime.now().year)

extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.viewcode",
"sphinx.ext.napoleon",
"sphinx.ext.todo",
"sphinx.ext.coverage",
]

source_suffix = ".rst"
master_doc = "index"

autoclass_content = "both"
autodoc_member_order = "bysource"
default_role = "py:obj"

html_theme = "sphinx_rtd_theme"
htmlhelp_basename = "{}doc".format(project)

napoleon_use_rtype = False
16 changes: 16 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Amazon Braket Python SDK
========================

Amazon Braket Python SDK is an open source library for interacting with quantum devices on Amazon Braket.

TODO describe the different feature sets / abstractions.

Here you'll find an overview and API documentation for Amazon Braket Python SDK. The project homepage is in GitHub, https://github.com/aws/braket-python-sdk, where you can find the SDK source and installation instructions for the library.

Indices and tables
__________________

* :doc:`_apidoc/modules`
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
line-length = 100
27 changes: 27 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[aliases]
test=pytest

[tool:pytest]
xfail_strict = true
addopts =
--verbose
testpaths = test/unit_tests

[isort]
line_length = 100
multi_line_output = 3
include_trailing_comma = true

[flake8]
ignore =
E203, # not pep8, black adds whitespace before ':'
W503, # not pep8, black adds line break before binary operator
max_line_length = 100
max-complexity = 10
exclude =
__pycache__
.tox
.git
bin
build
venv
Loading

0 comments on commit 89f2915

Please sign in to comment.