Skip to content

Commit

Permalink
group error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjaskula-aws committed Dec 22, 2023
1 parent 9fdf1b1 commit d693daa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/braket/circuits/circuit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# language governing permissions and limitations under the License.

from braket.circuits import Circuit, ResultType
from braket.circuits.gates import GPhase


def validate_circuit_and_shots(circuit: Circuit, shots: int) -> None:
Expand All @@ -29,10 +28,10 @@ def validate_circuit_and_shots(circuit: Circuit, shots: int) -> None:
if circuit has observables that cannot be simultaneously measured and `shots>0`;
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 all(isinstance(inst.operator, GPhase) and not inst.control for inst in circuit.instructions):
raise ValueError("Circuit must have at least one non-GPhase gate to run on a device")
if not circuit.instructions or all(
not (inst.target or inst.control) for inst in circuit.instructions
):
raise ValueError("Circuit must have at least one non-zero-qubit gate 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.circuits.result_types`"
Expand Down
10 changes: 7 additions & 3 deletions test/unit_tests/braket/circuits/test_circuit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@


def test_validate_circuit_and_shots_no_instructions():
with pytest.raises(ValueError, match="Circuit must have instructions to run on a device"):
with pytest.raises(
ValueError, match="Circuit must have at least one non-zero-qubit gate to run on a device"
):
validate_circuit_and_shots(Circuit(), 100)


def test_validate_circuit_and_shots_only_gphase():
with pytest.raises(
ValueError, match="Circuit must have at least one non-GPhase gate to run on a device"
ValueError, match="Circuit must have at least one non-zero-qubit gate to run on a device"
):
validate_circuit_and_shots(Circuit().gphase(0.15), 100)

Expand All @@ -34,7 +36,9 @@ def test_validate_circuit_and_shots_ctrl_gphase():


def test_validate_circuit_and_shots_0_no_instructions():
with pytest.raises(ValueError, match="Circuit must have instructions to run on a device"):
with pytest.raises(
ValueError, match="Circuit must have at least one non-zero-qubit gate to run on a device"
):
validate_circuit_and_shots(Circuit(), 0)


Expand Down

0 comments on commit d693daa

Please sign in to comment.