TuringQ / deepquantum

DeepQuantum for quantum computing
https://deepquantum.turingq.com/
Apache License 2.0
34 stars 7 forks source link

`UAny` gate cuda compatibility #23

Closed Jooyuza closed 3 months ago

Jooyuza commented 3 months ago

Problem Description

I am trying to run a QumodeCircuit on GPU with UAny gate. Initial states are set on 'cuda', but when the input unitary is np.array, RuntimeError turns out, saying unitary on cpu device.

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat2 in method wrapper_CUDA_mm)

Potential Reasons

UAnyGateclass in gate.py returns a new tensor without declaring the device, in line 830 :

if not isinstance(unitary, torch.Tensor): unitary = torch.tensor(unitary, dtype=torch.cfloat).reshape(-1, len(self.wires))

Possible Solutions

Though reasons are clear, I don't know how to refer device = init_state.device, as they seems to be run seperately.

If restricting users to revert unitary to torch.tensor and to('cuda') if they want to run on GPU, then we should convert it to complex data type instead of asserting a warning.

sansiro77 commented 3 months ago

Add cir.to('cuda:0') before running forward function