Skip to content

Commit

Permalink
Merge pull request #243 from QuTech-Delft/83-do-checks-at-check-gener…
Browse files Browse the repository at this point in the history
…ator-f-args

Move out of bounds checks to _check_generator_f_args
  • Loading branch information
juanboschero authored Jun 27, 2024
2 parents f4ced2f + ecc4e65 commit ab0fcf7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
30 changes: 15 additions & 15 deletions opensquirrel/circuit_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ def _add_comment(self, comment_string: str) -> Self:
self.ir.add_comment(Comment(comment_string))
return self

def _verify_register_size(self) -> None:
current_statement = self.ir.statements[-1]
if not isinstance(current_statement, Gate) and not isinstance(current_statement, Measure):
return
for qubit in current_statement.get_qubit_operands():
if qubit.index >= self.register_manager.get_qubit_register_size():
raise IndexError("Qubit index does not exist in circuit.")
if isinstance(current_statement, Measure):
for bit in current_statement.get_bit_operands():
if bit.index >= self.register_manager.get_bit_register_size():
raise IndexError("Bit index does not exist in circuit.")

def _add_instruction(self, attr: str, *args: Any) -> Self:
if any(attr == measure.__name__ for measure in self.measurement_set):
generator_f_measure = MeasurementLibrary.get_measurement_f(self, attr)
Expand All @@ -88,11 +76,19 @@ def _add_instruction(self, attr: str, *args: Any) -> Self:
generator_f_gate = GateLibrary.get_gate_f(self, attr)
self._check_generator_f_args(generator_f_gate, attr, args)
self.ir.add_gate(generator_f_gate(*args))
self._verify_register_size()
return self

@staticmethod
def _check_generator_f_args(generator_f: Callable[..., Gate | Measure], attr: str, args: tuple[Any, ...]) -> None:
def _check_qubit_out_of_bounds_access(self, index: int) -> None:
if index >= self.register_manager.get_qubit_register_size():
raise IndexError("Qubit index is out of bounds")

def _check_bit_out_of_bounds_access(self, index: int) -> None:
if index >= self.register_manager.get_bit_register_size():
raise IndexError("Bit index is out of bounds")

def _check_generator_f_args(
self, generator_f: Callable[..., Gate | Measure], attr: str, args: tuple[Any, ...]
) -> None:
for i, par in enumerate(inspect.signature(generator_f).parameters.values()):
if isinstance(par.annotation, str):
if args[i].__class__.__name__ != par.annotation:
Expand All @@ -103,6 +99,10 @@ def _check_generator_f_args(generator_f: Callable[..., Gate | Measure], attr: st
raise TypeError(
f"Wrong argument type for instruction `{attr}`, got {type(args[i])} but expected {par.annotation}"
)
if args[i].__class__.__name__ == "Qubit":
self._check_qubit_out_of_bounds_access(args[i].index)
elif args[i].__class__.__name__ == "Bit":
self._check_bit_out_of_bounds_access(args[i].index)

def to_circuit(self) -> Circuit:
return Circuit(self.register_manager, self.ir)
8 changes: 4 additions & 4 deletions test/test_circuit_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ def test_chain(self):
def test_gate_index_error(self):
builder = CircuitBuilder(2)

with pytest.raises(Exception) as exception_info:
with pytest.raises(IndexError) as exception_info:
builder.H(Qubit(0)).CNOT(Qubit(0), Qubit(12)).to_circuit()
assert re.search("Qubit index does not exist in circuit.", str(exception_info.value))
assert re.search("Qubit index is out of bounds", str(exception_info.value))

def test_measurement_index_error(self):
builder = CircuitBuilder(2, 1)
with pytest.raises(Exception) as exception_info:
with pytest.raises(IndexError) as exception_info:
builder.H(Qubit(0)).measure(Qubit(0), Bit(10)).to_circuit()
assert re.search("Bit index does not exist in circuit.", str(exception_info.value))
assert re.search("Bit index is out of bounds", str(exception_info.value))

def test_unknown_instruction(self):
builder = CircuitBuilder(3)
Expand Down

0 comments on commit ab0fcf7

Please sign in to comment.