Skip to content

Improve coverage for amazon_braket_backend.py to 99% by covering regi…#1082

Merged
guan404ming merged 5 commits intoapache:mainfrom
alisha-1000:improve-amazon-braket-backend-coverage
Feb 24, 2026
Merged

Improve coverage for amazon_braket_backend.py to 99% by covering regi…#1082
guan404ming merged 5 commits intoapache:mainfrom
alisha-1000:improve-amazon-braket-backend-coverage

Conversation

@alisha-1000
Copy link
Contributor

Related Issues

Closes #1058

Changes

  • Test

Why

The current coverage for qumat/amazon_braket_backend.py was 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:

    • Local simulator
    • Default simulator
    • Unsupported simulator type fallback
    • Region-based AWS session creation
  • Gate helper wrappers (RX/RY/RZ/U and other single-qubit/multi-qubit gates)

  • execute_circuit:

    • With parameters
    • Without parameters
    • Parameter filtering logic
  • get_final_state_vector:

    • With parameters
    • Without parameters
    • Ensured state_vector() path is exercised
  • calculate_prob_zero edge cases

Coverage impact:

  • amazon_braket_backend.py: ~90% → 99%

Checklist

  • Added or updated unit tests for all changes
  • Added or updated documentation for all changes

…on handling, execution branches, and state vector path
@guan404ming
Copy link
Member

Please help handle the pre-commit error in ci, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. 🤔

@alisha-1000
Copy link
Contributor Author

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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_backend covering 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 U decomposition.
  • Added tests for execute_circuit, get_final_state_vector, and calculate_prob_zero edge cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 81 to 83
mock_boto.assert_called_once_with(region_name="us-west-2")
mock_aws_session.assert_called_once()
mock_device.assert_called_once()
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment on lines 220 to 226
"parameter_values": {"theta": 0.5},
}

execute_circuit(circuit, mock_backend, config)
mock_backend.run.assert_called_once()


Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
"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}

Copilot uses AI. Check for mistakes.
Comment on lines +116 to +124
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()
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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)

Copilot uses AI. Check for mistakes.
@alisha-1000
Copy link
Contributor Author

@ryankert01 Hi, I’ve addressed the Copilot feedback and updated the tests accordingly.
Could you please approve the workflows so that CI can run?
Thank you!

Copy link
Member

@guan404ming guan404ming left a comment

Choose a reason for hiding this comment

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

LGTM

@guan404ming guan404ming merged commit bdc1a7b into apache:main Feb 24, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Enhance Module Testing Coverage

5 participants