Skip to content

Add a test for an uncovered case involving add_op_to_opstring#1061

Open
mhucka wants to merge 2 commits into
quantumlib:mainfrom
mhucka:add-opstring-controlled-gate-test
Open

Add a test for an uncovered case involving add_op_to_opstring#1061
mhucka wants to merge 2 commits into
quantumlib:mainfrom
mhucka:add-opstring-controlled-gate-test

Conversation

@mhucka
Copy link
Copy Markdown
Collaborator

@mhucka mhucka commented May 10, 2026

Added a new test case test_add_op_to_opstring_controlled_gate_error to qsimcirq_tests/qsimcirq_test.py to verify that add_op_to_opstring correctly raises a ValueError when a controlled operation is passed.

mhucka added 2 commits May 10, 2026 02:31
Added a new test case `test_add_op_to_opstring_controlled_gate_error` to
`qsimcirq_tests/qsimcirq_test.py` to verify that `add_op_to_opstring`
correctly raises a `ValueError` when a controlled operation is passed.
@github-actions github-actions Bot added the size: S 10< lines changed <50 label May 10, 2026
@mhucka mhucka marked this pull request as ready for review May 10, 2026 02:33
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_opstring_controlled_gate_error, to verify that add_op_to_opstring correctly raises a ValueError when encountering unsupported gate types. Feedback was provided indicating that the current test implementation using controlled_by would trigger a qubit count validation error before reaching the intended gate kind check. Additionally, the reviewer noted that the gate kind would be identified as kX rather than kCX, and suggested using cirq.CX to correctly trigger and validate the expected error message.


def test_add_op_to_opstring_controlled_gate_error():
q0, q1 = cirq.LineQubit.range(2)
op = cirq.X(q0).controlled_by(q1)
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

The test uses cirq.X(q0).controlled_by(q1), which is a 2-qubit operation. In add_op_to_opstring, this will trigger the qubit count validation (len(qsim_op.qubits) != 1) at line 254 of qsim_circuit.py before it can reach the gate kind check. Furthermore, _cirq_gate_kind for a ControlledGate returns the kind of its sub-gate, so for cirq.X.controlled_by(q1), the kind would be kX, not kCX. To match the expected error message and correctly trigger the gate kind validation, use cirq.CX(q1, q0) instead.

Suggested change
op = cirq.X(q0).controlled_by(q1)
op = cirq.CX(q1, q0)

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