vmore2
Initial release: QSVAPS v0.1.0 - Quantum Superposition Verification for Agent Plan Safety
ce8c08a | """Tests for the Grover search engine.""" | |
| import pytest | |
| from qsvaps.grover_search import GroverSearchEngine, SearchResult | |
| class TestGroverSearch: | |
| def setup_method(self): | |
| self.engine = GroverSearchEngine() | |
| def test_empty_violations(self): | |
| result = self.engine.search([], 3) | |
| assert result.violation_states_found == [] | |
| assert result.num_iterations == 0 | |
| def test_finds_single_violation(self): | |
| """Grover should amplify the single marked state.""" | |
| violations = [5] # State |101⟩ in 3 qubits | |
| result = self.engine.search(violations, 3, shots=2048) | |
| assert len(result.violation_states_found) >= 1 | |
| assert 5 in result.violation_states_found | |
| assert result.num_iterations > 0 | |
| def test_finds_multiple_violations(self): | |
| """Grover should find multiple marked states.""" | |
| violations = [0, 3, 7] # Three violations in 3 qubits | |
| result = self.engine.search(violations, 3, shots=4096) | |
| found = set(result.violation_states_found) | |
| # Should find at least some of the violations | |
| assert len(found & set(violations)) >= 1 | |
| def test_two_qubit_search(self): | |
| """Simple 2-qubit case: mark state |10⟩ = 2.""" | |
| violations = [2] | |
| result = self.engine.search(violations, 2, shots=2048) | |
| # With 1 violation in 4 states, Grover should find it with high prob | |
| assert 2 in result.violation_states_found | |
| def test_result_structure(self): | |
| result = self.engine.search([1], 2, shots=100) | |
| assert isinstance(result, SearchResult) | |
| assert result.num_shots == 100 | |
| assert result.circuit_depth > 0 | |
| assert result.circuit_gate_count > 0 | |
| assert result.execution_time_ms > 0 | |
| class TestClassicalSearch: | |
| def setup_method(self): | |
| self.engine = GroverSearchEngine() | |
| def test_finds_all(self): | |
| target = {2, 5, 7} | |
| found, time_ms = self.engine.classical_search( | |
| lambda s: s in target, 3 | |
| ) | |
| assert set(found) == target | |
| assert time_ms >= 0 | |
| def test_finds_none(self): | |
| found, _ = self.engine.classical_search(lambda s: False, 3) | |
| assert found == [] | |
| class TestBenchmark: | |
| def test_benchmark_returns_metrics(self): | |
| engine = GroverSearchEngine() | |
| violations = [1, 2] | |
| result = engine.benchmark(violations, 2, shots=512) | |
| assert "num_qubits" in result | |
| assert "total_states" in result | |
| assert "quantum_time_ms" in result | |
| assert "classical_time_ms" in result | |
| assert "theoretical_speedup" in result | |
| assert "detection_rate" in result | |
| assert result["total_states"] == 4 | |
| assert result["num_violations"] == 2 | |