Skip to content

MCM simulation returns measurement strings padded to total qubit count instead of bit[N] size #375

@yitchen-tim

Description

@yitchen-tim

Describe the bug

When a program contains mid-circuit measurements (MCM), measurement_counts returns bitstrings padded to the total declared qubit count instead of the size of the classical register that was assigned. An equivalent non-MCM program returns the correctly sized bitstrings, so the issue is specific to the branched MCM execution path.

In the example below, a single qubit is measured into a bit[1] register, but each shot string is two characters wide.

To reproduce

  1. Install amazon-braket-default-simulator (reproduced on 1.39.3.dev0 / main) and amazon-braket-sdk.

  2. Run the following script:

    from braket.devices import LocalSimulator
    from braket.ir.openqasm import Program
    
    qasm = """OPENQASM 3.0;
    qubit[2] q;
    bit[1] b;
    x q[0];
    b[0] = measure q[0];
    x q[0];
    b[0] = measure q[0];
    x q[0];
    b[0] = measure q[0];
    """
    
    res = LocalSimulator().run(Program(source=qasm), shots=100).result()
    print(res.measurement_counts)
  3. Observe the output:

    Counter({'10': 100})
    
  4. For contrast, run the same shape without MCM and confirm it works correctly:

    qasm = """OPENQASM 3.0;
    bit[1] b;
    qubit[2] q;
    h q[1];
    b[0] = measure q[1];
    """

    Output:

    Counter({'1': 56, '0': 44})
    

Expected behavior

Since b is declared as bit[1] and only one qubit is assigned into it, each shot should produce a 1-bit string. The expected output for the MCM program is:

Counter({'1': 100})

In general, the width of each measurement string for an MCM program should match the size of the classical register being assigned (i.e., the qubits flowing into b), not the total number of declared qubits.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions