Cirq
Cirq copied to clipboard
Add cirq.sample_final_state_vector method
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.
Good first issue for the ambitious.
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 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 Great, I'll give it a shot! Thank you!
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 Awesome, I'll try doing that
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?
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 Yeah I installed all of the requirements.txt and restarted the terminal, still not recognizing the command.
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.
Ok, I'll give that a try, thanks!
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?
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?
Also @Strilanc, how would one differentiate between mixtures and fully-general channels? (Sorry for all the questions 😂)
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).
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?
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.
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?
Yes, please open an issue. That's a serious bug.
@Strilanc Will do! Any idea what could be causing the bug?
Probably something silly like checking for _has_mixture_
but not _has_unitary_
(which implies that a mixture can be derived).
I can try this out
An example of a channel to test mixtures with can be created using cirq.bit_flip
.
Is help still needed on this? I see that the final_wavefunction is being deprecated for final_state_vector.
Yes @satw1knandala, we'd love to see this implemented, yes, I renamed the issue to state_vector.
Sure, I will get started on this one. You can assign me. Thanks!
update: still working on this. should be done by end of week
@Strilanc @balopat i was able to create the function sample_final_state_vector, but I am writing tests and have a few questions -
- 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?
- 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!
@balopat are we still tracking this? thank you
@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.