Skip to content

Add error path test for invalid number of targets to add_op_to_circuit#1039

Closed
mhucka wants to merge 3 commits intoquantumlib:mainfrom
mhucka:testing-improvement-add-op-to-circuit-target-limit-16945383875852359583
Closed

Add error path test for invalid number of targets to add_op_to_circuit#1039
mhucka wants to merge 3 commits intoquantumlib:mainfrom
mhucka:testing-improvement-add-op-to-circuit-target-limit-16945383875852359583

Conversation

@mhucka
Copy link
Copy Markdown
Collaborator

@mhucka mhucka commented Apr 6, 2026

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.

…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.
@github-actions github-actions Bot added the size: S 10< lines changed <50 label Apr 6, 2026
@mhucka mhucka marked this pull request as ready for review April 6, 2026 02:00
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +882 to +894
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This new test case is redundant and likely contains a bug that will prevent it from running.

  1. Redundancy: The NotImplementedError for controlled gates with more than 4 target qubits is already explicitly verified in test_control_limits (lines 876-879), which uses the public simulate API to trigger the same code path and matches the same error message.
  2. Likely AttributeError: add_op_to_circuit is defined in the qsim_circuit module. Unless it is explicitly exported in qsimcirq/__init__.py, the call to qsimcirq.add_op_to_circuit will fail. Based on the usage in qsim_simulator.py (line 22), it should likely be accessed via qsimcirq.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.

@mhucka mhucka closed this Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: S 10< lines changed <50

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant