pasqal-io / pyqtorch

PyTorch-based state vector simulator
https://pasqal-io.github.io/pyqtorch/
Apache License 2.0
43 stars 15 forks source link

[Bug] `tensor` method incorrectly handles control gates #210

Open EthanObadia opened 2 weeks ago

EthanObadia commented 2 weeks ago

tensor method of Primitive does not work correctly for control gates. Specifically, it fails to differentiate between different control and target qubit configurations. As a result, it incorrectly returns the same tensor for different control-target configurations of the same gate.

The following code demonstrates the issue:

print(torch.allclose(
    pyq.CNOT(control=0, target=1).tensor(),
    pyq.CNOT(control=1, target=0).tensor()
))
# Output: True

The function should return different tensors for CNOT gates with different control and target qubits. A potential fix involves ensuring the function correctly accounts for the positions of control and target qubits.

jpmoutinho commented 1 week ago

Yes the .tensor() method is currently mishandling the qubit support, I've also identified a different but related problem. I'm still trying to think on the best approach to solving it.

This is also relevant to the code that you have written regarding operator - operator products and promoting operators, because if we do tensor() on a sequence that is essentially doing an operator - operator product with (potentially) different qubit support. This all needs to be harmonized.