-
Notifications
You must be signed in to change notification settings - Fork 117
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
W node support #128
W node support #128
Conversation
I should have synced the fork before creating the non-flexsymmetric branch
Non flexsymmetric
It looks like you didn't add the functionality for drawing W spiders in PyZX itself. So if you were to call zx.draw(g) on a graph containing a W it would probably crash. I know you are adding this to support it in ZXLive, but it feels kinda weird to have a new feature which then does not work with some of the other basic things, like drawing, that exist in PyZX. |
Alright, I will do something about the drawing. What are the other basic things that need to be accounted for? |
Thanks! I think exporting to the json format, although this is not super important. Support for calculating the tensor is pretty useful (this will be useful in ZXLive anyway). For the drawing ideally it would support both the matplotlib backend, and the d3 backend. In |
I had already implemented import/export from json and tikz because that's necessary for saving zxlive diagrams. Regarding the d3 backend, can you point me to where I need to add the code for W node? |
The relevant changes to the d3 backend should probably be made in js/zx_viewer.inline.js. |
I have implemented everything except calculating the tensor. I am confused about how to convert W to tensor so I would appreciate any help there. |
So for the tensor, is it true that we can view the 'output' node of the W-spider as a NOT gate and the 'input' node as something flexsymmetric? Because if so, then it shouldn't be too hard to extend the tensor function to this. I think I should be able to do that quite easily in that case. |
Yes, for qubits the W_INPUT is a NOT gate and W_OUTPUT is a W state which is flexsymmetric. |
So here's the idea:
This should be giving the tensor of the flexsymmetric W-spider (double-check that the 0-arity one should be the zero scalar). You see the part where it says:
You just add the case here for
If we define a tensor You probably also want to include some sanity checks, like making sure the arity of W_INPUT is 2, and that its phase is zero, and also that the phase of the W_OUTPUT is zero. Does that all make sense? |
Thanks, the strategy makes sense. I don't fully understand the W_to_tensor method. Can you clarify what n is in the code below? def W_to_tensor(arity: int) -> np.ndarray:
m = np.zeros([2]*arity,dtype=complex)
if arity == 0: # not sure what this should be
return m # Maybe just a zero scalar?
for i in range(arity):
index = (0,)*n # What is n?
index[i] = 1
m[index] = 1
return m |
Oh woops, that n should be arity. This method essentially builds the state vector of the W, which is a superposition of all the |00..100> states, so at that index we need to put a 1 into the tensor. |
great, that makes sense |
Just pushed W_to_tensor and verified that it gives the right answer. |
Nice! So do you think everything is implemented and working now? If it is not too much bother, you could also make a Jupyter notebook in the demos folder demonstrating how the W-spider works. In any case at some points the docs should be updated, because this is yet again a feature that does not appear anywhere in the docs. |
Yeah, everything is now implemented. I want to implement more rules such as ZW bialgebra, trialgebra, WW (almost) bialgebra, and Z copying through W but I would prefer to do that gradually. For now, there isn't too much to show in a demo jupyter notebook. I can create a notebook that shows how to build diagrams with W and demonstrate the fusion rule. |
I have added a demo notebook and also made the drawing slightly better. |
I added w fusion to basicrules.py, which means we can drag and drop w nodes in zxlive to fuse them. |
Alright, so is this ready to merge now? |
Yes, looks good to me |
This PR adds basic support for W nodes in PyZX. It allows for creating diagrams with W, saving/loading them, and implements a W fusion rule.