unitaryfund / mitiq

Mitiq is an open source toolkit for implementing error mitigation techniques on most current intermediate-scale quantum computers.
https://mitiq.readthedocs.io
GNU General Public License v3.0
360 stars 158 forks source link

Add tutorial example where mitiq makes a variational problem trainable #529

Closed willzeng closed 3 years ago

willzeng commented 3 years ago

This suggestion is inspired by a question from @mstechly.

"Have you seen an example where the landscape is flat without error mitigation and you can't optimize it, but becomes peaked with error mitigation and then you can optimize it?"

This would be a cool example to see and we could consider adding it to the documentation. It could even be for a relatively artificial variational problem, e.g. optimize some small ansatz against some example Hamiltonian using VQE.

Ideally the tutorial would show:

  1. Training not converging or converging poorly at a given noise rate
  2. Adding ZNE and/or PEC with a few lines that causes training to converge
  3. Plotting a cut of the variational landscapes with and without error-mitigation to give a visual representation. An example of such a landscape cut plot is Fig 9 in https://arxiv.org/pdf/2012.03819.pdf
mstechly commented 3 years ago

As another reference, I've done an experiment on how the optimization landscape looks like for different noise parameters when I was working on my VQF implementation, you can read the report here: https://github.com/mstechly/vqf/blob/master/research/2019_05_12_visualize_optimization_space/report/report.md .

And for some more realistic plots I recommend Fig 3 in https://arxiv.org/abs/2004.04197 .

I don't have time to work on this myself, but if anyone would like to, I'd be happy to chat :)

crazy4pi314 commented 3 years ago

We may want to save this for some future hackathon events so I am gonna remove it from the milestone for now.

DSamuel1 commented 3 years ago

I've been looking for a project to work on this Summer and @rmlarose recommended I check out this issue. Would I be able to take on this task and work on it this Summer? I would probably be able to start on it in June.

rmlarose commented 3 years ago

Thanks @DSamuel1! Assigned to you.

Misty-W commented 3 years ago

I'm interested in solving this as part of the hackathon (as it appears on the project page), but the above conversation has me a little confused...is it cool if I give it a try?

crazy4pi314 commented 3 years ago

@Misty-W Absolutely! Since these are examples in the docs, we can never have enough šŸ˜Š If you have any other questions, feel free to ask here or on the Unitary Fund Discord.

Misty-W commented 3 years ago

Hi Sarah @crazy4pi314 , thanks for your reply! Iā€™ve been working on the problem with PyQuil, but Iā€™ve run into a few snags. To start, I used the problem shown in the Grove Noisy VQE tutorial at https://grove-docs.readthedocs.io/en/latest/vqe.html#links-and-further-reading as a kind of benchmark.

  1. Whenever I run the VQE instance with the QVM from api.QVMConnection, I get the error 'QVMConnection' object has no attribute 'compile' I used a workaround noisy_qvm = get_qc("9q-square-noisy-qvm") to allow my code to run, but Iā€™ll need to manipulate the noise level in the actual solution. Any ideas how to fix?
  2. When using noisy_qvm, the result I obtained with ZNE didnā€™t make sense (6, when I believe the value should be close to -1). I also tried PEC but stopped it after running for ~ 1hr with no result. a. I wrote an executable function to pass to the error mitigation function, and that executable calls the vqe.run. I realize now that's probably not the right approach. Shouldnā€™t the mitigation be performed on the intermediate expectation values, inside the ā€œvqe runā€? If so, then I assume I need to make my own VQE function and call the error mitigation function inside it. Unless there is another way? b. Since the deadline is approaching, Iā€™m not sure Iā€™ll have time to create a VQE function from scratch and solve the original issue. Is it allowed to make a modified version? How do I credit the developers of the original function?
  3. Given the challenges Iā€™m facing in PyQuil, would you recommend starting over in Qiskit instead? Iā€™m new to both, but I do have an IBM-Q account, so maybe it would be easier to run on their devices. šŸ˜Š
andreamari commented 3 years ago

Thanks @Misty-W! I noticed your post and so I add my thoughts:

Hi Sarah @crazy4pi314 , thanks for your reply! Iā€™ve been working on the problem with PyQuil, but Iā€™ve run into a few snags. To start, I used the problem shown in the Grove Noisy VQE tutorial at https://grove-docs.readthedocs.io/en/latest/vqe.html#links-and-further-reading as a kind of benchmark.

Nice, a PyQuil example would be great!

  1. Whenever I run the VQE instance with the QVM from api.QVMConnection, I get the error 'QVMConnection' object has no attribute 'compile' I used a workaround noisy_qvm = get_qc("9q-square-noisy-qvm") to allow my code to run, but Iā€™ll need to manipulate the noise level in the actual solution. Any ideas how to fix?

As far as I remember, one could define custom noise models by manually defining the associated Kraus operators. But I think your idea of using a simulated real device with noisy_qvm = get_qc("9q-square-noisy-qvm") is even better and simpler.

In principle with Mitiq you can scale the noise indirectly by scaling the depth of your circuits without changing the physical noise of the beck-end (see e.g. here). So, maybe, you don't actually need to change the noise of the QVM.

  1. When using noisy_qvm, the result I obtained with ZNE didnā€™t make sense (6, when I believe the value should be close to -1). I also tried PEC but stopped it after running for ~ 1hr with no result.

I wouldn't use PEC in this example, since it requires the tomography of the gateset of your noisy QVM unless you manually define a custom noise model. ZNE is usually easier to apply but sometimes it may fail too. My suggestion is to start with the simplest extrapolation model factory = LinearFactory(scale_factors = [1.0, 3.0]) . If this fails too, there could be other problems.

a. I wrote an executable function to pass to the error mitigation function, and that executable calls the vqe.run. I realize now that's probably not the right approach. Shouldnā€™t the mitigation be performed on the intermediate expectation values, inside the ā€œvqe runā€? If so, then I assume I need to make my own VQE function and call the error mitigation function inside it. Unless there is another way?

The executor function should take a circuit as input and return an expectation value. In a VQE algorithm there is a step in which the expectation value of the full Hamiltonian (or of its local terms) is evaluated from an input circuit. This is the step in which Mitiq should be used. If your object grove.pyvqe.vqe.VQE hides this step it may be a bit complicated to use Mitiq. Maybe one needs to override the grove.pyvqe.vqe.VQE.expectation method of this class...

b. Since the deadline is approaching, Iā€™m not sure Iā€™ll have time to create a VQE function from scratch and solve the original issue. Is it allowed to make a modified version? How do I credit the developers of the original function?

I would say that any nice example in which Mitiq is useful for solving a known NISQ algorithm is interesting. It doesn't need to be necessarily a VQE or MaxCut. Coping the code from some existing function is fine as long as credit is given. But these details can be fixed in a review process of a possible PR.

  1. Given the challenges Iā€™m facing in PyQuil, would you recommend starting over in Qiskit instead? Iā€™m new to both, but I do have an IBM-Q account, so maybe it would be easier to run on their devices.

Any quantum library is fine. We already have a Qiskit example in the docs and so it would be nice to have something in PyQuil. But this is not necessary.

Misty-W commented 3 years ago

Thanks for the suggestions @andreamari! I'll try them out and post any additional questions here.

mstechly commented 3 years ago

@Misty-W I'm not sure if that's the case, but I discourage you from using VQE implementation in Grove, as it's not being maintained for over 2 years and has some bugs.

Misty-W commented 3 years ago

@mstechly thank you for the suggestion!

Misty-W commented 3 years ago

I opened a PR and tagged it [unitaryHACK]. The full flow doesn't run quite yet, but improvement suggestions are much appreciated.

@crazy4pi314, are you able to see my PR? I don't have much experience with GitHub, so I'm not sure if it ended up in the right place.

Even though the event is ending, I hope to get my code working and have the PR approved soon. It's been a lot of fun, and I really appreciate everyone's help! Special thanks to the organizers for setting it up!

crazy4pi314 commented 3 years ago

@Misty-W I don't see your PR, did you make it to your own fork perhaps? If you want help getting it pushed to the right place just send me a DM on Discord and I can help ya out šŸ’Ŗ

Misty-W commented 3 years ago

@crazy4pi314, I think you're right. I just messaged you on Discord.

github-actions[bot] commented 3 years ago

This issue had no activity for 2 months, and will be closed in one week unless there is new activity. Cheers!

Misty-W commented 3 years ago

Still working on this! Almost done I think.

willzeng commented 3 years ago

Cool looking forward to it @Misty-W !

Misty-W commented 3 years ago

@willzeng I might have spoken a little too soon... my VQE example exhibited the desired behavior twice in a row, but I haven't been able to reproduce it since. Trying out some different noise models to see if that solves the problem.