tencent-quantum-lab / tensorcircuit

Tensor network based quantum software framework for the NISQ era
https://tensorcircuit.readthedocs.io
Apache License 2.0
280 stars 79 forks source link

Allow sparse matrix as circuit input #197

Open royess opened 12 months ago

royess commented 12 months ago

Issue Description

In some cases, we would like a sparse matrix as circuit input.

Right now, this seems impossible because of type conversion. For example, DMCircuit does this:

dminputs = backend.convert_to_tensor(dminputs)

which throws an error like "TypeError: Failed to convert elements of SparseTensor(...) to Tensor" in TensorFlow.

Proposed Solution

I'm not sure whether we can just modify type conversions.

refraction-ray commented 12 months ago

It is not only a type conversion. If you keep the input density matrix in sparse matrix form, how to deal with gate application in the language of tensor network in implementation level? It requires heavy code refactoring if possible.

An alternative is to use mpo_dminputs where you specify the input density matrix in the form of MPO instead of sparse matrix to save the memory. Of course, not every sparse matrix has efficient MPO representation, it depends on your sepcific user scenario.

royess commented 12 months ago

OK, I can understand the difficulty.

If you keep the input density matrix in sparse matrix form, how to deal with gate application in the language of tensor network in implementation level?

Notice that sparse matrices for observable expectations are already implemented. I am wondering whether we can do a similar thing for input states. Because input states and observables seem to be dual to each other in my understanding.

refraction-ray commented 12 months ago

Notice that sparse matrices for observable expectations are already implemented. I am wondering whether we can do a similar thing for input states. Because input states and observables seem to be dual to each other in my understanding.

A very interesting point! Of course the input state and the observable are dual where you can simulate the same quantity by specifying the observable as the input state in mpo_dminputs form and get the tensor network representation of the "density matrix". The final step will involved compute the trace of a mpo based matrix and a spares matrix then, which I am not sure whether there is a sparse matrix tailored primitive for this. In the pure state case, to compute some expectation, the final computation is a sparse matrix multiplied by the wavefunction vector which can be well captured by a sparse_matmul primitive.

Essentially, the observable expectation for sparse matrix is only valid for pure state simulator as well.