Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/actions/checkout-4…
Browse files Browse the repository at this point in the history
….1.5
  • Loading branch information
speller26 authored Jun 26, 2024
2 parents 24be004 + eeb7630 commit 1cd8d60
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: |
tox -e unit-tests
- name: Upload coverage report to Codecov
uses: codecov/codecov-action@54bcd8715eee62d40e33596ef5e8f0f48dbbccab # v4.1.0
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0
with:
token: ${{ secrets.CODECOV_TOKEN }}
if: ${{ strategy.job-index }} == 0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
package_dir={"": "src"},
install_requires=[
"amazon-braket-schemas>=1.21.3",
"amazon-braket-default-simulator>=1.21.4",
"amazon-braket-default-simulator>=1.25.0",
"oqpy~=0.3.5",
"backoff",
"boltons",
Expand Down
14 changes: 8 additions & 6 deletions src/braket/circuits/braket_program_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

from collections.abc import Iterable
from typing import Optional, Union

import numpy as np
Expand Down Expand Up @@ -161,16 +162,17 @@ def handle_parameter_value(
return FreeParameterExpression(evaluated_value)
return value

def add_measure(self, target: tuple[int]) -> None:
def add_measure(
self, target: tuple[int], classical_targets: Optional[Iterable[int]] = None
) -> None:
"""Add a measure instruction to the circuit
Args:
target (tuple[int]): the target qubits to be measured.
classical_targets (Optional[Iterable[int]]): the classical registers
to use in the qubit measurement.
"""
for index, qubit in enumerate(target):
for iter, qubit in enumerate(target):
index = classical_targets[iter] if classical_targets else iter
instruction = Instruction(Measure(index=index), qubit)
self._circuit.add_instruction(instruction)
if self._circuit._measure_targets:
self._circuit._measure_targets.append(qubit)
else:
self._circuit._measure_targets = [qubit]
9 changes: 5 additions & 4 deletions src/braket/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ def add_instruction(
# Check if there is a measure instruction on the circuit
self._check_if_qubit_measured(instruction, target, target_mapping)

# Update measure targets if instruction is a measurement
if isinstance(instruction.operator, Measure):
measure_target = target or instruction.target[0]
self._measure_targets = (self._measure_targets or []) + [measure_target]

if not target_mapping and not target:
# Nothing has been supplied, add instruction
instructions_to_add = [instruction]
Expand Down Expand Up @@ -710,10 +715,6 @@ def _add_measure(self, target_qubits: QubitSetInput) -> None:
target=target,
)
)
if self._measure_targets:
self._measure_targets.append(target)
else:
self._measure_targets = [target]

def measure(self, target_qubits: QubitSetInput) -> Circuit:
"""
Expand Down
29 changes: 29 additions & 0 deletions test/unit_tests/braket/circuits/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3610,3 +3610,32 @@ def test_circuit_with_global_phase():
"b[0] = measure $0;",
]
)


def test_from_ir_round_trip_transformation_with_targeted_measurements():
circuit = (
Circuit()
.h(0)
.cnot(0, 1)
.add_instruction(Instruction(Measure(index=2), 1))
.add_instruction(Instruction(Measure(index=1), 2))
.add_instruction(Instruction(Measure(index=0), 0))
)
ir = OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"bit[3] b;",
"qubit[3] q;",
"h q[0];",
"cnot q[0], q[1];",
"b[2] = measure q[1];",
"b[1] = measure q[2];",
"b[0] = measure q[0];",
]
),
inputs={},
)

assert Circuit.from_ir(ir) == Circuit.from_ir(circuit.to_ir("OPENQASM"))
assert circuit.to_ir("OPENQASM") == Circuit.from_ir(ir).to_ir("OPENQASM")

0 comments on commit 1cd8d60

Please sign in to comment.