Skip to main content

An open source library that contains the schemas for Amazon Braket

Reason this release was yanked:

Deprecated

Project description

Amazon Braket Python Schemas

Latest Version Supported Python Versions Build Status codecov Documentation Status Code Style: Black

Amazon Braket Python Schemas is an open source library that contains the schemas for Braket, including:

  • intermediate representations (IR) for Amazon Braket quantum tasks and offers serialization and deserialization of those IR payloads. Think of the IR as the contract between the Amazon Braket SDK and Amazon Braket API for quantum programs.
  • schemas for the S3 results of each quantum task
  • schemas for the device capabilities of each device

Installation

Prerequisites

  • Python 3.7+

Steps

The preferred way to get Amazon Braket Python Schemas is by installing the Amazon Braket Python SDK, which will pull in the schemas. Follow the instructions in the README for setup.

However, if you only want to use the schemas, it can be installed on its own as follows:

pip install amazon-braket-schemas

You can install from source by cloning this repository and running a pip install command in the root directory of the repository:

git clone https://github.com/aws/amazon-braket-schemas-python.git
cd amazon-braket-schemas-python
pip install .

You can check your currently installed version of amazon-braket-schemas with pip show:

pip show amazon-braket-schemas

or alternatively from within Python:

>>> import braket._schemas as braket_schemas
>>> braket_schemas.__version__

Usage

There are currently two types of IR, including jacqd (JsonAwsQuantumCircuitDescription) and annealing. See below for their usage.

Serializing python structures

from braket.ir.jaqcd import CNot, H, Program, Expectation
from braket.ir.annealing import Problem, ProblemType

program = Program(instructions=[H(target=0), CNot(control=0, target=1)])
print(program.json(indent=2))

"""
{
  "braketSchemaHeader": {
    "name": "braket.ir.jaqcd.program",
    "version": "1"
  },
  "instructions": [
    {
      "target": 0,
      "type": "h"
    },
    {
      "control": 0,
      "target": 1,
      "type": "cnot"
    }
  ],
  "results": null,
  "basis_rotation_instructions": null,
}
"""

program = Program(
    instructions=[H(target=0), CNot(control=0, target=1)],
    results=[Expectation(targets=[0], observable=['x'])],
    basis_rotation_instructions=[H(target=0)]
)
print(program.json(indent=2))

"""
{
  "braketSchemaHeader": {
    "name": "braket.ir.jaqcd.program",
    "version": "1"
  },
  "instructions": [
    {
      "target": 0,
      "type": "h"
    },
    {
      "control": 0,
      "target": 1,
      "type": "cnot"
    }
  ],
  "results": [
    {
      "observable": [
        "x"
      ],
      "targets": [
        0
      ],
      "type": "expectation"
    }
  ],
  "basis_rotation_instructions": [
    {
      "target": 0,
      "type": "h"
    }
  ]
}
"""

problem = Problem(type=ProblemType.QUBO, linear={0: 0.3, 4: -0.3}, quadratic={"0,5": 0.667})
print(problem.json(indent=2))

"""
{
  "braketSchemaHeader": {
    "name": "braket.ir.annealing.problem",
    "version": "1"
  },
  "type": "QUBO",
  "linear": {0: 0.3, 4: -0.3},
  "quadratic": {"0,5": 0.667}
}
"""

Deserializing into python structures

from braket.ir.jaqcd import Program
from braket.ir.annealing import Problem

jaqcd_string = """
{
  "instructions": [
    {
      "target": 0,
      "type": "h"
    },
    {
      "control": 0,
      "target": 1,
      "type": "cnot"
    }
  ],
  "results": [
    {
      "observable": [
        "x"
      ],
      "targets": [
        0
      ],
      "type": "expectation"
    }
  ],
  "basis_rotation_instructions": [
    {
      "target": 0,
      "type": "h"
    }
  ]
}
"""

program = Program.parse_raw(jaqcd_string)
print(program)

"""
braketSchemaHeader=BraketSchemaHeader(name='braket.ir.jaqcd.program', version='1') instructions=[H(target=0, type=<Type.h: 'h'>), CNot(control=0, target=1, type=<Type.cnot: 'cnot'>)] results=[Expectation(observable=['x'], targets=[0], type=<Type.expectation: 'expectation'>)] basis_rotation_instructions=[H(target=0, type=<Type.h: 'h'>)]
"""

annealing_string = """
{
  "type": "QUBO",
  "linear": {0: 0.3, 4: -0.3},
  "quadratic": {"0,5": 0.667}
}
"""

problem = Problem.parse_raw(annealing_string)
print(problem)

"""
braketSchemaHeader=BraketSchemaHeader(name='braket.ir.annealing.problem', version='1') type=<ProblemType.QUBO: 'QUBO'>, linear={0: 0.3, 4: -0.3}, quadratic={'0,5': 0.667}
"""

Documentation

Detailed documentation, including the API reference, can be found on Read the Docs.

You can also generate the docs from source. First, install tox:

pip install tox

To build the Sphinx docs, run the following command in the root repo directory:

tox -e docs

You can then find the generated HTML files in build/documentation/html.

Testing

Make sure to install test dependencies first:

pip install -e "amazon-braket-schemas-python[test]"

To run the unit tests:

tox -e unit-tests

You can also pass in various pytest arguments to run selected tests:

tox -e unit-tests -- your-arguments

To run linters and doc generators and unit tests:

tox

For more information, please see pytest usage.

License

This project is licensed under the Apache-2.0 License.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

amazon-braket-schemas-1.2.1.tar.gz (29.3 kB view details)

Uploaded Source

Built Distribution

amazon_braket_schemas-1.2.1-py3-none-any.whl (67.3 kB view details)

Uploaded Python 3

File details

Details for the file amazon-braket-schemas-1.2.1.tar.gz.

File metadata

  • Download URL: amazon-braket-schemas-1.2.1.tar.gz
  • Upload date:
  • Size: 29.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.2.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for amazon-braket-schemas-1.2.1.tar.gz
Algorithm Hash digest
SHA256 e823c9046b172051c3a6f969533bf2aaa076fd7511c5227dab2973e1c1f67061
MD5 93372c0dea741b568bff3c3db33e3b33
BLAKE2b-256 e9c0a94847a52d1208e384ec514397fc8ce0596e33e6d1b93a7db22b06d5806e

See more details on using hashes here.

File details

Details for the file amazon_braket_schemas-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: amazon_braket_schemas-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 67.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.2.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for amazon_braket_schemas-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f41724bae872800ef176e3fc31aea58630a1e5e6e9497463e12a07fa90dd0ea
MD5 2aa048a0555a5e5dc64ee59cf3ffea41
BLAKE2b-256 b7fb6ab9f455df7ee7a85d5d0ee6015c438cd0aa543f998b902942b60119d982

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page