Hi @George_LA and @mlxd and @isaacdevlugt,
Even I tried the solution given by @mlxd, however, I was not able to run the script.
So, instead, I did this:
import torch
device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu")
I created the quantum circuit under a class. Then, I sent it to the GPU. It was able to utilize it.
class QCNN(nn.Module):
def CONVCircuit(phi, wires, i=0):
"""
quantum convolution Node
"""
# parameter
theta = np.pi / 2
qml.RX(phi[0] * np.pi, wires=0)
qml.RX(phi[1] * np.pi, wires=1)
qml.RX(phi[2] * np.pi, wires=2)
qml.RX(phi[3] * np.pi, wires=3)
qml.CRZ(theta, wires=[1, 0])
qml.CRZ(theta, wires=[3, 2])
qml.CRX(theta, wires=[1, 0])
qml.CRX(theta, wires=[3, 2])
qml.CRZ(theta, wires=[2, 0])
qml.CRX(theta, wires=[2, 0])
# Expectation value
measurement = qml.expval(qml.PauliZ(wires=0))
return measurement
def QCONV1(X, image_number, image_total, step=2):
"""
quantum convolutional layer
"""
#H, W, CH = X.shape
H, W = X.shape
step2 = 2
out = np.zeros(((H//step), (W//step)))
#progress = 0
for i in range(0, W, step):
#print("processing image "+str(image_number)+"/ "+str(image_total)+": "+str(int(((i/W+1))*100))+"% ", end="\r")
print("processing image "+str(image_number)+"/ "+str(image_total)+": "+str(i)+"px ", end="\r")
for j in range(0, H, step):
# get 2x2 pixels and make them 1D array
phi = X[i:i+2, j:j+2].flatten()
# Get Measurement
measurement = CONVCircuit(phi, len(phi))
out[i//step, j//step] = measurement
return out
qcnn = QCNN().to(device)