Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/braket/default_simulator/openqasm/program_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from abc import ABC, abstractmethod
from collections.abc import Iterable
from copy import copy
from dataclasses import fields
from functools import singledispatchmethod
from typing import TYPE_CHECKING, Any
Expand Down Expand Up @@ -1970,7 +1971,7 @@ def _initialize_paths_from_circuit(self) -> None:
existing variables from the shared variable table to the path.
"""
initial_path = self._paths[0]
initial_path._instructions = list(self._circuit.instructions)
initial_path._instructions = [copy(ins) for ins in self._circuit.instructions]
initial_path.shots = self._shots

for name, value in self.variable_table.items():
Expand Down
33 changes: 33 additions & 0 deletions test/unit_tests/braket/default_simulator/test_mcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3284,6 +3284,39 @@ def test_measure_only_in_if_branch_z(self, simulator):
assert counter == {"00": 1000}


class TestMCMNonContiguousClassicalIndices:
"""MCMs that write non-contiguous classical indices."""

def test_minimal_unassigned_low_bit_stays_zero(self, simulator):
"""Untouched ``q[0]`` stays 0 when only ``c[1]`` is assigned."""
qasm = """
OPENQASM 3.0;
qubit[2] q;
bit[2] c;
h q[1];
c[1] = measure q[1];
if (c[1]) { x q[1]; }
"""
result = simulator.run_openqasm(OpenQASMProgram(source=qasm, inputs={}), shots=2000)
counter = Counter(["".join(m) for m in result.measurements])
assert counter == {"00": 2000}

def test_if_branch_preserves_captured_bit_position(self, simulator):
"""``if (c[N])`` keeps the captured value at ``q[N]``'s position."""
qasm = """
OPENQASM 3.0;
qubit[3] q;
bit[3] c;
h q[1];
c[1] = measure q[1];
if (c[1]) { x q[2]; }
c[2] = measure q[2];
"""
result = simulator.run_openqasm(OpenQASMProgram(source=qasm, inputs={}), shots=2000)
counter = Counter(["".join(m) for m in result.measurements])
assert set(counter) == {"000", "011"}


class TestMCMBranchedInstructionRouting:
"""Cover branched-mode instruction routing (add_*_instruction, add_measure, etc.)."""

Expand Down
Loading