Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

Add cirq.sample_final_state_vector method

Open Strilanc opened this issue 5 years ago • 30 comments

When a circuit has noise, it does not have a well defined final wavefunction. But as long as the noise is all in the form of mixtures, it is still possible to sample from the possible final wavefunctions as efficiently as if you were running a unitary simulation.

Things this issue involves:

  • Update cirq.final_wavefunction error message to mention the sample_final_wavefunction as an alternative that can be used.
  • Basically copy-paste cirq.final_wavefunction, but tweak it to allow mixtures but not fully general channels.

Strilanc avatar Jun 21 '19 22:06 Strilanc

Good first issue for the ambitious.

vtomole avatar Jun 22 '19 10:06 vtomole

I'm no expert, but I would like to try to give one of these issues a try. How difficult do you think this will be to resolve @vtomole?

Lucaman99 avatar Jun 24 '19 18:06 Lucaman99

@Lucaman99 I encourage you to try it out and let us know when you get stuck (if you do). Note: there are other easier issues here: https://github.com/quantumlib/Cirq/labels/good%20first%20issue

vtomole avatar Jun 24 '19 19:06 vtomole

@vtomole Great, I'll give it a shot! Thank you!

Lucaman99 avatar Jun 24 '19 19:06 Lucaman99

cirq.Simulator will already do the right thing if given a circuit with mixture gates, so there's no need to write new simulation code. It's mostly a matter of short-circuiting the simulation quickly when it's going to fail anyways.

Strilanc avatar Jun 24 '19 20:06 Strilanc

@Strilanc Awesome, I'll try doing that

Lucaman99 avatar Jun 25 '19 00:06 Lucaman99

Hey @vtomole @Strilanc, sorry, this is irrelevant to the actual issue, but I've been trying to setup the virtual environment for Cirq development for a few hours now and for some the reason the virtualenvwrapper library is just not working. It doesn't recognize commands when I try to create a new virtual environment. Would you happen to have any suggestions for resolving this issue?

Lucaman99 avatar Jun 26 '19 02:06 Lucaman99

Sometimes you need to open a new terminal after installing.

virtualenvwrapper is a system dependency, so pip installing cirq or its python dependencies won't install it.

Strilanc avatar Jun 26 '19 02:06 Strilanc

@Strilanc Yeah I installed all of the requirements.txt and restarted the terminal, still not recognizing the command.

Lucaman99 avatar Jun 26 '19 02:06 Lucaman99

In that case I'm not sure what to do. You can always fall back to using virtual envs in the standard way, with a .venv folder somewhere.

Strilanc avatar Jun 26 '19 02:06 Strilanc

Ok, I'll give that a try, thanks!

Lucaman99 avatar Jun 26 '19 02:06 Lucaman99

Hey @Strilanc, I've been playing around with the cirq.final_wavefunction method and it is making sense. I was wondering though, when you say you don't want this new method to allow "fully general channels", what exactly do you mean by that?

Lucaman99 avatar Jul 04 '19 05:07 Lucaman99

Hey @Strilanc - So to construct the cirq.sample_final_wavefunction I took the cirq.final_wavefunction method and removed the condition that all gates have to be unitary. I tested it with a bit flip method acting on a qubit in a small circuit, and it seemed to work (it outputted the correct wavefunction). The issue is that I can only call this new method as cirq.sim.mux.sample_final_wavefunction. If I simply try to call cirq.sample_final_wavefunction, it doesn't work. Do you know why that would be happening?

Lucaman99 avatar Jul 04 '19 18:07 Lucaman99

Also @Strilanc, how would one differentiate between mixtures and fully-general channels? (Sorry for all the questions 😂)

Lucaman99 avatar Jul 05 '19 01:07 Lucaman99

In addition, I tried using the cirq.has_mixture as opposed to cirq.has_unitary method, but for some reason, this didn't work (it kept throwing the same error, meaning that cirq.has_mixture(arg) = False for an argument that should return True).

Lucaman99 avatar Jul 05 '19 02:07 Lucaman99

Hey @Lucaman99 ,

when you say you don't want this new method to allow "fully general channels", what exactly do you mean by that?

A fully general channel is an operation that implements _channel_ but not _mixture_ or _unitary_. That is to say, it does not have an interpretation as a probabilistic set of unitaries acting only on the system qubits. @dabacon do you know a specific concrete example of such a channel?

can only call this new method as cirq.sim.mux.sample_final_wavefunction

You have to import it within cirq/sim/__init__.py and then cirq/__init__.py.

I tried using the cirq.has_mixture as opposed to cirq.has_unitary method, but for some reason, this didn't work (it kept throwing the same error, meaning that cirq.has_mixture(arg) = False for an argument that should return True).

This is likely a bug in has_mixture. What's the specific operation that's failing?

Strilanc avatar Jul 08 '19 17:07 Strilanc

Hey @Strilanc

Thank you for your response! For the has_mixture method, it's essentially just failing when I try to replace the has_unitary with it. I played around with the function a bit more, and created a few simple circuits with some mixer gates (bit flip, phase damp, amplitude damp, etc.) and the has_mixture method seems to always return false, regardless of whether the gates in the circuit are unitary, mixture, etc.

Lucaman99 avatar Jul 09 '19 05:07 Lucaman99

On the other hand, the has_unitary method is working exactly as expected, which leads me to believe that this is in fact a bug. Should I open an issue?

Lucaman99 avatar Jul 09 '19 05:07 Lucaman99

Yes, please open an issue. That's a serious bug.

Strilanc avatar Jul 09 '19 22:07 Strilanc

@Strilanc Will do! Any idea what could be causing the bug?

Lucaman99 avatar Jul 09 '19 23:07 Lucaman99

Probably something silly like checking for _has_mixture_ but not _has_unitary_ (which implies that a mixture can be derived).

Strilanc avatar Jul 10 '19 00:07 Strilanc

I can try this out

jkc1113 avatar Sep 20 '19 00:09 jkc1113

An example of a channel to test mixtures with can be created using cirq.bit_flip.

dabacon avatar May 04 '20 20:05 dabacon

Is help still needed on this? I see that the final_wavefunction is being deprecated for final_state_vector.

artvandalay404 avatar May 26 '20 02:05 artvandalay404

Yes @satw1knandala, we'd love to see this implemented, yes, I renamed the issue to state_vector.

balopat avatar Sep 25 '20 19:09 balopat

Sure, I will get started on this one. You can assign me. Thanks!

artvandalay404 avatar Sep 26 '20 18:09 artvandalay404

update: still working on this. should be done by end of week

artvandalay404 avatar Oct 12 '20 19:10 artvandalay404

@Strilanc @balopat i was able to create the function sample_final_state_vector, but I am writing tests and have a few questions -

  1. to test against a fully general channel, can i just create a random non unitary matrix and test it on that? or do you have a better solution to this?
  2. should i replicate the tests for final_state_vector to sample_final_state_vector in mux_tests.py? the functions only really differ in one line of code (checking for mixture vs unitary) so I'm wondering if it might be repetitive?

Thanks!

artvandalay404 avatar Oct 12 '20 22:10 artvandalay404

@balopat are we still tracking this? thank you

artvandalay404 avatar Oct 23 '20 19:10 artvandalay404

@artvandalay404 Feel free to post a PR with what you have. My knee jerk reaction is that similar test should be fine, but would have to see the actual code. Maybe there's a middle ground. Testing against a channel, I'd just use an existing channel like phase damp. I don't see a need to make a custom matrix.

daxfohl avatar Nov 06 '21 15:11 daxfohl