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

Add results to jacqd program #12

Merged
merged 4 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# the repo. Unless a later match takes precedence, these accounts
# will be requested forreview when someone opens a pull request.
* @floralph

* @avawang1
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Amazon Braket Python IR is an open source library that contains all the intermed
```

## Usage
There is currently only one kind of IR and that is the JsonAwsQuantumCircuitDescription, jaqcd. See below for it's usage.
There are currently two types of IR, including jacqd (JsonAwsQuantumCircuitDescription) and annealing. See below for their usage.

**Serializing python structures**
```python
from braket.ir.jaqcd import CNot, H, Program
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)])
Expand All @@ -41,6 +41,37 @@ print(program.json(indent=2))
"target": 1,
"type": "cnot"
}
],
"results": null
}
"""

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

"""
{
"instructions": [
{
"target": 0,
"type": "h"
},
{
"control": 0,
"target": 1,
"type": "cnot"
}
],
"results": [
{
"observable": [
"x"
],
"targets": [
1
],
"type": "expectation"
}
]
}
"""
Expand Down Expand Up @@ -74,6 +105,17 @@ jaqcd_string = """
"target": 1,
"type": "cnot"
}
],
"results": [
{
"observable": [
"x"
],
"targets": [
1
],
"type": "expectation"
}
]
}
"""
Expand All @@ -82,7 +124,7 @@ program = Program.parse_raw(jaqcd_string)
print(program)

"""
instructions=[H(target=0, type=<Type.h: 'h'>), CNot(control=0, target=1, type=<Type.cnot: 'cnot'>)]
instructions=[H(target=0, type=<Type.h: 'h'>), CNot(control=0, target=1, type=<Type.cnot: 'cnot'>)] results=[Expectation(observable=['x'], targets=[1], type=<Type.expectation: 'expectation'>)]
"""

annealing_string = """
Expand Down
8 changes: 8 additions & 0 deletions src/braket/ir/jaqcd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@
Z,
)
from braket.ir.jaqcd.program import Program # noqa: F401
from braket.ir.jaqcd.results import ( # noqa: F401
Amplitude,
Expectation,
Probability,
Sample,
StateVector,
Variance,
)
13 changes: 12 additions & 1 deletion src/braket/ir/jaqcd/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

from typing import List, Union
from typing import List, Optional, Union

from braket.ir.jaqcd.instructions import (
CY,
Expand Down Expand Up @@ -47,6 +47,14 @@
Y,
Z,
)
from braket.ir.jaqcd.results import (
Amplitude,
Expectation,
Probability,
Sample,
StateVector,
Variance,
)
from pydantic import BaseModel


Expand Down Expand Up @@ -97,3 +105,6 @@ class Program(BaseModel):
ZZ,
]
]
results: Optional[
floralph marked this conversation as resolved.
Show resolved Hide resolved
List[Union[Amplitude, Expectation, Probability, Sample, StateVector, Variance]]
]
144 changes: 144 additions & 0 deletions src/braket/ir/jaqcd/results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# 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.

from enum import Enum

from braket.ir.jaqcd.shared_models import Observable, OptionalMultiState, OptionalMultiTarget


class Expectation(OptionalMultiTarget, Observable):
"""
Expectation of specified targets and observable as requested result.

Attributes:
avawang1 marked this conversation as resolved.
Show resolved Hide resolved
type (str): The result type. default = "expectation". (type) is optional.
This should be unique among all result types.
targets (Optional[List[int]]): The target qubits. This is a list of int >= 0.

Examples:
>>> Expectation(targets=[1], observable=["x"])
"""

class Type(str, Enum):
expectation = "expectation"

type = Type.expectation


class Sample(OptionalMultiTarget, Observable):
"""
Sample for specified targets and observable as requested result.

Attributes:
type (str): The result type. default = "sample". (type) is optional.
This should be unique among all result types.
targets (Optional[List[int]]): The target qubits. This is a list of int >= 0.
observable (List[Union[str, List[List[List[float]]]]): A list with at least
avawang1 marked this conversation as resolved.
Show resolved Hide resolved
one item and items are strings matching the observable regex
or a two dimensional matrix with complex entries.
Each complex number is represented using a List[float] of size 2, with
element[0] being the real part and element[1] imaginary.
inf, -inf, and NaN are not allowable inputs for the element.

Examples:
>>> Sample(targets=[1], observable=["x"])
"""

class Type(str, Enum):
floralph marked this conversation as resolved.
Show resolved Hide resolved
sample = "sample"

type = Type.sample


class Variance(OptionalMultiTarget, Observable):
"""
Variance of specified targets and observables as requested result.

Attributes:
type (str): The result type. default = "variance". (type) is optional.
This should be unique among all result types.
targets (List[int]): The target qubits. This is a list of int >= 0.
observable (List[Union[str, List[List[List[float]]]]): A list with at least
one item and items are strings matching the observable regex
or a two dimensional matrix with complex entries.
Each complex number is represented using a List[float] of size 2, with
element[0] being the real part and element[1] imaginary.
inf, -inf, and NaN are not allowable inputs for the element.

Examples:
>>> Variance(targets=[1], observable=["x"])
"""

class Type(str, Enum):
variance = "variance"

type = Type.variance


class StateVector(OptionalMultiState):
"""
State vector of specified states as requested result.

Attributes:
type (str): The result type. default = "statevector". (type) is optional.
This should be unique among all result types.
states (Optional[List[string]]): Variable length list with with all strings
matching the state regex

Examples:
>>> StateVector(states=["01", "10"])
avawang1 marked this conversation as resolved.
Show resolved Hide resolved
"""

class Type(str, Enum):
statevector = "statevector"

type = Type.statevector


class Amplitude(OptionalMultiState):
"""
Amplitudes of specified states as requested result.

Attributes:
type (str): The result type. default = "amplitude". (type) is optional.
This should be unique among all result types.
states (Optional[List[string]]): Variable length list with with all strings
matching the state regex

Examples:
>>> Amplitude(states=["01", "10"])
"""

class Type(str, Enum):
amplitude = "amplitude"

type = Type.amplitude


class Probability(OptionalMultiTarget):
"""
Probability of specified targets as requested result.
avawang1 marked this conversation as resolved.
Show resolved Hide resolved

Attributes:
type (str): The result type. default = "probability". (type) is optional.
This should be unique among all result types.
targets (Optional[List[int]]): The target qubits. This is a list of int >= 0.

Examples:
>>> Probability(targets=[1, 2])
"""

class Type(str, Enum):
probability = "probability"

type = Type.probability
67 changes: 65 additions & 2 deletions src/braket/ir/jaqcd/shared_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

from pydantic import BaseModel, confloat, conint, conlist
from typing import Optional, Union

from pydantic import BaseModel, confloat, conint, conlist, constr


class SingleTarget(BaseModel):
Expand Down Expand Up @@ -47,7 +49,7 @@ class MultiTarget(BaseModel):
Variable length target indices.

Attributes:
targets (List[int]): A list with at least two items and all items are int >= 0.
targets (List[int]): A list with items that are all int >= 0.

Examples:
>>> MultiTarget(targets=[0, 1])
Expand All @@ -56,6 +58,20 @@ class MultiTarget(BaseModel):
targets: conlist(conint(ge=0), min_items=1)


class OptionalMultiTarget(BaseModel):
"""
Optional variable length target indices

Attributes:
targets (Optional[List[int]]): A list with items that are all int >= 0.

Examples:
>>> OptionalMultiTarget(targets=[0, 1])
"""

targets: Optional[conlist(conint(ge=0), min_items=1)]


class MultiControl(BaseModel):
"""
Variable length control indices.
Expand Down Expand Up @@ -134,3 +150,50 @@ class TwoDimensionalMatrix(BaseModel):
),
min_items=1,
)


class Observable(BaseModel):
"""
An observable - could be a single operator or the tensor product of multiple operators.

Attributes:
observable (List[Union[str, List[List[List[float]]]]): A list with at least
avawang1 marked this conversation as resolved.
Show resolved Hide resolved
one item and items are strings matching the observable regex
or a two dimensional matrix with complex entries.
Each complex number is represented using a List[float] of size 2, with
element[0] being the real part and element[1] imaginary.
inf, -inf, and NaN are not allowable inputs for the element.

Examples:
>>> Observable(observable=["x"])
>>> Observable(observable=[[[0, 0], [1, 0]], [[1, 0], [0, 0]]])
"""

observable: conlist(
Union[
constr(regex="(x|y|z|h|i)"),
conlist(
conlist(
conlist(confloat(gt=float("-inf"), lt=float("inf")), min_items=2, max_items=2),
min_items=1,
avawang1 marked this conversation as resolved.
Show resolved Hide resolved
),
min_items=1,
avawang1 marked this conversation as resolved.
Show resolved Hide resolved
),
],
min_items=1,
)


class OptionalMultiState(BaseModel):
"""
A list of states in bitstring form.

Attributes:
states (Optional[List[string]]): Variable length list with all strings matching the
state regex or None

Examples:
>>> OptionalMultiState(states=["10", "10"])
"""

states: Optional[conlist(constr(regex="^[01]+$", min_length=1), min_items=1)]
Loading