vmore2
Initial release: QSVAPS v0.1.0 - Quantum Superposition Verification for Agent Plan Safety
ce8c08a | """Tests for the quantum oracle builder.""" | |
| import pytest | |
| import numpy as np | |
| from qiskit import QuantumCircuit | |
| from qiskit_aer import AerSimulator | |
| from qsvaps.oracle_builder import OracleBuilder | |
| class TestPhaseOracle: | |
| def test_empty_violations(self): | |
| oracle = OracleBuilder.build_phase_oracle([], 3) | |
| assert oracle.num_qubits == 3 | |
| assert oracle.size() == 0 # No gates | |
| def test_single_violation(self): | |
| """Oracle should flip phase of exactly one state.""" | |
| oracle = OracleBuilder.build_phase_oracle([0], 2) | |
| assert oracle.num_qubits == 2 | |
| assert oracle.size() > 0 | |
| def test_oracle_correctness(self): | |
| """Verify oracle flips phase of marked states using statevector.""" | |
| from qiskit_aer import AerSimulator | |
| num_qubits = 2 | |
| violations = [1, 3] # States |01⟩ and |11⟩ | |
| oracle = OracleBuilder.build_phase_oracle(violations, num_qubits) | |
| # Create test circuit: H → Oracle → measure statevector | |
| qc = QuantumCircuit(num_qubits) | |
| qc.h(range(num_qubits)) | |
| qc.compose(oracle, inplace=True) | |
| qc.save_statevector() | |
| sim = AerSimulator(method="statevector") | |
| result = sim.run(qc).result() | |
| sv = result.get_statevector(qc) | |
| amplitudes = np.array(sv) | |
| # After H|0⟩, all amplitudes are +0.5 | |
| # Oracle should flip signs of violation states | |
| for i in range(2**num_qubits): | |
| expected_sign = -1 if i in violations else 1 | |
| assert np.isclose( | |
| amplitudes[i].real, expected_sign * 0.5, atol=1e-8 | |
| ), f"State {i}: expected sign {expected_sign}" | |
| class TestDiffuser: | |
| def test_creates_circuit(self): | |
| diffuser = OracleBuilder.build_diffuser(3) | |
| assert diffuser.num_qubits == 3 | |
| assert diffuser.size() > 0 | |
| def test_single_qubit(self): | |
| diffuser = OracleBuilder.build_diffuser(1) | |
| assert diffuser.num_qubits == 1 | |
| class TestOptimalIterations: | |
| def test_no_violations(self): | |
| assert OracleBuilder.optimal_iterations(16, 0) == 0 | |
| def test_all_violations(self): | |
| assert OracleBuilder.optimal_iterations(16, 16) == 0 | |
| def test_single_violation(self): | |
| # π/4 × √(16/1) = π/4 × 4 ≈ 3.14 → 3 | |
| k = OracleBuilder.optimal_iterations(16, 1) | |
| assert k == 3 | |
| def test_many_violations(self): | |
| # π/4 × √(16/8) = π/4 × √2 ≈ 1.11 → 1 | |
| k = OracleBuilder.optimal_iterations(16, 8) | |
| assert k == 1 | |
| def test_minimum_one(self): | |
| k = OracleBuilder.optimal_iterations(4, 3) | |
| assert k >= 1 | |
| class TestGroverCircuit: | |
| def test_builds_circuit(self): | |
| oracle = OracleBuilder.build_phase_oracle([0], 2) | |
| diffuser = OracleBuilder.build_diffuser(2) | |
| qc = OracleBuilder.build_grover_circuit(oracle, diffuser, 2, 1) | |
| assert qc.num_qubits == 2 | |
| assert qc.num_clbits == 2 # Measurement bits | |
| def test_circuit_runs(self): | |
| """Ensure the assembled circuit executes without error.""" | |
| oracle = OracleBuilder.build_phase_oracle([2], 2) | |
| diffuser = OracleBuilder.build_diffuser(2) | |
| qc = OracleBuilder.build_grover_circuit(oracle, diffuser, 2, 1) | |
| sim = AerSimulator() | |
| job = sim.run(qc, shots=100) | |
| counts = job.result().get_counts(qc) | |
| assert len(counts) > 0 | |