Skip to content

Commit

Permalink
Add exception if amplitude state is not a list (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
avawang1 authored Jun 15, 2020
1 parent 76daafc commit de10b49
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/bell_result_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
.cnot(0, 1)
.probability(target=[0])
.expectation(observable=Observable.Z(), target=[1])
.amplitude(state="00")
.amplitude(state=["00"])
.state_vector()
)

Expand Down
12 changes: 9 additions & 3 deletions src/braket/circuits/result_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,19 @@ def __init__(self, state: List[str]):
state (List[str]): list of quantum states as strings with "0" and "1"
Raises:
ValueError: If state is None or an empty list
ValueError: If state is None or an empty list, or
state is not a list of strings of '0' and '1'
Examples:
>>> ResultType.Amplitude(state=['01', '10'])
"""
if not state or not all(
isinstance(amplitude, str) and re.fullmatch("^[01]+$", amplitude) for amplitude in state
if (
not state
or not isinstance(state, List)
or not all(
isinstance(amplitude, str) and re.fullmatch("^[01]+$", amplitude)
for amplitude in state
)
):
raise ValueError(
"A non-empty list of states must be specified in binary encoding e.g. ['01', '10']"
Expand Down
3 changes: 2 additions & 1 deletion test/unit_tests/braket/circuits/test_result_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def test_result_equality(testclass, subroutine_name, irclass, input, ir_input):

@pytest.mark.xfail(raises=ValueError)
@pytest.mark.parametrize(
"state", ((["2", "11"]), ([1, 0]), ([0.1, 0]), ("-0", "1"), (["", ""]), (None), ([None, None]))
"state",
((["2", "11"]), ([1, 0]), ([0.1, 0]), ("-0", "1"), (["", ""]), (None), ([None, None]), ("10")),
)
def test_amplitude_init_invalid_state_value_error(state):
ResultType.Amplitude(state=state)
Expand Down

0 comments on commit de10b49

Please sign in to comment.