Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support single-register measurements in from_ir #934

Merged
merged 3 commits into from
Apr 5, 2024
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
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():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add tests to do a round trip transformation? Circuit -> OQ3 -> Circuit.from_ir and assert that the original and the parsed circuit are the same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a round trip test.

This test_from_ir_with_single_measure test would not have the same OpenQASM output though because Circuit.from_ir would not output b = measure q since the BDK does not handle the measure assignment.

So instead it would output:

b[0] = measure q[0];
b[1] = measure q[1];

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
Comment on lines +851 to +852
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already covered by other test cases, right? Not that I'm complaining.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is covered by other test cases

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