Skip to content

Commit

Permalink
add invalidation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
splch committed Aug 21, 2023
1 parent d58c242 commit 9108cbb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/test_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ def test_non_unitary_gate():
non_unitary_gate = np.array([[2, 0], [0, 0.5]])
with pytest.raises(ValueError):
Gates._validate_gate(non_unitary_gate)


def test_create_controlled_gate_invalid_qubits():
# Define a scenario where the control and target qubits are out of range
with pytest.raises(IndexError):
Gates.create_controlled_gate(
Gates.X, control_qubit=4, target_qubit=5, num_qubits=3
)
13 changes: 13 additions & 0 deletions tests/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,25 @@ def test_invalid_qubit_index():
simulator.h(2) # Index out of range


def test_reset_invalid_qubit_index():
simulator = QubitSimulator(3)
simulator.num_qubits = -1 # Set an invalid value for num_qubits
with pytest.raises(ValueError):
simulator.reset() # Resetting with an invalid value should raise an error


def test_invalid_control_and_target_index():
simulator = QubitSimulator(1)
with pytest.raises(IndexError):
simulator.cx(1, 0) # Control qubit cannot be out of range


def test_apply_gate_invalid_control_qubit():
simulator = QubitSimulator(1)
with pytest.raises(IndexError):
simulator._apply_gate("X", Gates.X, target_qubit=0, control_qubit=2)


def test_error_messages():
with pytest.raises(ValueError, match="Number of qubits must be non-negative."):
QubitSimulator(-1)
Expand Down

0 comments on commit 9108cbb

Please sign in to comment.