zxcalc / pyzx

Python library for quantum circuit rewriting and optimisation using the ZX-calculus
Apache License 2.0
387 stars 116 forks source link

replacing to_matrix() with reshape #68

Open alexandrupaler opened 3 years ago

alexandrupaler commented 3 years ago

For performance purposes would it make sense to replace the functionality from

https://github.com/Quantomatic/pyzx/blob/c5b63016c667f1f15a91f9f139ef06a6449b4502/pyzx/tensor.py#L159

with something like tensor.reshape([2 ** outputs, 2 ** inputs])?

I ran some preliminary tests and the outputs are equal, but reshape is around 10^5 times faster.

jvdwetering commented 3 years ago

Oh wow, I hadn't realised that that method was so slow. Using reshape there might be some issues in where the elements are positioned though. When I wrote this I had to be very careful that the tensor of a CNOT for instance actually resulted in the matrix we commonly associate to the CNOT.

What are you using this method for that speed is important? I mostly implemented it to for visualisation purposes, so only small matrices.

alexandrupaler commented 3 years ago

I tested the reshape with random circuits that included CNOTs too. I will look more into making sure I understand the code correctly. It seems however that the permutations for achieving correct matrices are in tensorfy.

It is more of a learning process now to find out if and how to include a tensor (contraction) library. At this stage I am just reading code am curious how difficult it is to use sparse matrices.

akissinger commented 2 years ago

@paultirleson has been working on integrating pyzx with the tensor network library quimb. Still somewhat of a WIP, but you might want to check out the new to_quimb_tensor method for ZX graphs.