Improve coverage for amazon_braket_backend.py to 99% by covering regi…#1082
Conversation
…on handling, execution branches, and state vector path
|
Please help handle the pre-commit error in ci, thanks! |
There was a problem hiding this comment.
Why do we need this change?
|
Thanks for pointing this out @guan404ming @viiccwen . The Rust test changes were introduced unintentionally while addressing pre-commit checks. I’ve reverted them to keep this PR strictly scoped to improving amazon_braket_backend.py coverage. |
There was a problem hiding this comment.
Pull request overview
Adds a dedicated test module to increase coverage of qumat/amazon_braket_backend.py by exercising backend initialization branches, gate wrapper helpers, circuit execution paths (with/without parameters), state-vector retrieval, and probability calculation edge cases.
Changes:
- Added new unit tests for
initialize_backendcovering local/default/unsupported simulator selection and region-based session creation. - Added tests for basic gate wrappers plus RX/RY/RZ parameter vs numeric handling and
Udecomposition. - Added tests for
execute_circuit,get_final_state_vector, andcalculate_prob_zeroedge cases.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| mock_boto.assert_called_once_with(region_name="us-west-2") | ||
| mock_aws_session.assert_called_once() | ||
| mock_device.assert_called_once() |
There was a problem hiding this comment.
test_initialize_backend_with_region only asserts that AwsSession() and AwsDevice() were called, but not that the region-derived boto3.Session(region_name=...) object is actually passed into AwsSession(boto_session=...) and that the resulting aws session is forwarded into AwsDevice(..., aws_session=...). Without these assertions, the test would still pass even if the region logic is broken or ignored.
| mock_boto.assert_called_once_with(region_name="us-west-2") | |
| mock_aws_session.assert_called_once() | |
| mock_device.assert_called_once() | |
| # Ensure boto3.Session is created with the configured region | |
| mock_boto.assert_called_once_with(region_name="us-west-2") | |
| # Ensure the boto3 session object is passed into AwsSession | |
| mock_aws_session.assert_called_once_with(boto_session=mock_boto.return_value) | |
| # Ensure the resulting AwsSession is forwarded into AwsDevice | |
| mock_device.assert_called_once() | |
| _, device_kwargs = mock_device.call_args | |
| assert device_kwargs.get("aws_session") is mock_aws_session.return_value |
| "parameter_values": {"theta": 0.5}, | ||
| } | ||
|
|
||
| execute_circuit(circuit, mock_backend, config) | ||
| mock_backend.run.assert_called_once() | ||
|
|
||
|
|
There was a problem hiding this comment.
test_execute_circuit_with_parameters doesn't assert that backend.run receives the expected inputs mapping (or that it filters to only circuit.parameters). As written, it would pass even if execute_circuit ignored parameter_values entirely. Add an assertion on mock_backend.run.call_args (and ideally the returned measurement counts) to lock in the intended behavior.
| "parameter_values": {"theta": 0.5}, | |
| } | |
| execute_circuit(circuit, mock_backend, config) | |
| mock_backend.run.assert_called_once() | |
| # Include an extra parameter to ensure only circuit.parameters are used. | |
| "parameter_values": {"theta": 0.5, "phi": 0.7}, | |
| } | |
| result = execute_circuit(circuit, mock_backend, config) | |
| # Ensure backend.run was called once with the expected inputs mapping. | |
| mock_backend.run.assert_called_once() | |
| _, kwargs = mock_backend.run.call_args | |
| assert "inputs" in kwargs | |
| assert kwargs["inputs"] == {"theta": 0.5} | |
| # Ensure the measurement counts from the backend are returned. | |
| assert result == {"00": 1} |
| circuit.x.assert_called() | ||
| circuit.h.assert_called() | ||
| circuit.cnot.assert_called() | ||
| circuit.ccnot.assert_called() | ||
| circuit.swap.assert_called() | ||
| circuit.cswap.assert_called() | ||
| circuit.y.assert_called() | ||
| circuit.z.assert_called() | ||
| circuit.t.assert_called() |
There was a problem hiding this comment.
test_basic_gate_wrappers uses broad assert_called() checks, so it won’t fail if a wrapper calls the right method but with the wrong qubit indices/arity (and some calls like x happen twice). Consider switching to assert_called_once_with/assert_any_call per wrapper (or separate tests) so each wrapper’s signature and arguments are actually validated.
| circuit.x.assert_called() | |
| circuit.h.assert_called() | |
| circuit.cnot.assert_called() | |
| circuit.ccnot.assert_called() | |
| circuit.swap.assert_called() | |
| circuit.cswap.assert_called() | |
| circuit.y.assert_called() | |
| circuit.z.assert_called() | |
| circuit.t.assert_called() | |
| # apply_not_gate and apply_pauli_x_gate are both expected to call x on qubit 0 | |
| circuit.x.assert_any_call(0) | |
| assert circuit.x.call_count >= 2 | |
| circuit.h.assert_called_once_with(1) | |
| circuit.cnot.assert_called_once_with(0, 1) | |
| circuit.ccnot.assert_called_once_with(0, 1, 2) | |
| circuit.swap.assert_called_once_with(0, 1) | |
| circuit.cswap.assert_called_once_with(0, 1, 2) | |
| circuit.y.assert_called_once_with(1) | |
| circuit.z.assert_called_once_with(2) | |
| circuit.t.assert_called_once_with(0) |
|
@ryankert01 Hi, I’ve addressed the Copilot feedback and updated the tests accordingly. |
Related Issues
Closes #1058
Changes
Why
The current coverage for
qumat/amazon_braket_backend.pywas around ~90%.This PR improves coverage to 99% by adding unit tests for previously uncovered branches.
Improving coverage in backend modules increases reliability and ensures
correct handling of edge cases such as AWS session initialization and parameter filtering.
How
Added unit tests covering:
Backend initialization:
Gate helper wrappers (RX/RY/RZ/U and other single-qubit/multi-qubit gates)
execute_circuit:get_final_state_vector:calculate_prob_zeroedge casesCoverage impact:
amazon_braket_backend.py: ~90% → 99%Checklist