Skip to content

Commit

Permalink
Finish code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rmshaffer committed May 16, 2024
1 parent c75a4de commit 1327d4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/autoqasm/simulator/program_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
)
from braket.default_simulator.openqasm.program_context import ProgramContext, Table
from braket.default_simulator.operation import GateOperation
from sympy import Integer

from autoqasm.simulator.conversion import convert_to_output

Expand Down Expand Up @@ -103,7 +104,9 @@ def _(self, identifier: IndexedIdentifier) -> tuple[int]: # noqa: C901
# validate indices manually, since we use addition instead of indexing to
# accommodate symbolic indices
for q in target:
if isinstance(q, int) and (relative_index := q - self[name][0]) >= len(self[name]):
if isinstance(q, (int, Integer)) and (relative_index := q - self[name][0]) >= len(
self[name]
):
raise IndexError(
f"qubit register index `{relative_index}` out of range for qubit register "
f"of length {len(self[name])} `{name}`."
Expand Down Expand Up @@ -158,7 +161,7 @@ def _get_indices_length(
elif isinstance(last_index, DiscreteSet):
return len(last_index.values)
else:
raise NotImplementedError
raise TypeError(f"tuple indices must be integers or slices, not {type(last_index)}")

def get_qubit_size(self, identifier: Union[Identifier, IndexedIdentifier]) -> int:
"""_summary_
Expand Down
24 changes: 18 additions & 6 deletions test/unit_tests/autoqasm/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ def test_qubit_index_out_of_range_symbolic():
qasm = """
OPENQASM 3.0;
qubit[2] __qubits__;
for int[32] i in [0:3] {
x __qubits__[i];
}
h __qubits__[2+pi-pi];
bit[2] __bit_0__ = measure __qubits__;
"""
with pytest.raises(IndexError, match="qubit register index `2` out of range"):
NativeInterpreter(Simulation(2, 1, 1)).simulate(qasm)
Expand Down Expand Up @@ -151,11 +150,24 @@ def test_qubit_register_indexing():
assert result["__bit_0__"] == ["01"]


def test_qubit_register_indexing_multi_dimensions():
qasm = """
@pytest.mark.parametrize("invalid_index", ("1.23", "pi", "{0, pi}"))
def test_qubit_register_invalid_index(invalid_index):
qasm = f"""
OPENQASM 3.0;
qubit[2] __qubits__;
x __qubits__[{invalid_index}];
bit[2] __bit_0__ = measure __qubits__;
"""
with pytest.raises(TypeError, match="tuple indices must be integers or slices"):
NativeInterpreter(Simulation(1, 1, 1)).simulate(qasm)


@pytest.mark.parametrize("multi_dimension_index", ("[0, 1]", "[0][1][2]"))
def test_qubit_register_indexing_multi_dimensions(multi_dimension_index):
qasm = f"""
OPENQASM 3.0;
qubit[2] __qubits__;
x __qubits__[0, 1];
x __qubits__{multi_dimension_index};
"""
with pytest.raises(IndexError, match="Cannot index multiple dimensions for qubits."):
NativeInterpreter(Simulation(2, 1, 1)).simulate(qasm)

0 comments on commit 1327d4c

Please sign in to comment.