|
35 | 35 | (is (< result 100.0) "Cost should be reasonable for test data"))) |
36 | 36 |
|
37 | 37 | (testing "Error handling" |
38 | | - (let [invalid-params [] |
| 38 | + (let [invalid-params [] ; Empty params should trigger error handling in training |
39 | 39 | ansatz-fn (ansatz/hardware-efficient-ansatz 2 1) |
40 | 40 | valid-features [[0.5 0.3]] |
41 | 41 | valid-labels [0] |
42 | | - backend (sim/create-simulator) |
43 | | - |
44 | | - result (training/classification-cost invalid-params ansatz-fn valid-features valid-labels backend)] |
| 42 | + backend (sim/create-simulator)] |
45 | 43 |
|
46 | | - ;; The current implementation returns 1000.0 on error |
47 | | - (is (= 1000.0 result) "Should return high cost on error")))) |
| 44 | + ;; Test with empty parameters - should catch error and return 1000.0 |
| 45 | + (is (= 1000.0 (training/classification-cost invalid-params ansatz-fn valid-features valid-labels backend)) |
| 46 | + "Should return high cost on error")))) |
48 | 47 |
|
49 | 48 | (deftest test-parameter-shift-gradient-validation |
50 | 49 | (testing "Parameter shift gradient validation using optimization namespace" |
|
99 | 98 | ;; Property-based tests |
100 | 99 | (deftest test-cost-function-properties |
101 | 100 | (testing "Cost function properties" |
102 | | - (let [test-cases (take 5 (gen/sample |
| 101 | + ;; Generate test cases with properly sized parameters for 2-qubit, 1-layer ansatz |
| 102 | + (let [correct-param-gen (gen/vector (gen/double* {:min -3.14 :max 3.14 :NaN? false :infinite? false}) 6 6) |
| 103 | + test-cases (take 5 (gen/sample |
103 | 104 | (gen/hash-map |
104 | | - :parameters parameter-vector-gen |
| 105 | + :parameters correct-param-gen |
105 | 106 | :features feature-matrix-gen |
106 | 107 | :labels binary-labels-gen)))] |
107 | 108 |
|
108 | 109 | (doseq [test-case test-cases] |
109 | 110 | (when (= (count (:features test-case)) (count (:labels test-case))) |
110 | 111 | (let [ansatz-fn (ansatz/hardware-efficient-ansatz 2 1) |
111 | 112 | backend (sim/create-simulator) |
112 | | - cost (try |
113 | | - (training/classification-cost |
114 | | - (:parameters test-case) |
115 | | - ansatz-fn |
116 | | - (:features test-case) |
117 | | - (:labels test-case) |
118 | | - backend) |
119 | | - (catch Exception _ 1000.0))] |
| 113 | + cost (training/classification-cost |
| 114 | + (:parameters test-case) |
| 115 | + ansatz-fn |
| 116 | + (:features test-case) |
| 117 | + (:labels test-case) |
| 118 | + backend)] |
120 | 119 |
|
121 | 120 | (is (number? cost) "Cost should be numeric") |
122 | | - (is (>= cost 0.0) "Cost should be non-negative"))))))) |
| 121 | + (is (>= cost 0.0) "Cost should be non-negative") |
| 122 | + (is (< cost 1000.0) "Cost should be reasonable for valid inputs"))))))) |
123 | 123 |
|
124 | 124 | ;; Error handling tests |
125 | 125 | (deftest test-error-handling |
126 | 126 | (testing "Various error conditions" |
127 | 127 | (let [valid-params [0.1 0.2 0.3 0.4 0.5 0.6] |
128 | 128 | ansatz-fn (ansatz/hardware-efficient-ansatz 2 1) |
129 | | - backend (sim/create-simulator) |
130 | | - |
131 | | - ; Test mismatched features and labels |
132 | | - mismatched-result (try |
133 | | - (training/classification-cost |
134 | | - valid-params ansatz-fn |
135 | | - [[0.5 0.3]] [0 1] backend) ; 1 feature, 2 labels |
136 | | - (catch Exception _ 1000.0)) |
137 | | - |
138 | | - ; Test empty data |
139 | | - empty-result (try |
140 | | - (training/classification-cost |
141 | | - valid-params ansatz-fn [] [] backend) |
142 | | - (catch Exception _ 1000.0)) |
143 | | - |
144 | | - ; Test nil inputs |
145 | | - nil-result (try |
146 | | - (training/classification-cost |
147 | | - nil ansatz-fn [[0.5]] [0] backend) |
148 | | - (catch Exception _ 1000.0))] |
| 129 | + backend (sim/create-simulator)] |
| 130 | + |
| 131 | + ;; Test mismatched features and labels |
| 132 | + (is (= 1000.0 (training/classification-cost |
| 133 | + valid-params ansatz-fn |
| 134 | + [[0.5 0.3]] [0 1] backend)) ; 1 feature, 2 labels |
| 135 | + "Should handle mismatched data") |
| 136 | + |
| 137 | + ;; Test empty data |
| 138 | + (is (= 1000.0 (training/classification-cost |
| 139 | + valid-params ansatz-fn [] [] backend)) |
| 140 | + "Should handle empty data") |
149 | 141 |
|
150 | | - ; Current implementation returns 1000.0 on all errors |
151 | | - (is (= 1000.0 mismatched-result) "Should handle mismatched data") |
152 | | - (is (= 1000.0 empty-result) "Should handle empty data") |
153 | | - (is (= 1000.0 nil-result) "Should handle nil inputs")))) |
| 142 | + ;; Test nil inputs - these should be caught by the try-catch in classification-cost |
| 143 | + (is (= 1000.0 (training/classification-cost |
| 144 | + nil ansatz-fn [[0.5]] [0] backend)) |
| 145 | + "Should handle nil inputs")))) |
154 | 146 |
|
155 | 147 | ;; Rich comment block for REPL testing |
156 | 148 | (comment |
|
0 commit comments