amazon-braket / amazon-braket-sdk-python

A Python SDK for interacting with quantum devices on Amazon Braket
https://aws.amazon.com/braket/
Apache License 2.0
294 stars 118 forks source link

fix: add measure qubit targets in braket_program_context #947

Closed ashlhans closed 3 months ago

ashlhans commented 3 months ago

Issue #, if available:

Description of changes: Currently, when a measure instruction is added in braket_program_context.py it is not added to the circuit's measure_targets which results in measure instructions getting added twice.

Changes made

To reproduce the issue this solves:

qasm = """
OPENQASM 3.0;

def ghz(int[32] n) {
    h q[0];
    for int i in [0:n - 1] {
        cnot q[i], q[i + 1];
    }
}

int[32] n = 5;
bit[n + 1] c;
qubit[n + 1] q;

ghz(n);

c = measure q;
"""
circuit = Circuit.from_ir(qasm)
print(sv1.run(circuit, shots=1000).result().measurement_counts)

returns a ValidationException: An error occurred (ValidationException) when calling the CreateQuantumTask operation: [line 16] qubit q[0] is already measured or captured. Cannot perform a new operation on it since the measurements are added twice.

Testing done: tox and tox -e integ-tests

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

codecov[bot] commented 3 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (f556567) to head (0bf0bb7).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #947 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 134 134 Lines 8911 8914 +3 Branches 2008 2009 +1 ========================================= + Hits 8911 8914 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.