PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.18k stars 569 forks source link

Improve measurement support in from_qasm #5646

Closed trbromley closed 3 weeks ago

trbromley commented 2 months ago

Feature details

The from_qasm function allows users to convert a QASM2 string into a PennyLane quantum function (see docs). Internally, this is done via the converter in the PennyLane-Qiskit plugin.

Similar to the recently-added improvement to from_qiskit, we want from_qasm to be able to

  1. optionally append PL measurements to the end of the circuit and
  2. faithfully convert measurements already present in the QASM2 string.

Currently, from_qasm ignores any measurements present in the QASM2 string.

Implementation

For point 1, PennyLane supports a host of measurements (MeasurementProcessobjects) like expectation values that cannot be represented in QASM2. We would like for a user to be able to convert their QASM2 string with from_qasm and append their desired PennyLane measurement to the end of the circuit like so:

>>> hadamard_qasm = 'OPENQASM 2.0;' \
...                 'include "qelib1.inc";' \
...                 'qreg q[1];' \
...                 'h q[0];'
>>> my_circuit = qml.from_qasm(hadamard_qasm, measurements=qml.expval(qml.Z(0)))

For point 2, a QASM2 string can contain simple measure primitives representing qubit measurement in the standard basis. This can in principle be mapped to PennyLane's qml.measure(). We would like for any measure primitives in the input QASM2 string to be faithfully converted when using from_qasm, following the same behaviour specified in from_qiskit.

A successful PR must include:

Check out our development guide for more information, and don't hesitate to reach out to us if you need assistance.

How important would you say this feature is?

1: Not important. Would be nice to have.

Additional information

This issue will require work in both the core PennyLane repo and the PennyLane-Qiskit plugin repo where the existing conversion functionality lives. The solution to this issue can rely on existing QASM2 to Qiskit conversion support, e.g., via qiskit.qasm2.

Coffee4MePlz commented 1 month ago

Hi, I would like to tackle this issue as part of the UnitaryHack

trbromley commented 1 month ago

Hi @Coffee4MePlz! That would be great :rocket:

We're here to help with guidance if you need it. Is it clear how to get started?

Coffee4MePlz commented 1 month ago

Great. I'm studying the plugin first and from_qiskit to get a sense of how things were implemented . If any doubts arise I'll get in touch. Thank you.

MashAliK commented 1 month ago

Hello,

I've updated both the core library and the plugin to solve this issue. I've created a pull request in the plugin repo (https://github.com/PennyLaneAI/pennylane-qiskit/pull/555). How will the CI tests work if I create a PR in this repo since it will rely on the other PR? Please let me know what the process for developing on the two different repos simultaneously is. Thank you.

trbromley commented 3 weeks ago

Thanks @MashAliK! Great job with this, and congrats on completing this bounty :medal_military:

Would you optionally like to be tagged in any of our announcements around unitaryHACK or the upcoming PennyLane 0.37 release at the start of July? This would be on LinkedIn and Twitter, so if you would like, please share your username(s).

MashAliK commented 3 weeks ago

@trbromley Thanks a lot to you and the PL team! I had a great time working on this. Absolutely! I don't have Twitter but here is my LinkedIn. Thanks again.