| 92 | |
| 93 | |
| 94 | class QCNNet(nn.Module): |
| 95 | def __init__(self, num_qubits=1, |
| 96 | backend=qiskit.Aer.get_backend('qasm_simulator'), |
| 97 | shift=np.pi/2, |
| 98 | copies=1000): |
| 99 | super(QCNNet, self).__init__() |
| 100 | self.conv1 = nn.Conv2d(1, 6, kernel_size=5) |
| 101 | self.conv2 = nn.Conv2d(6, 16, kernel_size=5) |
| 102 | self.dropout = nn.Dropout2d() |
| 103 | self.fc1 = nn.Linear(256, 64) |
| 104 | self.fc2 = nn.Linear(64, 1) |
| 105 | self.q_layer = QuantumLayer(num_qubits=num_qubits, |
| 106 | backend=backend, |
| 107 | shift=shift, |
| 108 | copies=copies) |
| 109 | |
| 110 | def forward(self, x): |
| 111 | x = F.relu(self.conv1(x)) |
| 112 | x = F.max_pool2d(x, 2) |
| 113 | x = F.relu(self.conv2(x)) |
| 114 | x = F.max_pool2d(x, 2) |
| 115 | x = self.dropout(x) |
| 116 | x = x.view(1, -1) |
| 117 | x = F.relu(self.fc1(x)) |
| 118 | x = self.fc2(x) |
| 119 | x = self.q_layer(x) |
| 120 | return torch.cat((x, 1 - x), -1) |
| 121 | |
| 122 | |
| 123 | |