Add error path test for invalid number of targets to add_op_to_circuit#1039
Conversation
…get qubits in add_op_to_circuit Addresses a testing gap in `qsimcirq/qsim_circuit.py` where the `NotImplementedError` for controlled gates with more than 4 target qubits was not explicitly tested. 🎯 **What:** The testing gap addressed - Added `test_add_op_to_circuit_too_many_targets` to `qsimcirq_tests/qsimcirq_test.py`. 📊 **Coverage:** What scenarios are now tested - Controlled gates with more than 4 target qubits (specifically 5 targets) are now verified to raise `NotImplementedError` with the correct error message. ✨ **Result:** The improvement in test coverage - Increased reliability of the `add_op_to_circuit` function by covering a previously untested error path.
…-limit-16945383875852359583
There was a problem hiding this comment.
Code Review
This pull request adds a new test case, test_add_op_to_circuit_too_many_targets, to verify that controlled gates with more than four target qubits correctly raise a NotImplementedError. However, the feedback indicates that this test is redundant because the error path is already validated in test_control_limits. Furthermore, the test is likely broken due to an incorrect reference to add_op_to_circuit, which would result in an AttributeError.
| def test_add_op_to_circuit_too_many_targets(): | ||
| # qsim supports up to 4 target qubits for controlled gates. | ||
| qubits = cirq.LineQubit.range(6) | ||
| # A gate with 5 targets and 1 control. | ||
| big_gate = cirq.MatrixGate(np.eye(32)).on(*qubits[1:]).controlled_by(qubits[0]) | ||
|
|
||
| qsim_circuit = qsimcirq.qsim.Circuit() | ||
| qubit_to_index = {q: i for i, q in enumerate(qubits)} | ||
|
|
||
| with pytest.raises( | ||
| NotImplementedError, match="Received control gate on 5 target qubits" | ||
| ): | ||
| qsimcirq.add_op_to_circuit(big_gate, 0, qubit_to_index, qsim_circuit) |
There was a problem hiding this comment.
This new test case is redundant and likely contains a bug that will prevent it from running.
- Redundancy: The
NotImplementedErrorfor controlled gates with more than 4 target qubits is already explicitly verified intest_control_limits(lines 876-879), which uses the publicsimulateAPI to trigger the same code path and matches the same error message. - Likely AttributeError:
add_op_to_circuitis defined in theqsim_circuitmodule. Unless it is explicitly exported inqsimcirq/__init__.py, the call toqsimcirq.add_op_to_circuitwill fail. Based on the usage inqsim_simulator.py(line 22), it should likely be accessed viaqsimcirq.qsim_circuit.add_op_to_circuit.
Since the error path is already covered by an existing integration test and the unit test implementation is likely broken, it is recommended to remove this redundant test case.
Addresses a testing gap in
qsimcirq/qsim_circuit.pywhere theNotImplementedErrorfor controlled gates with more than 4 target qubits was not explicitly tested.