Skip to content

Commit

Permalink
fix: Support single-register measurements in from_ir (#934)
Browse files Browse the repository at this point in the history
* fix: add support for measuring a single register in an OpenQASM program

* add round trip transformation test from circuit to OQ3 to circuit
  • Loading branch information
ashlhans authored Apr 5, 2024
1 parent c57a9ba commit b98bce5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/braket/circuits/braket_program_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,6 @@ def add_measure(self, target: tuple[int]) -> None:
Args:
target (tuple[int]): the target qubits to be measured.
"""
instruction = Instruction(Measure(), list(target))
self._circuit.add_instruction(instruction)
for index, qubit in enumerate(target):
instruction = Instruction(Measure(index=index), qubit)
self._circuit.add_instruction(instruction)
42 changes: 42 additions & 0 deletions test/unit_tests/braket/circuits/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,48 @@ def test_from_ir_with_measure():
assert Circuit.from_ir(source=ir.source, inputs=ir.inputs) == expected_circ


def test_from_ir_with_single_measure():
ir = OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"bit[2] b;",
"qubit[2] q;",
"h q[0];",
"cnot q[0], q[1];",
"b = measure q;",
]
),
inputs={},
)
expected_circ = Circuit().h(0).cnot(0, 1).measure(0).measure(1)
assert Circuit.from_ir(source=ir.source, inputs=ir.inputs) == expected_circ


def test_from_ir_round_trip_transformation():
circuit = Circuit().h(0).cnot(0, 1).measure(0).measure(1)
ir = OpenQasmProgram(
source="\n".join(
[
"OPENQASM 3.0;",
"bit[2] b;",
"qubit[2] q;",
"h q[0];",
"cnot q[0], q[1];",
"b[0] = measure q[0];",
"b[1] = measure q[1];",
]
),
inputs={},
)
new_ir = circuit.to_ir("OPENQASM")
new_circuit = Circuit.from_ir(new_ir)

assert new_ir == ir
assert Circuit.from_ir(source=ir.source, inputs=ir.inputs) == circuit
assert new_circuit == circuit


def test_add_with_instruction_with_default(cnot_instr):
circ = Circuit().add(cnot_instr)
assert circ == Circuit().add_instruction(cnot_instr)
Expand Down

0 comments on commit b98bce5

Please sign in to comment.