Skip to content

Commit

Permalink
Improve code coverage for program_context.py
Browse files Browse the repository at this point in the history
  • Loading branch information
rmshaffer committed May 15, 2024
1 parent 0dc6f6e commit 8fad137
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/autoqasm/simulator/program_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _(self, identifier: IndexedIdentifier) -> tuple[int]: # noqa: C901

if isinstance(primary_index, (IntegerLiteral, SymbolLiteral)):
if isinstance(primary_index, IntegerLiteral):
self._validate_qubit_in_range(primary_index.value)
self._validate_qubit_in_range(primary_index.value, name)
target = (self[name][0] + primary_index.value,)
elif isinstance(primary_index, RangeDefinition):
target = tuple(np.array(self[name])[convert_range_def_to_slice(primary_index)])
Expand All @@ -92,7 +92,7 @@ def _(self, identifier: IndexedIdentifier) -> tuple[int]: # noqa: C901
index_list = convert_discrete_set_to_list(primary_index)
for index in index_list:
if isinstance(index, int):
self._validate_qubit_in_range(index)
self._validate_qubit_in_range(index, name)
target = tuple([self[name][0] + index for index in index_list])

if len(indices) == 2:
Expand Down Expand Up @@ -157,6 +157,8 @@ def _get_indices_length(
return (stop - start) // step
elif isinstance(last_index, DiscreteSet):
return len(last_index.values)
else:
raise NotImplementedError

def get_qubit_size(self, identifier: Union[Identifier, IndexedIdentifier]) -> int:
"""_summary_
Expand All @@ -180,9 +182,10 @@ def __init__(self, circuit: Optional[Circuit] = None):
circuit (Optional[Circuit]): A partially-built circuit to continue building with this
context. Default: None.
"""
super(ProgramContext, self).__init__()
self.qubit_mapping = QubitTable()
self.outputs = {}
self._circuit = circuit or Circuit()
super(ProgramContext, self).__init__()

def pop_instructions(self) -> list[GateOperation]:
"""_summary_
Expand Down
56 changes: 56 additions & 0 deletions test/unit_tests/autoqasm/simulator/test_native_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,59 @@ def test_qubit_register():
"""
result = NativeInterpreter(Simulation(2, 1, 1)).simulate(qasm)
assert result["__bit_0__"] == ["01"]


def test_qubit_index_out_of_range():
qasm = """
OPENQASM 3.0;
qubit[2] __qubits__;
x __qubits__[4];
"""
with pytest.raises(IndexError, match="qubit register index `4` out of range"):
NativeInterpreter(Simulation(2, 1, 1)).simulate(qasm)


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];
}
"""
with pytest.raises(IndexError, match="qubit register index `2` out of range"):
NativeInterpreter(Simulation(2, 1, 1)).simulate(qasm)


def test_physical_qubit_identifier():
qasm = """
OPENQASM 3.0;
x $0;
bit __bit_0__ = measure $0;
"""
result = NativeInterpreter(Simulation(1, 1, 1)).simulate(qasm)
assert result["__bit_0__"] == [1]


def test_qubit_register_indexing():
qasm = """
OPENQASM 3.0;
qubit[2] __qubits__;
int a = 0;
x __qubits__[a+1];
h __qubits__[0:1];
h __qubits__[{a, 1}];
bit[2] __bit_0__ = measure __qubits__;
"""
result = NativeInterpreter(Simulation(1, 1, 1)).simulate(qasm)
assert result["__bit_0__"] == ["01"]


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

0 comments on commit 8fad137

Please sign in to comment.