Skip to content

Add cirq.sample_final_state_vector method #1740

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

Open
Strilanc opened this issue Jun 21, 2019 · 31 comments
Open

Add cirq.sample_final_state_vector method #1740

Strilanc opened this issue Jun 21, 2019 · 31 comments
Labels
area/channels area/protocols/mixtures area/simulation good for learning For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues" kind/feature-request Describes new functionality skill-level/advanced One or more of the areas need a solid understanding. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@Strilanc
Copy link
Contributor

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.
@vtomole vtomole added the good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. label Jun 22, 2019
@vtomole
Copy link
Collaborator

vtomole commented Jun 22, 2019

Good first issue for the ambitious.

@Lucaman99
Copy link
Contributor

Lucaman99 commented Jun 24, 2019

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?

@vtomole
Copy link
Collaborator

vtomole commented Jun 24, 2019

@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: good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue.

@Lucaman99
Copy link
Contributor

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

@Strilanc
Copy link
Contributor Author

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.

@Lucaman99
Copy link
Contributor

@Strilanc Awesome, I'll try doing that

@Lucaman99
Copy link
Contributor

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?

@Strilanc
Copy link
Contributor Author

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.

@Lucaman99
Copy link
Contributor

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

@Strilanc
Copy link
Contributor Author

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.

@Lucaman99
Copy link
Contributor

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

@Lucaman99
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

Lucaman99 commented Jul 5, 2019

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

@Lucaman99
Copy link
Contributor

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).

@Strilanc
Copy link
Contributor Author

Strilanc commented Jul 8, 2019

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?

@Lucaman99
Copy link
Contributor

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
Copy link
Contributor

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?

@Strilanc
Copy link
Contributor Author

Strilanc commented Jul 9, 2019

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

@Lucaman99
Copy link
Contributor

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

@Strilanc
Copy link
Contributor Author

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

@jkc1113
Copy link

jkc1113 commented Sep 20, 2019

I can try this out

@dabacon
Copy link
Collaborator

dabacon commented May 4, 2020

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

@artvandalay404
Copy link

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

@balopat balopat added area/channels area/protocols/mixtures and removed good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. labels Sep 25, 2020
@balopat balopat added area/simulation good for learning For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues" skill-level/advanced One or more of the areas need a solid understanding. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on labels Sep 25, 2020
@balopat balopat changed the title Add cirq.sample_final_wavefunction method Add cirq.sample_final_state_vector method Sep 25, 2020
@balopat
Copy link
Contributor

balopat commented Sep 25, 2020

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

@balopat balopat added the kind/feature-request Describes new functionality label Sep 25, 2020
@artvandalay404
Copy link

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

@artvandalay404
Copy link

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

@artvandalay404
Copy link

@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
Copy link

@balopat are we still tracking this? thank you

@daxfohl
Copy link
Collaborator

daxfohl commented Nov 6, 2021

@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
Copy link
Collaborator

daxfohl commented Feb 7, 2025

@Strilanc Does this issue mean (a) sample a result efficiently from the union of possible final state vectors, or (b) run a simulation to get a single sample final state vector in its entirety?

  • I think it's (b), but if so, why does it need to be limited to mixtures? The state vector simulator can work with channels as well. Maybe this was not the case in 2019?
  • If (a), the mixture restriction makes sense, as you can calculate a set of possible final state vectors and sample from them efficiently. Though the set will grow exponentially with each noise gate and be impractical for all but the shortest of circuits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/channels area/protocols/mixtures area/simulation good for learning For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues" kind/feature-request Describes new functionality skill-level/advanced One or more of the areas need a solid understanding. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

No branches or pull requests

10 participants