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

feat: Track classical register indices for measurements #1006

Closed

Conversation

Altanali
Copy link
Contributor

Issue #, if available:
#1004

Description of changes:
Update the BraketProgramContext to extend the changes introduced in amazon-braket/amazon-braket-default-simulator-python#266, which provide a classical register index to the BraketProgramContext add_measure method when a measurement assignment is used in an OpenQASM program (e.g. b[1] = measure q[1];). This allows translations to and from Braket BDK Circuits and OpenQASM to be consistent with regards to classical register indices used in measurement assignments as currently, these indices are discarded entirely.

Additionally, updating the _measure_targets array in the Circuit object was moved to the add_instruction method from the _add_measure method. This is because users of the Circuit class are able to add measurement instructions via the add_instruction method, but _measure_targets will not be updated to reflect qubits used in the measurement. This results in incorrect behavior when using to_ir to take a circuit with measurements added via add_instruction method calls to OpenQASM as to_ir will check the _measure_targets variable to see if any qubits are measured, see that the list is empty, and apply a measurement to all the qubits in the circuit, including those already measured via the original add_instruction method calls.

Testing done:

A round trip test was added to verify that classical register assignments are persisted when going from BDK Circuit to OpenQASM and back to BDK Circuit via the to_ir and from_ir methods; this is similarly checked when going from OpenQASM to BDK Circuit back to OpenQASM.

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@Altanali Altanali requested a review from a team as a code owner June 24, 2024 21:16
@Altanali Altanali changed the title Introduce tracking of classical register indices for measurement operations. feat: Introduce tracking of classical register indices for measurement operations. Jun 25, 2024
@speller26 speller26 self-requested a review June 26, 2024 17:51
@speller26 speller26 changed the title feat: Introduce tracking of classical register indices for measurement operations. feat: Tracking classical register indices for measurements Jun 26, 2024
@speller26 speller26 changed the title feat: Tracking classical register indices for measurements feat: Track classical register indices for measurements Jun 26, 2024
Comment on lines 894 to 922
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")


Copy link
Member

Choose a reason for hiding this comment

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

How to get around breaking dependencies:

  1. Remove this test from this PR and revert the versions in setup.py and tox.ini
  2. Merge this PR
  3. Merge feat: Track classical register indices for measurements amazon-braket-default-simulator-python#266
  4. Create and merge new PR that reintroduces this test and bumps the default simulator version in setup.py to latest release
Suggested change
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")

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've removed the test and reverted the targets for the default simulator version in the setup.py and tox.ini files in the following commit: 7b3ebf3 !

@Altanali Altanali closed this Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants