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

feature: add support for braket measure instruction #169

Merged
merged 6 commits into from
Apr 8, 2024
Merged

Conversation

ashlhans
Copy link
Collaborator

@ashlhans ashlhans commented Apr 1, 2024

Summary

Add support for Braket's measure instruction.

This solves the bug detailed in Issue #130 where translating a circuit in qiskit to braket returned all the measurements instead of the requested measured qubit.

Details and comments

Now, users can measure a subset of qubits on local simulators or any device that supports partial measurement.

Example:

sim = BraketLocalBackend()
qc = QuantumCircuit(2, 1)
qc.h(0)
qc.cx(0, 1)
qc.measure(0, 0)
task = transpile(qc, sim)
task = sim.run(task)
result = task.result()
counts = result.get_counts()
print(counts)

{'1': 518, '0': 506}

To keep the current functionality of the bug and measure all the qubits, simply change measure(0, 0) to measure_all()

sim = BraketLocalBackend()
qc = QuantumCircuit(2, 1)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
task = transpile(qc, sim)
task = sim.run(task)
result = task.result()
counts = result.get_counts()
print(counts)

{'00': 503, '11': 521}

List of changes

  • Update the amazon-braket-sdk version
  • add measure to the Braket gates
  • update adapter.py to use Braket's measure
  • add tests

@CLAassistant
Copy link

CLAassistant commented Apr 1, 2024

CLA assistant check
All committers have signed the CLA.

@ashlhans ashlhans marked this pull request as ready for review April 1, 2024 22:17
Copy link
Collaborator

@speller26 speller26 left a comment

Choose a reason for hiding this comment

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

Looking great!

qiskit_braket_provider/providers/adapter.py Outdated Show resolved Hide resolved
@@ -128,6 +129,7 @@
"zz": lambda angle: [braket_gates.ZZ(2 * pi * angle)],
# Global phase
_GPHASE_GATE_NAME: lambda phase: [braket_gates.GPhase(phase)],
"measure": lambda: [measure.Measure()],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this necessary? This dict is only used for gates.

Suggested change
"measure": lambda: [measure.Measure()],

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is used to match the qiskit measure to the Braket Measure so without it, the Braket measure isn't added to the Circuit.

I do agree that this might not work well in this particular dict. Maybe there can be a _ADDITIONAL_INSTRUCTION_NAME_TO_BRAKET_NAME dict for things like measure?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That should be handled here:

if gate_name == "measure":
qubit = qubits[0] # qubit count = 1 for measure
qubit_index = circuit.find_bit(qubit).index
braket_circuit.measure(qubit_index)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Gotcha, that does work for the Qiskit to Braket conversion but the Braket to Qiskit conversion here checks for gates in the _GATE_NAME_TO_QISKIT_GATE dict.

Is there a better way to implement the Braket to Qiskit conversion for measure?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, it's fine to leave it in the Braket->Qiskit direction; you could do a special check for measure operators, but that's not necessary.

Copy link
Collaborator Author

@ashlhans ashlhans Apr 5, 2024

Choose a reason for hiding this comment

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

Removed the measure instruction from _GATE_NAME_TO_BRAKET_GATE but left it in _GATE_NAME_TO_QISKIT_GATE

qiskit_braket_provider/providers/adapter.py Outdated Show resolved Hide resolved
@speller26 speller26 merged commit 387a9c1 into main Apr 8, 2024
7 checks passed
@speller26 speller26 deleted the addMeasure branch April 8, 2024 03:23
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