From c4cfefe219b96db667806170381e4be0a343bfc2 Mon Sep 17 00:00:00 2001 From: Ava Wang Date: Thu, 1 Oct 2020 15:56:56 -0700 Subject: [PATCH 1/2] fix: Add error message for running a circuit without instructions --- src/braket/circuits/circuit_helpers.py | 2 ++ .../unit_tests/braket/circuits/test_circuit_helpers.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/braket/circuits/circuit_helpers.py b/src/braket/circuits/circuit_helpers.py index 5f493208b..042faef22 100644 --- a/src/braket/circuits/circuit_helpers.py +++ b/src/braket/circuits/circuit_helpers.py @@ -27,6 +27,8 @@ def validate_circuit_and_shots(circuit: Circuit, shots: int) -> None: See `braket.circuit.result_types`. Or, if `StateVector` or `Amplitude` are specified as result types when `shots > 0`. """ + if not circuit.instructions: + raise ValueError("Circuit must have instructions to run on a device") if not shots and not circuit.result_types: raise ValueError( "No result types specified for circuit and shots=0. See `braket.circuit.result_types`" diff --git a/test/unit_tests/braket/circuits/test_circuit_helpers.py b/test/unit_tests/braket/circuits/test_circuit_helpers.py index d6a91515f..4805ce009 100644 --- a/test/unit_tests/braket/circuits/test_circuit_helpers.py +++ b/test/unit_tests/braket/circuits/test_circuit_helpers.py @@ -17,6 +17,16 @@ from braket.circuits.circuit_helpers import validate_circuit_and_shots +@pytest.mark.xfail(raises=ValueError) +def test_validate_circuit_and_shots_no_instructions(): + validate_circuit_and_shots(Circuit(), 100) + + +@pytest.mark.xfail(raises=ValueError) +def test_validate_circuit_and_shots_0_no_instructions(): + validate_circuit_and_shots(Circuit(), 0) + + @pytest.mark.xfail(raises=ValueError) def test_validate_circuit_and_shots_0_no_results(): validate_circuit_and_shots(Circuit().h(0), 0) From 25f1258992e226bcddec751776cb182951a51420 Mon Sep 17 00:00:00 2001 From: Ava Wang Date: Thu, 1 Oct 2020 16:04:16 -0700 Subject: [PATCH 2/2] Add docstring --- src/braket/circuits/circuit_helpers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/braket/circuits/circuit_helpers.py b/src/braket/circuits/circuit_helpers.py index 042faef22..d62ca19a6 100644 --- a/src/braket/circuits/circuit_helpers.py +++ b/src/braket/circuits/circuit_helpers.py @@ -23,9 +23,9 @@ def validate_circuit_and_shots(circuit: Circuit, shots: int) -> None: shots (int): shots to validate Raises: - ValueError: If no result types specified for circuit and `shots=0`. - See `braket.circuit.result_types`. Or, if `StateVector` or `Amplitude` - are specified as result types when `shots > 0`. + ValueError: If circuit has no instructions. Also, if no result types + specified for circuit and `shots=0`. See `braket.circuit.result_types`. + Or, if `StateVector` or `Amplitude` are specified as result types when `shots > 0`. """ if not circuit.instructions: raise ValueError("Circuit must have instructions to run on a device")